diff --git a/rust-lightclient/src/lightclient.rs b/rust-lightclient/src/lightclient.rs index 2a671f3..f618b69 100644 --- a/rust-lightclient/src/lightclient.rs +++ b/rust-lightclient/src/lightclient.rs @@ -48,7 +48,7 @@ impl LightClient { } - pub fn new(seed_phrase: Option<&str>) -> io::Result { + pub fn new(seed_phrase: Option) -> 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 if !seed_phrase.is_none() { @@ -150,7 +150,7 @@ impl LightClient { println!("ERR = {:?}", e); }); - tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap() + tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap(); } pub fn do_seed_phrase(&self) -> String { @@ -668,7 +668,7 @@ impl LightClient { println!("ERR = {:?}", e); }); - tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap() + tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap(); } pub fn broadcast_raw_tx(&self, tx_bytes: Box<[u8]>) { @@ -686,7 +686,7 @@ impl LightClient { println!("ERR = {:?}", e); }); - tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap() + tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap(); } pub fn fetch_latest_block(&self, mut c : F) @@ -705,7 +705,7 @@ impl LightClient { println!("ERR = {:?}", e); }); - tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap() + tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap(); } fn make_grpc_client(&self, uri: http::Uri) -> Result + Send>, Box> { diff --git a/rust-lightclient/src/lightwallet.rs b/rust-lightclient/src/lightwallet.rs index 71d6e78..559b78d 100644 --- a/rust-lightclient/src/lightwallet.rs +++ b/rust-lightclient/src/lightwallet.rs @@ -566,7 +566,7 @@ impl LightWallet { (extsk, extfvk, address) } - pub fn new(seed_phrase: Option<&str>) -> io::Result { + pub fn new(seed_phrase: Option) -> io::Result { use rand::{FromEntropy, ChaChaRng, Rng}; let mut seed_bytes = [0u8; 32]; diff --git a/rust-lightclient/src/main.rs b/rust-lightclient/src/main.rs index c1ba335..be0ee23 100644 --- a/rust-lightclient/src/main.rs +++ b/rust-lightclient/src/main.rs @@ -28,15 +28,32 @@ pub fn main() { .takes_value(true)) .get_matches(); - let mut lightclient = match LightClient::new(matches.value_of("seed")) { - Ok(lc) => lc, - Err(e) => { - eprintln!("Failed to start wallet. Error was:\n{}", e); - return; - } - }; + let seed: Option = matches.value_of("seed").map(|s| s.to_string()); - println!("Starting Light Client"); + let (command_tx, command_rx) = std::sync::mpsc::channel::<(String, Vec)>(); + let (resp_tx, resp_rx) = std::sync::mpsc::channel::(); + + 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); + resp_tx.send("Finished command".to_string()).unwrap(); + }, + _ => {} + } + } + + println!("finished running"); + }); // `()` can be used when no completer is required let mut rl = Editor::<()>::new(); @@ -45,7 +62,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()); @@ -63,8 +80,15 @@ pub fn main() { } let cmd = cmd_args.remove(0); - let args: Vec<&str> = cmd_args.iter().map(|s| s.as_ref()).collect(); - commands::do_user_command(&cmd, &args, &mut lightclient); + let args: Vec = cmd_args; + // commands::do_user_command(&cmd, &args, &mut lightclient); + command_tx.send((cmd, args)).unwrap(); + + // Wait for the response + match resp_rx.recv() { + Ok(response) => println!("{}", response), + _ => { eprintln!("Error receiveing response");} + } // Special check for Quit command. if line == "quit" { @@ -73,12 +97,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) => {