Skip to content

Commit

Permalink
feat: add apply_block_relevant/batch_insert_relevant_uncofirmed to wa…
Browse files Browse the repository at this point in the history
…llet
  • Loading branch information
vladimirfomene committed Oct 23, 2023
1 parent 9b4107a commit 0e58d0b
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1989,12 +1989,46 @@ impl<D> Wallet<D> {

/// 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<ChangeSet>,
{
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<Item = (&'t Transaction, u64)>,
) where
D: PersistBackend<ChangeSet>,
{
let indexed_graph_changeset = self
.indexed_graph
.batch_insert_relevant_unconfirmed(unconfirmed_txs);
self.persist.stage(ChangeSet::from(indexed_graph_changeset));
}
}

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

0 comments on commit 0e58d0b

Please sign in to comment.