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

Refactor revm_spec function to simplify hardfork detection logic #74

Closed
wants to merge 2 commits into from

Conversation

cypherpepe
Copy link

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.

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.
Comment on lines 256 to 257
.hardforks
.iter()
Copy link
Member

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 {
Copy link
Member

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

Copy link
Author

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?

Copy link
Member

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

Copy link
Member

@mattsse mattsse left a 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

@cypherpepe
Copy link
Author

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.

@mattsse
Copy link
Member

mattsse commented Oct 29, 2024

appreciate it

@mattsse mattsse closed this Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants