mirror of
https://github.com/Qortal/piratewallet-light-cli.git
synced 2025-02-12 02:05:47 +00:00
Sync before commands
This commit is contained in:
parent
059db8cd1c
commit
e7ee69154d
@ -74,6 +74,7 @@ impl Command for InfoCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String {
|
fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String {
|
||||||
|
lightclient.do_sync();
|
||||||
lightclient.do_info()
|
lightclient.do_info()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,8 +90,9 @@ impl Command for AddressCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String {
|
fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String {
|
||||||
let res = lightclient.do_address();
|
lightclient.do_sync();
|
||||||
format!("{}", res.pretty(2))
|
|
||||||
|
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};
|
let memo = if args.len() == 3 { Some(args[2].to_string()) } else {None};
|
||||||
|
|
||||||
|
lightclient.do_sync();
|
||||||
|
|
||||||
lightclient.do_send(args[0], value, memo)
|
lightclient.do_send(args[0], value, memo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -168,8 +172,9 @@ impl Command for TransactionsCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String {
|
fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String {
|
||||||
let txns = lightclient.do_list_transactions();
|
lightclient.do_sync();
|
||||||
format!("{}", txns.pretty(2))
|
|
||||||
|
format!("{}", lightclient.do_list_transactions().pretty(2))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,8 +205,9 @@ impl Command for NotesCommand {
|
|||||||
false
|
false
|
||||||
};
|
};
|
||||||
|
|
||||||
let txns = lightclient.do_list_notes(all_notes);
|
lightclient.do_sync();
|
||||||
format!("{}", txns.pretty(2))
|
|
||||||
|
format!("{}", lightclient.do_list_notes(all_notes).pretty(2))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -366,17 +366,19 @@ impl LightClient {
|
|||||||
// 3. Find all new Txns that don't have the full Tx, and get them as full transactions
|
// 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
|
// and scan them, mainly to get the memos
|
||||||
let mut last_scanned_height = self.wallet.last_scanned_height() as u64;
|
let mut last_scanned_height = self.wallet.last_scanned_height() as u64;
|
||||||
let mut end_height = last_scanned_height + 1000;
|
|
||||||
|
|
||||||
// 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));
|
||||||
// TODO: this could be a oneshot channel
|
let lbh = latest_block_height.clone();
|
||||||
let latest_block_height_clone = latest_block_height.clone();
|
|
||||||
self.fetch_latest_block(move |block: BlockId| {
|
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);
|
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));
|
let bytes_downloaded = Arc::new(AtomicUsize::new(0));
|
||||||
|
|
||||||
// Fetch CompactBlocks in increments
|
// Fetch CompactBlocks in increments
|
||||||
@ -384,8 +386,10 @@ impl LightClient {
|
|||||||
let local_light_wallet = self.wallet.clone();
|
let local_light_wallet = self.wallet.clone();
|
||||||
let local_bytes_downloaded = bytes_downloaded.clone();
|
let local_bytes_downloaded = bytes_downloaded.clone();
|
||||||
|
|
||||||
print!("Syncing {}/{}, Balance = {} \r",
|
// Show updates only if we're syncing a lot of blocks
|
||||||
last_scanned_height, last_block, self.wallet.balance(None));
|
if end_height - last_scanned_height > 100 {
|
||||||
|
print!("Syncing {}/{}\r", last_scanned_height, last_block);
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch compact blocks
|
// Fetch compact blocks
|
||||||
self.fetch_blocks(last_scanned_height, end_height,
|
self.fetch_blocks(last_scanned_height, end_height,
|
||||||
@ -416,6 +420,7 @@ impl LightClient {
|
|||||||
end_height = last_block;
|
end_height = last_block;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
println!(); // Print a new line, to finalize the syncing updates
|
||||||
|
|
||||||
let mut responses = vec![];
|
let mut responses = vec![];
|
||||||
responses.push(format!("Synced to {}, Downloaded {} kB", last_block, bytes_downloaded.load(Ordering::SeqCst) / 1024));
|
responses.push(format!("Synced to {}, Downloaded {} kB", last_block, bytes_downloaded.load(Ordering::SeqCst) / 1024));
|
||||||
|
@ -32,18 +32,22 @@ pub fn main() {
|
|||||||
|
|
||||||
let seed: Option<String> = matches.value_of("seed").map(|s| s.to_string());
|
let seed: Option<String> = matches.value_of("seed").map(|s| s.to_string());
|
||||||
|
|
||||||
|
println!("Creating Light Wallet");
|
||||||
|
|
||||||
let lightclient = match LightClient::new(seed) {
|
let lightclient = match LightClient::new(seed) {
|
||||||
Ok(lc) => Arc::new(lc),
|
Ok(lc) => Arc::new(lc),
|
||||||
Err(e) => { eprintln!("Failed to start wallet. Error was:\n{}", e); return; }
|
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<String>)>();
|
let (command_tx, command_rx) = std::sync::mpsc::channel::<(String, Vec<String>)>();
|
||||||
let (resp_tx, resp_rx) = std::sync::mpsc::channel::<String>();
|
let (resp_tx, resp_rx) = std::sync::mpsc::channel::<String>();
|
||||||
|
|
||||||
let lc = lightclient.clone();
|
let lc = lightclient.clone();
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
println!("Starting Light Client");
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match command_rx.recv() {
|
match command_rx.recv() {
|
||||||
Ok((cmd, args)) => {
|
Ok((cmd, args)) => {
|
||||||
@ -58,8 +62,6 @@ pub fn main() {
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("finished running");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// `()` can be used when no completer is required
|
// `()` can be used when no completer is required
|
||||||
|
Loading…
x
Reference in New Issue
Block a user