From d71e37152b6a79a141cb6795f80d9e75c75cd082 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Thu, 12 Sep 2019 20:32:47 -0700 Subject: [PATCH] Add rescan command --- rust-lightclient/src/commands.rs | 18 +++++++++++++++++- rust-lightclient/src/lightclient.rs | 22 +++++++++++++++++++--- rust-lightclient/src/lightwallet.rs | 10 ++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/rust-lightclient/src/commands.rs b/rust-lightclient/src/commands.rs index c990497..c5b75f7 100644 --- a/rust-lightclient/src/commands.rs +++ b/rust-lightclient/src/commands.rs @@ -26,8 +26,23 @@ impl Command for SyncCommand { } } -struct HelpCommand {} +struct RescanCommand {} +impl Command for RescanCommand { + fn help(&self) { + println!("Rescan the wallet from it's initial state, rescanning and downloading all blocks and transactions."); + } + fn short_help(&self) -> String { + "Rescan the wallet, downloading and scanning all blocks and transactions".to_string() + } + + fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { + lightclient.do_rescan(); + } +} + + +struct HelpCommand {} impl Command for HelpCommand { fn help(&self) { println!("Lists all available commands"); @@ -205,6 +220,7 @@ pub fn get_commands() -> Box>> { let mut map: HashMap> = HashMap::new(); map.insert("sync".to_string(), Box::new(SyncCommand{})); + map.insert("rescan".to_string(), Box::new(RescanCommand{})); map.insert("help".to_string(), Box::new(HelpCommand{})); map.insert("address".to_string(), Box::new(AddressCommand{})); map.insert("info".to_string(), Box::new(InfoCommand{})); diff --git a/rust-lightclient/src/lightclient.rs b/rust-lightclient/src/lightclient.rs index c1998ce..99e3474 100644 --- a/rust-lightclient/src/lightclient.rs +++ b/rust-lightclient/src/lightclient.rs @@ -40,6 +40,13 @@ pub struct LightClient { impl LightClient { + pub fn set_wallet_initial_state(&self) { + self.wallet.set_initial_block(500000, + "004fada8d4dbc5e80b13522d2c6bd0116113c9b7197f0c6be69bc7a62f2824cd", + "01b733e839b5f844287a6a491409a991ec70277f39a50c99163ed378d23a829a0700100001916db36dfb9a0cf26115ed050b264546c0fa23459433c31fd72f63d188202f2400011f5f4e3bd18da479f48d674dbab64454f6995b113fa21c9d8853a9e764fb3e1f01df9d2c233ca60360e3c2bb73caf5839a1be634c8b99aea22d02abda2e747d9100001970d41722c078288101acd0a75612acfb4c434f2a55aab09fb4e812accc2ba7301485150f0deac7774dcd0fe32043bde9ba2b6bbfff787ad074339af68e88ee70101601324f1421e00a43ef57f197faf385ee4cac65aab58048016ecbd94e022973701e1b17f4bd9d1b6ca1107f619ac6d27b53dd3350d5be09b08935923cbed97906c0000000000011f8322ef806eb2430dc4a7a41c1b344bea5be946efc7b4349c1c9edb14ff9d39"); + + } + pub fn new(seed_phrase: Option<&str>) -> io::Result { let mut lc = if Path::new("wallet.dat").exists() { // Make sure that if a wallet exists, there is no seed phrase being attempted @@ -63,9 +70,7 @@ impl LightClient { sapling_spend : vec![] }; - l.wallet.set_initial_block(500000, - "004fada8d4dbc5e80b13522d2c6bd0116113c9b7197f0c6be69bc7a62f2824cd", - "01b733e839b5f844287a6a491409a991ec70277f39a50c99163ed378d23a829a0700100001916db36dfb9a0cf26115ed050b264546c0fa23459433c31fd72f63d188202f2400011f5f4e3bd18da479f48d674dbab64454f6995b113fa21c9d8853a9e764fb3e1f01df9d2c233ca60360e3c2bb73caf5839a1be634c8b99aea22d02abda2e747d9100001970d41722c078288101acd0a75612acfb4c434f2a55aab09fb4e812accc2ba7301485150f0deac7774dcd0fe32043bde9ba2b6bbfff787ad074339af68e88ee70101601324f1421e00a43ef57f197faf385ee4cac65aab58048016ecbd94e022973701e1b17f4bd9d1b6ca1107f619ac6d27b53dd3350d5be09b08935923cbed97906c0000000000011f8322ef806eb2430dc4a7a41c1b344bea5be946efc7b4349c1c9edb14ff9d39"); + l.set_wallet_initial_state(); l }; @@ -268,6 +273,17 @@ impl LightClient { JsonValue::Array(tx_list) } + pub fn do_rescan(&self) { + // First, clear the state from the wallet + self.wallet.clear_blocks(); + + // Then set the inital block + self.set_wallet_initial_state(); + + // Then, do a sync, which will force a full rescan from the initial state + self.do_sync(); + } + pub fn do_sync(&self) { // Sync is 3 parts // 1. Get the latest block diff --git a/rust-lightclient/src/lightwallet.rs b/rust-lightclient/src/lightwallet.rs index 8483d61..492a2a1 100644 --- a/rust-lightclient/src/lightwallet.rs +++ b/rust-lightclient/src/lightwallet.rs @@ -574,6 +574,16 @@ impl LightWallet { Ok(()) } + + + // Clears all the downloaded blocks and resets the state back to the inital block. + // After this, the wallet's initial state will need to be set + // and the wallet will need to be rescanned + pub fn clear_blocks(&self) { + self.blocks.write().unwrap().clear(); + self.txs.write().unwrap().clear(); + } + pub fn set_initial_block(&self, height: i32, hash: &str, sapling_tree: &str) -> bool { let mut blocks = self.blocks.write().unwrap(); if !blocks.is_empty() {