Skip to content

Commit 6a4ada5

Browse files
committed
Merge #2005: Add populate_anchor_cache method to bdk
191bdb1 feat(electrum): Add `populate_anchor_cache` method (+Sharon) Pull request description: This PR addresses #1982 It introduces functionality to populate the anchor cache, improving how BDK handles stateful or performance-critical anchor data. ### Changes - Add `populate_anchor_cache` method to `BdkElectrumClient `in `bdk-core` - Improves sync speed by caching anchor transactions in advance - Enables pre-caching of anchor transactions to reduce redundant network calls Follow-up work will extend this functionality to `bdk-wallet`. ACKs for top commit: evanlinjin: ACK 191bdb1 Tree-SHA512: b14e9ee5a5deb439951175195419c2e07e513c23a6c242254e610b1f6d38a37cb71f2ae8d82f9c34628acf665126256eab7f37bf9ba6634547e7218433136cbe
2 parents 7b8fd5f + 191bdb1 commit 6a4ada5

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

crates/electrum/src/bdk_electrum_client.rs

Lines changed: 19 additions & 2 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,8 +38,24 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
3738
}
3839
}
3940

40-
/// Inserts transactions into the transaction cache so that the client will not fetch these
41-
/// transactions.
41+
/// Insert anchors into the anchor cache so that the client will not re-fetch them.
42+
///
43+
/// Typically used to pre-populate the cache from an existing `TxGraph`.
44+
pub fn populate_anchor_cache(
45+
&self,
46+
tx_anchors: impl IntoIterator<Item = (Txid, impl IntoIterator<Item = ConfirmationBlockTime>)>,
47+
) {
48+
let mut cache = self.anchor_cache.lock().unwrap();
49+
for (txid, anchors) in tx_anchors {
50+
for anchor in anchors {
51+
cache.insert((txid, anchor.block_id.hash), anchor);
52+
}
53+
}
54+
}
55+
56+
/// Insert transactions into the transaction cache so that the client will not re-fetch them.
57+
///
58+
/// Typically used to pre-populate the cache from an existing `TxGraph`.
4259
pub fn populate_tx_cache(&self, txs: impl IntoIterator<Item = impl Into<Arc<Transaction>>>) {
4360
let mut tx_cache = self.tx_cache.lock().unwrap();
4461
for tx in txs {

examples/example_electrum/src/main.rs

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

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

129-
// Tell the electrum client about the txs we've already got locally so it doesn't re-download
130-
// them
131-
client.populate_tx_cache(
132-
graph
133-
.lock()
134-
.unwrap()
135-
.graph()
136-
.full_txs()
137-
.map(|tx_node| tx_node.tx),
138-
);
129+
// Tell the electrum client about the txs and anchors we've already got locally so it doesn't
130+
// re-download .them
131+
{
132+
let graph = graph.lock().unwrap();
133+
client.populate_tx_cache(graph.graph().full_txs().map(|tx_node| tx_node.tx));
134+
client.populate_anchor_cache(graph.graph().all_anchors().clone());
135+
}
139136

140137
let (chain_update, tx_update, keychain_update) = match electrum_cmd.clone() {
141138
ElectrumCommands::Scan {

0 commit comments

Comments
 (0)