From d19423b3185f751be411dc1fecbcef6311a7aeb0 Mon Sep 17 00:00:00 2001 From: mandreyel Date: Thu, 27 Apr 2023 14:07:56 +0400 Subject: [PATCH] feat: replace DIFFICULTY with PREVRANDAO since merge (EIP-4399) --- benches/loop.rs | 1 + core/src/error.rs | 7 ++++--- runtime/src/eval/mod.rs | 2 +- runtime/src/eval/system.rs | 19 +++++++++++++++++++ runtime/src/handler.rs | 2 ++ runtime/src/lib.rs | 19 +++++++++++++++++++ src/backend/memory.rs | 8 ++++++++ src/backend/mod.rs | 2 ++ src/executor/stack/executor.rs | 5 ++++- src/executor/stack/memory.rs | 3 +++ 10 files changed, 63 insertions(+), 5 deletions(-) diff --git a/benches/loop.rs b/benches/loop.rs index 447c8a792..b9ca7ae78 100644 --- a/benches/loop.rs +++ b/benches/loop.rs @@ -19,6 +19,7 @@ fn run_loop_contract() { block_gas_limit: Default::default(), chain_id: U256::one(), block_base_fee_per_gas: U256::zero(), + block_randomness: None, }; let mut state = BTreeMap::new(); diff --git a/core/src/error.rs b/core/src/error.rs index 510d6df4a..b5d2473a1 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -127,9 +127,6 @@ pub enum ExitError { /// Create init code exceeds limit (runtime). #[cfg_attr(feature = "with-codec", codec(index = 7))] CreateContractLimit, - /// Invalid opcode during execution or starting byte is 0xef. See [EIP-3541](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3541.md). - #[cfg_attr(feature = "with-codec", codec(index = 15))] - InvalidCode(Opcode), /// An opcode accesses external information, but the request is off offset /// limit (runtime). @@ -154,6 +151,10 @@ pub enum ExitError { /// Other normal errors. #[cfg_attr(feature = "with-codec", codec(index = 13))] Other(Cow<'static, str>), + + /// Invalid opcode during execution or starting byte is 0xef. See [EIP-3541](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3541.md). + #[cfg_attr(feature = "with-codec", codec(index = 15))] + InvalidCode(Opcode), } impl From for ExitReason { diff --git a/runtime/src/eval/mod.rs b/runtime/src/eval/mod.rs index c65370fca..a100d4cf9 100644 --- a/runtime/src/eval/mod.rs +++ b/runtime/src/eval/mod.rs @@ -40,7 +40,7 @@ pub fn eval(state: &mut Runtime, opcode: Opcode, handler: &mut H) -> Opcode::COINBASE => system::coinbase(state, handler), Opcode::TIMESTAMP => system::timestamp(state, handler), Opcode::NUMBER => system::number(state, handler), - Opcode::DIFFICULTY => system::difficulty(state, handler), + Opcode::DIFFICULTY => system::prevrandao_or_difficulty(state, handler), Opcode::GASLIMIT => system::gaslimit(state, handler), Opcode::SLOAD => system::sload(state, handler), Opcode::SSTORE => system::sstore(state, handler), diff --git a/runtime/src/eval/system.rs b/runtime/src/eval/system.rs index 574355437..2cbb2d3e3 100644 --- a/runtime/src/eval/system.rs +++ b/runtime/src/eval/system.rs @@ -185,6 +185,25 @@ pub fn difficulty(runtime: &mut Runtime, handler: &H) -> Control Control::Continue } +pub fn prevrandao_or_difficulty(runtime: &mut Runtime, handler: &H) -> Control { + match handler.block_randomness() { + Some(rand) => { + // Convert between U256 and H256. U256 is in little-endian but since H256 is just + // a string-like byte array, it's big endian (MSB is the first element of the array). + // + // Byte order here is important because this opcode has the same value as DIFFICULTY + // (0x44), and so for older forks of Ethereum, the threshold value of 2^64 is used to + // distinguish between the two: if it's below, the value corresponds to the DIFFICULTY + // opcode, otherwise to the PREVRANDAO opcode. + let mut buf = [0u8; 32]; + rand.to_big_endian(&mut buf); + push!(runtime, H256(buf)); + Control::Continue + } + None => difficulty(runtime, handler), + } +} + pub fn gaslimit(runtime: &mut Runtime, handler: &H) -> Control { push_u256!(runtime, handler.block_gas_limit()); Control::Continue diff --git a/runtime/src/handler.rs b/runtime/src/handler.rs index e7e25182d..5c97bdd32 100644 --- a/runtime/src/handler.rs +++ b/runtime/src/handler.rs @@ -54,6 +54,8 @@ pub trait Handler { fn block_timestamp(&self) -> U256; /// Get environmental block difficulty. fn block_difficulty(&self) -> U256; + /// Get environmental block randomness. + fn block_randomness(&self) -> Option; /// Get environmental gas limit. fn block_gas_limit(&self) -> U256; /// Environmental block base fee. diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index a5d3a54b5..8809e58f3 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -409,6 +409,11 @@ impl Config { Self::config_with_derived_values(DerivedConfigInputs::london()) } + /// The Merge (Paris) hard fork configuration. + pub const fn merge() -> Config { + Self::config_with_derived_values(DerivedConfigInputs::merge()) + } + /// Shanghai hard fork configuration. pub const fn shanghai() -> Config { Self::config_with_derived_values(DerivedConfigInputs::shanghai()) @@ -535,6 +540,20 @@ impl DerivedConfigInputs { } } + const fn merge() -> Self { + Self { + gas_storage_read_warm: 100, + gas_sload_cold: 2100, + gas_access_list_storage_key: 1900, + decrease_clears_refund: true, + has_base_fee: true, + has_push0: false, + disallow_executable_format: true, + warm_coinbase_address: false, + max_initcode_size: None, + } + } + const fn shanghai() -> Self { Self { gas_storage_read_warm: 100, diff --git a/src/backend/memory.rs b/src/backend/memory.rs index 7382a5e22..93f001add 100644 --- a/src/backend/memory.rs +++ b/src/backend/memory.rs @@ -31,6 +31,11 @@ pub struct MemoryVicinity { pub block_gas_limit: U256, /// Environmental base fee per gas. pub block_base_fee_per_gas: U256, + /// Environmental randomness. + /// + /// In Ethereum, this is the randomness beacon provided by the beacon + /// chain and is only enabled post Merge. + pub block_randomness: Option, } /// Account information of a memory backend. @@ -110,6 +115,9 @@ impl<'vicinity> Backend for MemoryBackend<'vicinity> { fn block_difficulty(&self) -> U256 { self.vicinity.block_difficulty } + fn block_randomness(&self) -> Option { + self.vicinity.block_randomness + } fn block_gas_limit(&self) -> U256 { self.vicinity.block_gas_limit } diff --git a/src/backend/mod.rs b/src/backend/mod.rs index 1a503cbcc..200e71f00 100644 --- a/src/backend/mod.rs +++ b/src/backend/mod.rs @@ -66,6 +66,8 @@ pub trait Backend { fn block_timestamp(&self) -> U256; /// Environmental block difficulty. fn block_difficulty(&self) -> U256; + /// Get environmental block randomness. + fn block_randomness(&self) -> Option; /// Environmental block gas limit. fn block_gas_limit(&self) -> U256; /// Environmental block base fee. diff --git a/src/executor/stack/executor.rs b/src/executor/stack/executor.rs index b50e68af2..353d6dbee 100644 --- a/src/executor/stack/executor.rs +++ b/src/executor/stack/executor.rs @@ -304,7 +304,7 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> /// Execute using Runtimes on the call_stack until it returns. fn execute_with_call_stack( &mut self, - call_stack: &mut Vec, + call_stack: &mut Vec>, ) -> (ExitReason, Option, Vec) { // This `interrupt_runtime` is used to pass the runtime obtained from the // `Capture::Trap` branch in the match below back to the top of the call stack. @@ -1108,6 +1108,9 @@ impl<'config, 'precompiles, S: StackState<'config>, P: PrecompileSet> Handler fn block_difficulty(&self) -> U256 { self.state.block_difficulty() } + fn block_randomness(&self) -> Option { + self.state.block_randomness() + } fn block_gas_limit(&self) -> U256 { self.state.block_gas_limit() } diff --git a/src/executor/stack/memory.rs b/src/executor/stack/memory.rs index ca04885c1..5803b52bd 100644 --- a/src/executor/stack/memory.rs +++ b/src/executor/stack/memory.rs @@ -421,6 +421,9 @@ impl<'backend, 'config, B: Backend> Backend for MemoryStackState<'backend, 'conf fn block_difficulty(&self) -> U256 { self.backend.block_difficulty() } + fn block_randomness(&self) -> Option { + self.backend.block_randomness() + } fn block_gas_limit(&self) -> U256 { self.backend.block_gas_limit() }