Repository: farcaster-project/farcaster-node (rust)

Issue:
monero wallet rpc log previous upon successful sweeping
```
2021-11-29 03:04:09.237	E wrong number of additional derivations
2021-11-29 03:04:54.492	W Transaction extra has unsupported format: <eff6ab2c488124caf1ea02874a7a1606ed58df11eebf44e4c7818014f9472235>
... ^^ thousands of the line above with different values
2021-11-29 03:04:54.550	W Received money: 0.000100000000, with tx: <fe6fa11669b3b21afc981815954d5428739cfde4ab0a974c5689b65757be7964>
2021-11-29 03:04:55.240	W Loaded wallet keys file, with public address: 5BMsjsoFbVEKaN5jDwszevUrt5evxAtFSEQDGfNnnpcyFomiVpFVYfRJtbi9kUBBo95zgJiPfSHcm3pGurKbdUDEJMAUqJy
2021-11-29 03:04:56.334	W Loaded wallet keys file, with public address: 5BMsjsoFbVEKaN5jDwszevUrt5evxAtFSEQDGfNnnpcyFomiVpFVYfRJtbi9kUBBo95zgJiPfSHcm3pGurKbdUDEJMAUqJy
2021-11-29 03:04:56.759	W amount=0.000100000000, real_output=10, real_output_in_tx_index=1, indexes: 4346198 4364888 4365462 4372823 4372946 4373634 4374205 4374206 4374690 4374753 4374866 
2021-11-29 03:04:56.763	W amount=0.000100000000, real_output=10, real_output_in_tx_index=1, indexes: 4346198 4364888 4365462 4372823 4372946 4373634 4374205 4374206 4374690 4374753 4374866 
2021-11-29 03:04:56.767	W amount=0.000100000000, real_output=10, real_output_in_tx_index=1, indexes: 4346198 4364888 4365462 4372823 4372946 4373634 4374205 4374206 4374690 4374753 4374866 
2021-11-29 03:04:59.166	W Loaded wallet keys file, with public address: 5BMsjsoFbVEKaN5jDwszevUrt5evxAtFSEQDGfNnnpcyFomiVpFVYfRJtbi9kUBBo95zgJiPfSHcm3pGurKbdUDEJMAUqJy
```

Pull request description:
Monero syncer: Add restore height for sweep wallet
This is supposed to fix #254 , please test if this makes the excessive monero-wallet-rpc logging disappear.

Patch:
diff --git a/src/swapd/runtime.rs b/src/swapd/runtime.rs
index 110c3c5f..c4e7aff2 100644
--- a/src/swapd/runtime.rs
+++ b/src/swapd/runtime.rs
@@ -714,6 +714,7 @@ impl SyncerState {
             id,
             lifetime,
             addendum,
+            from_height: None,
         };
         Task::SweepAddress(sweep_task)
     }
