diff --git a/Cargo.lock b/Cargo.lock index 1debc9f..75efad3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -202,7 +202,7 @@ dependencies = [ "env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.12.35 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-http-server 11.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "jsonrpc-http-server 14.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "mongodb 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)", "ocean-rpc 0.8.0 (git+https://github.com/commerceblock/rust-ocean-rpc)", @@ -535,7 +535,7 @@ dependencies = [ [[package]] name = "jsonrpc-core" -version = "11.0.0" +version = "14.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", @@ -547,28 +547,28 @@ dependencies = [ [[package]] name = "jsonrpc-http-server" -version = "11.0.0" +version = "14.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "hyper 0.12.35 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-core 11.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-server-utils 11.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "jsonrpc-core 14.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "jsonrpc-server-utils 14.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicase 2.6.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "jsonrpc-server-utils" -version = "11.0.0" +version = "14.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "globset 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", - "jsonrpc-core 11.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "jsonrpc-core 14.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-codec 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "unicase 2.6.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1737,9 +1737,9 @@ dependencies = [ "checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" "checksum jsonrpc 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "436f3455a8a4e9c7b14de9f1206198ee5d0bdc2db1b560339d2141093d7dd389" -"checksum jsonrpc-core 11.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "97b83fdc5e0218128d0d270f2f2e7a5ea716f3240c8518a58bc89e6716ba8581" -"checksum jsonrpc-http-server 11.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "541257be6c8f75a41812575150dfa1120d3ee3a852601f2ca8ac9bcb73575c4e" -"checksum jsonrpc-server-utils 11.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e3372b3248a53abcca8f61924f188052bb0c4cd80b482b2b4eaf9f8667efb9f4" +"checksum jsonrpc-core 14.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fe3b688648f1ef5d5072229e2d672ecb92cbff7d1c79bcf3fd5898f3f3df0970" +"checksum jsonrpc-http-server 14.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "2d83d348120edee487c560b7cdd2565055d61cda053aa0d0ef0f8b6a18429048" +"checksum jsonrpc-server-utils 14.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "95b7635e618a0edbbe0d2a2bbbc69874277c49383fcf6c3c0414491cfb517d22" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a" "checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73" diff --git a/Cargo.toml b/Cargo.toml index 2fe52f8..1512d7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ config = "0.9" serde = { version = "1.0", features = ["derive"] } serde_json="1.0" mongodb = "0.3.11" -jsonrpc-http-server = "11.0" +jsonrpc-http-server = "14.0.5" rust-ocean = { git = "https://github.com/commerceblock/rust-ocean"} ocean-rpc = { git = "https://github.com/commerceblock/rust-ocean-rpc"} bitcoin = { version = "0.20", features = [ "use-serde" ] } diff --git a/src/api.rs b/src/api.rs index a9f51de..2496264 100644 --- a/src/api.rs +++ b/src/api.rs @@ -11,7 +11,9 @@ use base64::decode as b64decode; use bitcoin::hashes::sha256d; use hyper::{Body, Request, StatusCode}; use jsonrpc_http_server::jsonrpc_core::{Error, ErrorCode, IoHandler, Params, Value}; -use jsonrpc_http_server::{hyper::header, AccessControlAllowOrigin, DomainsValidation, Response, ServerBuilder}; +use jsonrpc_http_server::{ + hyper::header, AccessControlAllowOrigin, CloseHandle, DomainsValidation, Response, ServerBuilder, +}; use serde::{Deserialize, Serialize}; use crate::config::ApiConfig; @@ -148,10 +150,7 @@ fn authorize(our_auth: &str, request: &Request) -> bool { /// Run Api RPC server for external requests that require information from the /// coordinator. Data returned to the caller are drawn from the storage /// interface which is shared with the main coordinator process -pub fn run_api_server( - config: &ApiConfig, - storage: Arc, -) -> thread::JoinHandle<()> { +pub fn run_api_server(config: &ApiConfig, storage: Arc) -> CloseHandle { let mut io = IoHandler::default(); let storage_ref = storage.clone(); io.add_method("getrequestresponse", move |params: Params| { @@ -189,7 +188,9 @@ pub fn run_api_server( .start_http(&addr[0]) .expect("api error"); - thread::spawn(move || server.wait()) + let close_handle = server.close_handle(); + let _ = thread::spawn(move || server.wait()); + close_handle // handler to stop the server from the main thread } #[cfg(test)] diff --git a/src/coordinator.rs b/src/coordinator.rs index 8d1201f..6965c43 100644 --- a/src/coordinator.rs +++ b/src/coordinator.rs @@ -24,22 +24,30 @@ pub fn run(config: Config) -> Result<()> { let storage = Arc::new(MongoStorage::new(config.storage.clone())?); let genesis_hash = sha256d::Hash::from_hex(&config.clientchain.genesis_hash)?; - let _ = ::api::run_api_server(&config.api, storage.clone()); + let api_handler = ::api::run_api_server(&config.api, storage.clone()); let (req_send, req_recv): (Sender, Receiver) = channel(); let _ = ::payments::run_payments(config.clientchain.clone(), storage.clone(), req_recv)?; // This loop runs continuously fetching and running challenge requests, // generating challenge responses and fails on any errors that occur loop { - if let Some(request_id) = run_request(&config, &service, &clientchain, storage.clone(), genesis_hash)? { - // if challenge request succeeds print responses - req_send.send(request_id).unwrap(); - info! {"***** Response *****"} - let resp = storage.get_response(request_id)?.unwrap(); - info! {"{}", serde_json::to_string_pretty(&resp).unwrap()}; + match run_request(&config, &service, &clientchain, storage.clone(), genesis_hash) { + Ok(res) => { + if let Some(request_id) = res { + // if challenge request succeeds print responses + req_send.send(request_id).unwrap(); + info! {"***** Response *****"} + let resp = storage.get_response(request_id)?.unwrap(); + info! {"{}", serde_json::to_string_pretty(&resp).unwrap()}; + } + info! {"Sleeping for {} sec...", config.block_time} + thread::sleep(time::Duration::from_secs(config.block_time)) + } + Err(err) => { + api_handler.close(); // try closing the api rpc server + return Err(err); + } } - info! {"Sleeping for {} sec...", config.block_time} - thread::sleep(time::Duration::from_secs(config.block_time)) } }