]> Untitled Git - bdk/commitdiff
feat(electrum)!: depend on `bdk_core` instead of `bdk_chain`
author志宇 <hello@evanlinjin.me>
Fri, 23 Aug 2024 18:26:28 +0000 (18:26 +0000)
committer志宇 <hello@evanlinjin.me>
Sat, 24 Aug 2024 16:19:17 +0000 (16:19 +0000)
`.populate_tx_cache` has been changed to take in a collection of
`Arc<Transaction>`.

crates/electrum/Cargo.toml
crates/electrum/src/bdk_electrum_client.rs
crates/electrum/src/lib.rs
example-crates/example_electrum/src/main.rs
example-crates/wallet_electrum/src/main.rs

index eff11daac66a418e170fef6bc31e00455e9df5c2..b91c136626ae036aee745e4a5bea9a5290c4a089 100644 (file)
@@ -10,11 +10,12 @@ license = "MIT OR Apache-2.0"
 readme = "README.md"
 
 [dependencies]
-bdk_chain = { path = "../chain", version = "0.17.0" }
+bdk_core = { path = "../core", version = "0.1" }
 electrum-client = { version = "0.21", features = ["proxy"], default-features = false }
 
 [dev-dependencies]
 bdk_testenv = { path = "../testenv", default-features = false }
+bdk_chain = { path = "../chain", version = "0.17.0" }
 
 [features]
 default = ["use-rustls"]
