diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 0d44911..64b8b69 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -147,7 +147,7 @@ pub fn start_interactive(command_tx: Sender<(String, Vec)>, resp_rx: Rec loop { // Read the height first - let height = json::parse(&send_command("height".to_string(), vec![])).unwrap()["height"].as_i64().unwrap(); + let height = json::parse(&send_command("height".to_string(), vec!["false".to_string()])).unwrap()["height"].as_i64().unwrap(); let readline = rl.readline(&format!("({}) Block:{} (type 'help') >> ", chain_name, height)); diff --git a/lib/src/commands.rs b/lib/src/commands.rs index 2d7bff8..31a1392 100644 --- a/lib/src/commands.rs +++ b/lib/src/commands.rs @@ -111,6 +111,32 @@ impl Command for RescanCommand { } +struct ClearCommand {} +impl Command for ClearCommand { + fn help(&self) -> String { + let mut h = vec![]; + h.push("Clear the wallet state, rolling back the wallet to an empty state."); + h.push("Usage:"); + h.push("clear"); + h.push(""); + h.push("This command will clear all notes, utxos and transactions from the wallet, setting up the wallet to be synced from scratch."); + + h.join("\n") + } + + fn short_help(&self) -> String { + "Clear the wallet state, rolling back the wallet to an empty state.".to_string() + } + + fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { + lightclient.clear_state(); + + let result = object!{ "result" => "success" }; + + result.pretty(2) + } +} + struct HelpCommand {} impl Command for HelpCommand { fn help(&self) -> String { @@ -614,10 +640,11 @@ struct HeightCommand {} impl Command for HeightCommand { fn help(&self) -> String { let mut h = vec![]; - h.push("Get the latest block height that the wallet is at"); + h.push("Get the latest block height that the wallet is at."); h.push("Usage:"); - h.push("height"); + h.push("height [do_sync = true | false]"); h.push(""); + h.push("Pass 'true' (default) to sync to the server to get the latest block height. Pass 'false' to get the latest height in the wallet without checking with the server."); h.join("\n") } @@ -626,10 +653,18 @@ impl Command for HeightCommand { "Get the latest block height that the wallet is at".to_string() } - fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { - match lightclient.do_sync(true) { - Ok(_) => format!("{}", object! { "height" => lightclient.last_scanned_height()}.pretty(2)), - Err(e) => e + fn exec(&self, args: &[&str], lightclient: &LightClient) -> String { + if args.len() > 1 { + return format!("Didn't understand arguments\n{}", self.help()); + } + + if args.len() == 1 && args[0].trim() == "true" { + match lightclient.do_sync(true) { + Ok(_) => format!("{}", object! { "height" => lightclient.last_scanned_height()}.pretty(2)), + Err(e) => e + } + } else { + format!("{}", object! { "height" => lightclient.last_scanned_height()}.pretty(2)) } } } @@ -761,6 +796,7 @@ pub fn get_commands() -> Box>> { map.insert("syncstatus".to_string(), Box::new(SyncStatusCommand{})); map.insert("encryptionstatus".to_string(), Box::new(EncryptionStatusCommand{})); map.insert("rescan".to_string(), Box::new(RescanCommand{})); + map.insert("clear".to_string(), Box::new(ClearCommand{})); map.insert("help".to_string(), Box::new(HelpCommand{})); map.insert("balance".to_string(), Box::new(BalanceCommand{})); map.insert("addresses".to_string(), Box::new(AddressCommand{})); diff --git a/lib/src/lightclient.rs b/lib/src/lightclient.rs index ab5e41a..f0b36d1 100644 --- a/lib/src/lightclient.rs +++ b/lib/src/lightclient.rs @@ -842,14 +842,20 @@ impl LightClient { Ok(array![new_address]) } - pub fn do_rescan(&self) -> Result { - info!("Rescan starting"); + pub fn clear_state(&self) { // First, clear the state from the wallet self.wallet.read().unwrap().clear_blocks(); // Then set the initial block self.set_wallet_initial_state(self.wallet.read().unwrap().get_birthday()); + info!("Cleared wallet state"); + } + + pub fn do_rescan(&self) -> Result { + info!("Rescan starting"); + self.clear_state(); + // Then, do a sync, which will force a full rescan from the initial state let response = self.do_sync(true); info!("Rescan finished");