Browse Source

Add sync status command

checkpoints
Aditya Kulkarni 5 years ago
parent
commit
17bbb71a69
  1. 29
      lib/src/commands.rs
  2. 54
      lib/src/lightclient.rs

29
lib/src/commands.rs

@ -32,6 +32,34 @@ impl Command for SyncCommand {
}
}
struct SyncStatusCommand {}
impl Command for SyncStatusCommand {
fn help(&self) -> String {
let mut h = vec![];
h.push("Get the sync status of the wallet");
h.push("Usage:");
h.push("syncstatus");
h.push("");
h.join("\n")
}
fn short_help(&self) -> String {
"Get the sync status of the wallet".to_string()
}
fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String {
let status = lightclient.do_scan_status();
match status.is_syncing {
false => object!{ "syncing" => "false" },
true => object!{ "syncing" => "true",
"synced_blocks" => status.synced_blocks,
"total_blocks" => status.total_blocks }
}.pretty(2)
}
}
struct RescanCommand {}
impl Command for RescanCommand {
fn help(&self) -> String {
@ -691,6 +719,7 @@ pub fn get_commands() -> Box<HashMap<String, Box<dyn Command>>> {
let mut map: HashMap<String, Box<dyn Command>> = HashMap::new();
map.insert("sync".to_string(), Box::new(SyncCommand{}));
map.insert("syncstatus".to_string(), Box::new(SyncStatusCommand{}));
map.insert("rescan".to_string(), Box::new(RescanCommand{}));
map.insert("help".to_string(), Box::new(HelpCommand{}));
map.insert("balance".to_string(), Box::new(BalanceCommand{}));

54
lib/src/lightclient.rs

@ -31,6 +31,22 @@ pub const DEFAULT_SERVER: &str = "https://lightd-main.zecwallet.co:443";
pub const WALLET_NAME: &str = "zecwallet-light-wallet.dat";
pub const LOGFILE_NAME: &str = "zecwallet-light-wallet.debug.log";
#[derive(Clone, Debug)]
pub struct WalletStatus {
pub is_syncing: bool,
pub total_blocks: u64,
pub synced_blocks: u64,
}
impl WalletStatus {
pub fn new() -> Self {
WalletStatus {
is_syncing: false,
total_blocks: 0,
synced_blocks: 0
}
}
}
#[derive(Clone, Debug)]
pub struct LightClientConfig {
@ -203,6 +219,7 @@ pub struct LightClient {
pub sapling_spend : Vec<u8>,
sync_lock : Mutex<()>,
sync_status : Arc<RwLock<WalletStatus>>, // The current syncing status of the Wallet.
}
impl LightClient {
@ -235,6 +252,7 @@ impl LightClient {
sapling_output : vec![],
sapling_spend : vec![],
sync_lock : Mutex::new(()),
sync_status : Arc::new(RwLock::new(WalletStatus::new())),
};
l.set_wallet_initial_state(0);
@ -260,6 +278,7 @@ impl LightClient {
sapling_output : vec![],
sapling_spend : vec![],
sync_lock : Mutex::new(()),
sync_status : Arc::new(RwLock::new(WalletStatus::new())),
};
l.set_wallet_initial_state(latest_block);
@ -283,6 +302,7 @@ impl LightClient {
sapling_output : vec![],
sapling_spend : vec![],
sync_lock : Mutex::new(()),
sync_status : Arc::new(RwLock::new(WalletStatus::new())),
};
println!("Setting birthday to {}", birthday);
@ -310,6 +330,7 @@ impl LightClient {
sapling_output : vec![],
sapling_spend : vec![],
sync_lock : Mutex::new(()),
sync_status : Arc::new(RwLock::new(WalletStatus::new())),
};
lc.read_sapling_params();
@ -735,6 +756,11 @@ impl LightClient {
response
}
/// Return the syncing status of the wallet
pub fn do_scan_status(&self) -> WalletStatus {
self.sync_status.read().unwrap().clone()
}
pub fn do_sync(&self, print_updates: bool) -> String {
// We can only do one sync at a time because we sync blocks in serial order
// If we allow multiple syncs, they'll all get jumbled up.
@ -750,10 +776,12 @@ impl LightClient {
// This will hold the latest block fetched from the RPC
let latest_block_height = Arc::new(AtomicU64::new(0));
let lbh = latest_block_height.clone();
fetch_latest_block(&self.get_server_uri(), self.config.no_cert_verification, move |block: BlockId| {
fetch_latest_block(&self.get_server_uri(), self.config.no_cert_verification,
move |block: BlockId| {
lbh.store(block.height, Ordering::SeqCst);
});
let latest_block = latest_block_height.load(Ordering::SeqCst);
if latest_block < last_scanned_height {
let w = format!("Server's latest block({}) is behind ours({})", latest_block, last_scanned_height);
@ -772,6 +800,13 @@ impl LightClient {
return "".to_string();
}
{
let mut status = self.sync_status.write().unwrap();
status.is_syncing = true;
status.synced_blocks = last_scanned_height;
status.total_blocks = latest_block;
}
// Count how many bytes we've downloaded
let bytes_downloaded = Arc::new(AtomicUsize::new(0));
@ -795,11 +830,18 @@ impl LightClient {
info!("Start height is {}", start_height);
// Show updates only if we're syncing a lot of blocks
if print_updates && end_height - start_height > 100 {
if print_updates && (latest_block - start_height) > 100 {
print!("Syncing {}/{}\r", start_height, latest_block);
io::stdout().flush().ok().expect("Could not flush stdout");
}
{
let mut status = self.sync_status.write().unwrap();
status.is_syncing = true;
status.synced_blocks = start_height;
status.total_blocks = latest_block;
}
// Fetch compact blocks
info!("Fetching blocks {}-{}", start_height, end_height);
@ -909,7 +951,13 @@ impl LightClient {
info!("Synced to {}, Downloaded {} kB", latest_block, bytes_downloaded.load(Ordering::SeqCst) / 1024);
responses.push(format!("Synced to {}, Downloaded {} kB", latest_block, bytes_downloaded.load(Ordering::SeqCst) / 1024));
{
let mut status = self.sync_status.write().unwrap();
status.is_syncing = false;
status.synced_blocks = latest_block;
status.total_blocks = latest_block;
}
// Get the Raw transaction for all the wallet transactions
// We need to first copy over the Txids from the wallet struct, because

Loading…
Cancel
Save