Skip to content

Commit 08fac47

Browse files
committed
Merge #1413: Introduce universal sync/full-scan structures for spk-based syncing
c0374a0 feat(chain): `SyncRequest` now uses `ExactSizeIterator`s (志宇) 0f94f24 feat(esplora)!: update to use new sync/full-scan structures (志宇) 4c52f3e feat(wallet): make wallet compatible with sync/full-scan structures (志宇) cdfec5f feat(chain): add sync/full-scan structures for spk-based syncing (志宇) Pull request description: Fixes #1153 Replaces #1194 ### Description Introduce universal structures that represent sync/full-scan requests/results. ### Notes to the reviewers This is based on #1194 but is different in the following ways: * The functionality to print scan/sync progress is not reduced. * `SyncRequest` and `FullScanRequest` is simplified and fields are exposed for more flexibility. ### Changelog notice * Add universal structures for initiating/receiving sync/full-scan requests/results for spk-based syncing. * Updated `bdk_esplora` chain-source to make use of new universal sync/full-scan structures. ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### New Features: * [x] I've added tests for the new feature * [x] I've added docs for the new feature ACKs for top commit: notmandatory: tACK c0374a0 Tree-SHA512: c2ad66d972a6785079bca615dfd128edcedf6b7a02670651a0ab1ce5b5174dd96f54644680eedbf55e3f1955fe5c34f632eadbd3f71d7ffde658753c6c6d42be
2 parents ed3ccc1 + c0374a0 commit 08fac47

File tree

11 files changed

+705
-265
lines changed

11 files changed

+705
-265
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use bdk_chain::{
2626
local_chain::{
2727
self, ApplyHeaderError, CannotConnectError, CheckPoint, CheckPointIter, LocalChain,
2828
},
29+
spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult},
2930
tx_graph::{CanonicalTx, TxGraph},
3031
Append, BlockId, ChainPosition, ConfirmationTime, ConfirmationTimeHeightAnchor, FullTxOut,
3132
IndexedTxGraph,
@@ -111,6 +112,26 @@ pub struct Update {
111112
pub chain: Option<CheckPoint>,
112113
}
113114

115+
impl From<FullScanResult<KeychainKind>> for Update {
116+
fn from(value: FullScanResult<KeychainKind>) -> Self {
117+
Self {
118+
last_active_indices: value.last_active_indices,
119+
graph: value.graph_update,
120+
chain: Some(value.chain_update),
121+
}
122+
}
123+
}
124+
125+
impl From<SyncResult> for Update {
126+
fn from(value: SyncResult) -> Self {
127+
Self {
128+
last_active_indices: BTreeMap::new(),
129+
graph: value.graph_update,
130+
chain: Some(value.chain_update),
131+
}
132+
}
133+
}
134+
114135
/// The changes made to a wallet by applying an [`Update`].
115136
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, Default)]
116137
pub struct ChangeSet {
@@ -2263,7 +2284,8 @@ impl Wallet {
22632284
/// transactions related to your wallet into it.
22642285
///
22652286
/// [`commit`]: Self::commit
2266-
pub fn apply_update(&mut self, update: Update) -> Result<(), CannotConnectError> {
2287+
pub fn apply_update(&mut self, update: impl Into<Update>) -> Result<(), CannotConnectError> {
2288+
let update = update.into();
22672289
let mut changeset = match update.chain {
22682290
Some(chain_update) => ChangeSet::from(self.chain.apply_update(chain_update)?),
22692291
None => ChangeSet::default(),
@@ -2388,6 +2410,31 @@ impl Wallet {
23882410
}
23892411
}
23902412

2413+
/// Methods to construct sync/full-scan requests for spk-based chain sources.
2414+
impl Wallet {
2415+
/// Create a partial [`SyncRequest`] for this wallet for all revealed spks.
2416+
///
2417+
/// This is the first step when performing a spk-based wallet partial sync, the returned
2418+
/// [`SyncRequest`] collects all revealed script pubkeys from the wallet keychain needed to
2419+
/// start a blockchain sync with a spk based blockchain client.
2420+
pub fn start_sync_with_revealed_spks(&self) -> SyncRequest {
2421+
SyncRequest::from_chain_tip(self.chain.tip())
2422+
.populate_with_revealed_spks(&self.indexed_graph.index, ..)
2423+
}
2424+
2425+
/// Create a [`FullScanRequest] for this wallet.
2426+
///
2427+
/// This is the first step when performing a spk-based wallet full scan, the returned
2428+
/// [`FullScanRequest] collects iterators for the wallet's keychain script pub keys needed to
2429+
/// start a blockchain full scan with a spk based blockchain client.
2430+
///
2431+
/// This operation is generally only used when importing or restoring a previously used wallet
2432+
/// in which the list of used scripts is not known.
2433+
pub fn start_full_scan(&self) -> FullScanRequest<KeychainKind> {
2434+
FullScanRequest::from_keychain_txout_index(self.chain.tip(), &self.indexed_graph.index)
2435+
}
2436+
}
2437+
23912438
impl AsRef<bdk_chain::tx_graph::TxGraph<ConfirmationTimeHeightAnchor>> for Wallet {
23922439
fn as_ref(&self) -> &bdk_chain::tx_graph::TxGraph<ConfirmationTimeHeightAnchor> {
23932440
self.indexed_graph.graph()

crates/chain/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub use descriptor_ext::DescriptorExt;
4949
mod spk_iter;
5050
#[cfg(feature = "miniscript")]
5151
pub use spk_iter::*;
52+
pub mod spk_client;
5253

5354
#[allow(unused_imports)]
5455
#[macro_use]

0 commit comments

Comments
 (0)