]> Untitled Git - bdk/commitdiff
feat(electrum): Add `populate_anchor_cache` method
author+Sharon <+wanjiku.kahira@gmail.com>
Fri, 1 Aug 2025 00:59:10 +0000 (03:59 +0300)
committer志宇 <hello@evanlinjin.me>
Fri, 29 Aug 2025 00:17:18 +0000 (00:17 +0000)
Also updates the `example_electrum` example and `populate_tx_cache`
documentation to be consistent with `populate_anchor_cache`.

crates/electrum/src/bdk_electrum_client.rs
examples/example_electrum/src/main.rs

index 5962a5ec7845e4a5eff06d38755fa796ed904746..d7a26b95e9aed7cebf253d2d591f2a0abe56ed68 100644 (file)
@@ -7,6 +7,7 @@ use bdk_core::{
     BlockId, CheckPoint, ConfirmationBlockTime, TxUpdate,
 };
 use electrum_client::{ElectrumApi, Error, HeaderNotification};
+use std::convert::TryInto;
 use std::sync::{Arc, Mutex};
 
 /// We include a chain suffix of a certain length for the purpose of robustness.
@@ -37,8 +38,24 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
         }
     }
 
-    /// Inserts transactions into the transaction cache so that the client will not fetch these
-    /// transactions.
+    /// Insert anchors into the anchor cache so that the client will not re-fetch them.
+    ///
+    /// Typically used to pre-populate the cache from an existing `TxGraph`.
+    pub fn populate_anchor_cache(
+        &self,
+        tx_anchors: impl IntoIterator<Item = (Txid, impl IntoIterator<Item = ConfirmationBlockTime>)>,
+    ) {
+        let mut cache = self.anchor_cache.lock().unwrap();
+        for (txid, anchors) in tx_anchors {
+            for anchor in anchors {
+                cache.insert((txid, anchor.block_id.hash), anchor);
+            }
+        }
+    }
+
+    /// Insert transactions into the transaction cache so that the client will not re-fetch them.
+    ///
+    /// Typically used to pre-populate the cache from an existing `TxGraph`.
     pub fn populate_tx_cache(&self, txs: impl IntoIterator<Item = impl Into<Arc<Transaction>>>) {
         let mut tx_cache = self.tx_cache.lock().unwrap();
         for tx in txs {
index 01d11e78b04ea1609d2fde36f3999e6a0280b6fe..bc76776a6e87a8842d14102d36a970ba33bc62be 100644 (file)
@@ -126,16 +126,13 @@ fn main() -> anyhow::Result<()> {
 
     let client = BdkElectrumClient::new(electrum_cmd.electrum_args().client(network)?);
 
-    // Tell the electrum client about the txs we've already got locally so it doesn't re-download
-    // them
-    client.populate_tx_cache(
-        graph
-            .lock()
-            .unwrap()
-            .graph()
-            .full_txs()
-            .map(|tx_node| tx_node.tx),
-    );
+    // Tell the electrum client about the txs and anchors we've already got locally so it doesn't
+    // re-download .them
+    {
+        let graph = graph.lock().unwrap();
+        client.populate_tx_cache(graph.graph().full_txs().map(|tx_node| tx_node.tx));
+        client.populate_anchor_cache(graph.graph().all_anchors().clone());
+    }
 
     let (chain_update, tx_update, keychain_update) = match electrum_cmd.clone() {
         ElectrumCommands::Scan {