Browse Source

Add clear command

pull/14/head
Aditya Kulkarni 5 years ago
parent
commit
165c22e39e
  1. 2
      cli/src/lib.rs
  2. 48
      lib/src/commands.rs
  3. 10
      lib/src/lightclient.rs

2
cli/src/lib.rs

@ -147,7 +147,7 @@ pub fn start_interactive(command_tx: Sender<(String, Vec<String>)>, 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));

48
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<HashMap<String, Box<dyn Command>>> {
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{}));

10
lib/src/lightclient.rs

@ -842,14 +842,20 @@ impl LightClient {
Ok(array![new_address])
}
pub fn do_rescan(&self) -> Result<JsonValue, String> {
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<JsonValue, String> {
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");

Loading…
Cancel
Save