Add testing for finding terminal block

This commit is contained in:
Paul Hauner
2021-09-28 17:14:36 +10:00
parent 20106bb3fd
commit c0692447ed
2 changed files with 41 additions and 4 deletions

View File

@@ -307,7 +307,7 @@ impl ExecutionLayer {
.await?
.ok_or(ApiError::ExecutionHeadBlockNotFound)?;
self.execution_blocks().await.put(block.parent_hash, block);
self.execution_blocks().await.put(block.block_hash, block);
loop {
if block.total_difficulty >= self.terminal_total_difficulty() {
@@ -447,11 +447,20 @@ mod test {
}
}
pub async fn move_to_block_prior_to_terminal_block(self) -> Self {
{
let mut block_gen = self.server.execution_block_generator().await;
let target_block = block_gen.terminal_block_number.checked_sub(1).unwrap();
block_gen.set_clock_for_block_number(target_block)
}
self
}
pub async fn move_to_terminal_block(self) -> Self {
{
let mut block_gen = self.server.execution_block_generator().await;
block_gen.seconds_since_genesis =
block_gen.terminal_block_number * block_gen.block_interval_secs;
let target_block = block_gen.terminal_block_number;
block_gen.set_clock_for_block_number(target_block)
}
self
}
@@ -485,6 +494,29 @@ mod test {
#[tokio::test]
async fn finds_valid_terminal_block_hash() {
SingleEngineTester::new()
.move_to_block_prior_to_terminal_block()
.await
.with_terminal_block_number(|el, _| async move {
assert_eq!(
el.get_pow_block_hash_at_total_difficulty().await.unwrap(),
None
)
})
.await
.move_to_terminal_block()
.await
.with_terminal_block_number(|el, terminal_block_number| async move {
assert_eq!(
el.get_pow_block_hash_at_total_difficulty().await.unwrap(),
Some(block_number_to_hash(terminal_block_number))
)
})
.await;
}
#[tokio::test]
async fn verifies_valid_terminal_block_hash() {
SingleEngineTester::new()
.move_to_terminal_block()
.await

View File

@@ -20,6 +20,12 @@ impl ExecutionBlockGenerator {
}
}
pub fn set_clock_for_block_number(&mut self, number: u64) {
self.seconds_since_genesis = number
.checked_mul(self.block_interval_secs)
.expect("overflow setting clock");
}
pub fn increment_seconds_since_genesis(&mut self, inc: u64) {
self.seconds_since_genesis += inc;
}
@@ -159,7 +165,6 @@ mod test {
*/
let next_i = i + 1;
dbg!(next_i);
assert!(generator.block_by_number(next_i).is_none());
assert!(generator
.block_by_hash(block_number_to_hash(next_i))