Skip to content

Commit e327ff1

Browse files
feat: add apply_block_relevant/batch_insert_relevant_uncofirmed to wallet
1 parent 9b4107a commit e327ff1

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 42 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;
@@ -1949,6 +1949,18 @@ impl<D> Wallet<D> {
19491949
Ok(())
19501950
}
19511951

1952+
/// Stages wallet changes in memory for later commit.
1953+
///
1954+
/// To commit changes to the persistence backend, call [`commit`].
1955+
///
1956+
/// [`commit`]: Self::commit
1957+
pub fn stage(&mut self, changeset: ChangeSet)
1958+
where
1959+
D: PersistBackend<ChangeSet>,
1960+
{
1961+
self.persist.stage(changeset);
1962+
}
1963+
19521964
/// Commits all currently [`staged`] changed to the persistence backend returning and error when
19531965
/// this fails.
19541966
///
@@ -1989,12 +2001,39 @@ impl<D> Wallet<D> {
19892001

19902002
/// Set lookahead for all keychains.
19912003
pub fn set_lookahead_for_all(&mut self, lookahead: u32) {
1992-
// Having a lookahead of 0, means if we sync the chain
2004+
// Having a lookahead of 0, means if we sync the chain
19932005
// we would not find any update since we don't have any
19942006
// store scripts in our indexer.
19952007
assert_ne!(lookahead, 0, "lookahead must be greater than 0");
19962008
self.indexed_graph.index.set_lookahead_for_all(lookahead);
19972009
}
2010+
2011+
/// Insert all the block's relevant transactions into the IndexedTxGraph.
2012+
pub fn apply_block_relevant(
2013+
&mut self,
2014+
block: Block,
2015+
height: u32,
2016+
) -> Result<ChangeSet, CannotConnectError> {
2017+
let chain_update = CheckPoint::from_header(&block.header, height).into_update(false);
2018+
let mut changeset = ChangeSet::from(self.chain.apply_update(chain_update)?);
2019+
changeset.append(ChangeSet::from(
2020+
self.indexed_graph.apply_block_relevant(block, height),
2021+
));
2022+
Ok(changeset)
2023+
}
2024+
2025+
/// Batch insert unconfirmed transactions into the IndexedTxGraph.
2026+
/// Filtering out those that are not relevant.
2027+
///
2028+
/// Read more here: [`self.indexed_graph.batch_insert_relevant_unconfirmed()`]
2029+
pub fn batch_insert_relevant_unconfirmed<'t>(
2030+
&mut self,
2031+
unconfirmed_txs: impl IntoIterator<Item = (&'t Transaction, u64)>,
2032+
) -> indexed_tx_graph::ChangeSet<ConfirmationTimeAnchor, keychain::ChangeSet<KeychainKind>>
2033+
{
2034+
self.indexed_graph
2035+
.batch_insert_relevant_unconfirmed(unconfirmed_txs)
2036+
}
19982037
}
19992038

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

0 commit comments

Comments
 (0)