Skip to content

Commit a14a02f

Browse files
feat: add apply_block_relevant/batch_insert_relevant_uncofirmed to wallet
1 parent ef63f87 commit a14a02f

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

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

0 commit comments

Comments
 (0)