Skip to content

Commit a868aa8

Browse files
committed
refactor: change Wallet apply updates or blocks to return events
BREAKING CHANGE: 1. changed functions to return WalletEvents: - Wallet::apply_update - Wallet::apply_block - Wallet::apply_block_connected_to 2. removed functions: - Wallet::apply_update_events - Wallet::apply_block_events - Wallet::apply_block_connected_to_events
1 parent e0bef3b commit a868aa8

File tree

3 files changed

+61
-124
lines changed

3 files changed

+61
-124
lines changed

src/wallet/event.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub enum WalletEvent {
5959
/// This can happen after an RBF is broadcast or if a third party double spends an input of
6060
/// a received payment transaction before it is confirmed.
6161
///
62-
/// The conflicts field contains the txid and vin (in which it conflicts) of the conflicting
62+
/// The 'conflicts' field contains the txid and vin (in which it conflicts) of the conflicting
6363
/// transactions.
6464
TxReplaced {
6565
/// Transaction id.
@@ -83,7 +83,8 @@ pub enum WalletEvent {
8383
}
8484

8585
/// Generate events by comparing the chain tip and wallet transactions before and after applying
86-
/// `wallet::Update` to `Wallet`. Any changes are added to the list of returned `WalletEvent`s.
86+
/// `wallet::Update` or a `bitcoin::Block` to `Wallet`. Any changes are added to the list of
87+
/// returned `WalletEvent`s.
8788
pub(crate) fn wallet_events(
8889
wallet: &mut Wallet,
8990
chain_tip1: BlockId,
@@ -92,7 +93,6 @@ pub(crate) fn wallet_events(
9293
wallet_txs2: BTreeMap<Txid, (Arc<Transaction>, ChainPosition<ConfirmationBlockTime>)>,
9394
) -> Vec<WalletEvent> {
9495
let mut events: Vec<WalletEvent> = Vec::new();
95-
9696
// find chain tip change
9797
if chain_tip1 != chain_tip2 {
9898
events.push(WalletEvent::ChainTipChanged {

src/wallet/mod.rs

Lines changed: 32 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,12 +2343,8 @@ impl Wallet {
23432343

23442344
/// Applies an update to the wallet and stages the changes (but does not persist them).
23452345
///
2346-
/// Usually you create an `update` by interacting with some blockchain data source and inserting
2347-
/// transactions related to your wallet into it.
2348-
///
2349-
/// After applying updates you should persist the staged wallet changes. For an example of how
2350-
/// to persist staged wallet changes see [`Wallet::reveal_next_address`].
2351-
pub fn apply_update(&mut self, update: impl Into<Update>) -> Result<(), CannotConnectError> {
2346+
/// This is the inner private function called by `apply_update`.
2347+
fn apply_update_inner(&mut self, update: impl Into<Update>) -> Result<(), CannotConnectError> {
23522348
let update = update.into();
23532349
let mut changeset = match update.chain {
23542350
Some(chain_update) => ChangeSet::from(self.chain.apply_update(chain_update)?),
@@ -2370,8 +2366,8 @@ impl Wallet {
23702366
/// Usually you create an `update` by interacting with some blockchain data source and inserting
23712367
/// transactions related to your wallet into it. Staged changes are NOT persisted.
23722368
///
2373-
/// After applying updates you should process the events in your app before persisting the
2374-
/// staged wallet changes. For an example of how to persist staged wallet changes see
2369+
/// After applying updates, you should process the events in your app before persisting the
2370+
/// staged wallet changes. For an example of how to persist staged wallet changes, see
23752371
/// [`Wallet::reveal_next_address`].
23762372
///
23772373
/// ```rust,no_run
@@ -2380,7 +2376,7 @@ impl Wallet {
23802376
/// use bdk_wallet::event::WalletEvent;
23812377
/// # let wallet_update = Update::default();
23822378
/// # let mut wallet = doctest_wallet!();
2383-
/// let events = wallet.apply_update_events(wallet_update)?;
2379+
/// let events = wallet.apply_update(wallet_update)?;
23842380
/// // Handle wallet relevant events from this update.
23852381
/// events.iter().for_each(|event| {
23862382
/// match event {
@@ -2445,7 +2441,7 @@ impl Wallet {
24452441
/// # Ok::<(), anyhow::Error>(())
24462442
/// ```
24472443
/// [`TxBuilder`]: crate::TxBuilder
2448-
pub fn apply_update_events(
2444+
pub fn apply_update(
24492445
&mut self,
24502446
update: impl Into<Update>,
24512447
) -> Result<Vec<WalletEvent>, CannotConnectError> {
@@ -2462,7 +2458,7 @@ impl Wallet {
24622458
.collect::<BTreeMap<Txid, (Arc<Transaction>, ChainPosition<ConfirmationBlockTime>)>>();
24632459

24642460
// apply update
2465-
self.apply_update(update)?;
2461+
self.apply_update_inner(update)?;
24662462

24672463
// chain tip and transactions after update
24682464
let chain_tip2 = self.chain.tip().block_id();
@@ -2523,14 +2519,22 @@ impl Wallet {
25232519
&self.chain
25242520
}
25252521

2526-
/// Introduces a `block` of `height` to the wallet, and tries to connect it to the
2522+
/// Introduces a `block` of `height` to the wallet and tries to connect it to the
25272523
/// `prev_blockhash` of the block's header.
25282524
///
2529-
/// This is a convenience method that is equivalent to calling [`apply_block_connected_to`]
2530-
/// with `prev_blockhash` and `height-1` as the `connected_to` parameter.
2525+
/// This is a convenience method that is equivalent to calling
2526+
/// [`apply_block_connected_to`] with `prev_blockhash` and `height-1` as the
2527+
/// `connected_to` parameter.
2528+
///
2529+
/// See [`apply_update`] for more information on the returned [`WalletEvent`]s.
25312530
///
25322531
/// [`apply_block_connected_to`]: Self::apply_block_connected_to
2533-
pub fn apply_block(&mut self, block: &Block, height: u32) -> Result<(), CannotConnectError> {
2532+
/// [`apply_update`]: Self::apply_update
2533+
pub fn apply_block(
2534+
&mut self,
2535+
block: &Block,
2536+
height: u32,
2537+
) -> Result<Vec<WalletEvent>, CannotConnectError> {
25342538
let connected_to = match height.checked_sub(1) {
25352539
Some(prev_height) => BlockId {
25362540
height: prev_height,
@@ -2550,22 +2554,24 @@ impl Wallet {
25502554
})
25512555
}
25522556

2553-
/// Introduces a `block` of `height` to the wallet, and tries to connect it to the
2554-
/// `prev_blockhash` of the block's header.
2557+
/// Applies relevant transactions from `block` of `height` to the wallet and connects the
2558+
/// block to the internal chain.
25552559
///
2556-
/// This is a convenience method that is equivalent to calling
2557-
/// [`apply_block_connected_to_events`] with `prev_blockhash` and `height-1` as the
2558-
/// `connected_to` parameter.
2560+
/// The `connected_to` parameter informs the wallet how this block connects to the internal
2561+
/// [`LocalChain`]. Relevant transactions are filtered from the `block` and inserted into the
2562+
/// internal [`TxGraph`].
25592563
///
2560-
/// See [`apply_update_events`] for more information on the returned [`WalletEvent`]s.
2564+
/// **WARNING**: You must persist the changes resulting from one or more calls to this method
2565+
/// if you need the inserted block data to be reloaded after closing the wallet.
2566+
/// See [`Wallet::reveal_next_address`].
25612567
///
2562-
/// [`apply_block_connected_to_events`]: Self::apply_block_connected_to_events
2563-
/// [`apply_update_events`]: Self::apply_update_events
2564-
pub fn apply_block_events(
2568+
/// See [`apply_update_events`] for more information on the returned [`WalletEvent`]s.
2569+
pub fn apply_block_connected_to(
25652570
&mut self,
25662571
block: &Block,
25672572
height: u32,
2568-
) -> Result<Vec<WalletEvent>, CannotConnectError> {
2573+
connected_to: BlockId,
2574+
) -> Result<Vec<WalletEvent>, ApplyHeaderError> {
25692575
// snapshot of chain tip and transactions before update
25702576
let chain_tip1 = self.chain.tip().block_id();
25712577
let wallet_txs1 = self
@@ -2578,45 +2584,7 @@ impl Wallet {
25782584
})
25792585
.collect::<BTreeMap<Txid, (Arc<Transaction>, ChainPosition<ConfirmationBlockTime>)>>();
25802586

2581-
self.apply_block(block, height)?;
2582-
2583-
// chain tip and transactions after update
2584-
let chain_tip2 = self.chain.tip().block_id();
2585-
let wallet_txs2 = self
2586-
.transactions()
2587-
.map(|wtx| {
2588-
(
2589-
wtx.tx_node.txid,
2590-
(wtx.tx_node.tx.clone(), wtx.chain_position),
2591-
)
2592-
})
2593-
.collect::<BTreeMap<Txid, (Arc<Transaction>, ChainPosition<ConfirmationBlockTime>)>>();
2594-
2595-
Ok(wallet_events(
2596-
self,
2597-
chain_tip1,
2598-
chain_tip2,
2599-
wallet_txs1,
2600-
wallet_txs2,
2601-
))
2602-
}
2603-
2604-
/// Applies relevant transactions from `block` of `height` to the wallet, and connects the
2605-
/// block to the internal chain.
2606-
///
2607-
/// The `connected_to` parameter informs the wallet how this block connects to the internal
2608-
/// [`LocalChain`]. Relevant transactions are filtered from the `block` and inserted into the
2609-
/// internal [`TxGraph`].
2610-
///
2611-
/// **WARNING**: You must persist the changes resulting from one or more calls to this method
2612-
/// if you need the inserted block data to be reloaded after closing the wallet.
2613-
/// See [`Wallet::reveal_next_address`].
2614-
pub fn apply_block_connected_to(
2615-
&mut self,
2616-
block: &Block,
2617-
height: u32,
2618-
connected_to: BlockId,
2619-
) -> Result<(), ApplyHeaderError> {
2587+
// apply block to wallet
26202588
let mut changeset = ChangeSet::default();
26212589
changeset.merge(
26222590
self.chain
@@ -2629,37 +2597,6 @@ impl Wallet {
26292597
.into(),
26302598
);
26312599
self.stage.merge(changeset);
2632-
Ok(())
2633-
}
2634-
2635-
/// Applies relevant transactions from `block` of `height` to the wallet, and connects the
2636-
/// block to the internal chain.
2637-
///
2638-
/// See [`apply_block_connected_to`] for more information.
2639-
///
2640-
/// See [`apply_update_events`] for more information on the returned [`WalletEvent`]s.
2641-
///
2642-
/// [`apply_block_connected_to`]: Self::apply_block_connected_to
2643-
/// [`apply_update_events`]: Self::apply_update_events
2644-
pub fn apply_block_connected_to_events(
2645-
&mut self,
2646-
block: &Block,
2647-
height: u32,
2648-
connected_to: BlockId,
2649-
) -> Result<Vec<WalletEvent>, ApplyHeaderError> {
2650-
// snapshot of chain tip and transactions before update
2651-
let chain_tip1 = self.chain.tip().block_id();
2652-
let wallet_txs1 = self
2653-
.transactions()
2654-
.map(|wtx| {
2655-
(
2656-
wtx.tx_node.txid,
2657-
(wtx.tx_node.tx.clone(), wtx.chain_position),
2658-
)
2659-
})
2660-
.collect::<BTreeMap<Txid, (Arc<Transaction>, ChainPosition<ConfirmationBlockTime>)>>();
2661-
2662-
self.apply_block_connected_to(block, height, connected_to)?;
26632600

26642601
// chain tip and transactions after update
26652602
let chain_tip2 = self.chain.tip().block_id();

0 commit comments

Comments
 (0)