Skip to content

Commit b1f861b

Browse files
committed
feat: update logging of electrum examples
* Syncing with `example_electrum` now shows progress as a percentage. * Flush stdout more aggressively.
1 parent a6fdfb2 commit b1f861b

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

example-crates/example_electrum/src/main.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,11 @@ fn main() -> anyhow::Result<()> {
180180
let mut once = BTreeSet::new();
181181
move |k, spk_i, _| {
182182
if once.insert(k) {
183-
eprint!("\nScanning {}: ", k);
183+
eprint!("\nScanning {}: {} ", k, spk_i);
184184
} else {
185185
eprint!("{} ", spk_i);
186186
}
187-
let _ = io::stdout().flush();
187+
io::stdout().flush().expect("must flush");
188188
}
189189
})
190190
};
@@ -229,7 +229,7 @@ fn main() -> anyhow::Result<()> {
229229
.map(|(k, i, spk)| (k.to_owned(), i, spk.to_owned()))
230230
.collect::<Vec<_>>();
231231
request = request.chain_spks(all_spks.into_iter().map(|(k, spk_i, spk)| {
232-
eprintln!("scanning {}: {}", k, spk_i);
232+
eprint!("Scanning {}: {}", k, spk_i);
233233
spk
234234
}));
235235
}
@@ -241,7 +241,7 @@ fn main() -> anyhow::Result<()> {
241241
.collect::<Vec<_>>();
242242
request =
243243
request.chain_spks(unused_spks.into_iter().map(move |(k, spk_i, spk)| {
244-
eprintln!(
244+
eprint!(
245245
"Checking if address {} {}:{} has been used",
246246
Address::from_script(&spk, args.network).unwrap(),
247247
k,
@@ -260,7 +260,7 @@ fn main() -> anyhow::Result<()> {
260260
.map(|(_, utxo)| utxo)
261261
.collect::<Vec<_>>();
262262
request = request.chain_outpoints(utxos.into_iter().map(|utxo| {
263-
eprintln!(
263+
eprint!(
264264
"Checking if outpoint {} (value: {}) has been spent",
265265
utxo.outpoint, utxo.txout.value
266266
);
@@ -279,10 +279,36 @@ fn main() -> anyhow::Result<()> {
279279
request = request.chain_txids(
280280
unconfirmed_txids
281281
.into_iter()
282-
.inspect(|txid| eprintln!("Checking if {} is confirmed yet", txid)),
282+
.inspect(|txid| eprint!("Checking if {} is confirmed yet", txid)),
283283
);
284284
}
285285

286+
let total_spks = request.spks.len();
287+
let total_txids = request.txids.len();
288+
let total_ops = request.outpoints.len();
289+
request = request
290+
.inspect_spks({
291+
let mut visited = 0;
292+
move |_| {
293+
visited += 1;
294+
eprintln!(" [ {:>6.2}% ]", (visited * 100) as f32 / total_spks as f32)
295+
}
296+
})
297+
.inspect_txids({
298+
let mut visited = 0;
299+
move |_| {
300+
visited += 1;
301+
eprintln!(" [ {:>6.2}% ]", (visited * 100) as f32 / total_txids as f32)
302+
}
303+
})
304+
.inspect_outpoints({
305+
let mut visited = 0;
306+
move |_| {
307+
visited += 1;
308+
eprintln!(" [ {:>6.2}% ]", (visited * 100) as f32 / total_ops as f32)
309+
}
310+
});
311+
286312
let res = client
287313
.sync(request, scan_options.batch_size)
288314
.context("scanning the blockchain")?;

example-crates/wallet_electrum/src/main.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const SEND_AMOUNT: Amount = Amount::from_sat(5000);
33
const STOP_GAP: usize = 50;
44
const BATCH_SIZE: usize = 5;
55

6+
use std::io::Write;
67
use std::str::FromStr;
78

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

41-
let request = wallet.start_full_scan().inspect_spks_for_all_keychains({
42-
let mut once = HashSet::<KeychainKind>::new();
43-
move |k, spk_i, _| match once.insert(k) {
44-
true => print!("\nScanning keychain [{:?}]", k),
45-
false => print!(" {:<3}", spk_i),
46-
}
47-
});
42+
let request = wallet
43+
.start_full_scan()
44+
.inspect_spks_for_all_keychains({
45+
let mut once = HashSet::<KeychainKind>::new();
46+
move |k, spk_i, _| {
47+
if once.insert(k) {
48+
print!("\nScanning keychain [{:?}]", k)
49+
} else {
50+
print!(" {:<3}", spk_i)
51+
}
52+
}
53+
})
54+
.inspect_spks_for_all_keychains(|_, _, _| std::io::stdout().flush().expect("must flush"));
4855

4956
let mut update = client
5057
.full_scan(request, STOP_GAP, BATCH_SIZE)?

0 commit comments

Comments
 (0)