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

fix(EVM): Use constant PREVRANDAO value (N-11) #1178

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Loading