]> Untitled Git - bdk/commitdiff
refactor: Edit ElectrumExt not to use WalletUpdate
authorVladimir Fomene <vladimirfomene@gmail.com>
Mon, 21 Aug 2023 08:20:38 +0000 (11:20 +0300)
committer志宇 <hello@evanlinjin.me>
Sat, 2 Sep 2023 16:54:23 +0000 (00:54 +0800)
crates/electrum/src/electrum_ext.rs
example-crates/example_electrum/src/main.rs
example-crates/wallet_electrum/src/main.rs

index c7859bdfe2dc9e8f39469d3206e8b686cbe0d546..e81ef1d324d2c06f61ec17784b2b6b680c02ee1e 100644 (file)
@@ -1,6 +1,5 @@
 use bdk_chain::{
     bitcoin::{OutPoint, ScriptBuf, Transaction, Txid},
-    keychain::WalletUpdate,
     local_chain::{self, CheckPoint},
     tx_graph::{self, TxGraph},
     Anchor, BlockId, ConfirmationHeightAnchor, ConfirmationTimeAnchor,
@@ -15,7 +14,8 @@ use std::{
 /// We assume that a block of this depth and deeper cannot be reorged.
 const ASSUME_FINAL_DEPTH: u32 = 8;
 
-/// Represents an update fetched from an Electrum server, but excludes full transactions.
+/// Represents an update fetched from an Electrum server, but excludes full
+/// transactions.
 ///
 /// To provide a complete update to [`TxGraph`], you'll need to call [`Self::missing_full_txs`] to
 /// determine the full transactions missing from [`TxGraph`]. Then call [`Self::finalize`] to fetch
@@ -58,7 +58,7 @@ impl<K, A: Anchor> ElectrumUpdate<K, A> {
         client: &Client,
         seen_at: Option<u64>,
         missing: Vec<Txid>,
-    ) -> Result<WalletUpdate<K, A>, Error> {
+    ) -> Result<(TxGraph<A>, BTreeMap<K, u32>, local_chain::CheckPoint), Error> {
         let new_txs = client.batch_transaction_get(&missing)?;
         let mut graph_update = TxGraph::<A>::new(new_txs);
         for (txid, anchors) in self.graph_update {
@@ -69,14 +69,7 @@ impl<K, A: Anchor> ElectrumUpdate<K, A> {
                 let _ = graph_update.insert_anchor(txid, anchor);
             }
         }
-        Ok(WalletUpdate {
-            last_active_indices: self.keychain_update,
-            graph: graph_update,
-            chain: local_chain::Update {
-                tip: self.new_tip,
-                introduce_older_blocks: true,
-            },
-        })
+        Ok((graph_update, self.keychain_update, self.new_tip))
     }
 }
 
@@ -92,13 +85,19 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> {
         client: &Client,
         seen_at: Option<u64>,
         missing: Vec<Txid>,
-    ) -> Result<WalletUpdate<K, ConfirmationTimeAnchor>, Error> {
-        let update = self.finalize(client, seen_at, missing)?;
+    ) -> Result<
+        (
+            TxGraph<ConfirmationTimeAnchor>,
+            BTreeMap<K, u32>,
+            local_chain::CheckPoint,
+        ),
+        Error,
+    > {
+        let (graph, keychain_update, update_tip) = self.finalize(client, seen_at, missing)?;
 
         let relevant_heights = {
             let mut visited_heights = HashSet::new();
-            update
-                .graph
+            graph
                 .all_anchors()
                 .iter()
                 .map(|(a, _)| a.confirmation_height_upper_bound())
@@ -118,7 +117,7 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> {
             .collect::<HashMap<u32, u64>>();
 
         let graph_changeset = {
-            let old_changeset = TxGraph::default().apply_update(update.graph.clone());
+            let old_changeset = TxGraph::default().apply_update(graph.clone());
             tx_graph::ChangeSet {
                 txs: old_changeset.txs,
                 txouts: old_changeset.txouts,
@@ -140,15 +139,10 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> {
             }
         };
 
-        Ok(WalletUpdate {
-            last_active_indices: update.last_active_indices,
-            graph: {
-                let mut graph = TxGraph::default();
-                graph.apply_changeset(graph_changeset);
-                graph
-            },
-            chain: update.chain,
-        })
+        let mut update = TxGraph::default();
+        update.apply_changeset(graph_changeset);
+
+        Ok((update, keychain_update, update_tip))
     }
 }
 
@@ -457,7 +451,6 @@ fn populate_with_outpoints<K>(
             };
 
             let anchor = determine_tx_anchor(cps, res.height, res.tx_hash);
-
             let tx_entry = update.graph_update.entry(res.tx_hash).or_default();
             if let Some(anchor) = anchor {
                 tx_entry.insert(anchor);
index 4c5fde6a3330958c2b83951c408eb4ac389c91f2..56cc144c4bf36081409c30276c40c0cf0fef1f37 100644 (file)
@@ -8,7 +8,7 @@ use bdk_chain::{
     bitcoin::{Address, Network, OutPoint, ScriptBuf, Txid},
     indexed_tx_graph::{self, IndexedTxGraph},
     keychain::WalletChangeSet,
-    local_chain::LocalChain,
+    local_chain::{self, LocalChain},
     Append, ConfirmationHeightAnchor,
 };
 use bdk_electrum::{
@@ -269,25 +269,27 @@ fn main() -> anyhow::Result<()> {
         .expect("must get time")
         .as_secs();
 
-    let final_update = response.finalize(&client, Some(now), missing_txids)?;
+    let (graph_update, keychain_update, update_tip) =
+        response.finalize(&client, Some(now), missing_txids)?;
 
     let db_changeset = {
         let mut chain = chain.lock().unwrap();
         let mut graph = graph.lock().unwrap();
 
-        let chain = chain.apply_update(final_update.chain)?;
+        let chain = chain.apply_update(local_chain::Update {
+            tip: update_tip,
+            introduce_older_blocks: true,
+        })?;
 
         let indexed_tx_graph = {
             let mut changeset =
                 indexed_tx_graph::ChangeSet::<ConfirmationHeightAnchor, _>::default();
-            let (_, indexer) = graph
-                .index
-                .reveal_to_target_multi(&final_update.last_active_indices);
+            let (_, indexer) = graph.index.reveal_to_target_multi(&keychain_update);
             changeset.append(indexed_tx_graph::ChangeSet {
                 indexer,
                 ..Default::default()
             });
-            changeset.append(graph.apply_update(final_update.graph));
+            changeset.append(graph.apply_update(graph_update));
             changeset
         };
 
index 52def58eb68c698bf7628bfe306ff07deed016f0..3ba7e5f4c1592d25f7337870ab45257c02f04970 100644 (file)
@@ -9,6 +9,7 @@ use std::str::FromStr;
 use bdk::bitcoin::Address;
 use bdk::SignOptions;
 use bdk::{bitcoin::Network, Wallet};
+use bdk_electrum::bdk_chain::{keychain::WalletUpdate, local_chain};
 use bdk_electrum::electrum_client::{self, ElectrumApi};
 use bdk_electrum::ElectrumExt;
 use bdk_file_store::Store;
@@ -57,9 +58,18 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
     println!();
 
     let missing = electrum_update.missing_full_txs(wallet.as_ref());
-    let update = electrum_update.finalize_as_confirmation_time(&client, None, missing)?;
-
-    wallet.apply_update(update)?;
+    let (graph_update, keychain_update, update_tip) =
+        electrum_update.finalize_as_confirmation_time(&client, None, missing)?;
+
+    let wallet_update = WalletUpdate {
+        last_active_indices: keychain_update,
+        graph: graph_update,
+        chain: local_chain::Update {
+            tip: update_tip,
+            introduce_older_blocks: true,
+        },
+    };
+    wallet.apply_update(wallet_update)?;
     wallet.commit()?;
 
     let balance = wallet.get_balance();