Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ethereum-forks: add unit tests for ForkCondition #10707

Merged
merged 4 commits into from
Sep 5, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 191 additions & 3 deletions crates/ethereum-forks/src/forkcondition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,17 @@
/// - The condition is satisfied by the timestamp;
/// - or the condition is satisfied by the total difficulty
pub fn active_at_head(&self, head: &Head) -> bool {
self.active_at_block(head.number) ||
self.active_at_timestamp(head.timestamp) ||
self.active_at_ttd(head.total_difficulty, head.difficulty)
match self {
Self::TTD { fork_block: Some(_), .. } => {
self.active_at_block(head.number) &&
self.active_at_ttd(head.total_difficulty, head.difficulty)
tcoratger marked this conversation as resolved.
Show resolved Hide resolved
}
_ => {
self.active_at_block(head.number) ||
self.active_at_timestamp(head.timestamp) ||
self.active_at_ttd(head.total_difficulty, head.difficulty)
}
}
tcoratger marked this conversation as resolved.
Show resolved Hide resolved
}

/// Get the total terminal difficulty for this fork condition.
Expand All @@ -108,3 +116,183 @@
}
}
}

#[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 there is a false relation betwen parent and

Check failure on line 222 in crates/ethereum-forks/src/forkcondition.rs

View workflow job for this annotation

GitHub Actions / codespell

betwen ==> between
// child
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 not be active as the block is higher than head"
);
tcoratger marked this conversation as resolved.
Show resolved Hide resolved

// Even if the block number is correct compare to head, the condition should not activate if
// the total difficulty is too high
let fork_condition =
ForkCondition::TTD { fork_block: Some(10), total_difficulty: U256::from(9000) };
assert!(
!fork_condition.active_at_head(&head),
"The condition should not be active as the total difficulty is higher than head"
);
}
tcoratger marked this conversation as resolved.
Show resolved Hide resolved
}
Loading