From b24d8372698257cbdc3edb03bc6073ae0fc65385 Mon Sep 17 00:00:00 2001 From: malik Date: Mon, 14 Oct 2024 09:24:17 +0100 Subject: [PATCH] optimized --- crates/node/benches/revm_spec.rs | 23 ++++++++--------------- crates/node/src/evm.rs | 24 +++++++++--------------- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/crates/node/benches/revm_spec.rs b/crates/node/benches/revm_spec.rs index 3f98c2d..4808aaf 100644 --- a/crates/node/benches/revm_spec.rs +++ b/crates/node/benches/revm_spec.rs @@ -5,7 +5,7 @@ use alloy_primitives:: U256; use reth_chainspec::{ ChainSpec, EthereumHardfork, ForkCondition, Head}; use reth_optimism_forks::OptimismHardfork; use reth_revm::primitives::SpecId; -use std::{cmp::Ordering, time::Duration}; +use std::time::Duration; fn revm_spec_one(chain_spec: &ChainSpec, block: &Head) -> SpecId { @@ -87,26 +87,19 @@ fn revm_spec_two(chain_spec: &ChainSpec, block: &Head) -> SpecId { (Hardfork::Ethereum(EthereumHardfork::Frontier), SpecId::FRONTIER), ]; + let hardforks = HARDFORKS.iter().rev(); - let mut left = 0; - let mut right = HARDFORKS.len() - 1; - - while left <= right { - let mid = left + (right - left) / 2; - let (ref fork, spec_id) = HARDFORKS[mid]; - + for (fork, spec_id) in hardforks { let is_active = match fork { - Hardfork::Ethereum(f) => chain_spec.fork(*f).active_at_head(block), - Hardfork::Optimism(f) => chain_spec.fork(*f).active_at_head(block), + Hardfork::Ethereum(f) => chain_spec.is_fork_active_at_block(f, block.number), + Hardfork::Optimism(f) => chain_spec.is_fork_active_at_block(f, block.number), }; - match is_active.cmp(&true) { - Ordering::Equal => return spec_id, - Ordering::Greater => right = mid - 1, - Ordering::Less => left = mid + 1, + if is_active { + return *spec_id; } } - + panic!( "invalid hardfork chainspec: expected at least one hardfork, got {:?}", chain_spec.hardforks diff --git a/crates/node/src/evm.rs b/crates/node/src/evm.rs index 2369628..5e37f8d 100644 --- a/crates/node/src/evm.rs +++ b/crates/node/src/evm.rs @@ -31,7 +31,7 @@ use reth_revm::{ }, ContextPrecompiles, Database, Evm, EvmBuilder, GetInspector, }; -use std::{cmp::Ordering, sync::Arc}; +use std:: sync::Arc; /// Custom EVM configuration #[derive(Debug, Clone)] @@ -266,7 +266,7 @@ fn revm_spec(chain_spec: &ChainSpec, block: &Head) -> SpecId { Optimism(OptimismHardfork), } - const HARDFORKS: &[(Hardfork, SpecId)] = &[ + static HARDFORKS: [(Hardfork, SpecId); 20] = [ (Hardfork::Ethereum(EthereumHardfork::Prague), SpecId::PRAGUE_EOF), (Hardfork::Optimism(OptimismHardfork::Granite), SpecId::GRANITE), (Hardfork::Optimism(OptimismHardfork::Fjord), SpecId::FJORD), @@ -289,25 +289,19 @@ fn revm_spec(chain_spec: &ChainSpec, block: &Head) -> SpecId { (Hardfork::Ethereum(EthereumHardfork::Frontier), SpecId::FRONTIER), ]; - let mut left = 0; - let mut right = HARDFORKS.len() - 1; - - while left <= right { - let mid = left + (right - left) / 2; - let (ref fork, spec_id) = HARDFORKS[mid]; + let hardforks = HARDFORKS.iter().rev(); + for (fork, spec_id) in hardforks { let is_active = match fork { - Hardfork::Ethereum(f) => chain_spec.fork(*f).active_at_head(block), - Hardfork::Optimism(f) => chain_spec.fork(*f).active_at_head(block), + Hardfork::Ethereum(f) => chain_spec.is_fork_active_at_block(f, block.number), + Hardfork::Optimism(f) => chain_spec.is_fork_active_at_block(f, block.number), }; - match is_active.cmp(&true) { - Ordering::Equal => return spec_id, - Ordering::Greater => right = mid - 1, - Ordering::Less => left = mid + 1, + if is_active { + return *spec_id; } } - + panic!( "invalid hardfork chainspec: expected at least one hardfork, got {:?}", chain_spec.hardforks