ethereum-forks: add unit tests for ForkCondition (#10707)

This commit is contained in:
Thomas Coratger
2024-09-05 11:33:02 -07:00
committed by GitHub
parent fdad74e50d
commit 7fd16914a4

View File

@@ -108,3 +108,179 @@ impl ForkCondition {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::U256;
#[test]
fn test_active_at_block() {
// Test if the condition is active at the current block number
let fork_condition = ForkCondition::Block(10);
assert!(fork_condition.active_at_block(10), "The condition should be active at block 10");
// Test if the condition is not active at a lower block number
assert!(
!fork_condition.active_at_block(9),
"The condition should not be active at block 9"
);
// Test if TTD-based condition with known block activates
let fork_condition =
ForkCondition::TTD { fork_block: Some(10), total_difficulty: U256::from(1000) };
assert!(
fork_condition.active_at_block(10),
"The TTD condition should be active at block 10"
);
// Test if TTD-based condition with unknown block does not activate
let fork_condition =
ForkCondition::TTD { fork_block: None, total_difficulty: U256::from(1000) };
assert!(
!fork_condition.active_at_block(10),
"The TTD condition should not be active at block 10 with an unknown block number"
);
}
#[test]
fn test_transitions_at_block() {
// Test if the condition transitions at the correct block number
let fork_condition = ForkCondition::Block(10);
assert!(
fork_condition.transitions_at_block(10),
"The condition should transition at block 10"
);
// Test if the condition does not transition at a different block number
assert!(
!fork_condition.transitions_at_block(9),
"The condition should not transition at a different block number"
);
assert!(
!fork_condition.transitions_at_block(11),
"The condition should not transition at a different block number"
);
}
#[test]
fn test_active_at_ttd() {
// Test if the condition activates at the correct total difficulty
let fork_condition =
ForkCondition::TTD { fork_block: Some(10), total_difficulty: U256::from(1000) };
assert!(
fork_condition.active_at_ttd(U256::from(1000000), U256::from(100)),
"The TTD condition should be active when the total difficulty matches"
);
// Test if the condition does not activate when the total difficulty is lower
assert!(
!fork_condition.active_at_ttd(U256::from(900), U256::from(100)),
"The TTD condition should not be active when the total difficulty is lower"
);
// Test with a saturated subtraction
assert!(
!fork_condition.active_at_ttd(U256::from(900), U256::from(1000)),
"The TTD condition should not be active when the subtraction saturates"
);
}
#[test]
fn test_active_at_timestamp() {
// Test if the condition activates at the correct timestamp
let fork_condition = ForkCondition::Timestamp(12345);
assert!(
fork_condition.active_at_timestamp(12345),
"The condition should be active at timestamp 12345"
);
// Test if the condition does not activate at an earlier timestamp
assert!(
!fork_condition.active_at_timestamp(12344),
"The condition should not be active at an earlier timestamp"
);
}
#[test]
fn test_transitions_at_timestamp() {
// Test if the condition transitions at the correct timestamp
let fork_condition = ForkCondition::Timestamp(12345);
assert!(
fork_condition.transitions_at_timestamp(12345, 12344),
"The condition should transition at timestamp 12345"
);
// Test if the condition does not transition if the parent timestamp is already the same
assert!(
!fork_condition.transitions_at_timestamp(12345, 12345),
"The condition should not transition if the parent timestamp is already 12345"
);
// Test with earlier timestamp
assert!(
!fork_condition.transitions_at_timestamp(123, 122),
"The condition should not transition if the parent timestamp is earlier"
);
}
#[test]
fn test_active_at_head() {
let head = Head {
hash: Default::default(),
number: 10,
timestamp: 12345,
total_difficulty: U256::from(1000),
difficulty: U256::from(100),
};
// Test if the condition activates based on block number
let fork_condition = ForkCondition::Block(10);
assert!(
fork_condition.active_at_head(&head),
"The condition should be active at the given head block number"
);
let fork_condition = ForkCondition::Block(11);
assert!(
!fork_condition.active_at_head(&head),
"The condition should not be active at the given head block number"
);
// Test if the condition activates based on timestamp
let fork_condition = ForkCondition::Timestamp(12345);
assert!(
fork_condition.active_at_head(&head),
"The condition should be active at the given head timestamp"
);
let fork_condition = ForkCondition::Timestamp(12346);
assert!(
!fork_condition.active_at_head(&head),
"The condition should not be active at the given head timestamp"
);
// Test if the condition activates based on total difficulty and block number
let fork_condition =
ForkCondition::TTD { fork_block: Some(9), total_difficulty: U256::from(900) };
assert!(
fork_condition.active_at_head(&head),
"The condition should be active at the given head total difficulty"
);
let fork_condition =
ForkCondition::TTD { fork_block: None, total_difficulty: U256::from(900) };
assert!(
fork_condition.active_at_head(&head),
"The condition should be active at the given head total difficulty as the block number is unknown"
);
let fork_condition =
ForkCondition::TTD { fork_block: Some(11), total_difficulty: U256::from(900) };
assert!(
fork_condition.active_at_head(&head),
"The condition should be active as the total difficulty is higher"
);
let fork_condition =
ForkCondition::TTD { fork_block: Some(10), total_difficulty: U256::from(9000) };
assert!(
fork_condition.active_at_head(&head),
"The condition should be active as the total difficulty is higher than head"
);
}
}