Skip to content

Commit 94dc5f4

Browse files
tnullnotmandatory
authored andcommitted
Add apply_block_events and apply_block_connected_to_events
Previously, we added a new `Wallet::apply_update_events` method that returned `WalletEvent`s. Unfortunately, no corresponding APIs were added for the `apply_block` counterparts. Here we fix this omission.
1 parent fc9d20e commit 94dc5f4

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

src/wallet/mod.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,6 +2550,41 @@ impl Wallet {
25502550
})
25512551
}
25522552

2553+
/// Introduces a `block` of `height` to the wallet, and tries to connect it to the
2554+
/// `prev_blockhash` of the block's header.
2555+
///
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.
2559+
///
2560+
/// See [`apply_update_events`] for more information on the returned [`WalletEvent`]s.
2561+
///
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(
2565+
&mut self,
2566+
block: &Block,
2567+
height: u32,
2568+
) -> Result<Vec<WalletEvent>, CannotConnectError> {
2569+
let connected_to = match height.checked_sub(1) {
2570+
Some(prev_height) => BlockId {
2571+
height: prev_height,
2572+
hash: block.header.prev_blockhash,
2573+
},
2574+
None => BlockId {
2575+
height,
2576+
hash: block.block_hash(),
2577+
},
2578+
};
2579+
self.apply_block_connected_to_events(block, height, connected_to)
2580+
.map_err(|err| match err {
2581+
ApplyHeaderError::InconsistentBlocks => {
2582+
unreachable!("connected_to is derived from the block so must be consistent")
2583+
}
2584+
ApplyHeaderError::CannotConnect(err) => err,
2585+
})
2586+
}
2587+
25532588
/// Applies relevant transactions from `block` of `height` to the wallet, and connects the
25542589
/// block to the internal chain.
25552590
///
@@ -2581,6 +2616,56 @@ impl Wallet {
25812616
Ok(())
25822617
}
25832618

2619+
/// Applies relevant transactions from `block` of `height` to the wallet, and connects the
2620+
/// block to the internal chain.
2621+
///
2622+
/// See [`apply_block_connected_to`] for more information.
2623+
///
2624+
/// See [`apply_update_events`] for more information on the returned [`WalletEvent`]s.
2625+
///
2626+
/// [`apply_block_connected_to`]: Self::apply_block_connected_to
2627+
/// [`apply_update_events`]: Self::apply_update_events
2628+
pub fn apply_block_connected_to_events(
2629+
&mut self,
2630+
block: &Block,
2631+
height: u32,
2632+
connected_to: BlockId,
2633+
) -> Result<Vec<WalletEvent>, ApplyHeaderError> {
2634+
// snapshot of chain tip and transactions before update
2635+
let chain_tip1 = self.chain.tip().block_id();
2636+
let wallet_txs1 = self
2637+
.transactions()
2638+
.map(|wtx| {
2639+
(
2640+
wtx.tx_node.txid,
2641+
(wtx.tx_node.tx.clone(), wtx.chain_position),
2642+
)
2643+
})
2644+
.collect::<BTreeMap<Txid, (Arc<Transaction>, ChainPosition<ConfirmationBlockTime>)>>();
2645+
2646+
self.apply_block_connected_to(block, height, connected_to)?;
2647+
2648+
// chain tip and transactions after update
2649+
let chain_tip2 = self.chain.tip().block_id();
2650+
let wallet_txs2 = self
2651+
.transactions()
2652+
.map(|wtx| {
2653+
(
2654+
wtx.tx_node.txid,
2655+
(wtx.tx_node.tx.clone(), wtx.chain_position),
2656+
)
2657+
})
2658+
.collect::<BTreeMap<Txid, (Arc<Transaction>, ChainPosition<ConfirmationBlockTime>)>>();
2659+
2660+
Ok(wallet_events(
2661+
self,
2662+
chain_tip1,
2663+
chain_tip2,
2664+
wallet_txs1,
2665+
wallet_txs2,
2666+
))
2667+
}
2668+
25842669
/// Apply relevant unconfirmed transactions to the wallet.
25852670
///
25862671
/// Transactions that are not relevant are filtered out.

0 commit comments

Comments
 (0)