Skip to content

Commit 66cedb4

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 66cedb4

File tree

3 files changed

+71
-142
lines changed

3 files changed

+71
-142
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: 42 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,37 +2341,13 @@ impl Wallet {
23412341
.to_string()
23422342
}
23432343

2344-
/// Applies an update to the wallet and stages the changes (but does not persist them).
2345-
///
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> {
2352-
let update = update.into();
2353-
let mut changeset = match update.chain {
2354-
Some(chain_update) => ChangeSet::from(self.chain.apply_update(chain_update)?),
2355-
None => ChangeSet::default(),
2356-
};
2357-
2358-
let index_changeset = self
2359-
.indexed_graph
2360-
.index
2361-
.reveal_to_target_multi(&update.last_active_indices);
2362-
changeset.merge(index_changeset.into());
2363-
changeset.merge(self.indexed_graph.apply_update(update.tx_update).into());
2364-
self.stage.merge(changeset);
2365-
Ok(())
2366-
}
2367-
23682344
/// Applies an update to the wallet, stages the changes, and returns events.
23692345
///
23702346
/// Usually you create an `update` by interacting with some blockchain data source and inserting
23712347
/// transactions related to your wallet into it. Staged changes are NOT persisted.
23722348
///
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
2349+
/// After applying updates, you should process the events in your app before persisting the
2350+
/// staged wallet changes. For an example of how to persist staged wallet changes, see
23752351
/// [`Wallet::reveal_next_address`].
23762352
///
23772353
/// ```rust,no_run
@@ -2380,7 +2356,7 @@ impl Wallet {
23802356
/// use bdk_wallet::event::WalletEvent;
23812357
/// # let wallet_update = Update::default();
23822358
/// # let mut wallet = doctest_wallet!();
2383-
/// let events = wallet.apply_update_events(wallet_update)?;
2359+
/// let events = wallet.apply_update(wallet_update)?;
23842360
/// // Handle wallet relevant events from this update.
23852361
/// events.iter().for_each(|event| {
23862362
/// match event {
@@ -2445,7 +2421,7 @@ impl Wallet {
24452421
/// # Ok::<(), anyhow::Error>(())
24462422
/// ```
24472423
/// [`TxBuilder`]: crate::TxBuilder
2448-
pub fn apply_update_events(
2424+
pub fn apply_update(
24492425
&mut self,
24502426
update: impl Into<Update>,
24512427
) -> Result<Vec<WalletEvent>, CannotConnectError> {
@@ -2462,7 +2438,19 @@ impl Wallet {
24622438
.collect::<BTreeMap<Txid, (Arc<Transaction>, ChainPosition<ConfirmationBlockTime>)>>();
24632439

24642440
// apply update
2465-
self.apply_update(update)?;
2441+
let update = update.into();
2442+
let mut changeset = match update.chain {
2443+
Some(chain_update) => ChangeSet::from(self.chain.apply_update(chain_update)?),
2444+
None => ChangeSet::default(),
2445+
};
2446+
2447+
let index_changeset = self
2448+
.indexed_graph
2449+
.index
2450+
.reveal_to_target_multi(&update.last_active_indices);
2451+
changeset.merge(index_changeset.into());
2452+
changeset.merge(self.indexed_graph.apply_update(update.tx_update).into());
2453+
self.stage.merge(changeset);
24662454

24672455
// chain tip and transactions after update
24682456
let chain_tip2 = self.chain.tip().block_id();
@@ -2523,14 +2511,22 @@ impl Wallet {
25232511
&self.chain
25242512
}
25252513

2526-
/// Introduces a `block` of `height` to the wallet, and tries to connect it to the
2514+
/// Introduces a `block` of `height` to the wallet and tries to connect it to the
25272515
/// `prev_blockhash` of the block's header.
25282516
///
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.
2517+
/// This is a convenience method that is equivalent to calling
2518+
/// [`apply_block_connected_to`] with `prev_blockhash` and `height-1` as the
2519+
/// `connected_to` parameter.
2520+
///
2521+
/// See [`apply_update`] for more information on the returned [`WalletEvent`]s.
25312522
///
25322523
/// [`apply_block_connected_to`]: Self::apply_block_connected_to
2533-
pub fn apply_block(&mut self, block: &Block, height: u32) -> Result<(), CannotConnectError> {
2524+
/// [`apply_update`]: Self::apply_update
2525+
pub fn apply_block(
2526+
&mut self,
2527+
block: &Block,
2528+
height: u32,
2529+
) -> Result<Vec<WalletEvent>, CannotConnectError> {
25342530
let connected_to = match height.checked_sub(1) {
25352531
Some(prev_height) => BlockId {
25362532
height: prev_height,
@@ -2550,22 +2546,24 @@ impl Wallet {
25502546
})
25512547
}
25522548

2553-
/// Introduces a `block` of `height` to the wallet, and tries to connect it to the
2554-
/// `prev_blockhash` of the block's header.
2549+
/// Applies relevant transactions from `block` of `height` to the wallet and connects the
2550+
/// block to the internal chain.
25552551
///
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.
2552+
/// The `connected_to` parameter informs the wallet how this block connects to the internal
2553+
/// [`LocalChain`]. Relevant transactions are filtered from the `block` and inserted into the
2554+
/// internal [`TxGraph`].
25592555
///
2560-
/// See [`apply_update_events`] for more information on the returned [`WalletEvent`]s.
2556+
/// **WARNING**: You must persist the changes resulting from one or more calls to this method
2557+
/// if you need the inserted block data to be reloaded after closing the wallet.
2558+
/// See [`Wallet::reveal_next_address`].
25612559
///
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(
2560+
/// See [`apply_update_events`] for more information on the returned [`WalletEvent`]s.
2561+
pub fn apply_block_connected_to(
25652562
&mut self,
25662563
block: &Block,
25672564
height: u32,
2568-
) -> Result<Vec<WalletEvent>, CannotConnectError> {
2565+
connected_to: BlockId,
2566+
) -> Result<Vec<WalletEvent>, ApplyHeaderError> {
25692567
// snapshot of chain tip and transactions before update
25702568
let chain_tip1 = self.chain.tip().block_id();
25712569
let wallet_txs1 = self
@@ -2578,45 +2576,7 @@ impl Wallet {
25782576
})
25792577
.collect::<BTreeMap<Txid, (Arc<Transaction>, ChainPosition<ConfirmationBlockTime>)>>();
25802578

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> {
2579+
// apply block to wallet
26202580
let mut changeset = ChangeSet::default();
26212581
changeset.merge(
26222582
self.chain
@@ -2629,37 +2589,6 @@ impl Wallet {
26292589
.into(),
26302590
);
26312591
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)?;
26632592

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

0 commit comments

Comments
 (0)