Skip to content

Commit

Permalink
Note: Drop this when bitcoindevkit#1172 is merged
Browse files Browse the repository at this point in the history
adds Wallet methods `set_lookahead_for_all`,
`apply_block_relevant`, `batch_insert_relevant_unconfirmed`
  • Loading branch information
ValuedMammal committed Nov 19, 2023
1 parent e009549 commit 6cabc5f
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2309,6 +2309,57 @@ impl<D> Wallet<D> {
pub fn local_chain(&self) -> &LocalChain {
&self.chain
}

/// Set lookahead for all keychains.
///
/// Passing a lookahead of 0 will result in an error. This is because if we sync
/// the chain with a lookahead of 0, we would not find any update since we don't
/// have any scripts stored.
pub fn set_lookahead_for_all(&mut self, lookahead: u32) {
const DEFAULT_KEYCHAIN_LOOKAHEAD: u32 = 10;

self.indexed_graph.index.set_lookahead_for_all(
if lookahead > 0 {
lookahead
} else {
DEFAULT_KEYCHAIN_LOOKAHEAD
}
);
}

/// Insert all the block's relevant transactions into the IndexedTxGraph.
pub fn apply_block_relevant(
&mut self,
block: bitcoin::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<ConfirmationTimeHeightAnchor>> for Wallet<D> {
Expand Down

0 comments on commit 6cabc5f

Please sign in to comment.