diff --git a/crates/bdk/src/wallet/mod.rs b/crates/bdk/src/wallet/mod.rs index 240cc10508..37ab662302 100644 --- a/crates/bdk/src/wallet/mod.rs +++ b/crates/bdk/src/wallet/mod.rs @@ -2309,6 +2309,57 @@ impl Wallet { pub fn local_chain(&self) -> &LocalChain { &self.chain } + + /// Set lookahead for all keychains. + /// + /// Passing a lookahead of 0 will result in an error. This is because if we sync + /// the chain with a lookahead of 0, we would not find any update since we don't + /// have any scripts stored. + pub fn set_lookahead_for_all(&mut self, lookahead: u32) { + const DEFAULT_KEYCHAIN_LOOKAHEAD: u32 = 10; + + self.indexed_graph.index.set_lookahead_for_all( + if lookahead > 0 { + lookahead + } else { + DEFAULT_KEYCHAIN_LOOKAHEAD + } + ); + } + + /// Insert all the block's relevant transactions into the IndexedTxGraph. + pub fn apply_block_relevant( + &mut self, + block: bitcoin::Block, + height: u32, + ) -> Result<(), CannotConnectError> + where + D: PersistBackend, + { + let chain_update = CheckPoint::from_header(&block.header, height).into_update(false); + let mut changeset = ChangeSet::from(self.chain.apply_update(chain_update)?); + changeset.append(ChangeSet::from( + self.indexed_graph.apply_block_relevant(block, height), + )); + self.persist.stage(changeset); + Ok(()) + } + + /// Batch insert unconfirmed transactions into the IndexedTxGraph. + /// Filtering out those that are not relevant. + /// + /// Read more here: [`self.indexed_graph.batch_insert_relevant_unconfirmed()`] + pub fn batch_insert_relevant_unconfirmed<'t>( + &mut self, + unconfirmed_txs: impl IntoIterator, + ) where + D: PersistBackend, + { + let indexed_graph_changeset = self + .indexed_graph + .batch_insert_relevant_unconfirmed(unconfirmed_txs); + self.persist.stage(ChangeSet::from(indexed_graph_changeset)); + } } impl AsRef> for Wallet {