Skip to content

Commit 5dc6a28

Browse files
committed
feat(WalletEvent): Add WalletEvent to rpc and cbf
- add WalletEvent to rpc and cbf clients - Update Wallet to v2.4.0
1 parent 4a41057 commit 5dc6a28

4 files changed

Lines changed: 100 additions & 69 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ readme = "README.md"
1212
license = "MIT"
1313

1414
[dependencies]
15-
bdk_wallet = { version = "2.3.0", features = ["rusqlite", "keys-bip39", "compiler", "std"] }
15+
bdk_wallet = { version = "2.4.0", features = ["rusqlite", "keys-bip39", "compiler", "std"] }
1616
clap = { version = "4.6", features = ["derive","env"] }
1717
clap_complete = "4.6"
1818
dirs = { version = "6.0.0" }

src/handlers.rs

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -667,27 +667,35 @@ pub(crate) async fn handle_online_wallet_subcommand(
667667
NO_EXPECTED_MEMPOOL_TXS,
668668
);
669669

670-
while let Some(block_event) = emitter.next_block()? {
671-
if block_event.block_height() % 10_000 == 0 {
672-
let percent_done = f64::from(block_event.block_height())
673-
/ f64::from(blockchain_info.headers as u32)
674-
* 100f64;
675-
println!(
676-
"Applying block at height: {}, {:.2}% done.",
670+
let block_events = wallet.events_helper(|w| {
671+
while let Some(block_event) = emitter.next_block()? {
672+
if block_event.block_height() % 10_000 == 0 {
673+
let percent_done = f64::from(block_event.block_height())
674+
/ f64::from(blockchain_info.headers as u32)
675+
* 100f64;
676+
println!(
677+
"Applying block at height: {}, {:.2}% done.",
678+
block_event.block_height(),
679+
percent_done
680+
);
681+
}
682+
683+
w.apply_block_connected_to(
684+
&block_event.block,
677685
block_event.block_height(),
678-
percent_done
679-
);
686+
block_event.connected_to(),
687+
)?;
680688
}
681-
682-
wallet.apply_block_connected_to(
683-
&block_event.block,
684-
block_event.block_height(),
685-
block_event.connected_to(),
686-
)?;
687-
}
689+
Ok::<_, Error>(())
690+
})?;
691+
print_wallet_events(&block_events);
688692

689693
let mempool_txs = emitter.mempool()?;
690-
wallet.apply_unconfirmed_txs(mempool_txs.update);
694+
let mempool_events = wallet.apply_unconfirmed_txs_events(mempool_txs.update);
695+
print_wallet_events(&mempool_events);
696+
697+
let evicted_events = wallet.apply_evicted_txs_events(mempool_txs.evicted);
698+
print_wallet_events(&evicted_events);
691699
}
692700
#[cfg(feature = "cbf")]
693701
KyotoClient { client } => {
@@ -1580,27 +1588,34 @@ pub async fn sync_wallet(client: &BlockchainClient, wallet: &mut Wallet) -> Resu
15801588
.filter(|tx| tx.chain_position.is_unconfirmed()),
15811589
);
15821590

1583-
while let Some(block_event) = emitter.next_block()? {
1584-
if block_event.block_height() % 10_000 == 0 {
1585-
let percent_done = f64::from(block_event.block_height())
1586-
/ f64::from(blockchain_info.headers as u32)
1587-
* 100f64;
1588-
println!(
1589-
"Applying block at height: {}, {:.2}% done.",
1591+
let block_events = wallet.events_helper(|w| {
1592+
while let Some(block_event) = emitter.next_block()? {
1593+
if block_event.block_height() % 10_000 == 0 {
1594+
let percent_done = f64::from(block_event.block_height())
1595+
/ f64::from(blockchain_info.headers as u32)
1596+
* 100f64;
1597+
println!(
1598+
"Applying block at height: {}, {:.2}% done.",
1599+
block_event.block_height(),
1600+
percent_done
1601+
);
1602+
}
1603+
w.apply_block_connected_to(
1604+
&block_event.block,
15901605
block_event.block_height(),
1591-
percent_done
1592-
);
1606+
block_event.connected_to(),
1607+
)?;
15931608
}
1594-
1595-
wallet.apply_block_connected_to(
1596-
&block_event.block,
1597-
block_event.block_height(),
1598-
block_event.connected_to(),
1599-
)?;
1600-
}
1609+
Ok::<_, Error>(())
1610+
})?;
1611+
print_wallet_events(&block_events);
16011612

16021613
let mempool_txs = emitter.mempool()?;
1603-
wallet.apply_unconfirmed_txs(mempool_txs.update);
1614+
let mempool_events = wallet.apply_unconfirmed_txs_events(mempool_txs.update);
1615+
print_wallet_events(&mempool_events);
1616+
1617+
let evicted_events = wallet.apply_evicted_txs_events(mempool_txs.evicted);
1618+
print_wallet_events(&evicted_events);
16041619
Ok(())
16051620
}
16061621
#[cfg(feature = "cbf")]

