]> Untitled Git - bdk/commitdiff
Merge bitcoindevkit/bdk#1601: fix(core): calling `CheckPoint::insert` with existing...
authorSteve Myers <steve@notmandatory.org>
Thu, 12 Sep 2024 21:11:37 +0000 (16:11 -0500)
committerSteve Myers <steve@notmandatory.org>
Thu, 12 Sep 2024 21:12:33 +0000 (16:12 -0500)
3ae9ecba8c893750fa2ed5dfdbb1f4ee84a0b228 test: fix off-by-one in `checkpoint_insert_existing` (valued mammal)
e6aeaea0c69472e4c2dcf0f5e2f632f43733529d doc(core): document panic for `CheckPoint::insert` (valued mammal)
2970b83f30ca7071f0502de395327eb3671a512b fix(core): calling `CheckPoint::insert` with existing block must succeed (志宇)

Pull request description:

  ### Description

  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.

  ### Notes to the reviewers

  For context:

  * https://github.com/lightningdevkit/ldk-node/pull/358
  * https://discord.com/channels/753336465005608961/978744259693916230/1283050849429356629

  ### Changelog notice

  * Fix `CheckPoint::insert` to not panic when inserting existing genesis block (same block height and hash).

  ### 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

  #### Bugfixes:

  ~~* [ ] This pull request breaks the existing API~~
  * [x] I've added tests to reproduce the issue which are now passing

ACKs for top commit:
  ValuedMammal:
    Self ACK 3ae9ecba8c893750fa2ed5dfdbb1f4ee84a0b228
  notmandatory:
    ACK 3ae9ecba8c893750fa2ed5dfdbb1f4ee84a0b228

Tree-SHA512: 638d8aacac59ad18b5ee369d08f39bb12cc37fa9b3f936404b60dbf44938ef850ca341d00566840b5a77909d31c9291ab56299d986d832005ca96cd91a0b0e55

1  2 
crates/core/src/checkpoint.rs

index f78aca9c5456b29df8d10d2eab982fb7d19a2e0b,10af13277135c7ab51dd7468e82ccf7bc6681292..23d5731f9bb89ddbdf563d94f994d16e98b1ce28
@@@ -169,12 -169,14 +169,14 @@@ impl CheckPoint 
      /// The effect of `insert` depends on whether a height already exists. If it doesn't the
      /// `block_id` we inserted and all pre-existing blocks higher than it will be re-inserted after
      /// it. If the height already existed and has a conflicting block hash then it will be purged
 -    /// along with all block followin it. The returned chain will have a tip of the `block_id`
 +    /// along with all block following it. The returned chain will have a tip of the `block_id`
      /// passed in. Of course, if the `block_id` was already present then this just returns `self`.
+     ///
+     /// # Panics
+     ///
+     /// This panics if called with a genesis block that differs from that of `self`.
      #[must_use]
      pub fn insert(self, block_id: BlockId) -> Self {
-         assert_ne!(block_id.height, 0, "cannot insert the genesis block");
          let mut cp = self.clone();
          let mut tail = vec![];
          let base = loop {