2021-04-08 14:52:10 +00:00
|
|
|
# Nimbus
|
2024-03-21 11:24:32 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2021-04-08 14:52:10 +00:00
|
|
|
# Licensed under either of
|
2024-06-07 08:24:32 +00:00
|
|
|
# * 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.
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
{.push raises: [].}
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
import
|
|
|
|
std/[macros],
|
2024-07-13 18:42:49 +00:00
|
|
|
stew/assign2,
|
2024-06-07 08:24:32 +00:00
|
|
|
eth/common,
|
|
|
|
./evm_errors,
|
|
|
|
./interpreter/utils/utils_numeric
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2021-04-23 09:45:50 +00:00
|
|
|
type
|
2024-07-13 18:42:49 +00:00
|
|
|
EvmStack* = ref object
|
2024-06-07 08:24:32 +00:00
|
|
|
values: seq[EvmStackElement]
|
2021-04-23 09:45:50 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
EvmStackElement = UInt256
|
|
|
|
EvmStackInts = uint64 | uint | int | GasInt
|
|
|
|
EvmStackBytes32 = array[32, byte]
|
2021-04-23 09:45:50 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func len*(stack: EvmStack): int {.inline.} =
|
2023-04-24 20:59:38 +00:00
|
|
|
len(stack.values)
|
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template toStackElem(v: UInt256, elem: EvmStackElement) =
|
|
|
|
elem = v
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
template toStackElem(v: EvmStackInts, elem: EvmStackElement) =
|
|
|
|
elem = v.u256
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
template toStackElem(v: EthAddress, elem: EvmStackElement) =
|
2023-04-24 20:59:38 +00:00
|
|
|
elem.initFromBytesBE(v)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
template toStackElem(v: MDigest, elem: EvmStackElement) =
|
|
|
|
elem.initFromBytesBE(v.data)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
template toStackElem(v: openArray[byte], elem: EvmStackElement) =
|
|
|
|
doAssert(v.len <= 32)
|
|
|
|
elem.initFromBytesBE(v)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
template fromStackElem(elem: EvmStackElement, _: type UInt256): UInt256 =
|
|
|
|
elem
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
func fromStackElem(elem: EvmStackElement, _: type EthAddress): EthAddress =
|
2024-07-13 18:42:49 +00:00
|
|
|
assign(result, elem.toBytesBE().toOpenArray(12, 31))
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
template fromStackElem(elem: EvmStackElement, _: type Hash256): Hash256 =
|
|
|
|
Hash256(data: elem.toBytesBE())
|
|
|
|
|
|
|
|
template fromStackElem(elem: EvmStackElement, _: type EvmStackBytes32): EvmStackBytes32 =
|
|
|
|
elem.toBytesBE()
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func pushAux[T](stack: var EvmStack, value: T): EvmResultVoid =
|
2024-06-07 08:24:32 +00:00
|
|
|
if len(stack.values) > 1023:
|
|
|
|
return err(stackErr(StackFull))
|
|
|
|
stack.values.setLen(stack.values.len + 1)
|
|
|
|
toStackElem(value, stack.values[^1])
|
|
|
|
ok()
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func ensurePop(stack: EvmStack, expected: int): EvmResultVoid =
|
2024-06-07 08:24:32 +00:00
|
|
|
if stack.values.len < expected:
|
|
|
|
return err(stackErr(StackInsufficient))
|
|
|
|
ok()
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func popAux(stack: var EvmStack, T: type): EvmResult[T] =
|
2024-06-07 08:24:32 +00:00
|
|
|
? ensurePop(stack, 1)
|
|
|
|
result = ok(fromStackElem(stack.values[^1], T))
|
2023-04-24 20:59:38 +00:00
|
|
|
stack.values.setLen(stack.values.len - 1)
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func internalPopTuple(stack: var EvmStack, T: type, tupleLen: static[int]): EvmResult[T] =
|
2024-06-07 08:24:32 +00:00
|
|
|
? ensurePop(stack, tupleLen)
|
|
|
|
var
|
|
|
|
i = 0
|
|
|
|
v: T
|
2023-04-24 20:59:38 +00:00
|
|
|
let sz = stack.values.high
|
2021-04-08 14:52:10 +00:00
|
|
|
for f in fields(v):
|
2024-06-07 08:24:32 +00:00
|
|
|
f = fromStackElem(stack.values[sz - i], UInt256)
|
2021-04-08 14:52:10 +00:00
|
|
|
inc i
|
2023-04-24 20:59:38 +00:00
|
|
|
stack.values.setLen(sz - tupleLen + 1)
|
2024-06-07 08:24:32 +00:00
|
|
|
ok(v)
|
2023-04-24 20:59:38 +00:00
|
|
|
|
|
|
|
macro genTupleType(len: static[int], elemType: untyped): untyped =
|
2021-04-08 14:52:10 +00:00
|
|
|
result = nnkTupleConstr.newNimNode()
|
|
|
|
for i in 0 ..< len: result.add(elemType)
|
|
|
|
|
2024-06-07 08:24:32 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func push*(stack: var EvmStack,
|
2024-06-07 08:24:32 +00:00
|
|
|
value: EvmStackInts | UInt256 | EthAddress | Hash256): EvmResultVoid =
|
|
|
|
pushAux(stack, value)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func push*(stack: var EvmStack, value: openArray[byte]): EvmResultVoid =
|
2024-06-07 08:24:32 +00:00
|
|
|
pushAux(stack, value)
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func popInt*(stack: var EvmStack): EvmResult[UInt256] =
|
2024-06-07 08:24:32 +00:00
|
|
|
popAux(stack, UInt256)
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func popSafeInt*(stack: var EvmStack): EvmResult[int] =
|
2024-06-07 08:24:32 +00:00
|
|
|
? ensurePop(stack, 1)
|
|
|
|
result = ok(fromStackElem(stack.values[^1], UInt256).safeInt)
|
|
|
|
stack.values.setLen(stack.values.len - 1)
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func popMemRef*(stack: var EvmStack): EvmResult[int] =
|
2024-06-07 08:24:32 +00:00
|
|
|
? ensurePop(stack, 1)
|
|
|
|
result = ok(fromStackElem(stack.values[^1], UInt256).cleanMemRef)
|
|
|
|
stack.values.setLen(stack.values.len - 1)
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func popInt*(stack: var EvmStack, numItems: static[int]): auto =
|
2024-06-07 08:24:32 +00:00
|
|
|
type T = genTupleType(numItems, UInt256)
|
|
|
|
stack.internalPopTuple(T, numItems)
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func popAddress*(stack: var EvmStack): EvmResult[EthAddress] =
|
2024-06-07 08:24:32 +00:00
|
|
|
popAux(stack, EthAddress)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func popTopic*(stack: var EvmStack): EvmResult[EvmStackBytes32] =
|
2024-06-07 08:24:32 +00:00
|
|
|
popAux(stack, EvmStackBytes32)
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func init*(_: type EvmStack): EvmStack =
|
|
|
|
EvmStack(
|
|
|
|
values: newSeqOfCap[EvmStackElement](128)
|
|
|
|
)
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func swap*(stack: var EvmStack, position: int): EvmResultVoid =
|
2021-04-08 14:52:10 +00:00
|
|
|
## Perform a SWAP operation on the stack
|
2024-06-07 08:24:32 +00:00
|
|
|
let idx = position + 1
|
|
|
|
if idx < stack.values.len + 1:
|
2023-04-24 20:59:38 +00:00
|
|
|
(stack.values[^1], stack.values[^idx]) = (stack.values[^idx], stack.values[^1])
|
2024-06-07 08:24:32 +00:00
|
|
|
ok()
|
2021-04-08 14:52:10 +00:00
|
|
|
else:
|
2024-06-07 08:24:32 +00:00
|
|
|
err(stackErr(StackInsufficient))
|
2021-04-08 14:52:10 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func dup*(stack: var EvmStack, position: int): EvmResultVoid =
|
2021-04-08 14:52:10 +00:00
|
|
|
## Perform a DUP operation on the stack
|
|
|
|
if position in 1 .. stack.len:
|
2023-04-24 20:59:38 +00:00
|
|
|
stack.push(stack.values[^position])
|
2021-04-08 14:52:10 +00:00
|
|
|
else:
|
2024-06-07 08:24:32 +00:00
|
|
|
err(stackErr(StackInsufficient))
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func peek*(stack: EvmStack): EvmResult[UInt256] =
|
2024-06-07 08:24:32 +00:00
|
|
|
if stack.values.len == 0:
|
|
|
|
return err(stackErr(StackInsufficient))
|
|
|
|
ok(fromStackElem(stack.values[^1], UInt256))
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func peekSafeInt*(stack: EvmStack): EvmResult[int] =
|
2024-06-07 08:24:32 +00:00
|
|
|
if stack.values.len == 0:
|
|
|
|
return err(stackErr(StackInsufficient))
|
|
|
|
ok(fromStackElem(stack.values[^1], UInt256).safeInt)
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func `[]`*(stack: EvmStack, i: BackwardsIndex, T: typedesc): EvmResult[T] =
|
2024-06-07 08:24:32 +00:00
|
|
|
? ensurePop(stack, int(i))
|
|
|
|
ok(fromStackElem(stack.values[i], T))
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func peekInt*(stack: EvmStack): EvmResult[UInt256] =
|
2024-06-07 08:24:32 +00:00
|
|
|
? ensurePop(stack, 1)
|
2024-07-18 11:59:53 +00:00
|
|
|
ok(fromStackElem(stack.values[^1], UInt256))
|
2024-06-07 08:24:32 +00:00
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func peekAddress*(stack: EvmStack): EvmResult[EthAddress] =
|
2024-06-07 08:24:32 +00:00
|
|
|
? ensurePop(stack, 1)
|
|
|
|
ok(fromStackElem(stack.values[^1], EthAddress))
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
func top*(stack: EvmStack,
|
2024-06-07 08:24:32 +00:00
|
|
|
value: EvmStackInts | UInt256 | EthAddress | Hash256): EvmResultVoid =
|
|
|
|
if stack.values.len == 0:
|
|
|
|
return err(stackErr(StackInsufficient))
|
|
|
|
toStackElem(value, stack.values[^1])
|
|
|
|
ok()
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
iterator items*(stack: EvmStack): UInt256 =
|
2024-06-07 08:24:32 +00:00
|
|
|
for v in stack.values:
|
|
|
|
yield v
|
|
|
|
|
2024-07-13 18:42:49 +00:00
|
|
|
iterator pairs*(stack: EvmStack): (int, UInt256) =
|
2024-06-07 08:24:32 +00:00
|
|
|
for i, v in stack.values:
|
|
|
|
yield (i, v)
|
2024-07-18 11:59:53 +00:00
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions with less safety
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template lsCheck*(stack: EvmStack, expected: int): EvmResultVoid =
|
|
|
|
ensurePop(stack, expected)
|
|
|
|
|
|
|
|
func lsTop*(stack: EvmStack,
|
|
|
|
value: EvmStackInts | UInt256 | EthAddress | Hash256) =
|
|
|
|
toStackElem(value, stack.values[^1])
|
|
|
|
|
|
|
|
func lsTop*(stack: var EvmStack, value: openArray[byte]) =
|
|
|
|
toStackElem(value, stack.values[^1])
|
|
|
|
|
|
|
|
func lsPeekInt*(stack: EvmStack, i: BackwardsIndex): UInt256 =
|
|
|
|
fromStackElem(stack.values[i], UInt256)
|
|
|
|
|
|
|
|
func lsPeekAddress*(stack: EvmStack, i: BackwardsIndex): EthAddress =
|
|
|
|
fromStackElem(stack.values[i], EthAddress)
|
|
|
|
|
|
|
|
func lsPeekMemRef*(stack: EvmStack, i: BackwardsIndex): int =
|
|
|
|
fromStackElem(stack.values[i], UInt256).cleanMemRef
|
|
|
|
|
|
|
|
func lsPeekSafeInt*(stack: EvmStack, i: BackwardsIndex): int =
|
|
|
|
fromStackElem(stack.values[i], UInt256).safeInt
|
|
|
|
|
|
|
|
func lsPeekTopic*(stack: EvmStack, i: BackwardsIndex): EvmStackBytes32 =
|
|
|
|
fromStackElem(stack.values[i], EvmStackBytes32)
|
|
|
|
|
|
|
|
func lsShrink*(stack: EvmStack, x: int) =
|
|
|
|
stack.values.setLen(stack.values.len - x)
|
|
|
|
|
|
|
|
template binaryOp*(stack: EvmStack, binOp): EvmResultVoid =
|
|
|
|
if stack.values.len >= 2:
|
|
|
|
stack.values[^2] = binOp(stack.values[^1], stack.values[^2])
|
|
|
|
stack.values.setLen(stack.values.len - 1)
|
|
|
|
EvmResultVoid.ok()
|
|
|
|
else:
|
|
|
|
EvmResultVoid.err(stackErr(StackInsufficient))
|
|
|
|
|
|
|
|
template unaryOp*(stack: EvmStack, unOp): EvmResultVoid =
|
|
|
|
if stack.values.len >= 1:
|
|
|
|
stack.values[^1] = unOp(stack.values[^1])
|
|
|
|
EvmResultVoid.ok()
|
|
|
|
else:
|
|
|
|
EvmResultVoid.err(stackErr(StackInsufficient))
|
|
|
|
|
|
|
|
template binaryWithTop*(stack: EvmStack, binOp): EvmResultVoid =
|
|
|
|
if stack.values.len >= 2:
|
|
|
|
binOp(stack.values[^2], stack.values[^1], stack.values[^2])
|
|
|
|
stack.values.setLen(stack.values.len - 1)
|
|
|
|
EvmResultVoid.ok()
|
|
|
|
else:
|
|
|
|
EvmResultVoid.err(stackErr(StackInsufficient))
|
|
|
|
|
|
|
|
template unaryWithTop*(stack: EvmStack, unOp): EvmResultVoid =
|
|
|
|
if stack.values.len >= 1:
|
|
|
|
unOp(stack.values[^1], stack.values[^1], toStackElem)
|
|
|
|
EvmResultVoid.ok()
|
|
|
|
else:
|
|
|
|
EvmResultVoid.err(stackErr(StackInsufficient))
|
|
|
|
|
|
|
|
template unaryAddress*(stack: EvmStack, unOp): EvmResultVoid =
|
|
|
|
if stack.values.len >= 1:
|
|
|
|
let address = fromStackElem(stack.values[^1], EthAddress)
|
|
|
|
toStackElem(unOp(address), stack.values[^1])
|
|
|
|
EvmResultVoid.ok()
|
|
|
|
else:
|
|
|
|
EvmResultVoid.err(stackErr(StackInsufficient))
|
|
|
|
|