Repository: farcaster-project/farcaster-node
Pull request #821 (2022-12-13 08:08:19)

Deps: Bump monero-rpc to latest version (v0.3.2)
Use the new monero-rpc builder pattern for instantiating monero rpc clients.


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index 94b77a99..ebdb2eca 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -948,7 +948,7 @@ dependencies = [
  "clap",
  "curve25519-dalek",
  "ecdsa_fun",
- "fixed-hash",
+ "fixed-hash 0.7.0",
  "hex",
  "inet2_addr",
  "lightning_encoding",
@@ -1044,6 +1044,18 @@ dependencies = [
  "static_assertions",
 ]
 
+[[package]]
+name = "fixed-hash"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534"
+dependencies = [
+ "byteorder",
+ "rand 0.8.5",
+ "rustc-hex",
+ "static_assertions",
+]
+
 [[package]]
 name = "fixedbitset"
 version = "0.4.2"
@@ -1752,7 +1764,7 @@ checksum = "8afa5d322a573d345588c3255aaf3316b6a13f3b94f8e776c5ffcde237ec481d"
 dependencies = [
  "base58-monero",
  "curve25519-dalek",
- "fixed-hash",
+ "fixed-hash 0.7.0",
  "hex",
  "hex-literal",
  "sealed",
@@ -1769,7 +1781,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "772fe43b864d2f2e6e1bd223640b32563fe3b9b7158cec32ac33236a6d3e0dd0"
 dependencies = [
  "anyhow",
- "fixed-hash",
+ "fixed-hash 0.7.0",
  "hex",
  "http",
  "jsonrpc-core",
@@ -1783,13 +1795,13 @@ dependencies = [
 
 [[package]]
 name = "monero-rpc"
-version = "0.2.0"
+version = "0.3.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "66a7d67be6fc453829e6e0913f239741b0520a40ff6c7e8300f35444a163d257"
+checksum = "e016b5ed7dbf76e123516b22f35653a95d8c4b784d2ce2113395863dfd9f482f"
 dependencies = [
  "anyhow",
  "chrono",
- "fixed-hash",
+ "fixed-hash 0.8.0",
  "hex",
  "http",
  "jsonrpc-core",
@@ -2531,6 +2543,7 @@ dependencies = [
  "serde_urlencoded",
  "tokio",
  "tokio-native-tls",
+ "tokio-socks",
  "tower-service",
  "url",
  "wasm-bindgen",
@@ -3264,6 +3277,18 @@ dependencies = [
  "tokio",
 ]
 
+[[package]]
+name = "tokio-socks"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "51165dfa029d2a65969413a6cc96f354b86b464498702f174a4efa13608fd8c0"
+dependencies = [
+ "either",
+ "futures-util",
+ "thiserror",
+ "tokio",
+]
+
 [[package]]
 name = "tokio-stream"
 version = "0.1.9"
diff --git a/Cargo.toml b/Cargo.toml
index 40a796ae..56b43d7c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -76,7 +76,7 @@ log = { version = "0.4", features = ["max_level_trace", "release_max_level_debug
 microservices = { version = "0.8.0", default-features = false, features = ['peer'] }
 monero = "0.17"
 monero-lws = "0.1"
-monero-rpc = "0.2"
+monero-rpc = "0.3"
 nix = { version = "0.19", optional = true }
 paste = "1.0"
 prost = "0.10.3"
diff --git a/src/farcasterd/trade_state_machine.rs b/src/farcasterd/trade_state_machine.rs
index c8a870cf..63a568c8 100644
--- a/src/farcasterd/trade_state_machine.rs
+++ b/src/farcasterd/trade_state_machine.rs
@@ -1463,9 +1463,10 @@ fn attempt_transition_to_end(
                         .unwrap();
                     rt.block_on(async {
                         let host = auto_fund_config.monero_rpc_wallet;
-                        let wallet_client =
-                            monero_rpc::RpcClient::new(host);
-                        let wallet = wallet_client.wallet();
+                        let wallet = monero_rpc::RpcClientBuilder::new()
+                            .build(host)
+                            .expect("client builder failed, cannot recover from bad configuration")
+                            .wallet();
                         let options = monero_rpc::TransferOptions::default();
 
                         let mut auto_funded = false;
diff --git a/src/syncerd/monero_syncer.rs b/src/syncerd/monero_syncer.rs
index 1a4b7d85..75e79932 100644
--- a/src/syncerd/monero_syncer.rs
+++ b/src/syncerd/monero_syncer.rs
@@ -72,10 +72,23 @@ pub struct Transaction {
 }
 
 impl MoneroRpc {
-    fn new(node_rpc_url: String) -> Self {
+    fn new(node_rpc_url: String, proxy_url: Option<String>) -> Self {
+        let mut client_builder = monero_rpc::RpcClientBuilder::new();
+        if let Some(proxy_url) = proxy_url {
+            client_builder = client_builder.proxy_address(proxy_url);
+        }
+        let daemon_json_rpc = client_builder
+            .clone()
+            .build(node_rpc_url.clone())
+            .expect("client builder failed, cannot recover from bad configuration")
+            .daemon();
+        let daemon_rpc = client_builder
+            .build(node_rpc_url)
+            .expect("client builder failed, cannot recover from bad configuration")
+            .daemon_rpc();
         MoneroRpc {
-            daemon_json_rpc: monero_rpc::RpcClient::new(node_rpc_url.clone()).daemon(),
-            daemon_rpc: monero_rpc::RpcClient::new(node_rpc_url).daemon_rpc(),
+            daemon_json_rpc,
+            daemon_rpc,
             height: 0,
             block_hash: vec![0],
         }
@@ -480,23 +493,27 @@ async fn run_syncerd_task_receiver(
                         }
                         Task::HealthCheck(HealthCheck { id }) => {
                             debug!("performing health check");
-                            let mut health = match monero_rpc::RpcClient::new(
-                                syncer_servers.monero_daemon.clone(),
-                            )
-                            .daemon()
-                            .get_block_count()
-                            .await
+                            let mut health = match monero_rpc::RpcClientBuilder::new()
+                                .build(syncer_servers.monero_daemon.clone())
+                                .expect(
+                                    "client builder failed, cannot recover from bad configuration",
+                                )
+                                .daemon()
+                                .get_block_count()
+                                .await
                             {
                                 Ok(_) => Health::Healthy,
                                 Err(err) => Health::FaultyMoneroDaemon(err.to_string()),
                             };
 
-                            health = match monero_rpc::RpcClient::new(
-                                syncer_servers.monero_rpc_wallet.clone(),
-                            )
-                            .wallet()
-                            .get_version()
-                            .await
+                            health = match monero_rpc::RpcClientBuilder::new()
+                                .build(syncer_servers.monero_rpc_wallet.clone())
+                                .expect(
+                                    "client builder failed, cannot recover from bad configuration",
+                                )
+                                .wallet()
+                                .get_version()
+                                .await
                             {
                                 Ok(_) => health,
                                 Err(err) => Health::FaultyMoneroRpcWallet(err.to_string()),
@@ -556,7 +573,7 @@ fn address_polling(
     wallet_mutex: Arc<Mutex<monero_rpc::WalletClient>>,
 ) -> tokio::task::JoinHandle<()> {
     tokio::task::spawn(async move {
-        let mut rpc = MoneroRpc::new(syncer_servers.monero_daemon);
+        let mut rpc = MoneroRpc::new(syncer_servers.monero_daemon, None);
         loop {
             let state_guard = state.lock().await;
             let mut addresses = state_guard.addresses.clone();
@@ -655,7 +672,7 @@ fn height_polling(
     syncer_servers: MoneroSyncerServers,
 ) -> tokio::task::JoinHandle<()> {
     tokio::task::spawn(async move {
-        let mut rpc = MoneroRpc::new(syncer_servers.monero_daemon);
+        let mut rpc = MoneroRpc::new(syncer_servers.monero_daemon, None);
         loop {
             let block_notif = match rpc.check_block().await {
                 Ok(notif) => Some(notif),
@@ -749,7 +766,7 @@ fn unseen_transaction_polling(
     syncer_servers: MoneroSyncerServers,
 ) -> tokio::task::JoinHandle<()> {
     tokio::task::spawn(async move {
-        let mut rpc = MoneroRpc::new(syncer_servers.monero_daemon);
+        let mut rpc = MoneroRpc::new(syncer_servers.monero_daemon, None);
         loop {
             let state_guard = state.lock().await;
             let unseen_transactions = state_guard.unseen_transactions.clone();
@@ -976,7 +993,11 @@ impl Synclet for MoneroSyncer {
                         .unwrap();
                     rt.block_on(async {
                         let wallet_mutex = Arc::new(Mutex::new(
-                            monero_rpc::RpcClient::new(syncer_servers.monero_rpc_wallet.clone())
+                            monero_rpc::RpcClientBuilder::new()
+                                .build(syncer_servers.monero_rpc_wallet.clone())
+                                .expect(
+                                    "client builder failed, cannot recover from bad configuration",
+                                )
                                 .wallet(),
                         ));
                         let (balance_get_tx, balance_get_rx): (


Test patch:
diff --git a/tests/monero.rs b/tests/monero.rs
index dcf40e3d..57b7e83f 100644
--- a/tests/monero.rs
+++ b/tests/monero.rs
@@ -922,13 +922,17 @@ async fn setup_monero() -> (
 ) {
     let conf = config::TestConfig::parse();
 
-    let client = monero_rpc::RpcClient::new(format!("{}", conf.monero.daemon));
+    let client = monero_rpc::RpcClientBuilder::new()
+        .build(format!("{}", conf.monero.daemon))
+        .unwrap();
     let regtest = client.daemon().regtest();
 
-    let client = monero_rpc::RpcClient::new(format!(
-        "{}",
-        conf.monero.get_wallet(config::WalletIndex::Primary)
-    ));
+    let client = monero_rpc::RpcClientBuilder::new()
+        .build(format!(
+            "{}",
+            conf.monero.get_wallet(config::WalletIndex::Primary)
+        ))
+        .unwrap();
     let wallet = client.wallet();
 
     // Ignore if fails, maybe the wallet already exists
diff --git a/tests/utils/fc.rs b/tests/utils/fc.rs
index bbe0764b..8f27f397 100644
--- a/tests/utils/fc.rs
+++ b/tests/utils/fc.rs
@@ -273,12 +273,16 @@ pub async fn monero_setup() -> (
     Arc<Mutex<monero_rpc::WalletClient>>,
 ) {
     let conf = config::TestConfig::parse();
-    let client = monero_rpc::RpcClient::new(format!("{}", conf.monero.daemon));
+    let client = monero_rpc::RpcClientBuilder::new()
+        .build(format!("{}", conf.monero.daemon))
+        .unwrap();
     let regtest = client.daemon().regtest();
-    let client = monero_rpc::RpcClient::new(format!(
-        "{}",
-        conf.monero.get_wallet(config::WalletIndex::Primary)
-    ));
+    let client = monero_rpc::RpcClientBuilder::new()
+        .build(format!(
+            "{}",
+            conf.monero.get_wallet(config::WalletIndex::Primary)
+        ))
+        .unwrap();
     let wallet = client.wallet();
 
     // error happens when wallet does not exist