-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): calling
CheckPoint::insert
with existing block must succeed
Previously, we were panicking when the caller tried to insert a block at height 0. However, this should succeed if the block hash does not change.
- Loading branch information
1 parent
8760653
commit 2970b83
Showing
3 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#[allow(unused_macros)] | ||
macro_rules! block_id { | ||
($height:expr, $hash:literal) => {{ | ||
bdk_chain::BlockId { | ||
height: $height, | ||
hash: bitcoin::hashes::Hash::hash($hash.as_bytes()), | ||
} | ||
}}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#[macro_use] | ||
mod common; | ||
|
||
use bdk_core::CheckPoint; | ||
|
||
/// Inserting a block that already exists in the checkpoint chain must always succeed. | ||
#[test] | ||
fn checkpoint_insert_existing() { | ||
let blocks = &[ | ||
block_id!(0, "genesis"), | ||
block_id!(1, "A"), | ||
block_id!(2, "B"), | ||
block_id!(3, "C"), | ||
]; | ||
|
||
// Index `i` allows us to test with chains of different length. | ||
// Index `j` allows us to test inserting different block heights. | ||
for i in 0..blocks.len() { | ||
let cp_chain = CheckPoint::from_block_ids(blocks[..=i].iter().copied()) | ||
.expect("must construct valid chain"); | ||
|
||
for j in 0..i { | ||
let block_to_insert = cp_chain | ||
.get(j as u32) | ||
.expect("cp of height must exist") | ||
.block_id(); | ||
let new_cp_chain = cp_chain.clone().insert(block_to_insert); | ||
|
||
assert_eq!( | ||
new_cp_chain, cp_chain, | ||
"must not divert from original chain" | ||
); | ||
assert!(new_cp_chain.eq_ptr(&cp_chain), "pointers must still match"); | ||
} | ||
} | ||
} |