Skip to content

Commit a56d289

Browse files
committed
[chain_redesign] Various LocalChain improvements
* Introduce `LocalChain::inner` method to get the inner map of block height to hash. * Replace `LocalChain::get_block` (which outputted `BlockId`, thus able to return invalid representation) with `get_blockhash` that just returns a `BlockHash`. * Remove `TODO` comments that should be github tickets.
1 parent 2ccc116 commit a56d289

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

crates/chain/src/local_chain.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ use bitcoin::BlockHash;
66
use crate::{BlockId, ChainOracle};
77

88
/// This is a local implementation of [`ChainOracle`].
9-
///
10-
/// TODO: We need a cache/snapshot thing for chain oracle.
11-
/// * Minimize calls to remotes.
12-
/// * Can we cache it forever? Should we drop stuff?
13-
/// * Assume anything deeper than (i.e. 10) blocks won't be reorged.
14-
/// * Is this a cache on txs or block? or both?
15-
/// TODO: Parents of children are confirmed if children are confirmed.
169
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
1710
pub struct LocalChain {
1811
blocks: BTreeMap<u32, BlockHash>,
@@ -71,18 +64,21 @@ impl LocalChain {
7164
}
7265
}
7366

67+
/// Get a reference to the inner map of block height to hash.
68+
pub fn inner(&self) -> &BTreeMap<u32, BlockHash> {
69+
&self.blocks
70+
}
71+
7472
pub fn tip(&self) -> Option<BlockId> {
7573
self.blocks
7674
.iter()
7775
.last()
7876
.map(|(&height, &hash)| BlockId { height, hash })
7977
}
8078

81-
/// Get a block at the given height.
82-
pub fn get_block(&self, height: u32) -> Option<BlockId> {
83-
self.blocks
84-
.get(&height)
85-
.map(|&hash| BlockId { height, hash })
79+
/// Get a [`BlockHash`] at the given height.
80+
pub fn get_blockhash(&self, height: u32) -> Option<BlockHash> {
81+
self.blocks.get(&height).cloned()
8682
}
8783

8884
/// This is like the sparsechain's logic, expect we must guarantee that all invalidated heights

crates/chain/tests/test_indexed_tx_graph.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bdk_chain::{
88
keychain::{Balance, DerivationAdditions, KeychainTxOutIndex},
99
local_chain::LocalChain,
1010
tx_graph::Additions,
11-
ConfirmationHeightAnchor, ObservedAs,
11+
BlockId, ConfirmationHeightAnchor, ObservedAs,
1212
};
1313
use bitcoin::{secp256k1::Secp256k1, BlockHash, OutPoint, Script, Transaction, TxIn, TxOut};
1414
use miniscript::Descriptor;
@@ -208,10 +208,12 @@ fn test_list_owned_txouts() {
208208

209209
let _ = graph.insert_relevant_txs(
210210
[&tx1, &tx2, &tx3, &tx6].iter().enumerate().map(|(i, tx)| {
211+
let height = i as u32;
211212
(
212213
*tx,
213214
local_chain
214-
.get_block(i as u32)
215+
.get_blockhash(height)
216+
.map(|hash| BlockId { height, hash })
215217
.map(|anchor_block| ConfirmationHeightAnchor {
216218
anchor_block,
217219
confirmation_height: anchor_block.height,
@@ -225,18 +227,34 @@ fn test_list_owned_txouts() {
225227

226228
// A helper lambda to extract and filter data from the graph.
227229
let fetch =
228-
|ht: u32, graph: &IndexedTxGraph<ConfirmationHeightAnchor, KeychainTxOutIndex<String>>| {
230+
|height: u32,
231+
graph: &IndexedTxGraph<ConfirmationHeightAnchor, KeychainTxOutIndex<String>>| {
229232
let txouts = graph
230-
.list_owned_txouts(&local_chain, local_chain.get_block(ht).unwrap())
233+
.list_owned_txouts(
234+
&local_chain,
235+
local_chain
236+
.get_blockhash(height)
237+
.map(|hash| BlockId { height, hash })
238+
.unwrap(),
239+
)
231240
.collect::<Vec<_>>();
232241

233242
let utxos = graph
234-
.list_owned_unspents(&local_chain, local_chain.get_block(ht).unwrap())
243+
.list_owned_unspents(
244+
&local_chain,
245+
local_chain
246+
.get_blockhash(height)
247+
.map(|hash| BlockId { height, hash })
248+
.unwrap(),
249+
)
235250
.collect::<Vec<_>>();
236251

237252
let balance = graph.balance(
238253
&local_chain,
239-
local_chain.get_block(ht).unwrap(),
254+
local_chain
255+
.get_blockhash(height)
256+
.map(|hash| BlockId { height, hash })
257+
.unwrap(),
240258
|spk: &Script| trusted_spks.contains(spk),
241259
);
242260

0 commit comments

Comments
 (0)