feat(chain): add get and range methods to CheckPoint
These methods allow us to query for checkpoints contained within the linked list by height and height range. This is useful to determine checkpoints to fetch for chain sources without having to refer back to the `LocalChain`. Currently this is not implemented efficiently, but in the future, we will change `CheckPoint` to use a skip list structure.
This commit is contained in:
@@ -7,7 +7,7 @@ use bdk_chain::{
|
||||
indexed_tx_graph::{self, IndexedTxGraph},
|
||||
keychain::{self, Balance, KeychainTxOutIndex},
|
||||
local_chain::LocalChain,
|
||||
tx_graph, BlockId, ChainPosition, ConfirmationHeightAnchor,
|
||||
tx_graph, ChainPosition, ConfirmationHeightAnchor,
|
||||
};
|
||||
use bitcoin::{secp256k1::Secp256k1, OutPoint, Script, ScriptBuf, Transaction, TxIn, TxOut};
|
||||
use miniscript::Descriptor;
|
||||
@@ -212,10 +212,8 @@ fn test_list_owned_txouts() {
|
||||
(
|
||||
*tx,
|
||||
local_chain
|
||||
.blocks()
|
||||
.get(&height)
|
||||
.cloned()
|
||||
.map(|hash| BlockId { height, hash })
|
||||
.get(height)
|
||||
.map(|cp| cp.block_id())
|
||||
.map(|anchor_block| ConfirmationHeightAnchor {
|
||||
anchor_block,
|
||||
confirmation_height: anchor_block.height,
|
||||
@@ -230,9 +228,8 @@ fn test_list_owned_txouts() {
|
||||
|height: u32,
|
||||
graph: &IndexedTxGraph<ConfirmationHeightAnchor, KeychainTxOutIndex<String>>| {
|
||||
let chain_tip = local_chain
|
||||
.blocks()
|
||||
.get(&height)
|
||||
.map(|&hash| BlockId { height, hash })
|
||||
.get(height)
|
||||
.map(|cp| cp.block_id())
|
||||
.unwrap_or_else(|| panic!("block must exist at {}", height));
|
||||
let txouts = graph
|
||||
.graph()
|
||||
|
||||
@@ -528,6 +528,52 @@ fn checkpoint_from_block_ids() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkpoint_query() {
|
||||
struct TestCase {
|
||||
chain: LocalChain,
|
||||
/// The heights we want to call [`CheckPoint::query`] with, represented as an inclusive
|
||||
/// range.
|
||||
///
|
||||
/// If a [`CheckPoint`] exists at that height, we expect [`CheckPoint::query`] to return
|
||||
/// it. If not, [`CheckPoint::query`] should return `None`.
|
||||
query_range: (u32, u32),
|
||||
}
|
||||
|
||||
let test_cases = [
|
||||
TestCase {
|
||||
chain: local_chain![(0, h!("_")), (1, h!("A"))],
|
||||
query_range: (0, 2),
|
||||
},
|
||||
TestCase {
|
||||
chain: local_chain![(0, h!("_")), (2, h!("B")), (3, h!("C"))],
|
||||
query_range: (0, 3),
|
||||
},
|
||||
];
|
||||
|
||||
for t in test_cases.into_iter() {
|
||||
let tip = t.chain.tip();
|
||||
for h in t.query_range.0..=t.query_range.1 {
|
||||
let query_result = tip.get(h);
|
||||
|
||||
// perform an exhausitive search for the checkpoint at height `h`
|
||||
let exp_hash = t
|
||||
.chain
|
||||
.iter_checkpoints()
|
||||
.find(|cp| cp.height() == h)
|
||||
.map(|cp| cp.hash());
|
||||
|
||||
match query_result {
|
||||
Some(cp) => {
|
||||
assert_eq!(Some(cp.hash()), exp_hash);
|
||||
assert_eq!(cp.height(), h);
|
||||
}
|
||||
None => assert!(exp_hash.is_none()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_chain_apply_header_connected_to() {
|
||||
fn header_from_prev_blockhash(prev_blockhash: BlockHash) -> Header {
|
||||
|
||||
Reference in New Issue
Block a user