Skip to content

Commit

Permalink
feat: update logging of electrum examples
Browse files Browse the repository at this point in the history
* Syncing with `example_electrum` now shows progress as a percentage.
* Flush stdout more aggressively.
  • Loading branch information
evanlinjin committed May 10, 2024
1 parent a6fdfb2 commit b1f861b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
38 changes: 32 additions & 6 deletions example-crates/example_electrum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,11 @@ fn main() -> anyhow::Result<()> {
let mut once = BTreeSet::new();
move |k, spk_i, _| {
if once.insert(k) {
eprint!("\nScanning {}: ", k);
eprint!("\nScanning {}: {} ", k, spk_i);
} else {
eprint!("{} ", spk_i);
}
let _ = io::stdout().flush();
io::stdout().flush().expect("must flush");
}
})
};
Expand Down Expand Up @@ -229,7 +229,7 @@ fn main() -> anyhow::Result<()> {
.map(|(k, i, spk)| (k.to_owned(), i, spk.to_owned()))
.collect::<Vec<_>>();
request = request.chain_spks(all_spks.into_iter().map(|(k, spk_i, spk)| {
eprintln!("scanning {}: {}", k, spk_i);
eprint!("Scanning {}: {}", k, spk_i);
spk
}));
}
Expand All @@ -241,7 +241,7 @@ fn main() -> anyhow::Result<()> {
.collect::<Vec<_>>();
request =
request.chain_spks(unused_spks.into_iter().map(move |(k, spk_i, spk)| {
eprintln!(
eprint!(
"Checking if address {} {}:{} has been used",
Address::from_script(&spk, args.network).unwrap(),
k,
Expand All @@ -260,7 +260,7 @@ fn main() -> anyhow::Result<()> {
.map(|(_, utxo)| utxo)
.collect::<Vec<_>>();
request = request.chain_outpoints(utxos.into_iter().map(|utxo| {
eprintln!(
eprint!(
"Checking if outpoint {} (value: {}) has been spent",
utxo.outpoint, utxo.txout.value
);
Expand All @@ -279,10 +279,36 @@ fn main() -> anyhow::Result<()> {
request = request.chain_txids(
unconfirmed_txids
.into_iter()
.inspect(|txid| eprintln!("Checking if {} is confirmed yet", txid)),
.inspect(|txid| eprint!("Checking if {} is confirmed yet", txid)),
);
}

let total_spks = request.spks.len();
let total_txids = request.txids.len();
let total_ops = request.outpoints.len();
request = request
.inspect_spks({
let mut visited = 0;
move |_| {
visited += 1;
eprintln!(" [ {:>6.2}% ]", (visited * 100) as f32 / total_spks as f32)
}
})
.inspect_txids({
let mut visited = 0;
move |_| {
visited += 1;
eprintln!(" [ {:>6.2}% ]", (visited * 100) as f32 / total_txids as f32)
}
})
.inspect_outpoints({
let mut visited = 0;
move |_| {
visited += 1;
eprintln!(" [ {:>6.2}% ]", (visited * 100) as f32 / total_ops as f32)
}
});

let res = client
.sync(request, scan_options.batch_size)
.context("scanning the blockchain")?;
Expand Down
21 changes: 14 additions & 7 deletions example-crates/wallet_electrum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const SEND_AMOUNT: Amount = Amount::from_sat(5000);
const STOP_GAP: usize = 50;
const BATCH_SIZE: usize = 5;

use std::io::Write;
use std::str::FromStr;

use bdk::bitcoin::{Address, Amount};
Expand Down Expand Up @@ -38,13 +39,19 @@ fn main() -> Result<(), anyhow::Error> {
print!("Syncing...");
let client = electrum_client::Client::new("ssl://electrum.blockstream.info:60002")?;

let request = wallet.start_full_scan().inspect_spks_for_all_keychains({
let mut once = HashSet::<KeychainKind>::new();
move |k, spk_i, _| match once.insert(k) {
true => print!("\nScanning keychain [{:?}]", k),
false => print!(" {:<3}", spk_i),
}
});
let request = wallet
.start_full_scan()
.inspect_spks_for_all_keychains({
let mut once = HashSet::<KeychainKind>::new();
move |k, spk_i, _| {
if once.insert(k) {
print!("\nScanning keychain [{:?}]", k)
} else {
print!(" {:<3}", spk_i)
}
}
})
.inspect_spks_for_all_keychains(|_, _, _| std::io::stdout().flush().expect("must flush"));

let mut update = client
.full_scan(request, STOP_GAP, BATCH_SIZE)?
Expand Down

0 comments on commit b1f861b

Please sign in to comment.