index 57166075669fc04cfcf7c700d999dc4f674c6079..d78d7f64c3b28b3a16e372ceb6dcb1e12681df49 100644 (file)
@@ -1,10 +1,8 @@
-use bdk_chain::{
+use bdk_core::{
     bitcoin::{block::Header, BlockHash, OutPoint, ScriptBuf, Transaction, Txid},
     collections::{BTreeMap, HashMap},
-    local_chain::CheckPoint,
     spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult},
-    tx_graph::{self, TxGraph},
-    Anchor, BlockId, ConfirmationBlockTime,
+    tx_graph, BlockId, CheckPoint, ConfirmationBlockTime,
 };
 use electrum_client::{ElectrumApi, Error, HeaderNotification};
 use std::{
@@ -39,14 +37,11 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
 
     /// Inserts transactions into the transaction cache so that the client will not fetch these
     /// transactions.
-    pub fn populate_tx_cache<A>(&self, tx_graph: impl AsRef<TxGraph<A>>) {
-        let txs = tx_graph
-            .as_ref()
-            .full_txs()
-            .map(|tx_node| (tx_node.txid, tx_node.tx));
-
+    pub fn populate_tx_cache(&self, txs: impl IntoIterator<Item = impl Into<Arc<Transaction>>>) {
         let mut tx_cache = self.tx_cache.lock().unwrap();
-        for (txid, tx) in txs {
+        for tx in txs {
+            let tx = tx.into();
+            let txid = tx.compute_txid();
             tx_cache.insert(txid, tx);
         }
     }
@@ -121,9 +116,10 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
     ///                        [`CalculateFeeError::MissingTxOut`] error if those `TxOut`s are not
     ///                        present in the transaction graph.
     ///
-    /// [`CalculateFeeError::MissingTxOut`]: bdk_chain::tx_graph::CalculateFeeError::MissingTxOut
-    /// [`Wallet.calculate_fee`]: https://docs.rs/bdk_wallet/latest/bdk_wallet/struct.Wallet.html#method.calculate_fee
-    /// [`Wallet.calculate_fee_rate`]: https://docs.rs/bdk_wallet/latest/bdk_wallet/struct.Wallet.html#method.calculate_fee_rate
+    /// [`bdk_chain`]: ../bdk_chain/index.html
+    /// [`CalculateFeeError::MissingTxOut`]: ../bdk_chain/tx_graph/enum.CalculateFeeError.html#variant.MissingTxOut
+    /// [`Wallet.calculate_fee`]: ../bdk_wallet/struct.Wallet.html#method.calculate_fee
+    /// [`Wallet.calculate_fee_rate`]: ../bdk_wallet/struct.Wallet.html#method.calculate_fee_rate
     pub fn full_scan<K: Ord + Clone>(
         &self,
         request: impl Into<FullScanRequest<K>>,
@@ -189,9 +185,10 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
     /// may include scripts that have been used, use [`full_scan`] with the keychain.
     ///
     /// [`full_scan`]: Self::full_scan
-    /// [`CalculateFeeError::MissingTxOut`]: bdk_chain::tx_graph::CalculateFeeError::MissingTxOut
-    /// [`Wallet.calculate_fee`]: https://docs.rs/bdk_wallet/latest/bdk_wallet/struct.Wallet.html#method.calculate_fee
-    /// [`Wallet.calculate_fee_rate`]: https://docs.rs/bdk_wallet/latest/bdk_wallet/struct.Wallet.html#method.calculate_fee_rate
+    /// [`bdk_chain`]: ../bdk_chain/index.html
+    /// [`CalculateFeeError::MissingTxOut`]: ../bdk_chain/tx_graph/enum.CalculateFeeError.html#variant.MissingTxOut
+    /// [`Wallet.calculate_fee`]: ../bdk_wallet/struct.Wallet.html#method.calculate_fee
+    /// [`Wallet.calculate_fee_rate`]: ../bdk_wallet/struct.Wallet.html#method.calculate_fee_rate
     pub fn sync<I: 'static>(
         &self,
         request: impl Into<SyncRequest<I>>,
@@ -514,20 +511,20 @@ fn fetch_tip_and_latest_blocks(
 
 // Add a corresponding checkpoint per anchor height if it does not yet exist. Checkpoints should not
 // surpass `latest_blocks`.
-fn chain_update<A: Anchor>(
+fn chain_update(
     mut tip: CheckPoint,
     latest_blocks: &BTreeMap<u32, BlockHash>,
-    anchors: impl Iterator<Item = (A, Txid)>,
+    anchors: impl Iterator<Item = (ConfirmationBlockTime, Txid)>,
 ) -> Result<CheckPoint, Error> {
-    for anchor in anchors {
-        let height = anchor.0.anchor_block().height;
+    for (anchor, _txid) in anchors {
+        let height = anchor.block_id.height;
 
         // Checkpoint uses the `BlockHash` from `latest_blocks` so that the hash will be consistent
         // in case of a re-org.
         if tip.get(height).is_none() && height <= tip.height() {
             let hash = match latest_blocks.get(&height) {
                 Some(&hash) => hash,
-                None => anchor.0.anchor_block().hash,
+                None => anchor.block_id.hash,
             };
             tip = tip.insert(BlockId { hash, height });
         }
index d303ee40367db856a6aa58494a83f1f4587f4097..914b4f19a4850b00b339d68da1f8136278259794 100644 (file)
@@ -1,22 +1,26 @@
-//! This crate is used for updating structures of [`bdk_chain`] with data from an Electrum server.
+//! This crate is used for returning updates from Electrum servers.
 //!
-//! The two primary methods are [`BdkElectrumClient::sync`] and [`BdkElectrumClient::full_scan`]. In most cases
-//! [`BdkElectrumClient::sync`] is used to sync the transaction histories of scripts that the application
-//! cares about, for example the scripts for all the receive addresses of a Wallet's keychain that it
-//! has shown a user. [`BdkElectrumClient::full_scan`] is meant to be used when importing or restoring a
-//! keychain where the range of possibly used scripts is not known. In this case it is necessary to
-//! scan all keychain scripts until a number (the "stop gap") of unused scripts is discovered. For a
-//! sync or full scan the user receives relevant blockchain data and output updates for
-//! [`bdk_chain`].
+//! Updates are returned as either a [`SyncResult`] (if [`BdkElectrumClient::sync()`] is called),
+//! or a [`FullScanResult`] (if [`BdkElectrumClient::full_scan()`] is called).
+//!
+//! In most cases [`BdkElectrumClient::sync()`] is used to sync the transaction histories of scripts
+//! that the application cares about, for example the scripts for all the receive addresses of a
+//! Wallet's keychain that it has shown a user.
+//!
+//! [`BdkElectrumClient::full_scan`] is meant to be used when importing or restoring a keychain
+//! where the range of possibly used scripts is not known. In this case it is necessary to scan all
+//! keychain scripts until a number (the "stop gap") of unused scripts is discovered.
 //!
 //! Refer to [`example_electrum`] for a complete example.
 //!
 //! [`example_electrum`]: https://github.com/bitcoindevkit/bdk/tree/master/example-crates/example_electrum
+//! [`SyncResult`]: bdk_core::spk_client::SyncResult
+//! [`FullScanResult`]: bdk_core::spk_client::FullScanResult
 
 #![warn(missing_docs)]
 
 mod bdk_electrum_client;
 pub use bdk_electrum_client::*;
 
-pub use bdk_chain;
+pub use bdk_core;
 pub use electrum_client;
index 662bc42373fdd1bbd031034140bb8b0d167f6fa9..21910cf6604576b4911e195473d120236c7c4971 100644 (file)
@@ -127,7 +127,14 @@ 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());
+    client.populate_tx_cache(
+        graph
+            .lock()
+            .unwrap()
+            .graph()
+            .full_txs()
+            .map(|tx_node| tx_node.tx),
+    );
 
     let (chain_update, graph_update, keychain_update) = match electrum_cmd.clone() {
         ElectrumCommands::Scan {
index c05184052be7a6075964699dea33ffb9004590dd..47cbfa15dc6e8197a3230a737f19de78184ebd14 100644 (file)
@@ -50,7 +50,7 @@ fn main() -> Result<(), anyhow::Error> {
 
     // Populate the electrum client's transaction cache so it doesn't redownload transaction we
     // already have.
-    client.populate_tx_cache(wallet.tx_graph());
+    client.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx));
 
     let request = wallet.start_full_scan().inspect({
         let mut stdout = std::io::stdout();