Skip to content

Commit

Permalink
Add target_doubling test
Browse files Browse the repository at this point in the history
  • Loading branch information
raychu86 committed Nov 30, 2022
1 parent ca03190 commit 461d9d1
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions node/consensus/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,46 @@ mod tests {
assert!(new_coinbase_target < previous_coinbase_target / 2);
}
}

#[test]
fn test_target_doubling() {
let mut rng = TestRng::default();

let minimum_coinbase_target: u64 = 2u64.pow(10) - 1;

let initial_coinbase_target: u64 = rng.gen_range(minimum_coinbase_target..u64::MAX / 2);
let initial_timestamp: i64 = rng.gen();
let mut previous_coinbase_target: u64 = initial_coinbase_target;
let mut previous_timestamp = initial_timestamp;
let mut num_blocks = 0;

// The custom block time that is faster than the anchor time.
let fast_block_time = 15;

while previous_coinbase_target < initial_coinbase_target * 2 {
// Targets increase (harder) when the timestamp is less than expected.
let new_timestamp = previous_timestamp + fast_block_time as i64;
let new_coinbase_target = coinbase_target::<true>(
previous_coinbase_target,
previous_timestamp,
new_timestamp,
CurrentNetwork::ANCHOR_TIME,
CurrentNetwork::NUM_BLOCKS_PER_EPOCH,
)
.unwrap();

assert!(new_coinbase_target > previous_coinbase_target);

previous_coinbase_target = new_coinbase_target;
previous_timestamp = new_timestamp;
num_blocks += 1;
}

println!(
"For block times of {}s and anchor time of {}s, doubling the coinbase target took {num_blocks} blocks. ({} seconds)",
fast_block_time,
CurrentNetwork::ANCHOR_TIME,
previous_timestamp - initial_timestamp
);
}
}

0 comments on commit 461d9d1

Please sign in to comment.