src/utils.rs

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ use cli_table::{Cell, CellStruct, Style, Table};
4242
feature = "rpc",
4343
feature = "cbf"
4444
))]
45-
use crate::commands::ClientType;
46-
#[cfg(any(feature = "electrum", feature = "esplora",))]
47-
use bdk_wallet::event::WalletEvent;
45+
use {crate::commands::ClientType, bdk_wallet::event::WalletEvent};
4846

4947
use bdk_wallet::Wallet;
5048
#[cfg(any(feature = "sqlite", feature = "redb"))]
@@ -359,10 +357,12 @@ pub async fn sync_kyoto_client(
359357

360358
let update = handle.update_subscriber.lock().await.update().await?;
361359
tracing::info!("Received update: applying to wallet");
362-
wallet
363-
.apply_update(update)
360+
let events = wallet
361+
.apply_update_events(update)
364362
.map_err(|e| Error::Generic(format!("Failed to apply update: {e}")))?;
365363

364+
print_wallet_events(&events);
365+
366366
tracing::info!(
367367
"Chain tip: {}, Transactions: {}, Balance: {}",
368368
wallet.local_chain().tip().height(),
@@ -665,45 +665,61 @@ pub fn load_wallet_config(
665665

666666
Ok((wallet_opts, network))
667667
}
668-
#[cfg(any(feature = "electrum", feature = "esplora",))]
669-
pub fn print_wallet_events(events: &Vec<WalletEvent>) {
668+
669+
#[cfg(any(
670+
feature = "electrum",
671+
feature = "esplora",
672+
feature = "cbf",
673+
feature = "rpc"
674+
))]
675+
pub fn print_wallet_events(events: &[WalletEvent]) {
670676
for event in events {
671677
match event {
672-
WalletEvent::TxConfirmed {
673-
txid,
674-
tx: _,
675-
block_time,
676-
old_block_time: _,
677-
} => {
678+
WalletEvent::ChainTipChanged { old_tip, new_tip } => {
678679
eprintln!(
679-
"Transaction {} confirmed in block {:?}",
680-
txid, block_time.block_id
680+
"Chain tip advanced from height {} to {}",
681+
old_tip.height, new_tip.height
681682
);
682683
}
684+
WalletEvent::TxConfirmed {
685+
txid,
686+
block_time,
687+
old_block_time,
688+
..
689+
} => match old_block_time {
690+
Some(old) => eprintln!(
691+
"Transaction {txid} re-confirmed at height {} (was height {}, likely a reorg)",
692+
block_time.block_id.height, old.block_id.height
693+
),
694+
None => eprintln!(
695+
"Transaction {txid} confirmed at height {}",
696+
block_time.block_id.height
697+
),
698+
},
683699
WalletEvent::TxUnconfirmed {
684700
txid,
685-
tx: _,
686-
old_block_time: _,
687-
} => {
688-
eprintln!("Transaction {txid} became unconfirmed");
689-
}
701+
old_block_time,
702+
..
703+
} => match old_block_time {
704+
Some(old) => eprintln!(
705+
"Transaction {txid} became unconfirmed (was confirmed at height {}, likely a reorg)",
706+
old.block_id.height
707+
),
708+
None => eprintln!("Transaction {txid} seen in mempool"),
709+
},
690710
WalletEvent::TxReplaced {
691-
txid,
692-
tx: _,
693-
conflicts: _,
711+
txid, conflicts, ..
694712
} => {
695-
eprintln!("Received new transaction: {txid}");
696-
}
697-
WalletEvent::TxDropped { txid, tx: _ } => {
698-
eprintln!("Transaction {txid} has been dropped");
699-
}
700-
WalletEvent::ChainTipChanged { old_tip, new_tip } => {
713+
let ids: Vec<String> = conflicts.iter().map(|(_, c)| c.to_string()).collect();
701714
eprintln!(
702-
"Wallet has synced to {:?} chain tip from {:?}",
703-
new_tip.height, old_tip.height
715+
"Transaction {txid} was replaced (conflicts with: {})",
716+
ids.join(", ")
704717
);
705718
}
706-
_ => eprintln!(),
719+
WalletEvent::TxDropped { txid, .. } => {
720+
eprintln!("Transaction {txid} dropped from the mempool");
721+
}
722+
_ => {}
707723
}
708724
}
709725
}

0 commit comments

Comments
 (0)