Sfoglia il codice sorgente

drk: disabled block scanning from specific slot, new subcommands added with placeholders, reset subcommand implemented

aggstam 3 anni fa
parent
commit
fcf66b78c4
3 ha cambiato i file con 62 aggiunte e 9 eliminazioni
  1. 34 4
      bin/drk/src/main.rs
  2. 11 5
      bin/drk/src/rpc_blockchain.rs
  3. 17 0
      bin/drk/src/rpc_wallet.rs

+ 34 - 4
bin/drk/src/main.rs

@@ -171,8 +171,17 @@ enum Subcmd {
 
 
     /// Scan the blockchain and parse relevant transactions
     /// Scan the blockchain and parse relevant transactions
     Scan {
     Scan {
-        /// Slot number to start scanning from (optional)
-        slot: Option<u64>,
+        #[arg(long)]
+        /// Reset Merkle tree and start scanning from first slot
+        reset: bool,
+
+        #[arg(long)]
+        /// List all available checkpoints
+        list: bool,
+
+        #[arg(short, long)]
+        /// Reset Merkle tree to checkpoint index and start scanning
+        checkpoint: Option<u64>,
     },
     },
 }
 }
 
 
@@ -525,14 +534,35 @@ async fn main() -> Result<()> {
             Ok(())
             Ok(())
         }
         }
 
 
-        Subcmd::Scan { slot } => {
+        Subcmd::Scan { reset, list, checkpoint } => {
             let rpc_client = RpcClient::new(args.endpoint)
             let rpc_client = RpcClient::new(args.endpoint)
                 .await
                 .await
                 .with_context(|| "Could not connect to darkfid RPC endpoint")?;
                 .with_context(|| "Could not connect to darkfid RPC endpoint")?;
 
 
             let drk = Drk { rpc_client };
             let drk = Drk { rpc_client };
 
 
-            drk.scan_blocks(slot).await.with_context(|| "Failed during scanning")?;
+            if reset {
+                eprintln!("Reset requested.");
+                drk.scan_blocks(true).await.with_context(|| "Failed during scanning")?;
+
+                return Ok(())
+            }
+
+            if list {
+                eprintln!("List requested.");
+                // TODO: implement
+
+                return Ok(())
+            }
+
+            if let Some(c) = checkpoint {
+                eprintln!("Checkpoint requested: {}", c);
+                // TODO: implement
+
+                return Ok(())
+            }
+
+            drk.scan_blocks(false).await.with_context(|| "Failed during scanning")?;
             eprintln!("Finished scanning blockchain");
             eprintln!("Finished scanning blockchain");
 
 
             Ok(())
             Ok(())

+ 11 - 5
bin/drk/src/rpc_blockchain.rs

@@ -313,11 +313,17 @@ impl Drk {
         }
         }
     }
     }
 
 
-    /// Scans the blockchain optionally starting from the given slot for relevant
-    /// money transfer transactions. Alternatively it looks for a checkpoint in the
-    /// wallet to start scanning from.
-    pub async fn scan_blocks(&self, slot: Option<u64>) -> Result<()> {
-        let mut sl = if let Some(sl) = slot { sl } else { self.wallet_last_scanned_slot().await? };
+    /// Scans the blockchain starting from the last scanned slot, for relevant
+    /// money transfer transactions. If reset flag is provided, Merkle tree state
+    /// and coins are reset, and start scanning from beginning. Alternatively,
+    /// it looks for a checkpoint in the wallet to reset and start scanning from.
+    pub async fn scan_blocks(&self, reset: bool) -> Result<()> {
+        let mut sl = if reset {
+            self.reset_tree().await?;
+            0
+        } else {
+            self.wallet_last_scanned_slot().await?
+        };
 
 
         let req = JsonRequest::new("blockchain.last_known_slot", json!([]));
         let req = JsonRequest::new("blockchain.last_known_slot", json!([]));
         let rep = self.rpc_client.request(req).await?;
         let rep = self.rpc_client.request(req).await?;

+ 17 - 0
bin/drk/src/rpc_wallet.rs

@@ -480,4 +480,21 @@ impl Drk {
 
 
         Ok(())
         Ok(())
     }
     }
+
+    /// Reset the Merkle tree and coins in the wallet
+    pub async fn reset_tree(&self) -> Result<()> {
+        println!("Resetting Merkle tree");
+        let tree = BridgeTree::<MerkleNode, MERKLE_DEPTH>::new(100);
+        self.put_tree(&tree).await?;
+        println!("Successfully reset Merkle tree");
+
+        println!("Resetting coins");
+        let query = format!("DELETE FROM {};", MONEY_COINS_TABLE);
+        let params = json!([query]);
+        let req = JsonRequest::new("wallet.exec_sql", params);
+        let _ = self.rpc_client.request(req).await?;
+        println!("Successfully coins");
+
+        Ok(())
+    }
 }
 }