Skip to content

Commit 348d117

Browse files
committed
refactor(electrum_ext): rename scan_without_keychain to sync and scan to full_scan
1 parent 908b0f9 commit 348d117

File tree

5 files changed

+47
-30
lines changed

5 files changed

+47
-30
lines changed

crates/electrum/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
# BDK Electrum
22

3-
BDK Electrum client library for updating the keychain tracker.
3+
BDK Electrum extends [`electrum-client`] to update [`bdk_chain`] structures
4+
from an Electrum server.
5+
6+
[`electrum-client`]: https://docs.rs/electrum-client/
7+
[`bdk_chain`]: https://docs.rs/bdk-chain/

crates/electrum/src/electrum_ext.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,19 @@ pub struct ElectrumUpdate {
134134

135135
/// Trait to extend [`Client`] functionality.
136136
pub trait ElectrumExt {
137-
/// Scan the blockchain (via electrum) for the data specified and returns updates for
138-
/// [`bdk_chain`] data structures.
137+
/// Full scan the keychain scripts specified with the blockchain (via an Electrum client) and
138+
/// returns updates for [`bdk_chain`] data structures.
139139
///
140140
/// - `prev_tip`: the most recent blockchain tip present locally
141141
/// - `keychain_spks`: keychains that we want to scan transactions for
142142
/// - `txids`: transactions for which we want updated [`Anchor`]s
143143
/// - `outpoints`: transactions associated with these outpoints (residing, spending) that we
144144
/// want to included in the update
145145
///
146-
/// The scan for each keychain stops after a gap of `stop_gap` script pubkeys with no associated
146+
/// The full scan for each keychain stops after a gap of `stop_gap` script pubkeys with no associated
147147
/// transactions. `batch_size` specifies the max number of script pubkeys to request for in a
148148
/// single batch request.
149-
fn scan<K: Ord + Clone>(
149+
fn full_scan<K: Ord + Clone>(
150150
&self,
151151
prev_tip: CheckPoint,
152152
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, ScriptBuf)>>,
@@ -156,10 +156,23 @@ pub trait ElectrumExt {
156156
batch_size: usize,
157157
) -> Result<(ElectrumUpdate, BTreeMap<K, u32>), Error>;
158158

159-
/// Convenience method to call [`scan`] without requiring a keychain.
159+
/// Sync a set of scripts with the blockchain (via an Electrum client) for the data specified
160+
/// and returns updates for [`bdk_chain`] data structures.
160161
///
161-
/// [`scan`]: ElectrumExt::scan
162-
fn scan_without_keychain(
162+
/// - `prev_tip`: the most recent blockchain tip present locally
163+
/// - `misc_spks`: an iterator of scripts we want to sync transactions for
164+
/// - `txids`: transactions for which we want updated [`Anchor`]s
165+
/// - `outpoints`: transactions associated with these outpoints (residing, spending) that we
166+
/// want to included in the update
167+
///
168+
/// `batch_size` specifies the max number of script pubkeys to request for in a single batch
169+
/// request.
170+
///
171+
/// If the scripts to sync are unknown, such as when restoring or importing a keychain that
172+
/// may include scripts that have been used, use [`full_scan`] with the keychain.
173+
///
174+
/// [`full_scan`]: ElectrumExt::full_scan
175+
fn sync(
163176
&self,
164177
prev_tip: CheckPoint,
165178
misc_spks: impl IntoIterator<Item = ScriptBuf>,
@@ -172,7 +185,7 @@ pub trait ElectrumExt {
172185
.enumerate()
173186
.map(|(i, spk)| (i as u32, spk));
174187

175-
let (electrum_update, _) = self.scan(
188+
let (electrum_update, _) = self.full_scan(
176189
prev_tip,
177190
[((), spk_iter)].into(),
178191
txids,
@@ -186,7 +199,7 @@ pub trait ElectrumExt {
186199
}
187200

188201
impl ElectrumExt for Client {
189-
fn scan<K: Ord + Clone>(
202+
fn full_scan<K: Ord + Clone>(
190203
&self,
191204
prev_tip: CheckPoint,
192205
keychain_spks: BTreeMap<K, impl IntoIterator<Item = (u32, ScriptBuf)>>,

crates/electrum/src/lib.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
//! This crate is used for updating structures of the [`bdk_chain`] crate with data from electrum.
1+
//! This crate is used for updating structures of [`bdk_chain`] with data from an Electrum server.
22
//!
3-
//! The star of the show is the [`ElectrumExt::scan`] method, which scans for relevant blockchain
4-
//! data (via electrum) and outputs updates for [`bdk_chain`] structures as a tuple of form:
3+
//! The two primary methods are [`ElectrumExt::sync`] and [`ElectrumExt::full_scan`]. In most cases
4+
//! [`ElectrumExt::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. [`ElectrumExt::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`] including [`RelevantTxids`].
511
//!
6-
//! ([`bdk_chain::local_chain::Update`], [`RelevantTxids`], `keychain_update`)
12+
//! The [`RelevantTxids`] only includes `txid`s and not full transactions. The caller is responsible
13+
//! for obtaining full transactions before applying new data to their [`bdk_chain`]. This can be
14+
//! done with these steps:
715
//!
8-
//! An [`RelevantTxids`] only includes `txid`s and no full transactions. The caller is
9-
//! responsible for obtaining full transactions before applying. This can be done with
10-
//! these steps:
16+
//! 1. Determine which full transactions are missing. Use [`RelevantTxids::missing_full_txs`].
1117
//!
12-
//! 1. Determine which full transactions are missing. The method [`missing_full_txs`] of
13-
//! [`RelevantTxids`] can be used.
18+
//! 2. Obtaining the full transactions. To do this via electrum use [`ElectrumApi::batch_transaction_get`].
1419
//!
15-
//! 2. Obtaining the full transactions. To do this via electrum, the method
16-
//! [`batch_transaction_get`] can be used.
20+
//! Refer to [`example_electrum`] for a complete example.
1721
//!
18-
//! Refer to [`bdk_electrum_example`] for a complete example.
19-
//!
20-
//! [`ElectrumClient::scan`]: electrum_client::ElectrumClient::scan
21-
//! [`missing_full_txs`]: RelevantTxids::missing_full_txs
22-
//! [`batch_transaction_get`]: electrum_client::ElectrumApi::batch_transaction_get
23-
//! [`bdk_electrum_example`]: https://github.com/LLFourn/bdk_core_staging/tree/master/bdk_electrum_example
22+
//! [`ElectrumApi::batch_transaction_get`]: electrum_client::ElectrumApi::batch_transaction_get
23+
//! [`example_electrum`]: https://github.com/bitcoindevkit/bdk/tree/master/example-crates/example_electrum
2424
2525
#![warn(missing_docs)]
2626

example-crates/example_electrum/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn main() -> anyhow::Result<()> {
172172
};
173173

174174
client
175-
.scan(
175+
.full_scan(
176176
tip,
177177
keychain_spks,
178178
core::iter::empty(),
@@ -279,7 +279,7 @@ fn main() -> anyhow::Result<()> {
279279
drop((graph, chain));
280280

281281
let electrum_update = client
282-
.scan_without_keychain(tip, spks, txids, outpoints, scan_options.batch_size)
282+
.sync(tip, spks, txids, outpoints, scan_options.batch_size)
283283
.context("scanning the blockchain")?;
284284
(electrum_update, BTreeMap::new())
285285
}

example-crates/wallet_electrum/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fn main() -> Result<(), anyhow::Error> {
6161
relevant_txids,
6262
},
6363
keychain_update,
64-
) = client.scan(prev_tip, keychain_spks, None, None, STOP_GAP, BATCH_SIZE)?;
64+
) = client.full_scan(prev_tip, keychain_spks, None, None, STOP_GAP, BATCH_SIZE)?;
6565

6666
println!();
6767

0 commit comments

Comments
 (0)