Skip to content

Commit 0a2efa6

Browse files
feat: add apply_block_relevant/batch_insert_relevant_uncofirmed to wallet
1 parent 7b542ea commit 0a2efa6

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use bitcoin::psbt;
3333
use bitcoin::secp256k1::Secp256k1;
3434
use bitcoin::sighash::{EcdsaSighashType, TapSighashType};
3535
use bitcoin::{
36-
absolute, Address, Network, OutPoint, Script, ScriptBuf, Sequence, Transaction, TxOut, Txid,
37-
Weight, Witness,
36+
absolute, Address, Block, Network, OutPoint, Script, ScriptBuf, Sequence, Transaction, TxOut,
37+
Txid, Weight, Witness,
3838
};
3939
use core::fmt;
4040
use core::ops::Deref;
@@ -1989,12 +1989,46 @@ impl<D> Wallet<D> {
19891989

19901990
/// Set lookahead for all keychains.
19911991
pub fn set_lookahead_for_all(&mut self, lookahead: u32) {
1992-
// Having a lookahead of 0, means if we sync the chain
1992+
// Having a lookahead of 0, means if we sync the chain
19931993
// we would not find any update since we don't have any
19941994
// store scripts in our indexer.
19951995
assert_ne!(lookahead, 0, "lookahead must be greater than 0");
19961996
self.indexed_graph.index.set_lookahead_for_all(lookahead);
19971997
}
1998+
1999+
/// Insert all the block's relevant transactions into the IndexedTxGraph.
2000+
pub fn apply_block_relevant(
2001+
&mut self,
2002+
block: Block,
2003+
height: u32,
2004+
) -> Result<(), CannotConnectError>
2005+
where
2006+
D: PersistBackend<ChangeSet>,
2007+
{
2008+
let chain_update = CheckPoint::from_header(&block.header, height).into_update(false);
2009+
let mut changeset = ChangeSet::from(self.chain.apply_update(chain_update)?);
2010+
changeset.append(ChangeSet::from(
2011+
self.indexed_graph.apply_block_relevant(block, height),
2012+
));
2013+
self.persist.stage(changeset);
2014+
Ok(())
2015+
}
2016+
2017+
/// Batch insert unconfirmed transactions into the IndexedTxGraph.
2018+
/// Filtering out those that are not relevant.
2019+
///
2020+
/// Read more here: [`self.indexed_graph.batch_insert_relevant_unconfirmed()`]
2021+
pub fn batch_insert_relevant_unconfirmed<'t>(
2022+
&mut self,
2023+
unconfirmed_txs: impl IntoIterator<Item = (&'t Transaction, u64)>,
2024+
) where
2025+
D: PersistBackend<ChangeSet>,
2026+
{
2027+
let indexed_graph_changeset = self
2028+
.indexed_graph
2029+
.batch_insert_relevant_unconfirmed(unconfirmed_txs);
2030+
self.persist.stage(ChangeSet::from(indexed_graph_changeset));
2031+
}
19982032
}
19992033

20002034
impl<D> AsRef<bdk_chain::tx_graph::TxGraph<ConfirmationTimeAnchor>> for Wallet<D> {

0 commit comments

Comments
 (0)