From d21c87acef27f8251046b03b19ed687d0a37d429 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 16 Sep 2019 13:14:15 -0700 Subject: [PATCH] Make lightclient not mut --- rust-lightclient/src/commands.rs | 26 +++++++++++++------------- rust-lightclient/src/main.rs | 25 ++++++++++++++++--------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/rust-lightclient/src/commands.rs b/rust-lightclient/src/commands.rs index 1364531..3b7ecd4 100644 --- a/rust-lightclient/src/commands.rs +++ b/rust-lightclient/src/commands.rs @@ -7,7 +7,7 @@ pub trait Command { fn short_help(&self) -> String; - fn exec(&self, _args: &[&str], lightclient: &mut LightClient); + fn exec(&self, _args: &[&str], lightclient: &LightClient); } struct SyncCommand {} @@ -20,7 +20,7 @@ impl Command for SyncCommand { "Download CompactBlocks and sync to the server".to_string() } - fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &LightClient) { lightclient.do_sync(); } } @@ -35,7 +35,7 @@ impl Command for RescanCommand { "Rescan the wallet, downloading and scanning all blocks and transactions".to_string() } - fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &LightClient) { lightclient.do_rescan(); } } @@ -51,7 +51,7 @@ impl Command for HelpCommand { "Lists all available commands".to_string() } - fn exec(&self, _args: &[&str], _: &mut LightClient) { + fn exec(&self, _args: &[&str], _: &LightClient) { // Print a list of all commands println!("Available commands:"); get_commands().iter().for_each(| (cmd, obj) | { @@ -70,7 +70,7 @@ impl Command for InfoCommand { "Get the lightwalletd server's info".to_string() } - fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &LightClient) { lightclient.do_info(); } } @@ -85,7 +85,7 @@ impl Command for AddressCommand { "List all current addresses".to_string() } - fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &LightClient) { let res = lightclient.do_address(); println!("{}", res.pretty(2)); } @@ -103,7 +103,7 @@ impl Command for SendCommand { "Send ZEC to the given address".to_string() } - fn exec(&self, args: &[&str], lightclient: &mut LightClient) { + fn exec(&self, args: &[&str], lightclient: &LightClient) { // Parse the args. // 1 - Destination address. T or Z address if args.len() < 2 || args.len() > 3 { @@ -136,7 +136,7 @@ impl Command for SaveCommand { "Save wallet file to disk".to_string() } - fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &LightClient) { lightclient.do_save(); } } @@ -151,7 +151,7 @@ impl Command for SeedCommand { "Display the seed phrase".to_string() } - fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &LightClient) { let phrase = lightclient.do_seed_phrase(); println!("PLEASE SAVE YOUR SEED PHRASE CAREFULLY AND DO NOT SHARE IT"); @@ -171,7 +171,7 @@ impl Command for TransactionsCommand { "List all transactions in the wallet".to_string() } - fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &LightClient) { let txns = lightclient.do_list_transactions(); println!("{}", txns.pretty(2)); } @@ -188,7 +188,7 @@ impl Command for NotesCommand { "List all sapling notes in the wallet".to_string() } - fn exec(&self, args: &[&str], lightclient: &mut LightClient) { + fn exec(&self, args: &[&str], lightclient: &LightClient) { // Parse the args. if args.len() > 1 { self.help(); @@ -222,7 +222,7 @@ impl Command for QuitCommand { "Quit the lightwallet, saving state to disk".to_string() } - fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &LightClient) { lightclient.do_save(); } } @@ -248,7 +248,7 @@ pub fn get_commands() -> Box>> { Box::new(map) } -pub fn do_user_command(cmd: &str, args: &Vec<&str>, lightclient: &mut LightClient) { +pub fn do_user_command(cmd: &str, args: &Vec<&str>, lightclient: &LightClient) { match get_commands().get(cmd) { Some(cmd) => cmd.exec(args, lightclient), None => { diff --git a/rust-lightclient/src/main.rs b/rust-lightclient/src/main.rs index be0ee23..ac5a13c 100644 --- a/rust-lightclient/src/main.rs +++ b/rust-lightclient/src/main.rs @@ -4,6 +4,8 @@ mod address; mod prover; mod commands; +use std::sync::Arc; + use lightclient::LightClient; use rustyline::error::ReadlineError; @@ -30,23 +32,28 @@ pub fn main() { let seed: Option = matches.value_of("seed").map(|s| s.to_string()); + let lightclient = match LightClient::new(seed) { + Ok(lc) => Arc::new(lc), + Err(e) => { eprintln!("Failed to start wallet. Error was:\n{}", e); return; } + }; + let (command_tx, command_rx) = std::sync::mpsc::channel::<(String, Vec)>(); let (resp_tx, resp_rx) = std::sync::mpsc::channel::(); + let lc = lightclient.clone(); std::thread::spawn(move || { - let mut lightclient = match LightClient::new(seed) { - Ok(lc) => lc, - Err(e) => { eprintln!("Failed to start wallet. Error was:\n{}", e); return; } - }; - println!("Starting Light Client"); loop { match command_rx.recv() { Ok((cmd, args)) => { let args = args.iter().map(|s| s.as_ref()).collect(); - commands::do_user_command(&cmd, &args, &mut lightclient); + commands::do_user_command(&cmd, &args, &lc); resp_tx.send("Finished command".to_string()).unwrap(); + + if cmd == "quit" { + break; + } }, _ => {} } @@ -62,7 +69,7 @@ pub fn main() { println!("Ready!"); loop { - let readline = rl.readline(">>"); //&format!("Block:{} (type 'help') >> ", lightclient.last_scanned_height())); + let readline = rl.readline(&format!("Block:{} (type 'help') >> ", lightclient.last_scanned_height())); match readline { Ok(line) => { rl.add_history_entry(line.as_str()); @@ -97,12 +104,12 @@ pub fn main() { }, Err(ReadlineError::Interrupted) => { println!("CTRL-C"); - //lightclient.do_save(); + lightclient.do_save(); break }, Err(ReadlineError::Eof) => { println!("CTRL-D"); - //lightclient.do_save(); + lightclient.do_save(); break }, Err(err) => {