From db15e03bdce78c6321f906f390b10b3d9e7501b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Sat, 22 Jul 2023 19:42:12 +0800 Subject: [PATCH] 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`. --- crates/bdk/src/wallet/mod.rs | 2 +- crates/chain/src/keychain.rs | 9 +++++---- crates/chain/src/local_chain.rs | 10 +++++++--- crates/chain/tests/test_local_chain.rs | 2 +- crates/electrum/src/electrum_ext.rs | 5 ++--- crates/esplora/src/async_ext.rs | 1 - example-crates/example_electrum/src/main.rs | 4 +++- example-crates/wallet_esplora/src/main.rs | 2 +- example-crates/wallet_esplora_async/src/main.rs | 2 +- 9 files changed, 21 insertions(+), 16 deletions(-) diff --git a/crates/bdk/src/wallet/mod.rs b/crates/bdk/src/wallet/mod.rs index 634c5c66c..73e8839d2 100644 --- a/crates/bdk/src/wallet/mod.rs +++ b/crates/bdk/src/wallet/mod.rs @@ -1711,7 +1711,7 @@ impl Wallet { let (_, index_additions) = self .indexed_graph .index - .reveal_to_target_multi(&update.keychain); + .reveal_to_target_multi(&update.last_active_indices); changeset.append(ChangeSet::from(IndexedAdditions::from(index_additions))); changeset.append(ChangeSet::from( self.indexed_graph.apply_update(update.graph), diff --git a/crates/chain/src/keychain.rs b/crates/chain/src/keychain.rs index cc85df4cf..317d5f2b4 100644 --- a/crates/chain/src/keychain.rs +++ b/crates/chain/src/keychain.rs @@ -91,8 +91,9 @@ impl AsRef> for DerivationAdditions { /// [`LocalChain`]: local_chain::LocalChain #[derive(Debug, Clone)] pub struct LocalUpdate { - /// Last active derivation index per keychain (`K`). - pub keychain: BTreeMap, + /// Contains the last active derivation indices per keychain (`K`), which is used to update the + /// [`KeychainTxOutIndex`]. + pub last_active_indices: BTreeMap, /// Update for the [`TxGraph`]. pub graph: TxGraph, @@ -104,12 +105,12 @@ pub struct LocalUpdate { } impl LocalUpdate { - /// Construct a [`LocalUpdate`] with a given [`CheckPoint`] tip. + /// Construct a [`LocalUpdate`] with a given [`local_chain::Update`]. /// /// [`CheckPoint`]: local_chain::CheckPoint pub fn new(chain_update: local_chain::Update) -> Self { Self { - keychain: BTreeMap::new(), + last_active_indices: BTreeMap::new(), graph: TxGraph::default(), chain: chain_update, } diff --git a/crates/chain/src/local_chain.rs b/crates/chain/src/local_chain.rs index 48e298350..1caf20b7a 100644 --- a/crates/chain/src/local_chain.rs +++ b/crates/chain/src/local_chain.rs @@ -10,9 +10,13 @@ use bitcoin::BlockHash; /// A structure that represents changes to [`LocalChain`]. pub type ChangeSet = BTreeMap>; -/// A blockchain of [`LocalChain`]. +/// A [`LocalChain`] checkpoint is used to find the agreement point between two chains and as a +/// transaction anchor. /// -/// The in a linked-list with newer blocks pointing to older ones. +/// Each checkpoint contains the height and hash of a block ([`BlockId`]). +/// +/// Internaly, checkpoints are nodes of a linked-list. This allows the caller to view the entire +/// chain without holding a lock to [`LocalChain`]. #[derive(Debug, Clone)] pub struct CheckPoint(Arc); @@ -382,7 +386,7 @@ impl LocalChain { } } - /// Get a reference to the internal index mapping the height to block hash + /// Get a reference to the internal index mapping the height to block hash. pub fn heights(&self) -> &BTreeMap { &self.index } diff --git a/crates/chain/tests/test_local_chain.rs b/crates/chain/tests/test_local_chain.rs index aaa2c371d..9ea8b7f3a 100644 --- a/crates/chain/tests/test_local_chain.rs +++ b/crates/chain/tests/test_local_chain.rs @@ -123,7 +123,7 @@ fn update_local_chain() { }, }, // Introduce an older checkpoint (A) that is not directly behind PoA - // | 1 | 2 | 3 + // | 2 | 3 | 4 // chain | B C // update | A C TestLocalChain { diff --git a/crates/electrum/src/electrum_ext.rs b/crates/electrum/src/electrum_ext.rs index 62f7aa7e3..666e06776 100644 --- a/crates/electrum/src/electrum_ext.rs +++ b/crates/electrum/src/electrum_ext.rs @@ -69,7 +69,7 @@ impl ElectrumUpdate { } } Ok(LocalUpdate { - keychain: self.keychain_update, + last_active_indices: self.keychain_update, graph: graph_update, chain: local_chain::Update { tip: self.new_tip, @@ -93,7 +93,6 @@ impl ElectrumUpdate { missing: Vec, ) -> Result, Error> { let update = self.finalize(client, seen_at, missing)?; - // client.batch_transaction_get(txid) let relevant_heights = { let mut visited_heights = HashSet::new(); @@ -141,7 +140,7 @@ impl ElectrumUpdate { }; Ok(LocalUpdate { - keychain: update.keychain, + last_active_indices: update.last_active_indices, graph: { let mut graph = TxGraph::default(); graph.apply_additions(graph_additions); diff --git a/crates/esplora/src/async_ext.rs b/crates/esplora/src/async_ext.rs index 5de02ffd0..6a72dac24 100644 --- a/crates/esplora/src/async_ext.rs +++ b/crates/esplora/src/async_ext.rs @@ -282,7 +282,6 @@ impl EsploraAsyncExt for esplora_client::AsyncClient { async move { client.get_tx_status(&txid).await.map(|s| (txid, s)) } }) .collect::>(); - // .collect::>>>(); if handles.is_empty() { break; diff --git a/example-crates/example_electrum/src/main.rs b/example-crates/example_electrum/src/main.rs index 537412f07..6b8d13526 100644 --- a/example-crates/example_electrum/src/main.rs +++ b/example-crates/example_electrum/src/main.rs @@ -278,7 +278,9 @@ fn main() -> anyhow::Result<()> { let indexed_additions = { let mut additions = IndexedAdditions::::default(); - let (_, index_additions) = graph.index.reveal_to_target_multi(&final_update.keychain); + let (_, index_additions) = graph + .index + .reveal_to_target_multi(&final_update.last_active_indices); additions.append(IndexedAdditions { index_additions, ..Default::default() diff --git a/example-crates/wallet_esplora/src/main.rs b/example-crates/wallet_esplora/src/main.rs index 530aee5ba..9cd4663e4 100644 --- a/example-crates/wallet_esplora/src/main.rs +++ b/example-crates/wallet_esplora/src/main.rs @@ -59,7 +59,7 @@ fn main() -> Result<(), Box> { let get_heights = wallet.tx_graph().missing_blocks(wallet.local_chain()); let chain_update = client.update_local_chain(prev_tip, get_heights)?; let update = LocalUpdate { - keychain: last_active_indices, + last_active_indices, graph: update_graph, ..LocalUpdate::new(chain_update) }; diff --git a/example-crates/wallet_esplora_async/src/main.rs b/example-crates/wallet_esplora_async/src/main.rs index fe1c85a22..a82b410b0 100644 --- a/example-crates/wallet_esplora_async/src/main.rs +++ b/example-crates/wallet_esplora_async/src/main.rs @@ -60,7 +60,7 @@ async fn main() -> Result<(), Box> { let get_heights = wallet.tx_graph().missing_blocks(wallet.local_chain()); let chain_update = client.update_local_chain(prev_tip, get_heights).await?; let update = LocalUpdate { - keychain: last_active_indices, + last_active_indices, graph: update_graph, ..LocalUpdate::new(chain_update) };