Skip to content

Commit 065c64a

Browse files
committed
[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.
1 parent a56d289 commit 065c64a

File tree

4 files changed

+19
-35
lines changed

4 files changed

+19
-35
lines changed

crates/chain/src/chain_oracle.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ pub trait ChainOracle {
1010
/// Error type.
1111
type Error: core::fmt::Debug;
1212

13-
/// Determines whether `block` of [`BlockId`] exists as an ancestor of `static_block`.
13+
/// Determines whether `block` of [`BlockId`] exists as an ancestor of `chain_tip`.
1414
///
15-
/// If `None` is returned, it means the implementation cannot determine whether `block` exists.
15+
/// If `None` is returned, it means the implementation cannot determine whether `block` exists
16+
/// under `chain_tip`.
1617
fn is_block_in_chain(
1718
&self,
1819
block: BlockId,
19-
static_block: BlockId,
20+
chain_tip: BlockId,
2021
) -> Result<Option<bool>, Self::Error>;
2122
}

crates/chain/src/local_chain.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ impl LocalChain {
6464
}
6565
}
6666

67-
/// Get a reference to the inner map of block height to hash.
68-
pub fn inner(&self) -> &BTreeMap<u32, BlockHash> {
67+
/// Get a reference to a map of block height to hash.
68+
pub fn blocks(&self) -> &BTreeMap<u32, BlockHash> {
6969
&self.blocks
7070
}
7171

@@ -76,11 +76,6 @@ impl LocalChain {
7676
.map(|(&height, &hash)| BlockId { height, hash })
7777
}
7878

79-
/// Get a [`BlockHash`] at the given height.
80-
pub fn get_blockhash(&self, height: u32) -> Option<BlockHash> {
81-
self.blocks.get(&height).cloned()
82-
}
83-
8479
/// This is like the sparsechain's logic, expect we must guarantee that all invalidated heights
8580
/// are to be re-filled.
8681
pub fn determine_changeset(&self, update: &Self) -> Result<ChangeSet, UpdateNotConnectedError> {

crates/chain/tests/test_indexed_tx_graph.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ fn test_list_owned_txouts() {
212212
(
213213
*tx,
214214
local_chain
215-
.get_blockhash(height)
216-
.map(|hash| BlockId { height, hash })
215+
.blocks()
216+
.get(&height)
217+
.map(|&hash| BlockId { height, hash })
217218
.map(|anchor_block| ConfirmationHeightAnchor {
218219
anchor_block,
219220
confirmation_height: anchor_block.height,
@@ -229,34 +230,22 @@ fn test_list_owned_txouts() {
229230
let fetch =
230231
|height: u32,
231232
graph: &IndexedTxGraph<ConfirmationHeightAnchor, KeychainTxOutIndex<String>>| {
233+
let chain_tip = local_chain
234+
.blocks()
235+
.get(&height)
236+
.map(|&hash| BlockId { height, hash })
237+
.expect("block must exist");
232238
let txouts = graph
233-
.list_owned_txouts(
234-
&local_chain,
235-
local_chain
236-
.get_blockhash(height)
237-
.map(|hash| BlockId { height, hash })
238-
.unwrap(),
239-
)
239+
.list_owned_txouts(&local_chain, chain_tip)
240240
.collect::<Vec<_>>();
241241

242242
let utxos = graph
243-
.list_owned_unspents(
244-
&local_chain,
245-
local_chain
246-
.get_blockhash(height)
247-
.map(|hash| BlockId { height, hash })
248-
.unwrap(),
249-
)
243+
.list_owned_unspents(&local_chain, chain_tip)
250244
.collect::<Vec<_>>();
251245

252-
let balance = graph.balance(
253-
&local_chain,
254-
local_chain
255-
.get_blockhash(height)
256-
.map(|hash| BlockId { height, hash })
257-
.unwrap(),
258-
|spk: &Script| trusted_spks.contains(spk),
259-
);
246+
let balance = graph.balance(&local_chain, chain_tip, |spk: &Script| {
247+
trusted_spks.contains(spk)
248+
});
260249

261250
assert_eq!(txouts.len(), 5);
262251
assert_eq!(utxos.len(), 4);

crates/chain/tests/test_tx_graph.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,6 @@ fn test_chain_spends() {
694694
.iter()
695695
.zip([&tx_0, &tx_1].into_iter())
696696
.for_each(|(ht, tx)| {
697-
// let block_id = local_chain.get_block(*ht).expect("block expected");
698697
let _ = graph.insert_anchor(
699698
tx.txid(),
700699
ConfirmationHeightAnchor {

0 commit comments

Comments
 (0)