]> Untitled Git - bdk/commitdiff
[chain_redesign] Rename `LocalChain::inner()` to `blocks()`
author志宇 <hello@evanlinjin.me>
Fri, 5 May 2023 11:49:30 +0000 (19:49 +0800)
committer志宇 <hello@evanlinjin.me>
Fri, 5 May 2023 11:49:30 +0000 (19:49 +0800)
Also, we can get rid of `LocalChain::get_blockhash`, since we can
already expose the internal map.

Additionally, tests and docs are improved.

crates/chain/src/chain_oracle.rs
crates/chain/src/local_chain.rs
crates/chain/tests/test_indexed_tx_graph.rs
crates/chain/tests/test_tx_graph.rs

index 2b4ad36fe2305ffe4ddb6c3c5bb46ef8c9e9cb49..58fbf6c18d1566882dcaadb6bc350de8c7e642d0 100644 (file)
@@ -10,12 +10,13 @@ pub trait ChainOracle {
     /// Error type.
     type Error: core::fmt::Debug;
 
-    /// Determines whether `block` of [`BlockId`] exists as an ancestor of `static_block`.
+    /// Determines whether `block` of [`BlockId`] exists as an ancestor of `chain_tip`.
     ///
-    /// If `None` is returned, it means the implementation cannot determine whether `block` exists.
+    /// If `None` is returned, it means the implementation cannot determine whether `block` exists
+    /// under `chain_tip`.
     fn is_block_in_chain(
         &self,
         block: BlockId,
-        static_block: BlockId,
+        chain_tip: BlockId,
     ) -> Result<Option<bool>, Self::Error>;
 }
index 74dbb5a820c22364a7ec8f6906b65a0aec486764..a32a615c250f8b3af0de31eef7880fbdec482090 100644 (file)
@@ -64,8 +64,8 @@ impl LocalChain {
         }
     }
 
-    /// Get a reference to the inner map of block height to hash.
-    pub fn inner(&self) -> &BTreeMap<u32, BlockHash> {
+    /// Get a reference to a map of block height to hash.
+    pub fn blocks(&self) -> &BTreeMap<u32, BlockHash> {
         &self.blocks
     }
 
@@ -76,11 +76,6 @@ impl LocalChain {
             .map(|(&height, &hash)| BlockId { height, hash })
     }
 
-    /// Get a [`BlockHash`] at the given height.
-    pub fn get_blockhash(&self, height: u32) -> Option<BlockHash> {
-        self.blocks.get(&height).cloned()
-    }
-
     /// This is like the sparsechain's logic, expect we must guarantee that all invalidated heights
     /// are to be re-filled.
     pub fn determine_changeset(&self, update: &Self) -> Result<ChangeSet, UpdateNotConnectedError> {
index f5011c3e93d298024fb24467f933550a8599adc1..f32ffe4f0b28dbec2a5316f1460e1d062f842db9 100644 (file)
@@ -212,8 +212,9 @@ fn test_list_owned_txouts() {
             (
                 *tx,
                 local_chain
-                    .get_blockhash(height)
-                    .map(|hash| BlockId { height, hash })
+                    .blocks()
+                    .get(&height)
+                    .map(|&hash| BlockId { height, hash })
                     .map(|anchor_block| ConfirmationHeightAnchor {
                         anchor_block,
                         confirmation_height: anchor_block.height,
@@ -229,34 +230,22 @@ fn test_list_owned_txouts() {
     let fetch =
         |height: u32,
          graph: &IndexedTxGraph<ConfirmationHeightAnchor, KeychainTxOutIndex<String>>| {
+            let chain_tip = local_chain
+                .blocks()
+                .get(&height)
+                .map(|&hash| BlockId { height, hash })
+                .expect("block must exist");
             let txouts = graph
-                .list_owned_txouts(
-                    &local_chain,
-                    local_chain
-                        .get_blockhash(height)
-                        .map(|hash| BlockId { height, hash })
-                        .unwrap(),
-                )
+                .list_owned_txouts(&local_chain, chain_tip)
                 .collect::<Vec<_>>();
 
             let utxos = graph
-                .list_owned_unspents(
-                    &local_chain,
-                    local_chain
-                        .get_blockhash(height)
-                        .map(|hash| BlockId { height, hash })
-                        .unwrap(),
-                )
+                .list_owned_unspents(&local_chain, chain_tip)
                 .collect::<Vec<_>>();
 
-            let balance = graph.balance(
-                &local_chain,
-                local_chain
-                    .get_blockhash(height)
-                    .map(|hash| BlockId { height, hash })
-                    .unwrap(),
-                |spk: &Script| trusted_spks.contains(spk),
-            );
+            let balance = graph.balance(&local_chain, chain_tip, |spk: &Script| {
+                trusted_spks.contains(spk)
+            });
 
             assert_eq!(txouts.len(), 5);
             assert_eq!(utxos.len(), 4);
index 427de71e7a57425cfb71ad080fe01cee49d87872..2b845611972a1f4794690edb600d3d6219e62051 100644 (file)
@@ -694,7 +694,6 @@ fn test_chain_spends() {
         .iter()
         .zip([&tx_0, &tx_1].into_iter())
         .for_each(|(ht, tx)| {
-            // let block_id = local_chain.get_block(*ht).expect("block expected");
             let _ = graph.insert_anchor(
                 tx.txid(),
                 ConfirmationHeightAnchor {