diff --git a/Cargo.toml b/Cargo.toml index df0a30c..22fb0c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,6 @@ members = [ "client", "integration_test", ] + +[workspace.dependencies] +tokio = { version = "1", features = ["full"] } diff --git a/client/Cargo.toml b/client/Cargo.toml index 5464c42..4cf66a2 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -31,4 +31,4 @@ serde_json = "1" [dev-dependencies] tempfile = "3.3.0" - +tokio.workspace = true diff --git a/client/examples/retry_client.rs b/client/examples/retry_client.rs index 83ed260..176f10d 100644 --- a/client/examples/retry_client.rs +++ b/client/examples/retry_client.rs @@ -13,6 +13,7 @@ extern crate jsonrpc_async; extern crate serde; extern crate serde_json; +use async_trait::async_trait; use defichain_rpc::{Client, Error, Result, RpcApi}; pub struct RetryClient { @@ -22,16 +23,17 @@ pub struct RetryClient { const INTERVAL: u64 = 1000; const RETRY_ATTEMPTS: u8 = 10; +#[async_trait] impl RpcApi for RetryClient { - fn call serde::de::Deserialize<'a>>( + async fn call serde::de::Deserialize<'a>>( &self, cmd: &str, args: &[serde_json::Value], ) -> Result { for _ in 0..RETRY_ATTEMPTS { - match self.client.call(cmd, args) { + match self.client.call(cmd, args).await { Ok(ret) => return Ok(ret), - Err(Error::JsonRpc(jsonrpc::error::Error::Rpc(ref rpcerr))) + Err(Error::JsonRpc(jsonrpc_async::error::Error::Rpc(ref rpcerr))) if rpcerr.code == -28 => { ::std::thread::sleep(::std::time::Duration::from_millis(INTERVAL)); @@ -40,7 +42,7 @@ impl RpcApi for RetryClient { Err(e) => return Err(e), } } - self.client.call(cmd, args) + self.client.call(cmd, args).await } } diff --git a/client/examples/test_against_node.rs b/client/examples/test_against_node.rs index 3613e6d..926168e 100644 --- a/client/examples/test_against_node.rs +++ b/client/examples/test_against_node.rs @@ -14,7 +14,7 @@ extern crate defichain_rpc; use defichain_rpc::{bitcoin, Auth, Client, Error, RpcApi}; -fn main_result() -> Result<(), Error> { +async fn main_result() -> Result<(), Error> { let mut args = std::env::args(); let _exe_name = args.next().unwrap(); @@ -23,26 +23,27 @@ fn main_result() -> Result<(), Error> { let user = args.next().expect("no user given"); let pass = args.next().expect("no pass given"); - let rpc = Client::new(&url, Auth::UserPass(user, pass)).unwrap(); + let rpc = Client::new(&url, Auth::UserPass(user, pass)).await.unwrap(); - let _blockchain_info = rpc.get_blockchain_info()?; + let _blockchain_info = rpc.get_blockchain_info().await?; - let best_block_hash = rpc.get_best_block_hash()?; + let best_block_hash = rpc.get_best_block_hash().await?; println!("best block hash: {}", best_block_hash); - let bestblockcount = rpc.get_block_count()?; + let bestblockcount = rpc.get_block_count().await?; println!("best block height: {}", bestblockcount); - let best_block_hash_by_height = rpc.get_block_hash(bestblockcount)?; + let best_block_hash_by_height = rpc.get_block_hash(bestblockcount).await?; println!("best block hash by height: {}", best_block_hash_by_height); assert_eq!(best_block_hash_by_height, best_block_hash); - let bitcoin_block: bitcoin::Block = rpc.get_by_id(&best_block_hash)?; + let bitcoin_block: bitcoin::Block = rpc.get_by_id(&best_block_hash).await?; println!("best block hash by `get`: {}", bitcoin_block.header.prev_blockhash); - let bitcoin_tx: bitcoin::Transaction = rpc.get_by_id(&bitcoin_block.txdata[0].txid())?; + let bitcoin_tx: bitcoin::Transaction = rpc.get_by_id(&bitcoin_block.txdata[0].txid()).await?; println!("tx by `get`: {}", bitcoin_tx.txid()); Ok(()) } -fn main() { - main_result().unwrap(); +#[tokio::main] +async fn main() { + main_result().await.unwrap(); } diff --git a/client/src/client.rs b/client/src/client.rs index aabd8b2..7a75b1c 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -1410,16 +1410,16 @@ mod tests { use crate::bitcoin; use serde_json; - #[test] + #[tokio::test] async fn test_raw_tx() { use crate::bitcoin::consensus::encode; - let client = Client::new("http://localhost/".into(), Auth::None).unwrap(); + let client = Client::new("http://localhost/".into(), Auth::None).await.unwrap(); let tx: bitcoin::Transaction = encode::deserialize(&Vec::::from_hex("0200000001586bd02815cf5faabfec986a4e50d25dbee089bd2758621e61c5fab06c334af0000000006b483045022100e85425f6d7c589972ee061413bcf08dc8c8e589ce37b217535a42af924f0e4d602205c9ba9cb14ef15513c9d946fa1c4b797883e748e8c32171bdf6166583946e35c012103dae30a4d7870cd87b45dd53e6012f71318fdd059c1c2623b8cc73f8af287bb2dfeffffff021dc4260c010000001976a914f602e88b2b5901d8aab15ebe4a97cf92ec6e03b388ac00e1f505000000001976a914687ffeffe8cf4e4c038da46a9b1d37db385a472d88acfd211500").unwrap()).unwrap(); - assert!(client.send_raw_transaction(&tx).is_err()); - assert!(client.send_raw_transaction(&encode::serialize(&tx)).is_err()); - assert!(client.send_raw_transaction("deadbeef").is_err()); - assert!(client.send_raw_transaction("deadbeef".to_owned()).is_err()); + assert!(client.send_raw_transaction(&tx).await.is_err()); + assert!(client.send_raw_transaction(&encode::serialize(&tx)).await.is_err()); + assert!(client.send_raw_transaction("deadbeef").await.is_err()); + assert!(client.send_raw_transaction("deadbeef".to_owned()).await.is_err()); } async fn test_handle_defaults_inner() -> Result<()> { @@ -1474,12 +1474,12 @@ mod tests { Ok(()) } - #[test] + #[tokio::test] async fn test_handle_defaults() { - test_handle_defaults_inner().unwrap(); + test_handle_defaults_inner().await.unwrap(); } - #[test] + #[tokio::test] async fn auth_cookie_file_ignores_newline() { let tempdir = tempfile::tempdir().unwrap(); let path = tempdir.path().join("cookie"); @@ -1490,7 +1490,7 @@ mod tests { ); } - #[test] + #[tokio::test] async fn auth_cookie_file_ignores_additional_lines() { let tempdir = tempfile::tempdir().unwrap(); let path = tempdir.path().join("cookie"); @@ -1501,7 +1501,7 @@ mod tests { ); } - #[test] + #[tokio::test] async fn auth_cookie_file_fails_if_colon_isnt_present() { let tempdir = tempfile::tempdir().unwrap(); let path = tempdir.path().join("cookie");