Skip to content

Commit 9845ca1

Browse files
feat: add apply_block_relevant/batch_insert_relevant_uncofirmed to wallet
1 parent 301bdf5 commit 9845ca1

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 36 additions & 2 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;
@@ -2001,6 +2001,40 @@ impl<D> Wallet<D> {
20012001
self.indexed_graph.index.set_lookahead_for_all(lookahead);
20022002
Ok(())
20032003
}
2004+
2005+
/// Insert all the block's relevant transactions into the IndexedTxGraph.
2006+
pub fn apply_block_relevant(
2007+
&mut self,
2008+
block: Block,
2009+
height: u32,
2010+
) -> Result<(), CannotConnectError>
2011+
where
2012+
D: PersistBackend<ChangeSet>,
2013+
{
2014+
let chain_update = CheckPoint::from_header(&block.header, height).into_update(false);
2015+
let mut changeset = ChangeSet::from(self.chain.apply_update(chain_update)?);
2016+
changeset.append(ChangeSet::from(
2017+
self.indexed_graph.apply_block_relevant(block, height),
2018+
));
2019+
self.persist.stage(changeset);
2020+
Ok(())
2021+
}
2022+
2023+
/// Batch insert unconfirmed transactions into the IndexedTxGraph.
2024+
/// Filtering out those that are not relevant.
2025+
///
2026+
/// Read more here: [`self.indexed_graph.batch_insert_relevant_unconfirmed()`]
2027+
pub fn batch_insert_relevant_unconfirmed<'t>(
2028+
&mut self,
2029+
unconfirmed_txs: impl IntoIterator<Item = (&'t Transaction, u64)>,
2030+
) where
2031+
D: PersistBackend<ChangeSet>,
2032+
{
2033+
let indexed_graph_changeset = self
2034+
.indexed_graph
2035+
.batch_insert_relevant_unconfirmed(unconfirmed_txs);
2036+
self.persist.stage(ChangeSet::from(indexed_graph_changeset));
2037+
}
20042038
}
20052039

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

0 commit comments

Comments
 (0)