2021-04-15 13:40:31 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
|
|
|
## EVM Opcode Handlers: Logging Operations
|
|
|
|
## =======================================
|
|
|
|
##
|
|
|
|
|
|
|
|
import
|
2021-04-21 17:04:54 +00:00
|
|
|
../../../constants,
|
2021-04-15 13:40:31 +00:00
|
|
|
../../../errors,
|
2021-04-26 16:00:46 +00:00
|
|
|
../../computation,
|
2021-04-22 16:05:58 +00:00
|
|
|
../../memory,
|
2021-04-21 17:04:54 +00:00
|
|
|
../../stack,
|
2021-04-22 16:05:58 +00:00
|
|
|
../../types,
|
2021-04-21 17:04:54 +00:00
|
|
|
../gas_costs,
|
|
|
|
../gas_meter,
|
|
|
|
../op_codes,
|
2021-04-22 16:05:58 +00:00
|
|
|
../utils/utils_numeric,
|
2021-04-15 13:40:31 +00:00
|
|
|
./oph_defs,
|
2021-04-23 10:47:58 +00:00
|
|
|
./oph_gen_handlers,
|
2021-04-15 13:40:31 +00:00
|
|
|
./oph_helpers,
|
|
|
|
eth/common,
|
2021-04-21 17:04:54 +00:00
|
|
|
eth/common,
|
|
|
|
sequtils,
|
|
|
|
stint,
|
|
|
|
strformat
|
2021-04-15 13:40:31 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private, names & settings
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc fnName(n: int): string {.compileTime.} =
|
2021-04-19 09:15:35 +00:00
|
|
|
&"log{n}Op"
|
2021-04-15 13:40:31 +00:00
|
|
|
|
|
|
|
proc opName(n: int): string {.compileTime.} =
|
|
|
|
&"Log{n}"
|
|
|
|
|
|
|
|
proc fnInfo(n: int): string {.compileTime.} =
|
|
|
|
var blurb = case n
|
|
|
|
of 1: "topic"
|
|
|
|
else: "topics"
|
|
|
|
&"Append log record with {n} {blurb}"
|
|
|
|
|
|
|
|
|
|
|
|
proc logImpl(c: Computation, opcode: Op, topicCount: int) =
|
|
|
|
doAssert(topicCount in 0 .. 4)
|
|
|
|
checkInStaticContext(c)
|
|
|
|
let (memStartPosition, size) = c.stack.popInt(2)
|
|
|
|
let (memPos, len) = (memStartPosition.cleanMemRef, size.cleanMemRef)
|
|
|
|
|
|
|
|
if memPos < 0 or len < 0:
|
|
|
|
raise newException(OutOfBoundsRead, "Out of bounds memory access")
|
|
|
|
|
|
|
|
c.gasMeter.consumeGas(
|
|
|
|
c.gasCosts[opcode].m_handler(c.memory.len, memPos, len),
|
|
|
|
reason = "Memory expansion, Log topic and data gas cost")
|
|
|
|
c.memory.extend(memPos, len)
|
|
|
|
|
|
|
|
var log: Log
|
|
|
|
log.topics = newSeqOfCap[Topic](topicCount)
|
|
|
|
for i in 0 ..< topicCount:
|
|
|
|
log.topics.add(c.stack.popTopic())
|
|
|
|
|
|
|
|
log.data = c.memory.read(memPos, len)
|
|
|
|
log.address = c.msg.contractAddress
|
|
|
|
c.addLogEntry(log)
|
|
|
|
|
|
|
|
const
|
|
|
|
inxRange = toSeq(0 .. 4)
|
|
|
|
logOpArg = block:
|
|
|
|
var rc: array[inxRange.len + 1, Op]
|
|
|
|
for n in inxRange:
|
|
|
|
rc[n] = Op(Log0.int + n)
|
|
|
|
rc
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private, op handlers implementation
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
proc wrapperFn(k: var Vm2Ctx; n: int) =
|
2021-04-15 13:40:31 +00:00
|
|
|
logImpl(k.cpt, logOpArg[n], n)
|
|
|
|
|
|
|
|
genOphHandlers fnName, fnInfo, inxRange, wrapperFn
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public, op exec table entries
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
genOphList fnName, fnInfo, inxRange, "vm2OpExecLog", opName
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|