Skip to content

Commit

Permalink
Use constant PREVRANDAO value
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov committed Jan 2, 2025
1 parent 26df944 commit 7f6e3f1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 34 deletions.
36 changes: 14 additions & 22 deletions system-contracts/contracts/EvmEmulator.yul
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ object "EvmEmulator" {
}

function ORIGIN_CACHE_OFFSET() -> offset {
offset := mul(23, 32)
offset := mul(24, 32)
}

function GASPRICE_CACHE_OFFSET() -> offset {
Expand All @@ -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 {
Expand Down Expand Up @@ -194,6 +190,10 @@ object "EvmEmulator" {
value := 0xffffffffffffffffffffffffffffffffffffffff
}

function PREVRANDAO_VALUE() -> value {
value := 2500000000000000 // This value is fixed in EraVM
}

////////////////////////////////////////////////////////////////
// GENERAL FUNCTIONS
////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -3014,7 +3010,7 @@ object "EvmEmulator" {
}

function ORIGIN_CACHE_OFFSET() -> offset {
offset := mul(23, 32)
offset := mul(24, 32)
}

function GASPRICE_CACHE_OFFSET() -> offset {
Expand All @@ -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 {
Expand Down Expand Up @@ -3121,6 +3113,10 @@ object "EvmEmulator" {
value := 0xffffffffffffffffffffffffffffffffffffffff
}

function PREVRANDAO_VALUE() -> value {
value := 2500000000000000 // This value is fixed in EraVM
}

////////////////////////////////////////////////////////////////
// GENERAL FUNCTIONS
////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion system-contracts/contracts/SystemContext.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
12 changes: 6 additions & 6 deletions system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
////////////////////////////////////////////////////////////////
Expand Down
6 changes: 1 addition & 5 deletions system-contracts/evm-emulator/EvmEmulatorLoop.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7f6e3f1

Please sign in to comment.