From 0970c2bc726067979e4801918ce097938a5adca2 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 19 Apr 2021 10:10:46 +1000 Subject: [PATCH] Initialize reqwest clients with verbose logging --- monero-harness/src/lib.rs | 21 +++++++++++++-------- monero-rpc/src/monerod.rs | 13 ++++++++----- monero-rpc/src/wallet.rs | 16 +++++++++------- swap/src/monero/wallet.rs | 2 +- swap/src/monero/wallet_rpc.rs | 2 +- swap/tests/harness/mod.rs | 4 ++-- 6 files changed, 34 insertions(+), 24 deletions(-) diff --git a/monero-harness/src/lib.rs b/monero-harness/src/lib.rs index 090ac7e4..c667e19e 100644 --- a/monero-harness/src/lib.rs +++ b/monero-harness/src/lib.rs @@ -177,6 +177,7 @@ pub struct Monerod { rpc_port: u16, name: String, network: String, + client: monerod::Client, } #[derive(Clone, Debug)] @@ -184,6 +185,7 @@ pub struct MoneroWalletRpc { rpc_port: u16, name: String, network: String, + client: wallet::Client, } impl<'c> Monerod { @@ -211,19 +213,20 @@ impl<'c> Monerod { rpc_port: monerod_rpc_port, name, network, + client: monerod::Client::localhost(monerod_rpc_port)?, }, docker, )) } - pub fn client(&self) -> monerod::Client { - monerod::Client::localhost(self.rpc_port) + pub fn client(&self) -> &monerod::Client { + &self.client } /// Spawns a task to mine blocks in a regular interval to the provided /// address pub async fn start_miner(&self, miner_wallet_address: &str) -> Result<()> { - let monerod = self.client(); + let monerod = self.client().clone(); let _ = tokio::spawn(mine(monerod, miner_wallet_address.to_string())); Ok(()) } @@ -256,23 +259,25 @@ impl<'c> MoneroWalletRpc { let docker = cli.run_with_args(image, run_args); // create new wallet - wallet::Client::localhost(wallet_rpc_port) + let client = wallet::Client::localhost(wallet_rpc_port)?; + + client .create_wallet(name.to_owned(), "English".to_owned()) - .await - .unwrap(); + .await?; Ok(( Self { rpc_port: wallet_rpc_port, name: name.to_string(), network, + client, }, docker, )) } - pub fn client(&self) -> wallet::Client { - wallet::Client::localhost(self.rpc_port) + pub fn client(&self) -> &wallet::Client { + &self.client } // It takes a little while for the wallet to sync with monerod. diff --git a/monero-rpc/src/monerod.rs b/monero-rpc/src/monerod.rs index 9926d7ac..8d044134 100644 --- a/monero-rpc/src/monerod.rs +++ b/monero-rpc/src/monerod.rs @@ -1,3 +1,4 @@ +use anyhow::{Context, Result}; use serde::Deserialize; #[jsonrpc_client::api(version = "2.0")] @@ -17,13 +18,15 @@ pub struct Client { impl Client { /// New local host monerod RPC client. - pub fn localhost(port: u16) -> Self { - Self { - inner: reqwest::Client::new(), + pub fn localhost(port: u16) -> Result { + Ok(Self { + inner: reqwest::ClientBuilder::new() + .connection_verbose(true) + .build()?, base_url: format!("http://127.0.0.1:{}/json_rpc", port) .parse() - .expect("url is well formed"), - } + .context("url is well formed")?, + }) } } diff --git a/monero-rpc/src/wallet.rs b/monero-rpc/src/wallet.rs index 67d7ce21..4f10f6ab 100644 --- a/monero-rpc/src/wallet.rs +++ b/monero-rpc/src/wallet.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use serde::{de::Error, Deserialize, Deserializer, Serialize}; #[jsonrpc_client::api(version = "2.0")] @@ -43,20 +43,22 @@ pub struct Client { impl Client { /// Constructs a monero-wallet-rpc client with localhost endpoint. - pub fn localhost(port: u16) -> Self { + pub fn localhost(port: u16) -> Result { Client::new( format!("http://127.0.0.1:{}/json_rpc", port) .parse() - .expect("url is well formed"), + .context("url is well formed")?, ) } /// Constructs a monero-wallet-rpc client with `url` endpoint. - pub fn new(url: reqwest::Url) -> Self { - Self { - inner: reqwest::Client::new(), + pub fn new(url: reqwest::Url) -> Result { + Ok(Self { + inner: reqwest::ClientBuilder::new() + .connection_verbose(true) + .build()?, base_url: url, - } + }) } /// Transfers `amount` monero from `account_index` to `address`. diff --git a/swap/src/monero/wallet.rs b/swap/src/monero/wallet.rs index 3f9cf9d4..93af277e 100644 --- a/swap/src/monero/wallet.rs +++ b/swap/src/monero/wallet.rs @@ -25,7 +25,7 @@ pub struct Wallet { impl Wallet { /// Connect to a wallet RPC and load the given wallet by name. pub async fn open_or_create(url: Url, name: String, env_config: Config) -> Result { - let client = wallet::Client::new(url); + let client = wallet::Client::new(url)?; let open_wallet_response = client.open_wallet(name.clone()).await; if open_wallet_response.is_err() { diff --git a/swap/src/monero/wallet_rpc.rs b/swap/src/monero/wallet_rpc.rs index a2aa9582..273337ef 100644 --- a/swap/src/monero/wallet_rpc.rs +++ b/swap/src/monero/wallet_rpc.rs @@ -165,7 +165,7 @@ impl WalletRpc { } // Send a json rpc request to make sure monero_wallet_rpc is ready - Client::localhost(port).get_version().await?; + Client::localhost(port)?.get_version().await?; Ok(WalletRpcProcess { _child: child, diff --git a/swap/tests/harness/mod.rs b/swap/tests/harness/mod.rs index 58c77ea1..7acec257 100644 --- a/swap/tests/harness/mod.rs +++ b/swap/tests/harness/mod.rs @@ -44,7 +44,7 @@ where let cli = Cli::default(); let _guard = tracing_subscriber::fmt() - .with_env_filter("warn,swap=debug,monero_harness=debug,monero_rpc=debug,bitcoin_harness=info,testcontainers=info") + .with_env_filter("warn,swap=debug,monero_harness=debug,monero_rpc=debug,bitcoin_harness=info,testcontainers=info") // add `reqwest::connect::verbose=trace` if you want to logs of the RPC clients .with_test_writer() .set_default(); @@ -276,7 +276,7 @@ async fn init_test_wallets( .unwrap(); let xmr_wallet = swap::monero::Wallet::connect( - monero.wallet(name).unwrap().client(), + monero.wallet(name).unwrap().client().clone(), name.to_string(), env_config, )