[chain_redesign] Rename LocalChain::inner() to blocks()

Also, we can get rid of `LocalChain::get_blockhash`, since we can
already expose the internal map.

Additionally, tests and docs are improved.
This commit is contained in:
志宇 2023-05-05 19:49:30 +08:00
parent a56d289eef
commit 065c64a675
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8
4 changed files with 19 additions and 35 deletions

View File

@ -10,12 +10,13 @@ pub trait ChainOracle {
/// Error type. /// Error type.
type Error: core::fmt::Debug; 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( fn is_block_in_chain(
&self, &self,
block: BlockId, block: BlockId,
static_block: BlockId, chain_tip: BlockId,
) -> Result<Option<bool>, Self::Error>; ) -> Result<Option<bool>, Self::Error>;
} }

View File

@ -64,8 +64,8 @@ impl LocalChain {
} }
} }
/// Get a reference to the inner map of block height to hash. /// Get a reference to a map of block height to hash.
pub fn inner(&self) -> &BTreeMap<u32, BlockHash> { pub fn blocks(&self) -> &BTreeMap<u32, BlockHash> {
&self.blocks &self.blocks
} }
@ -76,11 +76,6 @@ impl LocalChain {
.map(|(&height, &hash)| BlockId { height, hash }) .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 /// This is like the sparsechain's logic, expect we must guarantee that all invalidated heights
/// are to be re-filled. /// are to be re-filled.
pub fn determine_changeset(&self, update: &Self) -> Result<ChangeSet, UpdateNotConnectedError> { pub fn determine_changeset(&self, update: &Self) -> Result<ChangeSet, UpdateNotConnectedError> {

View File

@ -212,8 +212,9 @@ fn test_list_owned_txouts() {
( (
*tx, *tx,
local_chain local_chain
.get_blockhash(height) .blocks()
.map(|hash| BlockId { height, hash }) .get(&height)
.map(|&hash| BlockId { height, hash })
.map(|anchor_block| ConfirmationHeightAnchor { .map(|anchor_block| ConfirmationHeightAnchor {
anchor_block, anchor_block,
confirmation_height: anchor_block.height, confirmation_height: anchor_block.height,
@ -229,34 +230,22 @@ fn test_list_owned_txouts() {
let fetch = let fetch =
|height: u32, |height: u32,
graph: &IndexedTxGraph<ConfirmationHeightAnchor, KeychainTxOutIndex<String>>| { 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 let txouts = graph
.list_owned_txouts( .list_owned_txouts(&local_chain, chain_tip)
&local_chain,
local_chain
.get_blockhash(height)
.map(|hash| BlockId { height, hash })
.unwrap(),
)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let utxos = graph let utxos = graph
.list_owned_unspents( .list_owned_unspents(&local_chain, chain_tip)
&local_chain,
local_chain
.get_blockhash(height)
.map(|hash| BlockId { height, hash })
.unwrap(),
)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let balance = graph.balance( let balance = graph.balance(&local_chain, chain_tip, |spk: &Script| {
&local_chain, trusted_spks.contains(spk)
local_chain });
.get_blockhash(height)
.map(|hash| BlockId { height, hash })
.unwrap(),
|spk: &Script| trusted_spks.contains(spk),
);
assert_eq!(txouts.len(), 5); assert_eq!(txouts.len(), 5);
assert_eq!(utxos.len(), 4); assert_eq!(utxos.len(), 4);

View File

@ -694,7 +694,6 @@ fn test_chain_spends() {
.iter() .iter()
.zip([&tx_0, &tx_1].into_iter()) .zip([&tx_0, &tx_1].into_iter())
.for_each(|(ht, tx)| { .for_each(|(ht, tx)| {
// let block_id = local_chain.get_block(*ht).expect("block expected");
let _ = graph.insert_anchor( let _ = graph.insert_anchor(
tx.txid(), tx.txid(),
ConfirmationHeightAnchor { ConfirmationHeightAnchor {