Skip to content

Commit 030a4e0

Browse files
author
+Sharon
committed
Add populate_anchor_cache method to bdk
1 parent 8d9df97 commit 030a4e0

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

crates/chain/src/tx_graph.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,13 @@ impl std::error::Error for CalculateFeeError {}
298298
impl<A> TxGraph<A> {
299299
/// Iterate over all tx outputs known by [`TxGraph`].
300300
///
301+
/// This returns an iterator over all anchors in the graph as (txid, anchor) pairs
302+
pub fn anchors(&self) -> impl Iterator<Item = (Txid, &A)> + '_ {
303+
self.anchors
304+
.iter()
305+
.flat_map(|(txid, anchors)| anchors.iter().map(move |a| (*txid, a)))
306+
}
307+
301308
/// This includes txouts of both full transactions as well as floating transactions.
302309
pub fn all_txouts(&self) -> impl Iterator<Item = (OutPoint, &TxOut)> {
303310
self.txs.iter().flat_map(|(txid, tx)| match tx {

crates/electrum/src/bdk_electrum_client.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use bdk_core::{
77
BlockId, CheckPoint, ConfirmationBlockTime, TxUpdate,
88
};
99
use electrum_client::{ElectrumApi, Error, HeaderNotification};
10+
use std::convert::TryInto;
1011
use std::sync::{Arc, Mutex};
1112

1213
/// We include a chain suffix of a certain length for the purpose of robustness.
@@ -37,6 +38,18 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
3738
}
3839
}
3940

41+
/// Insert anchors into the anchor cache so that they don’t have to be fetched again from
42+
/// Electrum. Typically used to pre-populate the cache from an existing `TxGraph`.
43+
pub fn populate_anchor_cache(
44+
&self,
45+
anchors: impl IntoIterator<Item = (Txid, ConfirmationBlockTime)>,
46+
) {
47+
let mut cache = self.anchor_cache.lock().unwrap();
48+
for (txid, anchor) in anchors {
49+
cache.insert((txid, anchor.block_id.hash), anchor);
50+
}
51+
}
52+
4053
/// Inserts transactions into the transaction cache so that the client will not fetch these
4154
/// transactions.
4255
pub fn populate_tx_cache(&self, txs: impl IntoIterator<Item = impl Into<Arc<Transaction>>>) {

examples/example_electrum/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ fn main() -> anyhow::Result<()> {
126126

127127
let client = BdkElectrumClient::new(electrum_cmd.electrum_args().client(network)?);
128128

129+
// This avoids re-fetching known anchor confirmations from Electrum.
130+
// Hold the lock for the entire anchor extraction
131+
let graph_guard = graph.lock().unwrap();
132+
// Collect anchors to release the lock quickly
133+
let mut anchors = Vec::new();
134+
for (txid, anchor) in graph_guard.graph().anchors() {
135+
anchors.push((txid, *anchor)); // Dereference instead of clone for Copy types
136+
}
137+
client.populate_anchor_cache(anchors);
138+
129139
// Tell the electrum client about the txs we've already got locally so it doesn't re-download
130140
// them
131141
client.populate_tx_cache(

0 commit comments

Comments
 (0)