-
Notifications
You must be signed in to change notification settings - Fork 50
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
Refactor revm_spec function to simplify hardfork detection logic #74
Conversation
Replaced individual hardfork checks in revm_spec with a loop-based approach using find. Moved hardfork-to-SpecId mapping logic to a separate helper function (map_hardfork_to_spec_id) for cleaner and more maintainable code.
crates/node/src/evm.rs
Outdated
.hardforks | ||
.iter() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use forks_iter()
Replaced .iter() with .forks_iter() in revm_spec as recommended to improve clarity when iterating through hardfork conditions.
|
||
|
||
/// Map hardfork to the corresponding `SpecId`. | ||
fn map_hardfork_to_spec_id(fork: &EthereumHardfork) -> reth_revm::primitives::SpecId { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some of these are OptimismHardfork
, which means we'd need dynamic dispatch. not really sure this is the way to go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the issue with handling both EthereumHardfork and OptimismHardfork, would you recommend simplifying revm_spec by removing map_hardfork_to_spec_id altogether and directly matching on each fork type? Here’s what that would look like:
fn revm_spec(chain_spec: &ChainSpec, block: &Head) -> SpecId {
chain_spec
.forks_iter()
.find(|(_, condition)| condition.active_at_head(block))
.map_or_else(
|| {
panic!(
"Invalid chainspec: expected at least one active hardfork, got {:?}",
chain_spec.hardforks
);
},
|(fork, _)| {
if let Some(eth_fork) = fork.downcast_ref::<EthereumHardfork>() {
match eth_fork {
EthereumHardfork::Prague => SpecId::OSAKA,
// (other EthereumHardfork mappings)
}
} else if let Some(opt_fork) = fork.downcast_ref::<OptimismHardfork>() {
match opt_fork {
OptimismHardfork::Granite => SpecId::GRANITE,
// (other OptimismHardfork mappings)
}
} else {
panic!("Unsupported hardfork: {:?}", fork);
}
}
)
}
This approach would avoid dynamic dispatch and handle both fork types without additional casting. Do you think this is a better solution?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess, although I think it would be more readable with 2 mapping functions. I question if this is indeed more readable at that point though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find the if else chain easier to read/maintain
Thank you for the feedback. I'll keep your suggestions in mind and continue looking for ways to support the team. Let's close this thread, as these changes are not the right fit. |
appreciate it |
Replaced individual hardfork checks in revm_spec with a loop-based approach using find. Moved hardfork-to-SpecId mapping logic to a separate helper function (map_hardfork_to_spec_id) for cleaner and more maintainable code.