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

This commit is contained in:
aggstam
2022-12-10 16:51:50 +02:00
parent ff57e059d5
commit fcf66b78c4
3 changed files with 62 additions and 9 deletions

View File

@@ -171,8 +171,17 @@ enum Subcmd {
/// Scan the blockchain and parse relevant transactions
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(())
}
Subcmd::Scan { slot } => {
Subcmd::Scan { reset, list, checkpoint } => {
let rpc_client = RpcClient::new(args.endpoint)
.await
.with_context(|| "Could not connect to darkfid RPC endpoint")?;
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");
Ok(())

View File

@@ -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 rep = self.rpc_client.request(req).await?;

View File

@@ -480,4 +480,21 @@ impl Drk {
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(())
}
}