Skip to content

Commit 9a62d56

Browse files
committed
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.
1 parent 2bb6540 commit 9a62d56

File tree

7 files changed

+202
-104
lines changed

7 files changed

+202
-104
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,18 +1127,14 @@ impl<D> Wallet<D> {
11271127
// anchor tx to checkpoint with lowest height that is >= position's height
11281128
let anchor = self
11291129
.chain
1130-
.blocks()
11311130
.range(height..)
1132-
.next()
1131+
.last()
11331132
.ok_or(InsertTxError::ConfirmationHeightCannotBeGreaterThanTip {
11341133
tip_height: self.chain.tip().height(),
11351134
tx_height: height,
11361135
})
1137-
.map(|(&anchor_height, &hash)| ConfirmationTimeHeightAnchor {
1138-
anchor_block: BlockId {
1139-
height: anchor_height,
1140-
hash,
1141-
},
1136+
.map(|anchor_cp| ConfirmationTimeHeightAnchor {
1137+
anchor_block: anchor_cp.block_id(),
11421138
confirmation_height: height,
11431139
confirmation_time: time,
11441140
})?;

crates/bitcoind_rpc/tests/test_emitter.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ pub fn test_sync_local_chain() -> anyhow::Result<()> {
5757
}
5858

5959
assert_eq!(
60-
local_chain.blocks(),
61-
&exp_hashes
60+
local_chain
61+
.iter_checkpoints()
62+
.map(|cp| (cp.height(), cp.hash()))
63+
.collect::<BTreeSet<_>>(),
64+
exp_hashes
6265
.iter()
6366
.enumerate()
6467
.map(|(i, hash)| (i as u32, *hash))
65-
.collect(),
68+
.collect::<BTreeSet<_>>(),
6669
"final local_chain state is unexpected",
6770
);
6871

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

112115
assert_eq!(
113-
local_chain.blocks(),
114-
&exp_hashes
116+
local_chain
117+
.iter_checkpoints()
118+
.map(|cp| (cp.height(), cp.hash()))
119+
.collect::<BTreeSet<_>>(),
120+
exp_hashes
115121
.iter()
116122
.enumerate()
117123
.map(|(i, hash)| (i as u32, *hash))
118-
.collect(),
124+
.collect::<BTreeSet<_>>(),
119125
"final local_chain state is unexpected after reorg",
120126
);
121127

0 commit comments

Comments
 (0)