Skip to content

Commit 53791eb

Browse files
committed
Merge #1369: feat(chain): add get and range methods to CheckPoint
53942cc chore(chain)!: rm `From<LocalChain> for BTreeMap<u32, BlockHash>` (志宇) 2d1d95a feat(chain): impl `PartialEq` on `CheckPoint` (志宇) 9a62d56 feat(chain): add `get` and `range` methods to `CheckPoint` (志宇) Pull request description: Partially fixes #1354 ### Description 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. ### Notes to the reviewers Please refer to the conversation in #1354 for more details. In summary, both `TxGraph::missing_heights` and `tx_graph::ChangeSet::missing_heights_from` are problematic in their own ways. Additionally, it's a better API for chain sources if we only need to lock our receiving structures twice (once to obtain initial state and once for applying the update). Instead of relying on methods such as `EsploraExt::update_local_chain` to get relevant checkpoints, we can use these query methods instead. This allows up to get rid of `::missing_heights`-esc methods and remove the need to lock receiving structures in the middle of the sync. ### Changelog notice * Added `get` and `range` methods to `CheckPoint` (and in turn, `LocalChain`). This simulates an API where we have implemented a skip list of checkpoints (to implement in the future). This is a better API because we can query for any height or height range with just a checkpoint tip instead of relying on a separate checkpoint index (which needs to live in `LocalChain`). * Changed `LocalChain` to have a faster `Eq` implementation. We now maintain an xor value of all checkpoint block hashes. We compare this xor value to determine whether two chains are equal. * Added `PartialEq` implementation for `CheckPoint` and `local_chain::Update`. ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### New Features: * [x] I've added tests for the new feature * [x] I've added docs for the new feature ACKs for top commit: LLFourn: ACK 53942cc Tree-SHA512: 90ef8047fe1265daa54c9dfe8a8c520685c898a50d18efd6e803707fcb529d0790d20373c9e439b9c7ff301db32b47453020cae7db4da2ea64eba895aa047f30
2 parents 2bb6540 + 53942cc commit 53791eb

File tree

7 files changed

+198
-116
lines changed

7 files changed

+198
-116
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)