Skip to content

Commit db15e03

Browse files
committed
fix: improve more docs and more refactoring
Thank you @vladimirfomene for these suggestions. * Rename `LocalUpdate::keychain` to `LocalUpdate::last_active_indices`. * Change docs for `CheckPoint` to be more descriptive. * Fix incorrect logic in `update_local_chain` for `EsploraExt` and `EsploraAsyncExt`.
1 parent 95312d4 commit db15e03

File tree

9 files changed

+21
-16
lines changed

9 files changed

+21
-16
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ impl<D> Wallet<D> {
17111711
let (_, index_additions) = self
17121712
.indexed_graph
17131713
.index
1714-
.reveal_to_target_multi(&update.keychain);
1714+
.reveal_to_target_multi(&update.last_active_indices);
17151715
changeset.append(ChangeSet::from(IndexedAdditions::from(index_additions)));
17161716
changeset.append(ChangeSet::from(
17171717
self.indexed_graph.apply_update(update.graph),

crates/chain/src/keychain.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ impl<K> AsRef<BTreeMap<K, u32>> for DerivationAdditions<K> {
9191
/// [`LocalChain`]: local_chain::LocalChain
9292
#[derive(Debug, Clone)]
9393
pub struct LocalUpdate<K, A> {
94-
/// Last active derivation index per keychain (`K`).
95-
pub keychain: BTreeMap<K, u32>,
94+
/// Contains the last active derivation indices per keychain (`K`), which is used to update the
95+
/// [`KeychainTxOutIndex`].
96+
pub last_active_indices: BTreeMap<K, u32>,
9697

9798
/// Update for the [`TxGraph`].
9899
pub graph: TxGraph<A>,
@@ -104,12 +105,12 @@ pub struct LocalUpdate<K, A> {
104105
}
105106

106107
impl<K, A> LocalUpdate<K, A> {
107-
/// Construct a [`LocalUpdate`] with a given [`CheckPoint`] tip.
108+
/// Construct a [`LocalUpdate`] with a given [`local_chain::Update`].
108109
///
109110
/// [`CheckPoint`]: local_chain::CheckPoint
110111
pub fn new(chain_update: local_chain::Update) -> Self {
111112
Self {
112-
keychain: BTreeMap::new(),
113+
last_active_indices: BTreeMap::new(),
113114
graph: TxGraph::default(),
114115
chain: chain_update,
115116
}

crates/chain/src/local_chain.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ use bitcoin::BlockHash;
1010
/// A structure that represents changes to [`LocalChain`].
1111
pub type ChangeSet = BTreeMap<u32, Option<BlockHash>>;
1212

13-
/// A blockchain of [`LocalChain`].
13+
/// A [`LocalChain`] checkpoint is used to find the agreement point between two chains and as a
14+
/// transaction anchor.
1415
///
15-
/// The in a linked-list with newer blocks pointing to older ones.
16+
/// Each checkpoint contains the height and hash of a block ([`BlockId`]).
17+
///
18+
/// Internaly, checkpoints are nodes of a linked-list. This allows the caller to view the entire
19+
/// chain without holding a lock to [`LocalChain`].
1620
#[derive(Debug, Clone)]
1721
pub struct CheckPoint(Arc<CPInner>);
1822

@@ -382,7 +386,7 @@ impl LocalChain {
382386
}
383387
}
384388

385-
/// Get a reference to the internal index mapping the height to block hash
389+
/// Get a reference to the internal index mapping the height to block hash.
386390
pub fn heights(&self) -> &BTreeMap<u32, BlockHash> {
387391
&self.index
388392
}

crates/chain/tests/test_local_chain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn update_local_chain() {
123123
},
124124
},
125125
// Introduce an older checkpoint (A) that is not directly behind PoA
126-
// | 1 | 2 | 3
126+
// | 2 | 3 | 4
127127
// chain | B C
128128
// update | A C
129129
TestLocalChain {

crates/electrum/src/electrum_ext.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<K, A: Anchor> ElectrumUpdate<K, A> {
6969
}
7070
}
7171
Ok(LocalUpdate {
72-
keychain: self.keychain_update,
72+
last_active_indices: self.keychain_update,
7373
graph: graph_update,
7474
chain: local_chain::Update {
7575
tip: self.new_tip,
@@ -93,7 +93,6 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> {
9393
missing: Vec<Txid>,
9494
) -> Result<LocalUpdate<K, ConfirmationTimeAnchor>, Error> {
9595
let update = self.finalize(client, seen_at, missing)?;
96-
// client.batch_transaction_get(txid)
9796

9897
let relevant_heights = {
9998
let mut visited_heights = HashSet::new();
@@ -141,7 +140,7 @@ impl<K> ElectrumUpdate<K, ConfirmationHeightAnchor> {
141140
};
142141

143142
Ok(LocalUpdate {
144-
keychain: update.keychain,
143+
last_active_indices: update.last_active_indices,
145144
graph: {
146145
let mut graph = TxGraph::default();
147146
graph.apply_additions(graph_additions);

crates/esplora/src/async_ext.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ impl EsploraAsyncExt for esplora_client::AsyncClient {
282282
async move { client.get_tx_status(&txid).await.map(|s| (txid, s)) }
283283
})
284284
.collect::<FuturesOrdered<_>>();
285-
// .collect::<Vec<JoinHandle<Result<(Txid, TxStatus), Error>>>>();
286285

287286
if handles.is_empty() {
288287
break;

example-crates/example_electrum/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,9 @@ fn main() -> anyhow::Result<()> {
278278

279279
let indexed_additions = {
280280
let mut additions = IndexedAdditions::<ConfirmationHeightAnchor, _>::default();
281-
let (_, index_additions) = graph.index.reveal_to_target_multi(&final_update.keychain);
281+
let (_, index_additions) = graph
282+
.index
283+
.reveal_to_target_multi(&final_update.last_active_indices);
282284
additions.append(IndexedAdditions {
283285
index_additions,
284286
..Default::default()

example-crates/wallet_esplora/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5959
let get_heights = wallet.tx_graph().missing_blocks(wallet.local_chain());
6060
let chain_update = client.update_local_chain(prev_tip, get_heights)?;
6161
let update = LocalUpdate {
62-
keychain: last_active_indices,
62+
last_active_indices,
6363
graph: update_graph,
6464
..LocalUpdate::new(chain_update)
6565
};

example-crates/wallet_esplora_async/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
6060
let get_heights = wallet.tx_graph().missing_blocks(wallet.local_chain());
6161
let chain_update = client.update_local_chain(prev_tip, get_heights).await?;
6262
let update = LocalUpdate {
63-
keychain: last_active_indices,
63+
last_active_indices,
6464
graph: update_graph,
6565
..LocalUpdate::new(chain_update)
6666
};

0 commit comments

Comments
 (0)