@@ -1222,6 +1223,7 @@ impl Runtime {
                 view_key,
                 spend_key,
                 address,
+                ..
             }) if source == ServiceId::Wallet => {
                 let task = self.syncer_state.sweep_xmr(view_key, spend_key, address);
                 let request = Request::SyncerTask(task);
@@ -1497,9 +1499,13 @@ impl Runtime {
                             .expect("Checked above")
                             .pop()
                             .unwrap();
-                        if let (Request::SyncerTask(Task::SweepAddress(..)), ServiceBus::Ctl) =
-                            (&request, &bus_id)
+                        if let (Request::SyncerTask(Task::SweepAddress(mut task)), ServiceBus::Ctl) =
+                            (request.clone(), bus_id)
                         {
+                            // safe cast
+                            task.from_height = Some(self.syncer_state.monero_height - *confirmations as u64);
+                            let request = Request::SyncerTask(Task::SweepAddress(task));
+
                             info!("sweeping monero");
                             senders.send_to(bus_id, self.identity(), dest, request)?;
                         } else {
@@ -1674,6 +1680,7 @@ impl Runtime {
                             TxLabel::Cancel => {
                                 self.syncer_state.cancel_tx_confs = Some(request.clone());
                             }
+
                             _ => {}
                         }
 
diff --git a/src/syncerd/monero_syncer.rs b/src/syncerd/monero_syncer.rs
index 00f80a39..57956279 100644
--- a/src/syncerd/monero_syncer.rs
+++ b/src/syncerd/monero_syncer.rs
@@ -224,12 +224,19 @@ async fn sweep_address(
     spend: monero::PrivateKey,
     network: &monero::Network,
     wallet_mutex: Arc<Mutex<monero_rpc::WalletClient>>,
+    restore_height: Option<u64>,
 ) -> Result<Vec<Vec<u8>>, Error> {
     let keypair = monero::KeyPair { view, spend };
     let password = s!(" ");
     let address = monero::Address::from_keypair(*network, &keypair);
     let wallet_filename = format!("sweep:{}", address);
 
+    // add some extra leeway for the wallet restore height
+    // let restore_height = match from_height {
+    //     Some(height) if height > 10 => Some(height - 10),
+    //     _ => None,
+    // };
+
     let wallet = wallet_mutex.lock().await;
     trace!("taking sweep wallet lock");
 
@@ -243,7 +250,7 @@ async fn sweep_address(
         );
         wallet
             .generate_from_keys(GenerateFromKeysArgs {
-                restore_height: Some(1),
+                restore_height,
                 filename: wallet_filename.clone(),
                 address,
                 spendkey: Some(keypair.spend),
@@ -257,6 +264,7 @@ async fn sweep_address(
     // failsafe to check if the wallet really supports spending
     wallet.query_key(PrivateKeyType::Spend).await?;
     let (account, addrs) = (0, None);
+    wallet.refresh(restore_height).await?;
     let balance = wallet.get_balance(account, addrs).await?;
     // only sweep once all the balance is unlocked
     if balance.unlocked_balance != 0 {
@@ -502,6 +510,7 @@ fn sweep_polling(
                         addendum.spend_key,
                         &network,
                         Arc::clone(&wallet),
+                        sweep_address_task.from_height,
                     )
                     .await
                     .unwrap_or_else(|err| {
diff --git a/src/syncerd/syncer_state.rs b/src/syncerd/syncer_state.rs
index 56f7e2f3..64756aa4 100644
--- a/src/syncerd/syncer_state.rs
+++ b/src/syncerd/syncer_state.rs
@@ -929,6 +929,7 @@ async fn syncer_state_sweep_addresses() {
     let sweep_task = SweepAddress {
         id: TaskId(0),
         lifetime: 11,
+        from_height: None,
         addendum: SweepAddressAddendum::Monero(SweepXmrAddress {
             view_key: monero::PrivateKey::from_str(
                 "77916d0cd56ed1920aef6ca56d8a41bac915b68e4c46a589e0956e27a7b77404",
diff --git a/src/syncerd/types.rs b/src/syncerd/types.rs
index 170c665a..830e0164 100644
--- a/src/syncerd/types.rs
+++ b/src/syncerd/types.rs
@@ -77,6 +77,7 @@ pub struct SweepAddress {
     pub id: TaskId,
     pub lifetime: u64,
     pub addendum: SweepAddressAddendum,
+    pub from_height: Option<u64>,
 }
 
 #[derive(Clone, Debug, Display, StrictEncode, StrictDecode, Eq, PartialEq, Hash)]


Test patch:
diff --git a/tests/functional.rs b/tests/functional.rs
index 0c9e81b9..3221eac3 100644
--- a/tests/functional.rs
+++ b/tests/functional.rs
@@ -976,6 +976,7 @@ async fn monero_syncer_sweep_test() {
         task: Task::SweepAddress(SweepAddress {
             id: TaskId(0),
             lifetime: blocks + 40,
+            from_height: None,
             addendum: SweepAddressAddendum::Monero(SweepXmrAddress {
                 spend_key,
                 view_key,