Skip to content

Commit df444d0

Browse files
committed
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 c6f918d commit df444d0

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

wallet/src/wallet/mod.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,6 +2560,41 @@ impl Wallet {
25602560
})
25612561
}
25622562

2563+
/// Introduces a `block` of `height` to the wallet, and tries to connect it to the
2564+
/// `prev_blockhash` of the block's header.
2565+
///
2566+
/// This is a convenience method that is equivalent to calling
2567+
/// [`apply_block_connected_to_events`] with `prev_blockhash` and `height-1` as the
2568+
/// `connected_to` parameter.
2569+
///
2570+
/// See [`apply_update_events`] for more information on the returned [`WalletEvent`]s.
2571+
///
2572+
/// [`apply_block_connected_to_events`]: Self::apply_block_connected_to_events
2573+
/// [`apply_update_events`]: Self::apply_update_events
2574+
pub fn apply_block_events(
2575+
&mut self,
2576+
block: &Block,
2577+
height: u32,
2578+
) -> Result<Vec<WalletEvent>, CannotConnectError> {
2579+
let connected_to = match height.checked_sub(1) {
2580+
Some(prev_height) => BlockId {
2581+
height: prev_height,
2582+
hash: block.header.prev_blockhash,
2583+
},
2584+
None => BlockId {
2585+
height,
2586+
hash: block.block_hash(),
2587+
},
2588+
};
2589+
self.apply_block_connected_to_events(block, height, connected_to)
2590+
.map_err(|err| match err {
2591+
ApplyHeaderError::InconsistentBlocks => {
2592+
unreachable!("connected_to is derived from the block so must be consistent")
2593+
}
2594+
ApplyHeaderError::CannotConnect(err) => err,
2595+
})
2596+
}
2597+
25632598
/// Applies relevant transactions from `block` of `height` to the wallet, and connects the
25642599
/// block to the internal chain.
25652600
///
@@ -2591,6 +2626,56 @@ impl Wallet {
25912626
Ok(())
25922627
}
25932628

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

0 commit comments

Comments
 (0)