Skip to content

Commit 80be94d

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 d0b26fa commit 80be94d

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
@@ -2562,6 +2562,41 @@ impl Wallet {
25622562
})
25632563
}
25642564

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

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

0 commit comments

Comments
 (0)