diff --git a/crates/bdk/src/wallet/mod.rs b/crates/bdk/src/wallet/mod.rs index e79fe9255c..1d86db5b09 100644 --- a/crates/bdk/src/wallet/mod.rs +++ b/crates/bdk/src/wallet/mod.rs @@ -33,8 +33,8 @@ use bitcoin::psbt; use bitcoin::secp256k1::Secp256k1; use bitcoin::sighash::{EcdsaSighashType, TapSighashType}; use bitcoin::{ - absolute, Address, Network, OutPoint, Script, ScriptBuf, Sequence, Transaction, TxOut, Txid, - Weight, Witness, + absolute, Address, Block, Network, OutPoint, Script, ScriptBuf, Sequence, Transaction, TxOut, + Txid, Weight, Witness, }; use core::fmt; use core::ops::Deref; @@ -1989,12 +1989,46 @@ impl Wallet { /// Set lookahead for all keychains. pub fn set_lookahead_for_all(&mut self, lookahead: u32) { - // Having a lookahead of 0, means if we sync the chain + // Having a lookahead of 0, means if we sync the chain // we would not find any update since we don't have any // store scripts in our indexer. assert_ne!(lookahead, 0, "lookahead must be greater than 0"); self.indexed_graph.index.set_lookahead_for_all(lookahead); } + + /// Insert all the block's relevant transactions into the IndexedTxGraph. + pub fn apply_block_relevant( + &mut self, + block: 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 {