Skip to content

Commit

Permalink
test(bitcoind_rpc): add no_agreement_point test
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Oct 3, 2023
1 parent 409033a commit e728c53
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions crates/bitcoind_rpc/tests/test_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,3 +662,58 @@ 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
let addr = env.client.get_new_address(None, None)?.assume_checked();
env.mine_blocks(PREMINE_COUNT, Some(addr.clone()))?;

Check failure on line 687 in crates/bitcoind_rpc/tests/test_emitter.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant clone

error: redundant clone --> crates/bitcoind_rpc/tests/test_emitter.rs:687:45 | 687 | env.mine_blocks(PREMINE_COUNT, Some(addr.clone()))?; | ^^^^^^^^ help: remove this | note: this value is dropped without further use --> crates/bitcoind_rpc/tests/test_emitter.rs:687:41 | 687 | env.mine_blocks(PREMINE_COUNT, Some(addr.clone()))?; | ^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone = note: `-D clippy::redundant-clone` implied by `-D warnings`

// emit block 99a
let block_header_99a = emitter.next_header()?.expect("block 99a header");
let block_hash_99a = block_header_99a.header.block_hash();
let block_hash_98a = block_header_99a.header.prev_blockhash;

// emit block 100a
let block_header_100a = emitter.next_header()?.expect("block 100a header");
let block_hash_100a = block_header_100a.header.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
let addr = env.client.get_new_address(None, None)?.assume_checked();
env.mine_blocks(3, Some(addr.clone()))?;

Check failure on line 708 in crates/bitcoind_rpc/tests/test_emitter.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant clone

error: redundant clone --> crates/bitcoind_rpc/tests/test_emitter.rs:708:33 | 708 | env.mine_blocks(3, Some(addr.clone()))?; | ^^^^^^^^ help: remove this | note: this value is dropped without further use --> crates/bitcoind_rpc/tests/test_emitter.rs:708:29 | 708 | env.mine_blocks(3, Some(addr.clone()))?; | ^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

// emit block header 99b
let block_header_99b = emitter.next_header()?.expect("block 99b header");
let block_hash_99b = block_header_99b.header.block_hash();
let block_hash_98b = block_header_99b.header.prev_blockhash;

assert_ne!(block_hash_99a, block_hash_99b);
assert_eq!(block_hash_98a, block_hash_98b);

Ok(())
}

0 comments on commit e728c53

Please sign in to comment.