Skip to content

Commit 0d302f5

Browse files
committed
feat(electrum)!: depend on bdk_core instead of bdk_chain
`.populate_tx_cache` has been changed to take in a collection of `Arc<Transaction>`.
1 parent ab0315d commit 0d302f5

File tree

5 files changed

+44
-35
lines changed

5 files changed

+44
-35
lines changed

crates/electrum/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ license = "MIT OR Apache-2.0"
1010
readme = "README.md"
1111

1212
[dependencies]
13-
bdk_chain = { path = "../chain", version = "0.17.0" }
13+
bdk_core = { path = "../core", version = "0.1" }
1414
electrum-client = { version = "0.21", features = ["proxy"], default-features = false }
1515

1616
[dev-dependencies]
1717
bdk_testenv = { path = "../testenv", default-features = false }
18+
bdk_chain = { path = "../chain", version = "0.17.0" }
1819

1920
[features]
2021
default = ["use-rustls"]

crates/electrum/src/bdk_electrum_client.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use bdk_chain::{
1+
use bdk_core::{
22
bitcoin::{block::Header, BlockHash, OutPoint, ScriptBuf, Transaction, Txid},
33
collections::{BTreeMap, HashMap},
4-
local_chain::CheckPoint,
54
spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult},
6-
tx_graph::{self, TxGraph},
7-
Anchor, BlockId, ConfirmationBlockTime,
5+
tx_graph, BlockId, CheckPoint, ConfirmationBlockTime,
86
};
97
use electrum_client::{ElectrumApi, Error, HeaderNotification};
108
use std::{
@@ -39,14 +37,11 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
3937

4038
/// Inserts transactions into the transaction cache so that the client will not fetch these
4139
/// transactions.
42-
pub fn populate_tx_cache<A>(&self, tx_graph: impl AsRef<TxGraph<A>>) {
43-
let txs = tx_graph
44-
.as_ref()
45-
.full_txs()
46-
.map(|tx_node| (tx_node.txid, tx_node.tx));
47-
40+
pub fn populate_tx_cache(&self, txs: impl IntoIterator<Item = impl Into<Arc<Transaction>>>) {
4841
let mut tx_cache = self.tx_cache.lock().unwrap();
49-
for (txid, tx) in txs {
42+
for tx in txs {
43+
let tx = tx.into();
44+
let txid = tx.compute_txid();
5045
tx_cache.insert(txid, tx);
5146
}
5247
}
@@ -121,9 +116,10 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
121116
/// [`CalculateFeeError::MissingTxOut`] error if those `TxOut`s are not
122117
/// present in the transaction graph.
123118
///
124-
/// [`CalculateFeeError::MissingTxOut`]: bdk_chain::tx_graph::CalculateFeeError::MissingTxOut
125-
/// [`Wallet.calculate_fee`]: https://docs.rs/bdk_wallet/latest/bdk_wallet/struct.Wallet.html#method.calculate_fee
126-
/// [`Wallet.calculate_fee_rate`]: https://docs.rs/bdk_wallet/latest/bdk_wallet/struct.Wallet.html#method.calculate_fee_rate
119+
/// [`bdk_chain`]: ../bdk_chain/index.html
120+
/// [`CalculateFeeError::MissingTxOut`]: ../bdk_chain/tx_graph/enum.CalculateFeeError.html#variant.MissingTxOut
121+
/// [`Wallet.calculate_fee`]: ../bdk_wallet/struct.Wallet.html#method.calculate_fee
122+
/// [`Wallet.calculate_fee_rate`]: ../bdk_wallet/struct.Wallet.html#method.calculate_fee_rate
127123
pub fn full_scan<K: Ord + Clone>(
128124
&self,
129125
request: impl Into<FullScanRequest<K>>,
@@ -189,9 +185,10 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
189185
/// may include scripts that have been used, use [`full_scan`] with the keychain.
190186
///
191187
/// [`full_scan`]: Self::full_scan
192-
/// [`CalculateFeeError::MissingTxOut`]: bdk_chain::tx_graph::CalculateFeeError::MissingTxOut
193-
/// [`Wallet.calculate_fee`]: https://docs.rs/bdk_wallet/latest/bdk_wallet/struct.Wallet.html#method.calculate_fee
194-
/// [`Wallet.calculate_fee_rate`]: https://docs.rs/bdk_wallet/latest/bdk_wallet/struct.Wallet.html#method.calculate_fee_rate
188+
/// [`bdk_chain`]: ../bdk_chain/index.html
189+
/// [`CalculateFeeError::MissingTxOut`]: ../bdk_chain/tx_graph/enum.CalculateFeeError.html#variant.MissingTxOut
190+
/// [`Wallet.calculate_fee`]: ../bdk_wallet/struct.Wallet.html#method.calculate_fee
191+
/// [`Wallet.calculate_fee_rate`]: ../bdk_wallet/struct.Wallet.html#method.calculate_fee_rate
195192
pub fn sync<I: 'static>(
196193
&self,
197194
request: impl Into<SyncRequest<I>>,
@@ -514,20 +511,20 @@ fn fetch_tip_and_latest_blocks(
514511

515512
// Add a corresponding checkpoint per anchor height if it does not yet exist. Checkpoints should not
516513
// surpass `latest_blocks`.
517-
fn chain_update<A: Anchor>(
514+
fn chain_update(
518515
mut tip: CheckPoint,
519516
latest_blocks: &BTreeMap<u32, BlockHash>,
520-
anchors: impl Iterator<Item = (A, Txid)>,
517+
anchors: impl Iterator<Item = (ConfirmationBlockTime, Txid)>,
521518
) -> Result<CheckPoint, Error> {
522-
for anchor in anchors {
523-
let height = anchor.0.anchor_block().height;
519+
for (anchor, _txid) in anchors {
520+
let height = anchor.block_id.height;
524521

525522
// Checkpoint uses the `BlockHash` from `latest_blocks` so that the hash will be consistent
526523
// in case of a re-org.
527524
if tip.get(height).is_none() && height <= tip.height() {
528525
let hash = match latest_blocks.get(&height) {
529526
Some(&hash) => hash,
530-
None => anchor.0.anchor_block().hash,
527+
None => anchor.block_id.hash,
531528
};
532529
tip = tip.insert(BlockId { hash, height });
533530
}

crates/electrum/src/lib.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
//! This crate is used for updating structures of [`bdk_chain`] with data from an Electrum server.
1+
//! This crate is used for returning updates from Electrum servers.
22
//!
3-
//! The two primary methods are [`BdkElectrumClient::sync`] and [`BdkElectrumClient::full_scan`]. In most cases
4-
//! [`BdkElectrumClient::sync`] is used to sync the transaction histories of scripts that the application
5-
//! cares about, for example the scripts for all the receive addresses of a Wallet's keychain that it
6-
//! has shown a user. [`BdkElectrumClient::full_scan`] is meant to be used when importing or restoring a
7-
//! keychain where the range of possibly used scripts is not known. In this case it is necessary to
8-
//! scan all keychain scripts until a number (the "stop gap") of unused scripts is discovered. For a
9-
//! sync or full scan the user receives relevant blockchain data and output updates for
10-
//! [`bdk_chain`].
3+
//! Updates are returned as either a [`SyncResult`] (if [`BdkElectrumClient::sync()`] is called),
4+
//! or a [`FullScanResult`] (if [`BdkElectrumClient::full_scan()`] is called).
5+
//!
6+
//! In most cases [`BdkElectrumClient::sync()`] is used to sync the transaction histories of scripts
7+
//! that the application cares about, for example the scripts for all the receive addresses of a
8+
//! Wallet's keychain that it has shown a user.
9+
//!
10+
//! [`BdkElectrumClient::full_scan`] is meant to be used when importing or restoring a keychain
11+
//! where the range of possibly used scripts is not known. In this case it is necessary to scan all
12+
//! keychain scripts until a number (the "stop gap") of unused scripts is discovered.
1113
//!
1214
//! Refer to [`example_electrum`] for a complete example.
1315
//!
1416
//! [`example_electrum`]: https://github.com/bitcoindevkit/bdk/tree/master/example-crates/example_electrum
17+
//! [`SyncResult`]: bdk_core::spk_client::SyncResult
18+
//! [`FullScanResult`]: bdk_core::spk_client::FullScanResult
1519
1620
#![warn(missing_docs)]
1721

1822
mod bdk_electrum_client;
1923
pub use bdk_electrum_client::*;
2024

21-
pub use bdk_chain;
25+
pub use bdk_core;
2226
pub use electrum_client;

example-crates/example_electrum/src/main.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,14 @@ fn main() -> anyhow::Result<()> {
127127
let client = BdkElectrumClient::new(electrum_cmd.electrum_args().client(network)?);
128128

129129
// Tell the electrum client about the txs we've already got locally so it doesn't re-download them
130-
client.populate_tx_cache(&*graph.lock().unwrap());
130+
client.populate_tx_cache(
131+
graph
132+
.lock()
133+
.unwrap()
134+
.graph()
135+
.full_txs()
136+
.map(|tx_node| tx_node.tx),
137+
);
131138

132139
let (chain_update, graph_update, keychain_update) = match electrum_cmd.clone() {
133140
ElectrumCommands::Scan {

example-crates/wallet_electrum/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn main() -> Result<(), anyhow::Error> {
5050

5151
// Populate the electrum client's transaction cache so it doesn't redownload transaction we
5252
// already have.
53-
client.populate_tx_cache(wallet.tx_graph());
53+
client.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx));
5454

5555
let request = wallet.start_full_scan().inspect({
5656
let mut stdout = std::io::stdout();

0 commit comments

Comments
 (0)