Skip to content

Commit fb7232f

Browse files
committed
rpc: print status during full_scan and sync
Also when doing an rpc sync, re-sync the prior 200 blocks to handle any possible reorg scenarios.
1 parent 4a66557 commit fb7232f

2 files changed

Lines changed: 39 additions & 13 deletions

File tree

src/commands.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,9 @@ pub struct WalletOpts {
192192
default_value = "user:password",
193193
)]
194194
pub basic_auth: (String, String),
195-
196195
#[cfg(feature = "rpc")]
197196
/// Sets an optional cookie authentication.
198-
#[clap(name = "COOKIE", long = "cookie")]
197+
#[arg(env = "COOKIE")]
199198
pub cookie: Option<String>,
200199
}
201200

src/handlers.rs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,7 @@ use bdk_wallet::keys::{DerivableKey, DescriptorKey, ExtendedKey, GeneratableKey,
5151
use bdk_wallet::miniscript::miniscript;
5252
use serde_json::json;
5353
use std::collections::BTreeMap;
54-
#[cfg(any(
55-
feature = "electrum",
56-
feature = "esplora",
57-
feature = "cbf",
58-
feature = "rpc"
59-
))]
54+
#[cfg(any(feature = "electrum", feature = "esplora", feature = "cbf",))]
6055
use std::collections::HashSet;
6156
use std::convert::TryFrom;
6257
#[cfg(feature = "repl")]
@@ -368,7 +363,10 @@ pub(crate) async fn handle_online_wallet_subcommand(
368363
online_subcommand: OnlineWalletSubCommand,
369364
) -> Result<serde_json::Value, Error> {
370365
match online_subcommand {
371-
FullScan { stop_gap } => {
366+
FullScan {
367+
stop_gap: _stop_gap,
368+
} => {
369+
#[cfg(any(feature = "electrum", feature = "esplora"))]
372370
let request = wallet.start_full_scan().inspect({
373371
let mut stdout = std::io::stdout();
374372
let mut once = HashSet::<KeychainKind>::new();
@@ -388,7 +386,7 @@ pub(crate) async fn handle_online_wallet_subcommand(
388386
client
389387
.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx));
390388

391-
let update = client.full_scan(request, stop_gap, batch_size, false)?;
389+
let update = client.full_scan(request, _stop_gap, batch_size, false)?;
392390
wallet.apply_update(update)?;
393391
}
394392
#[cfg(feature = "esplora")]
@@ -397,14 +395,16 @@ pub(crate) async fn handle_online_wallet_subcommand(
397395
parallel_requests,
398396
} => {
399397
let update = client
400-
.full_scan(request, stop_gap, parallel_requests)
398+
.full_scan(request, _stop_gap, parallel_requests)
401399
.await
402400
.map_err(|e| *e)?;
403401
wallet.apply_update(update)?;
404402
}
405403

406404
#[cfg(feature = "rpc")]
407405
RpcClient { client } => {
406+
let blockchain_info = client.get_blockchain_info()?;
407+
408408
let genesis_block =
409409
bdk_wallet::bitcoin::constants::genesis_block(wallet.network());
410410
let genesis_cp = CheckPoint::new(BlockId {
@@ -415,6 +415,17 @@ pub(crate) async fn handle_online_wallet_subcommand(
415415
Emitter::new(&*client, genesis_cp.clone(), genesis_cp.height());
416416

417417
while let Some(block_event) = emitter.next_block()? {
418+
if block_event.block_height() % 10_000 == 0 {
419+
let percent_done = f64::from(block_event.block_height())
420+
/ f64::from(blockchain_info.headers as u32)
421+
* 100f64;
422+
println!(
423+
"Applying block at height: {}, {:.2}% done.",
424+
block_event.block_height(),
425+
percent_done
426+
);
427+
}
428+
418429
wallet.apply_block_connected_to(
419430
&block_event.block,
420431
block_event.block_height(),
@@ -429,6 +440,7 @@ pub(crate) async fn handle_online_wallet_subcommand(
429440
Ok(json!({}))
430441
}
431442
Sync => {
443+
#[cfg(any(feature = "electrum", feature = "esplora"))]
432444
let request = wallet
433445
.start_sync_with_revealed_spks()
434446
.inspect(|item, progress| {
@@ -459,10 +471,25 @@ pub(crate) async fn handle_online_wallet_subcommand(
459471
}
460472
#[cfg(feature = "rpc")]
461473
RpcClient { client } => {
474+
let blockchain_info = client.get_blockchain_info()?;
462475
let wallet_cp = wallet.latest_checkpoint();
463-
let mut emitter = Emitter::new(&*client, wallet_cp.clone(), wallet_cp.height());
476+
477+
// reload the last 200 blocks in case of a reorg
478+
let emitter_height = wallet_cp.height().saturating_sub(200);
479+
let mut emitter = Emitter::new(&*client, wallet_cp, emitter_height);
464480

465481
while let Some(block_event) = emitter.next_block()? {
482+
if block_event.block_height() % 10_000 == 0 {
483+
let percent_done = f64::from(block_event.block_height())
484+
/ f64::from(blockchain_info.headers as u32)
485+
* 100f64;
486+
println!(
487+
"Applying block at height: {}, {:.2}% done.",
488+
block_event.block_height(),
489+
percent_done
490+
);
491+
}
492+
466493
wallet.apply_block_connected_to(
467494
&block_event.block,
468495
block_event.block_height(),
@@ -480,7 +507,7 @@ pub(crate) async fn handle_online_wallet_subcommand(
480507
let tx = match (psbt, tx) {
481508
(Some(psbt), None) => {
482509
let psbt = BASE64_STANDARD
483-
.decode(&psbt)
510+
.decode(psbt)
484511
.map_err(|e| Error::Generic(e.to_string()))?;
485512
let psbt: Psbt = Psbt::deserialize(&psbt)?;
486513
is_final(&psbt)?;

0 commit comments

Comments
 (0)