From: Vihiga Tyonum Date: Sun, 28 Jun 2026 18:31:15 +0000 (+0100) Subject: feat(WalletEvent): Add WalletEvent to rpc and cbf X-Git-Tag: v4.0.0~11^2~1 X-Git-Url: http://internal-gitweb-vhost/struct.DescriptorId.html?a=commitdiff_plain;h=4b6094a4b7b379c589e00bd7cad4756a874c83c0;p=bdk-cli feat(WalletEvent): Add WalletEvent to rpc and cbf - add WalletEvent to rpc and cbf clients - Update Wallet to v2.4.0 --- diff --git a/Cargo.lock b/Cargo.lock index 077ed59..7bf049a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -331,9 +331,9 @@ dependencies = [ [[package]] name = "bdk_wallet" -version = "2.3.0" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b03f1e31ccc562f600981f747d2262b84428cbff52c9c9cdf14d15fb15bd2286" +checksum = "c99821af39c7df004bd411ece2ef22d9fba5a631b4489f3d9a0ac0d19637e2d0" dependencies = [ "bdk_chain", "bip39", diff --git a/Cargo.toml b/Cargo.toml index 47aa669..07a4336 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ readme = "README.md" license = "MIT" [dependencies] -bdk_wallet = { version = "2.3.0", features = ["rusqlite", "keys-bip39", "compiler", "std"] } +bdk_wallet = { version = "2.4.0", features = ["rusqlite", "keys-bip39", "compiler", "std"] } clap = { version = "4.6", features = ["derive","env"] } clap_complete = "4.6" dirs = { version = "6.0.0" } diff --git a/src/handlers.rs b/src/handlers.rs index 1b75e35..e1e640f 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -922,27 +922,35 @@ pub(crate) async fn handle_online_wallet_subcommand( NO_EXPECTED_MEMPOOL_TXS, ); - while let Some(block_event) = emitter.next_block()? { - if block_event.block_height() % 10_000 == 0 { - let percent_done = f64::from(block_event.block_height()) - / f64::from(blockchain_info.headers as u32) - * 100f64; - println!( - "Applying block at height: {}, {:.2}% done.", + let block_events = wallet.events_helper(|w| { + while let Some(block_event) = emitter.next_block()? { + if block_event.block_height() % 10_000 == 0 { + let percent_done = f64::from(block_event.block_height()) + / f64::from(blockchain_info.headers as u32) + * 100f64; + println!( + "Applying block at height: {}, {:.2}% done.", + block_event.block_height(), + percent_done + ); + } + + w.apply_block_connected_to( + &block_event.block, block_event.block_height(), - percent_done - ); + block_event.connected_to(), + )?; } - - wallet.apply_block_connected_to( - &block_event.block, - block_event.block_height(), - block_event.connected_to(), - )?; - } + Ok::<_, Error>(()) + })?; + print_wallet_events(&block_events); let mempool_txs = emitter.mempool()?; - wallet.apply_unconfirmed_txs(mempool_txs.update); + let mempool_events = wallet.apply_unconfirmed_txs_events(mempool_txs.update); + print_wallet_events(&mempool_events); + + let evicted_events = wallet.apply_evicted_txs_events(mempool_txs.evicted); + print_wallet_events(&evicted_events); } #[cfg(feature = "cbf")] KyotoClient { client } => { @@ -1879,27 +1887,34 @@ pub async fn sync_wallet(client: &BlockchainClient, wallet: &mut Wallet) -> Resu .filter(|tx| tx.chain_position.is_unconfirmed()), ); - while let Some(block_event) = emitter.next_block()? { - if block_event.block_height() % 10_000 == 0 { - let percent_done = f64::from(block_event.block_height()) - / f64::from(blockchain_info.headers as u32) - * 100f64; - println!( - "Applying block at height: {}, {:.2}% done.", + let block_events = wallet.events_helper(|w| { + while let Some(block_event) = emitter.next_block()? { + if block_event.block_height() % 10_000 == 0 { + let percent_done = f64::from(block_event.block_height()) + / f64::from(blockchain_info.headers as u32) + * 100f64; + println!( + "Applying block at height: {}, {:.2}% done.", + block_event.block_height(), + percent_done + ); + } + w.apply_block_connected_to( + &block_event.block, block_event.block_height(), - percent_done - ); + block_event.connected_to(), + )?; } - - wallet.apply_block_connected_to( - &block_event.block, - block_event.block_height(), - block_event.connected_to(), - )?; - } + Ok::<_, Error>(()) + })?; + print_wallet_events(&block_events); let mempool_txs = emitter.mempool()?; - wallet.apply_unconfirmed_txs(mempool_txs.update); + let mempool_events = wallet.apply_unconfirmed_txs_events(mempool_txs.update); + print_wallet_events(&mempool_events); + + let evicted_events = wallet.apply_evicted_txs_events(mempool_txs.evicted); + print_wallet_events(&evicted_events); Ok(()) } #[cfg(feature = "cbf")] diff --git a/src/utils.rs b/src/utils.rs index 3f77539..7d357e8 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -44,9 +44,7 @@ use cli_table::{Cell, CellStruct, Style, Table}; feature = "rpc", feature = "cbf" ))] -use crate::commands::ClientType; -#[cfg(any(feature = "electrum", feature = "esplora",))] -use bdk_wallet::event::WalletEvent; +use {crate::commands::ClientType, bdk_wallet::event::WalletEvent}; use bdk_wallet::Wallet; #[cfg(any(feature = "sqlite", feature = "redb"))] @@ -400,10 +398,12 @@ pub async fn sync_kyoto_client( let update = handle.update_subscriber.lock().await.update().await?; tracing::info!("Received update: applying to wallet"); - wallet - .apply_update(update) + let events = wallet + .apply_update_events(update) .map_err(|e| Error::Generic(format!("Failed to apply update: {e}")))?; + print_wallet_events(&events); + tracing::info!( "Chain tip: {}, Transactions: {}, Balance: {}", wallet.local_chain().tip().height(), @@ -706,45 +706,61 @@ pub fn load_wallet_config( Ok((wallet_opts, network)) } -#[cfg(any(feature = "electrum", feature = "esplora",))] -pub fn print_wallet_events(events: &Vec) { + +#[cfg(any( + feature = "electrum", + feature = "esplora", + feature = "cbf", + feature = "rpc" +))] +pub fn print_wallet_events(events: &[WalletEvent]) { for event in events { match event { - WalletEvent::TxConfirmed { - txid, - tx: _, - block_time, - old_block_time: _, - } => { + WalletEvent::ChainTipChanged { old_tip, new_tip } => { eprintln!( - "Transaction {} confirmed in block {:?}", - txid, block_time.block_id + "Chain tip advanced from height {} to {}", + old_tip.height, new_tip.height ); } + WalletEvent::TxConfirmed { + txid, + block_time, + old_block_time, + .. + } => match old_block_time { + Some(old) => eprintln!( + "Transaction {txid} re-confirmed at height {} (was height {}, likely a reorg)", + block_time.block_id.height, old.block_id.height + ), + None => eprintln!( + "Transaction {txid} confirmed at height {}", + block_time.block_id.height + ), + }, WalletEvent::TxUnconfirmed { txid, - tx: _, - old_block_time: _, - } => { - eprintln!("Transaction {txid} became unconfirmed"); - } + old_block_time, + .. + } => match old_block_time { + Some(old) => eprintln!( + "Transaction {txid} became unconfirmed (was confirmed at height {}, likely a reorg)", + old.block_id.height + ), + None => eprintln!("Transaction {txid} seen in mempool"), + }, WalletEvent::TxReplaced { - txid, - tx: _, - conflicts: _, + txid, conflicts, .. } => { - eprintln!("Received new transaction: {txid}"); - } - WalletEvent::TxDropped { txid, tx: _ } => { - eprintln!("Transaction {txid} has been dropped"); - } - WalletEvent::ChainTipChanged { old_tip, new_tip } => { + let ids: Vec = conflicts.iter().map(|(_, c)| c.to_string()).collect(); eprintln!( - "Wallet has synced to {:?} chain tip from {:?}", - new_tip.height, old_tip.height + "Transaction {txid} was replaced (conflicts with: {})", + ids.join(", ") ); } - _ => eprintln!(), + WalletEvent::TxDropped { txid, .. } => { + eprintln!("Transaction {txid} dropped from the mempool"); + } + _ => {} } } }