Skip to content

Commit 95312d4

Browse files
committed
fix: docs and some minor refactoring
Shout out to @LLFourn for these suggestions. * Improve/fix `LocalChain` documentation * Refactor `TxGraph::missing_blocks` to make it more explicit that `last_block` has state. * `update_local_chain` method of `EsploraExt` and `EsploraAsyncExt` now returns a `local_chain::Update` instead of just a `CheckPoint`.
1 parent 8bf7a99 commit 95312d4

File tree

4 files changed

+28
-37
lines changed

4 files changed

+28
-37
lines changed

crates/chain/src/local_chain.rs

+15-22
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,25 @@ impl IntoIterator for CheckPoint {
113113
}
114114
}
115115

116-
/// Represents an update to [`LocalChain`].
116+
/// A struct to update [`LocalChain`].
117+
///
118+
/// This is used as input for [`LocalChain::apply_update`]. It contains the update's chain `tip` and
119+
/// a `bool` which signals whether this update can introduce blocks below the original chain's tip
120+
/// without invalidating blocks residing on the original chain. Block-by-block syncing mechanisms
121+
/// would typically create updates that builds upon the previous tip. In this case, this paramater
122+
/// would be `false`. Script-pubkey based syncing mechanisms may not introduce transactions in a
123+
/// chronological order so some updates require introducing older blocks (to anchor older
124+
/// transactions). For script-pubkey based syncing, this parameter would typically be `true`.
117125
#[derive(Debug, Clone)]
118126
pub struct Update {
119-
/// The update's new [`CheckPoint`] tip.
127+
/// The update chain's new tip.
120128
pub tip: CheckPoint,
121129

122130
/// Whether the update allows for introducing older blocks.
123131
///
124-
/// Refer to [`LocalChain::apply_update`] for more.
132+
/// Refer to [struct-level documentation] for more.
125133
///
126-
/// [`LocalChain::apply_update`]: crate::local_chain::LocalChain::apply_update
134+
/// [struct-level documentation]: Update
127135
pub introduce_older_blocks: bool,
128136
}
129137

@@ -146,12 +154,6 @@ impl From<LocalChain> for BTreeMap<u32, BlockHash> {
146154
}
147155
}
148156

149-
impl From<ChangeSet> for LocalChain {
150-
fn from(value: ChangeSet) -> Self {
151-
Self::from_changeset(value)
152-
}
153-
}
154-
155157
impl From<BTreeMap<u32, BlockHash>> for LocalChain {
156158
fn from(value: BTreeMap<u32, BlockHash>) -> Self {
157159
Self::from_blocks(value)
@@ -244,18 +246,9 @@ impl LocalChain {
244246
self.tip.is_none()
245247
}
246248

247-
/// Updates [`Self`] with the given `update_tip`.
248-
///
249-
/// `introduce_older_blocks` specifies whether the `update_tip`'s history can introduce blocks
250-
/// below the original chain's tip without invalidating blocks. Block-by-block syncing
251-
/// mechanisms would typically create updates that builds upon the previous tip. In this case,
252-
/// this paramater would be false. Script-pubkey based syncing mechanisms may not introduce
253-
/// transactions in a chronological order so some updates require introducing older blocks (to
254-
/// anchor older transactions). For script-pubkey based syncing, this parameter would typically
255-
/// be true.
249+
/// Applies the given `update` to the chain.
256250
///
257-
/// The method returns [`ChangeSet`] on success. This represents the applied changes to
258-
/// [`Self`].
251+
/// The method returns [`ChangeSet`] on success. This represents the applied changes to `self`.
259252
///
260253
/// To update, the `update_tip` must *connect* with `self`. If `self` and `update_tip` has a
261254
/// mutual checkpoint (same height and hash), it can connect if:
@@ -275,7 +268,7 @@ impl LocalChain {
275268
///
276269
/// An error will occur if the update does not correctly connect with `self`.
277270
///
278-
/// Refer to [module-level documentation] for more.
271+
/// Refer to [`Update`] for more about the update struct.
279272
///
280273
/// [module-level documentation]: crate::local_chain
281274
pub fn apply_update(&mut self, update: Update) -> Result<ChangeSet, CannotConnectError> {

crates/chain/src/tx_graph.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -603,18 +603,16 @@ impl<A: Anchor> TxGraph<A> {
603603
/// This works by scanning through anchors, and seeing whether the anchor block of the anchor
604604
/// exists in the [`LocalChain`].
605605
pub fn missing_blocks<'a>(&'a self, chain: &'a LocalChain) -> impl Iterator<Item = u32> + 'a {
606+
let mut last_block = Option::<BlockId>::None;
606607
self.anchors
607608
.iter()
608609
.map(|(a, _)| a.anchor_block())
609-
.filter({
610-
let mut last_block = Option::<BlockId>::None;
611-
move |block| {
612-
if last_block.as_ref() == Some(block) {
613-
false
614-
} else {
615-
last_block = Some(*block);
616-
true
617-
}
610+
.filter(move |block| {
611+
if last_block.as_ref() == Some(block) {
612+
false
613+
} else {
614+
last_block = Some(*block);
615+
true
618616
}
619617
})
620618
.filter_map(|block| match chain.heights().get(&block.height) {

crates/chain/tests/test_indexed_tx_graph.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ fn insert_relevant_txs() {
107107
108108
fn test_list_owned_txouts() {
109109
// Create Local chains
110-
111-
let local_chain = (0..150)
112-
.map(|i| (i as u32, Some(h!("random"))))
113-
.collect::<BTreeMap<u32, Option<BlockHash>>>();
114-
let local_chain = LocalChain::from(local_chain);
110+
let local_chain = LocalChain::from(
111+
(0..150)
112+
.map(|i| (i as u32, h!("random")))
113+
.collect::<BTreeMap<u32, BlockHash>>(),
114+
);
115115

116116
// Initiate IndexedTxGraph
117117

crates/esplora/src/blocking_ext.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use bdk_chain::collections::{BTreeMap, BTreeSet};
66
use bdk_chain::{
77
bitcoin::{BlockHash, Script},
88
local_chain::{self, CheckPoint},
9+
BlockId, ConfirmationTimeAnchor, TxGraph,
910
};
10-
use bdk_chain::{BlockId, ConfirmationTimeAnchor, TxGraph};
1111
use esplora_client::{Error, TxStatus};
1212

1313
use crate::{anchor_from_status, ASSUME_FINAL_DEPTH};

0 commit comments

Comments
 (0)