Skip to content

Commit

Permalink
feat(chain): add get and range methods to CheckPoint
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
evanlinjin committed Apr 5, 2024
1 parent 2bb6540 commit 9a62d56
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 104 deletions.
10 changes: 3 additions & 7 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,18 +1127,14 @@ impl<D> Wallet<D> {
// anchor tx to checkpoint with lowest height that is >= position's height
let anchor = self
.chain
.blocks()
.range(height..)
.next()
.last()
.ok_or(InsertTxError::ConfirmationHeightCannotBeGreaterThanTip {
tip_height: self.chain.tip().height(),
tx_height: height,
})
.map(|(&anchor_height, &hash)| ConfirmationTimeHeightAnchor {
anchor_block: BlockId {
height: anchor_height,
hash,
},
.map(|anchor_cp| ConfirmationTimeHeightAnchor {
anchor_block: anchor_cp.block_id(),
confirmation_height: height,
confirmation_time: time,
})?;
Expand Down
18 changes: 12 additions & 6 deletions crates/bitcoind_rpc/tests/test_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ pub fn test_sync_local_chain() -> anyhow::Result<()> {
}

assert_eq!(
local_chain.blocks(),
&exp_hashes
local_chain
.iter_checkpoints()
.map(|cp| (cp.height(), cp.hash()))
.collect::<BTreeSet<_>>(),
exp_hashes
.iter()
.enumerate()
.map(|(i, hash)| (i as u32, *hash))
.collect(),
.collect::<BTreeSet<_>>(),
"final local_chain state is unexpected",
);

Expand Down Expand Up @@ -110,12 +113,15 @@ pub fn test_sync_local_chain() -> anyhow::Result<()> {
}

assert_eq!(
local_chain.blocks(),
&exp_hashes
local_chain
.iter_checkpoints()
.map(|cp| (cp.height(), cp.hash()))
.collect::<BTreeSet<_>>(),
exp_hashes
.iter()
.enumerate()
.map(|(i, hash)| (i as u32, *hash))
.collect(),
.collect::<BTreeSet<_>>(),
"final local_chain state is unexpected after reorg",
);

Expand Down
Loading

0 comments on commit 9a62d56

Please sign in to comment.