]> Untitled Git - bdk/commitdiff
test(bitcoind_rpc): add no_agreement_point test
author志宇 <hello@evanlinjin.me>
Wed, 4 Oct 2023 10:20:10 +0000 (18:20 +0800)
committer志宇 <hello@evanlinjin.me>
Mon, 9 Oct 2023 14:14:03 +0000 (22:14 +0800)
Co-authored-by: Steve Myers <steve@notmandatory.org>
crates/bitcoind_rpc/tests/test_emitter.rs

index e6073eee7140f64bd9b817c9295272bdee40e7a3..7a4b7e4d13f9fb063b59bcb28e8d23df0a2c3be2 100644 (file)
@@ -757,3 +757,56 @@ fn mempool_during_reorg() -> anyhow::Result<()> {
 
     Ok(())
 }
+
+/// If blockchain re-org includes the start height, emit new start height block
+///
+/// 1. mine 101 blocks
+/// 2. emmit blocks 99a, 100a
+/// 3. invalidate blocks 99a, 100a, 101a
+/// 4. mine new blocks 99b, 100b, 101b
+/// 5. emmit block 99b
+///
+/// The block hash of 99b should be different than 99a, but their previous block hashes should
+/// be the same.
+#[test]
+fn no_agreement_point() -> anyhow::Result<()> {
+    const PREMINE_COUNT: usize = 101;
+
+    let env = TestEnv::new()?;
+
+    // start height is 99
+    let mut emitter = Emitter::new(&env.client, (PREMINE_COUNT - 2) as u32);
+
+    // mine 101 blocks
+    env.mine_blocks(PREMINE_COUNT, None)?;
+
+    // emit block 99a
+    let (_, block_header_99a) = emitter.next_header()?.expect("block 99a header");
+    let block_hash_99a = block_header_99a.block_hash();
+    let block_hash_98a = block_header_99a.prev_blockhash;
+
+    // emit block 100a
+    let (_, block_header_100a) = emitter.next_header()?.expect("block 100a header");
+    let block_hash_100a = block_header_100a.block_hash();
+
+    // get hash for block 101a
+    let block_hash_101a = env.client.get_block_hash(101)?;
+
+    // invalidate blocks 99a, 100a, 101a
+    env.client.invalidate_block(&block_hash_99a)?;
+    env.client.invalidate_block(&block_hash_100a)?;
+    env.client.invalidate_block(&block_hash_101a)?;
+
+    // mine new blocks 99b, 100b, 101b
+    env.mine_blocks(3, None)?;
+
+    // emit block header 99b
+    let (_, block_header_99b) = emitter.next_header()?.expect("block 99b header");
+    let block_hash_99b = block_header_99b.block_hash();
+    let block_hash_98b = block_header_99b.prev_blockhash;
+
+    assert_ne!(block_hash_99a, block_hash_99b);
+    assert_eq!(block_hash_98a, block_hash_98b);
+
+    Ok(())
+}