Skip to content

Commit

Permalink
fix(EVM): Fix logs implementation (2) (#1137)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov committed Dec 10, 2024
1 parent 9e2e5a4 commit 286a4dc
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 258 deletions.
250 changes: 78 additions & 172 deletions system-contracts/contracts/EvmEmulator.yul
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,35 @@ object "EvmEmulator" {
$llvm_AlwaysInline_llvm$_copyRest(dest_end, 0, rest_len)
}
}

////////////////////////////////////////////////////////////////
// LOGS FUNCTIONALITY
////////////////////////////////////////////////////////////////

function _genericLog(sp, stackHead, evmGasLeft, topicCount, isStatic) -> newEvmGasLeft, offset, size, newSp, newStackHead {
newEvmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let rawOffset
popStackCheck(sp, add(2, topicCount))
rawOffset, newSp, newStackHead := popStackItemWithoutCheck(sp, stackHead)
size, newSp, newStackHead := popStackItemWithoutCheck(newSp, newStackHead)

checkMemIsAccessible(rawOffset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(rawOffset, size))
dynamicGas := add(dynamicGas, mul(375, topicCount))

newEvmGasLeft := chargeGas(newEvmGasLeft, dynamicGas)

if size {
offset := add(rawOffset, MEM_OFFSET())
}
}

function simulate(
isCallerEVM,
Expand Down Expand Up @@ -2641,133 +2670,57 @@ object "EvmEmulator" {
ip := add(ip, 1)
}
case 0xA0 { // OP_LOG0
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 2)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

log0(add(offset, MEM_OFFSET()), size)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 0, isStatic)
log0(offset, size)
ip := add(ip, 1)
}
case 0xA1 { // OP_LOG1
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 3)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 375)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 1, isStatic)
{
let topic1
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log1(add(offset, MEM_OFFSET()), size, topic1)
log1(offset, size, topic1)
}
ip := add(ip, 1)
}
case 0xA2 { // OP_LOG2
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 4)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 750)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 2, isStatic)

{
let topic1, topic2
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic2, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log2(add(offset, MEM_OFFSET()), size, topic1, topic2)
log2(offset, size, topic1, topic2)
}
ip := add(ip, 1)
}
case 0xA3 { // OP_LOG3
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 5)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 1125)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 3, isStatic)

{
let topic1, topic2, topic3
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic2, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic3, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log3(add(offset, MEM_OFFSET()), size, topic1, topic2, topic3)
log3(offset, size, topic1, topic2, topic3)
}
ip := add(ip, 1)
}
case 0xA4 { // OP_LOG4
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 6)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 1500)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 4, isStatic)

{
let topic1, topic2, topic3, topic4
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic2, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic3, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic4, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log4(add(offset, MEM_OFFSET()), size, topic1, topic2, topic3, topic4)
log4(offset, size, topic1, topic2, topic3, topic4)
}
ip := add(ip, 1)
}
Expand Down Expand Up @@ -4488,6 +4441,35 @@ object "EvmEmulator" {
$llvm_AlwaysInline_llvm$_copyRest(dest_end, 0, rest_len)
}
}

////////////////////////////////////////////////////////////////
// LOGS FUNCTIONALITY
////////////////////////////////////////////////////////////////

function _genericLog(sp, stackHead, evmGasLeft, topicCount, isStatic) -> newEvmGasLeft, offset, size, newSp, newStackHead {
newEvmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let rawOffset
popStackCheck(sp, add(2, topicCount))
rawOffset, newSp, newStackHead := popStackItemWithoutCheck(sp, stackHead)
size, newSp, newStackHead := popStackItemWithoutCheck(newSp, newStackHead)

checkMemIsAccessible(rawOffset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(rawOffset, size))
dynamicGas := add(dynamicGas, mul(375, topicCount))

newEvmGasLeft := chargeGas(newEvmGasLeft, dynamicGas)

if size {
offset := add(rawOffset, MEM_OFFSET())
}
}

function $llvm_NoInline_llvm$_simulate(
isCallerEVM,
Expand Down Expand Up @@ -5828,133 +5810,57 @@ object "EvmEmulator" {
ip := add(ip, 1)
}
case 0xA0 { // OP_LOG0
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 2)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

log0(add(offset, MEM_OFFSET()), size)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 0, isStatic)
log0(offset, size)
ip := add(ip, 1)
}
case 0xA1 { // OP_LOG1
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 3)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 375)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 1, isStatic)
{
let topic1
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log1(add(offset, MEM_OFFSET()), size, topic1)
log1(offset, size, topic1)
}
ip := add(ip, 1)
}
case 0xA2 { // OP_LOG2
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 4)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 750)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 2, isStatic)

{
let topic1, topic2
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic2, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log2(add(offset, MEM_OFFSET()), size, topic1, topic2)
log2(offset, size, topic1, topic2)
}
ip := add(ip, 1)
}
case 0xA3 { // OP_LOG3
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 5)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 1125)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 3, isStatic)

{
let topic1, topic2, topic3
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic2, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic3, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log3(add(offset, MEM_OFFSET()), size, topic1, topic2, topic3)
log3(offset, size, topic1, topic2, topic3)
}
ip := add(ip, 1)
}
case 0xA4 { // OP_LOG4
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 6)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 1500)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 4, isStatic)

{
let topic1, topic2, topic3, topic4
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic2, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic3, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic4, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log4(add(offset, MEM_OFFSET()), size, topic1, topic2, topic3, topic4)
log4(offset, size, topic1, topic2, topic3, topic4)
}
ip := add(ip, 1)
}
Expand Down
29 changes: 29 additions & 0 deletions system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -1238,4 +1238,33 @@ function $llvm_AlwaysInline_llvm$_memsetToZero(dest,len) {
if rest_len {
$llvm_AlwaysInline_llvm$_copyRest(dest_end, 0, rest_len)
}
}

////////////////////////////////////////////////////////////////
// LOGS FUNCTIONALITY
////////////////////////////////////////////////////////////////

function _genericLog(sp, stackHead, evmGasLeft, topicCount, isStatic) -> newEvmGasLeft, offset, size, newSp, newStackHead {
newEvmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let rawOffset
popStackCheck(sp, add(2, topicCount))
rawOffset, newSp, newStackHead := popStackItemWithoutCheck(sp, stackHead)
size, newSp, newStackHead := popStackItemWithoutCheck(newSp, newStackHead)

checkMemIsAccessible(rawOffset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(rawOffset, size))
dynamicGas := add(dynamicGas, mul(375, topicCount))

newEvmGasLeft := chargeGas(newEvmGasLeft, dynamicGas)

if size {
offset := add(rawOffset, MEM_OFFSET())
}
}
Loading

0 comments on commit 286a4dc

Please sign in to comment.