diff --git a/rust-lightclient/src/commands.rs b/rust-lightclient/src/commands.rs index 3bb7637..ec2e9f6 100644 --- a/rust-lightclient/src/commands.rs +++ b/rust-lightclient/src/commands.rs @@ -74,6 +74,7 @@ impl Command for InfoCommand { } fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { + lightclient.do_sync(); lightclient.do_info() } } @@ -88,9 +89,10 @@ impl Command for AddressCommand { "List all current addresses".to_string() } - fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { - let res = lightclient.do_address(); - format!("{}", res.pretty(2)) + fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { + lightclient.do_sync(); + + format!("{}", lightclient.do_address().pretty(2)) } } @@ -123,6 +125,8 @@ impl Command for SendCommand { let memo = if args.len() == 3 { Some(args[2].to_string()) } else {None}; + lightclient.do_sync(); + lightclient.do_send(args[0], value, memo) } } @@ -168,8 +172,9 @@ impl Command for TransactionsCommand { } fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { - let txns = lightclient.do_list_transactions(); - format!("{}", txns.pretty(2)) + lightclient.do_sync(); + + format!("{}", lightclient.do_list_transactions().pretty(2)) } } @@ -200,8 +205,9 @@ impl Command for NotesCommand { false }; - let txns = lightclient.do_list_notes(all_notes); - format!("{}", txns.pretty(2)) + lightclient.do_sync(); + + format!("{}", lightclient.do_list_notes(all_notes).pretty(2)) } } diff --git a/rust-lightclient/src/lightclient.rs b/rust-lightclient/src/lightclient.rs index 9334775..c32c719 100644 --- a/rust-lightclient/src/lightclient.rs +++ b/rust-lightclient/src/lightclient.rs @@ -365,18 +365,20 @@ impl LightClient { // 2. Get all the blocks that we don't have // 3. Find all new Txns that don't have the full Tx, and get them as full transactions // and scan them, mainly to get the memos - let mut last_scanned_height = self.wallet.last_scanned_height() as u64; - let mut end_height = last_scanned_height + 1000; + let mut last_scanned_height = self.wallet.last_scanned_height() as u64; // This will hold the latest block fetched from the RPC let latest_block_height = Arc::new(AtomicU64::new(0)); - // TODO: this could be a oneshot channel - let latest_block_height_clone = latest_block_height.clone(); + let lbh = latest_block_height.clone(); self.fetch_latest_block(move |block: BlockId| { - latest_block_height_clone.store(block.height, Ordering::SeqCst); + lbh.store(block.height, Ordering::SeqCst); }); let last_block = latest_block_height.load(Ordering::SeqCst); + // Get the end height to scan to. + let mut end_height = std::cmp::min(last_scanned_height + 1000, last_block); + + // Count how many bytes we've downloaded let bytes_downloaded = Arc::new(AtomicUsize::new(0)); // Fetch CompactBlocks in increments @@ -384,8 +386,10 @@ impl LightClient { let local_light_wallet = self.wallet.clone(); let local_bytes_downloaded = bytes_downloaded.clone(); - print!("Syncing {}/{}, Balance = {} \r", - last_scanned_height, last_block, self.wallet.balance(None)); + // Show updates only if we're syncing a lot of blocks + if end_height - last_scanned_height > 100 { + print!("Syncing {}/{}\r", last_scanned_height, last_block); + } // Fetch compact blocks self.fetch_blocks(last_scanned_height, end_height, @@ -416,6 +420,7 @@ impl LightClient { end_height = last_block; } } + println!(); // Print a new line, to finalize the syncing updates let mut responses = vec![]; responses.push(format!("Synced to {}, Downloaded {} kB", last_block, bytes_downloaded.load(Ordering::SeqCst) / 1024)); diff --git a/rust-lightclient/src/main.rs b/rust-lightclient/src/main.rs index 5f6be16..bcdc516 100644 --- a/rust-lightclient/src/main.rs +++ b/rust-lightclient/src/main.rs @@ -32,18 +32,22 @@ pub fn main() { let seed: Option = matches.value_of("seed").map(|s| s.to_string()); + println!("Creating Light Wallet"); + let lightclient = match LightClient::new(seed) { Ok(lc) => Arc::new(lc), Err(e) => { eprintln!("Failed to start wallet. Error was:\n{}", e); return; } }; + // At startup, run a sync + let sync_update = lightclient.do_sync(); + println!("{}", sync_update); + 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 || { - println!("Starting Light Client"); - loop { match command_rx.recv() { Ok((cmd, args)) => { @@ -58,8 +62,6 @@ pub fn main() { _ => {} } } - - println!("finished running"); }); // `()` can be used when no completer is required