From 7f6e3f124338173dd37d9b54b6fa53d688d58092 Mon Sep 17 00:00:00 2001 From: Vladislav Volosnikov Date: Thu, 2 Jan 2025 14:19:07 +0100 Subject: [PATCH] Use constant PREVRANDAO value --- system-contracts/contracts/EvmEmulator.yul | 36 ++++++++----------- system-contracts/contracts/SystemContext.sol | 2 +- .../EvmEmulatorFunctions.template.yul | 12 +++---- .../evm-emulator/EvmEmulatorLoop.template.yul | 6 +--- 4 files changed, 22 insertions(+), 34 deletions(-) diff --git a/system-contracts/contracts/EvmEmulator.yul b/system-contracts/contracts/EvmEmulator.yul index fa36d4921..3c784aa42 100644 --- a/system-contracts/contracts/EvmEmulator.yul +++ b/system-contracts/contracts/EvmEmulator.yul @@ -87,7 +87,7 @@ object "EvmEmulator" { } function ORIGIN_CACHE_OFFSET() -> offset { - offset := mul(23, 32) + offset := mul(24, 32) } function GASPRICE_CACHE_OFFSET() -> offset { @@ -106,12 +106,8 @@ object "EvmEmulator" { offset := add(BLOCKTIMESTAMP_CACHE_OFFSET(), 32) } - function PREVRANDAO_CACHE_OFFSET() -> offset { - offset := add(BLOCKNUMBER_CACHE_OFFSET(), 32) - } - function GASLIMIT_CACHE_OFFSET() -> offset { - offset := add(PREVRANDAO_CACHE_OFFSET(), 32) + offset := add(BLOCKNUMBER_CACHE_OFFSET(), 32) } function CHAINID_CACHE_OFFSET() -> offset { @@ -194,6 +190,10 @@ object "EvmEmulator" { value := 0xffffffffffffffffffffffffffffffffffffffff } + function PREVRANDAO_VALUE() -> value { + value := 2500000000000000 // This value is fixed in EraVM + } + //////////////////////////////////////////////////////////////// // GENERAL FUNCTIONS //////////////////////////////////////////////////////////////// @@ -1959,11 +1959,7 @@ object "EvmEmulator" { } case 0x44 { // OP_PREVRANDAO evmGasLeft := chargeGas(evmGasLeft, 2) - let _prevrandao := mload(PREVRANDAO_CACHE_OFFSET()) - if iszero(_prevrandao) { - _prevrandao := cached(PREVRANDAO_CACHE_OFFSET(), prevrandao()) - } - sp, stackHead := pushStackItem(sp, _prevrandao, stackHead) + sp, stackHead := pushStackItem(sp, PREVRANDAO_VALUE(), stackHead) ip := add(ip, 1) } case 0x45 { // OP_GASLIMIT @@ -3014,7 +3010,7 @@ object "EvmEmulator" { } function ORIGIN_CACHE_OFFSET() -> offset { - offset := mul(23, 32) + offset := mul(24, 32) } function GASPRICE_CACHE_OFFSET() -> offset { @@ -3033,12 +3029,8 @@ object "EvmEmulator" { offset := add(BLOCKTIMESTAMP_CACHE_OFFSET(), 32) } - function PREVRANDAO_CACHE_OFFSET() -> offset { - offset := add(BLOCKNUMBER_CACHE_OFFSET(), 32) - } - function GASLIMIT_CACHE_OFFSET() -> offset { - offset := add(PREVRANDAO_CACHE_OFFSET(), 32) + offset := add(BLOCKNUMBER_CACHE_OFFSET(), 32) } function CHAINID_CACHE_OFFSET() -> offset { @@ -3121,6 +3113,10 @@ object "EvmEmulator" { value := 0xffffffffffffffffffffffffffffffffffffffff } + function PREVRANDAO_VALUE() -> value { + value := 2500000000000000 // This value is fixed in EraVM + } + //////////////////////////////////////////////////////////////// // GENERAL FUNCTIONS //////////////////////////////////////////////////////////////// @@ -4874,11 +4870,7 @@ object "EvmEmulator" { } case 0x44 { // OP_PREVRANDAO evmGasLeft := chargeGas(evmGasLeft, 2) - let _prevrandao := mload(PREVRANDAO_CACHE_OFFSET()) - if iszero(_prevrandao) { - _prevrandao := cached(PREVRANDAO_CACHE_OFFSET(), prevrandao()) - } - sp, stackHead := pushStackItem(sp, _prevrandao, stackHead) + sp, stackHead := pushStackItem(sp, PREVRANDAO_VALUE(), stackHead) ip := add(ip, 1) } case 0x45 { // OP_GASLIMIT diff --git a/system-contracts/contracts/SystemContext.sol b/system-contracts/contracts/SystemContext.sol index 2ce75419d..ee9f2aaf4 100644 --- a/system-contracts/contracts/SystemContext.sol +++ b/system-contracts/contracts/SystemContext.sol @@ -42,10 +42,10 @@ contract SystemContext is ISystemContext, ISystemContextDeprecated, SystemContra /// @notice The `block.coinbase` in the current transaction. /// @dev For the support of coinbase, we will use the bootloader formal address for now - /// @dev (!) EVM emulator doesn't expect this value to change address public coinbase = BOOTLOADER_FORMAL_ADDRESS; /// @notice Formal `block.difficulty` parameter. + /// @dev (!) EVM emulator doesn't expect this value to change uint256 public difficulty = 2.5e15; /// @notice The `block.basefee`. diff --git a/system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul b/system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul index 0aecdccbc..f388086be 100644 --- a/system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul +++ b/system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul @@ -27,7 +27,7 @@ function MSG_VALUE_SYSTEM_CONTRACT() -> addr { } function ORIGIN_CACHE_OFFSET() -> offset { - offset := mul(23, 32) + offset := mul(24, 32) } function GASPRICE_CACHE_OFFSET() -> offset { @@ -46,12 +46,8 @@ function BLOCKNUMBER_CACHE_OFFSET() -> offset { offset := add(BLOCKTIMESTAMP_CACHE_OFFSET(), 32) } -function PREVRANDAO_CACHE_OFFSET() -> offset { - offset := add(BLOCKNUMBER_CACHE_OFFSET(), 32) -} - function GASLIMIT_CACHE_OFFSET() -> offset { - offset := add(PREVRANDAO_CACHE_OFFSET(), 32) + offset := add(BLOCKNUMBER_CACHE_OFFSET(), 32) } function CHAINID_CACHE_OFFSET() -> offset { @@ -134,6 +130,10 @@ function ADDRESS_MASK() -> value { // mask for lower 160 bits value := 0xffffffffffffffffffffffffffffffffffffffff } +function PREVRANDAO_VALUE() -> value { + value := 2500000000000000 // This value is fixed in EraVM +} + //////////////////////////////////////////////////////////////// // GENERAL FUNCTIONS //////////////////////////////////////////////////////////////// diff --git a/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul b/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul index 1d6027b7b..be56ed95b 100644 --- a/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul +++ b/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul @@ -623,11 +623,7 @@ for { } true { } { } case 0x44 { // OP_PREVRANDAO evmGasLeft := chargeGas(evmGasLeft, 2) - let _prevrandao := mload(PREVRANDAO_CACHE_OFFSET()) - if iszero(_prevrandao) { - _prevrandao := cached(PREVRANDAO_CACHE_OFFSET(), prevrandao()) - } - sp, stackHead := pushStackItem(sp, _prevrandao, stackHead) + sp, stackHead := pushStackItem(sp, PREVRANDAO_VALUE(), stackHead) ip := add(ip, 1) } case 0x45 { // OP_GASLIMIT