Skip to content

Commit

Permalink
[chain_redesign] Rename LocalChain::inner() to blocks()
Browse files Browse the repository at this point in the history
Also, we can get rid of `LocalChain::get_blockhash`, since we can
already expose the internal map.

Additionally, tests and docs are improved.
  • Loading branch information
evanlinjin committed May 5, 2023
1 parent a56d289 commit 065c64a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 35 deletions.
7 changes: 4 additions & 3 deletions crates/chain/src/chain_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}
9 changes: 2 additions & 7 deletions crates/chain/src/local_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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> {
Expand Down
37 changes: 13 additions & 24 deletions crates/chain/tests/test_indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion crates/chain/tests/test_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 065c64a

Please sign in to comment.