|
|
@ -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 WALLET_NAME: &str = "zecwallet-light-wallet.dat"; |
|
|
|
pub const LOGFILE_NAME: &str = "zecwallet-light-wallet.debug.log"; |
|
|
|
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)] |
|
|
|
#[derive(Clone, Debug)] |
|
|
|
pub struct LightClientConfig { |
|
|
|
pub struct LightClientConfig { |
|
|
@ -203,6 +219,7 @@ pub struct LightClient { |
|
|
|
pub sapling_spend : Vec<u8>, |
|
|
|
pub sapling_spend : Vec<u8>, |
|
|
|
|
|
|
|
|
|
|
|
sync_lock : Mutex<()>, |
|
|
|
sync_lock : Mutex<()>, |
|
|
|
|
|
|
|
sync_status : Arc<RwLock<WalletStatus>>, // The current syncing status of the Wallet.
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl LightClient { |
|
|
|
impl LightClient { |
|
|
@ -235,6 +252,7 @@ impl LightClient { |
|
|
|
sapling_output : vec![],
|
|
|
|
sapling_output : vec![],
|
|
|
|
sapling_spend : vec![], |
|
|
|
sapling_spend : vec![], |
|
|
|
sync_lock : Mutex::new(()), |
|
|
|
sync_lock : Mutex::new(()), |
|
|
|
|
|
|
|
sync_status : Arc::new(RwLock::new(WalletStatus::new())), |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
l.set_wallet_initial_state(0); |
|
|
|
l.set_wallet_initial_state(0); |
|
|
@ -260,6 +278,7 @@ impl LightClient { |
|
|
|
sapling_output : vec![],
|
|
|
|
sapling_output : vec![],
|
|
|
|
sapling_spend : vec![], |
|
|
|
sapling_spend : vec![], |
|
|
|
sync_lock : Mutex::new(()), |
|
|
|
sync_lock : Mutex::new(()), |
|
|
|
|
|
|
|
sync_status : Arc::new(RwLock::new(WalletStatus::new())), |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
l.set_wallet_initial_state(latest_block); |
|
|
|
l.set_wallet_initial_state(latest_block); |
|
|
@ -283,6 +302,7 @@ impl LightClient { |
|
|
|
sapling_output : vec![],
|
|
|
|
sapling_output : vec![],
|
|
|
|
sapling_spend : vec![], |
|
|
|
sapling_spend : vec![], |
|
|
|
sync_lock : Mutex::new(()), |
|
|
|
sync_lock : Mutex::new(()), |
|
|
|
|
|
|
|
sync_status : Arc::new(RwLock::new(WalletStatus::new())), |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
println!("Setting birthday to {}", birthday); |
|
|
|
println!("Setting birthday to {}", birthday); |
|
|
@ -310,6 +330,7 @@ impl LightClient { |
|
|
|
sapling_output : vec![],
|
|
|
|
sapling_output : vec![],
|
|
|
|
sapling_spend : vec![], |
|
|
|
sapling_spend : vec![], |
|
|
|
sync_lock : Mutex::new(()), |
|
|
|
sync_lock : Mutex::new(()), |
|
|
|
|
|
|
|
sync_status : Arc::new(RwLock::new(WalletStatus::new())), |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
lc.read_sapling_params(); |
|
|
|
lc.read_sapling_params(); |
|
|
@ -735,6 +756,11 @@ impl LightClient { |
|
|
|
response |
|
|
|
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 { |
|
|
|
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
|
|
|
|
// 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.
|
|
|
|
// If we allow multiple syncs, they'll all get jumbled up.
|
|
|
@ -750,11 +776,13 @@ impl LightClient { |
|
|
|
// This will hold the latest block fetched from the RPC
|
|
|
|
// This will hold the latest block fetched from the RPC
|
|
|
|
let latest_block_height = Arc::new(AtomicU64::new(0)); |
|
|
|
let latest_block_height = Arc::new(AtomicU64::new(0)); |
|
|
|
let lbh = latest_block_height.clone(); |
|
|
|
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); |
|
|
|
lbh.store(block.height, Ordering::SeqCst); |
|
|
|
}); |
|
|
|
}); |
|
|
|
let latest_block = latest_block_height.load(Ordering::SeqCst); |
|
|
|
let latest_block = latest_block_height.load(Ordering::SeqCst); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if latest_block < last_scanned_height { |
|
|
|
if latest_block < last_scanned_height { |
|
|
|
let w = format!("Server's latest block({}) is behind ours({})", latest_block, last_scanned_height); |
|
|
|
let w = format!("Server's latest block({}) is behind ours({})", latest_block, last_scanned_height); |
|
|
|
warn!("{}", w); |
|
|
|
warn!("{}", w); |
|
|
@ -772,6 +800,13 @@ impl LightClient { |
|
|
|
return "".to_string(); |
|
|
|
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
|
|
|
|
// Count how many bytes we've downloaded
|
|
|
|
let bytes_downloaded = Arc::new(AtomicUsize::new(0)); |
|
|
|
let bytes_downloaded = Arc::new(AtomicUsize::new(0)); |
|
|
|
|
|
|
|
|
|
|
@ -795,11 +830,18 @@ impl LightClient { |
|
|
|
info!("Start height is {}", start_height); |
|
|
|
info!("Start height is {}", start_height); |
|
|
|
|
|
|
|
|
|
|
|
// Show updates only if we're syncing a lot of blocks
|
|
|
|
// 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); |
|
|
|
print!("Syncing {}/{}\r", start_height, latest_block); |
|
|
|
io::stdout().flush().ok().expect("Could not flush stdout"); |
|
|
|
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
|
|
|
|
// Fetch compact blocks
|
|
|
|
info!("Fetching blocks {}-{}", start_height, end_height); |
|
|
|
info!("Fetching blocks {}-{}", start_height, end_height); |
|
|
|
|
|
|
|
|
|
|
@ -909,6 +951,12 @@ impl LightClient { |
|
|
|
|
|
|
|
|
|
|
|
info!("Synced to {}, Downloaded {} kB", latest_block, bytes_downloaded.load(Ordering::SeqCst) / 1024); |
|
|
|
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)); |
|
|
|
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
|
|
|
|
// Get the Raw transaction for all the wallet transactions
|
|
|
|
|
|
|
|
|
|
|
|