diff --git a/rust-lightclient/.gitignore b/rust-lightclient/.gitignore index 1ec7ed7..78cf4c1 100644 --- a/rust-lightclient/.gitignore +++ b/rust-lightclient/.gitignore @@ -1,3 +1,4 @@ target/ Cargo.lock -.vscode/ \ No newline at end of file +.vscode/ +history.txt diff --git a/rust-lightclient/Cargo.toml b/rust-lightclient/Cargo.toml index d66fedb..e70aa75 100644 --- a/rust-lightclient/Cargo.toml +++ b/rust-lightclient/Cargo.toml @@ -19,6 +19,7 @@ tower-service = "0.2" tower-util = "0.1" hex = "0.3" protobuf = "2" +rustyline = "5.0.2" [dependencies.bellman] diff --git a/rust-lightclient/src/lightclient.rs b/rust-lightclient/src/lightclient.rs index 101a809..e29f40d 100644 --- a/rust-lightclient/src/lightclient.rs +++ b/rust-lightclient/src/lightclient.rs @@ -151,7 +151,11 @@ impl Client { } let hash = match hex::decode(hash) { - Ok(hash) => BlockHash::from_slice(&hash), + Ok(hash) => { + let mut r = hash; + r.reverse(); + BlockHash::from_slice(&r) + }, Err(e) => { eprintln!("{}", e); return false; @@ -263,7 +267,7 @@ impl Client { // If the last scanned block is rescanned, check it still matches. if let Some(hash) = self.blocks.read().unwrap().last().map(|block| block.hash) { if block.hash() != hash { - eprintln!("Block hash does not match"); + eprintln!("Block hash does not match for block {}. {} vs {}", height, block.hash(), hash); return false; } } diff --git a/rust-lightclient/src/main.rs b/rust-lightclient/src/main.rs index 8bc8ee2..0f27485 100644 --- a/rust-lightclient/src/main.rs +++ b/rust-lightclient/src/main.rs @@ -14,6 +14,9 @@ mod lightclient; mod address; mod prover; +use rustyline::error::ReadlineError; +use rustyline::Editor; + use crate::grpc_client::{ChainSpec, BlockId, BlockRange}; pub mod grpc_client { @@ -23,6 +26,43 @@ pub mod grpc_client { pub fn main() { + // `()` can be used when no completer is required + let mut rl = Editor::<()>::new(); + if rl.load_history("history.txt").is_err() { + println!("No previous history."); + } + loop { + let readline = rl.readline(">> "); + match readline { + Ok(line) => { + rl.add_history_entry(line.as_str()); + do_user_command(line); + }, + Err(ReadlineError::Interrupted) => { + println!("CTRL-C"); + break + }, + Err(ReadlineError::Eof) => { + println!("CTRL-D"); + break + }, + Err(err) => { + println!("Error: {:?}", err); + break + } + } + } + rl.save_history("history.txt").unwrap(); +} + +pub fn do_user_command(cmd: String) { + match cmd.as_ref() { + "sync" => { do_sync() } + _ => { println!("Unknown command {}", cmd); } + } +} + +pub fn do_sync() { let lightclient = Arc::new(lightclient::Client::new()); lightclient.set_initial_block(500000, "004fada8d4dbc5e80b13522d2c6bd0116113c9b7197f0c6be69bc7a62f2824cd", @@ -47,7 +87,7 @@ pub fn main() { let simple_callback = move |encoded_block: &[u8]| { local_lightclient.scan_block(encoded_block); - println!("Block Height: {}, Balance = {}", local_lightclient.last_scanned_height(), local_lightclient.balance()); + print!("Block Height: {}, Balance = {}\r", local_lightclient.last_scanned_height(), local_lightclient.balance()); }; read_blocks(last_scanned_height, end_height, simple_callback);