mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-03-01 04:10:45 +00:00
* aristo: fork support via layers/txframes This change reorganises how the database is accessed: instead holding a "current frame" in the database object, a dag of frames is created based on the "base frame" held in `AristoDbRef` and all database access happens through this frame, which can be thought of as a consistent point-in-time snapshot of the database based on a particular fork of the chain. In the code, "frame", "transaction" and "layer" is used to denote more or less the same thing: a dag of stacked changes backed by the on-disk database. Although this is not a requirement, in practice each frame holds the change set of a single block - as such, the frame and its ancestors leading up to the on-disk state represents the state of the database after that block has been applied. "committing" means merging the changes to its parent frame so that the difference between them is lost and only the cumulative changes remain - this facility enables frames to be combined arbitrarily wherever they are in the dag. In particular, it becomes possible to consolidate a set of changes near the base of the dag and commit those to disk without having to re-do the in-memory frames built on top of them - this is useful for "flattening" a set of changes during a base update and sending those to storage without having to perform a block replay on top. Looking at abstractions, a side effect of this change is that the KVT and Aristo are brought closer together by considering them to be part of the "same" atomic transaction set - the way the code gets organised, applying a block and saving it to the kvt happens in the same "logical" frame - therefore, discarding the frame discards both the aristo and kvt changes at the same time - likewise, they are persisted to disk together - this makes reasoning about the database somewhat easier but has the downside of increased memory usage, something that perhaps will need addressing in the future. Because the code reasons more strictly about frames and the state of the persisted database, it also makes it more visible where ForkedChain should be used and where it is still missing - in particular, frames represent a single branch of history while forkedchain manages multiple parallel forks - user-facing services such as the RPC should use the latter, ie until it has been finalized, a getBlock request should consider all forks and not just the blocks in the canonical head branch. Another advantage of this approach is that `AristoDbRef` conceptually becomes more simple - removing its tracking of the "current" transaction stack simplifies reasoning about what can go wrong since this state now has to be passed around in the form of `AristoTxRef` - as such, many of the tests and facilities in the code that were dealing with "stack inconsistency" are now structurally prevented from happening. The test suite will need significant refactoring after this change. Once this change has been merged, there are several follow-ups to do: * there's no mechanism for keeping frames up to date as they get committed or rolled back - TODO * naming is confused - many names for the same thing for legacy reason * forkedchain support is still missing in lots of code * clean up redundant logic based on previous designs - in particular the debug and introspection code no longer makes sense * the way change sets are stored will probably need revisiting - because it's a stack of changes where each frame must be interrogated to find an on-disk value, with a base distance of 128 we'll at minimum have to perform 128 frame lookups for *every* database interaction - regardless, the "dag-like" nature will stay * dispose and commit are poorly defined and perhaps redundant - in theory, one could simply let the GC collect abandoned frames etc, though it's likely an explicit mechanism will remain useful, so they stay for now More about the changes: * `AristoDbRef` gains a `txRef` field (todo: rename) that "more or less" corresponds to the old `balancer` field * `AristoDbRef.stack` is gone - instead, there's a chain of `AristoTxRef` objects that hold their respective "layer" which has the actual changes * No more reasoning about "top" and "stack" - instead, each `AristoTxRef` can be a "head" that "more or less" corresponds to the old single-history `top` notion and its stack * `level` still represents "distance to base" - it's computed from the parent chain instead of being stored * one has to be careful not to use frames where forkedchain was intended - layers are only for a single branch of history! * fix layer vtop after rollback * engine fix * Fix test_txpool * Fix test_rpc * Fix copyright year * fix simulator * Fix copyright year * Fix copyright year * Fix tracer * Fix infinite recursion bug * Remove aristo and kvt empty files * Fic copyright year * Fix fc chain_kvt * ForkedChain refactoring * Fix merge master conflict * Fix copyright year * Reparent txFrame * Fix test * Fix txFrame reparent again * Cleanup and fix test * UpdateBase bugfix and fix test * Fixe newPayload bug discovered by hive * Fix engine api fcu * Clean up call template, chain_kvt, andn txguid * Fix copyright year * work around base block loading issue * Add test * Fix updateHead bug * Fix updateBase bug * Change func commitBase to proc commitBase * Touch up and fix debug mode crash --------- Co-authored-by: jangko <jangko128@gmail.com>
393 lines
14 KiB
Nim
393 lines
14 KiB
Nim
# Nimbus
|
|
# Copyright (c) 2018-2025 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.
|
|
|
|
import
|
|
std/[importutils, sequtils],
|
|
unittest2,
|
|
eth/common/[keys, transaction_utils],
|
|
../nimbus/common,
|
|
../nimbus/transaction,
|
|
../nimbus/evm/types,
|
|
../nimbus/evm/state,
|
|
../nimbus/evm/evm_errors,
|
|
../nimbus/evm/stack,
|
|
../nimbus/evm/memory,
|
|
../nimbus/evm/code_stream,
|
|
../nimbus/evm/internals,
|
|
../nimbus/constants,
|
|
../nimbus/core/pow/header,
|
|
../nimbus/db/ledger,
|
|
../nimbus/transaction/call_evm
|
|
|
|
template testPush(value: untyped, expected: untyped): untyped =
|
|
privateAccess(EvmStack)
|
|
var stack = EvmStack.init()
|
|
defer: stack.dispose()
|
|
check stack.push(value).isOk
|
|
check(toSeq(stack.items()) == @[expected])
|
|
|
|
proc runStackTests() =
|
|
suite "Stack tests":
|
|
test "push only valid":
|
|
testPush(0'u, 0.u256)
|
|
testPush(UINT_256_MAX, UINT_256_MAX)
|
|
# testPush("ves".toBytes, "ves".toBytes.bigEndianToInt)
|
|
|
|
test "push does not allow stack to exceed 1024":
|
|
var stack = EvmStack.init()
|
|
defer: stack.dispose()
|
|
for z in 0 ..< 1024:
|
|
check stack.push(z.uint).isOk
|
|
check(stack.len == 1024)
|
|
check stack.push(1025).error.code == EvmErrorCode.StackFull
|
|
|
|
test "dup does not allow stack to exceed 1024":
|
|
var stack = EvmStack.init()
|
|
defer: stack.dispose()
|
|
check stack.push(1.u256).isOk
|
|
for z in 0 ..< 1023:
|
|
check stack.dup(1).isOk
|
|
check(stack.len == 1024)
|
|
check stack.dup(1).error.code == EvmErrorCode.StackFull
|
|
|
|
test "pop returns latest stack item":
|
|
var stack = EvmStack.init()
|
|
defer: stack.dispose()
|
|
for element in @[1'u, 2'u, 3'u]:
|
|
check stack.push(element).isOk
|
|
check(stack.popInt.get == 3.u256)
|
|
|
|
test "pop requires stack item":
|
|
var stack = EvmStack.init()
|
|
defer: stack.dispose()
|
|
check:
|
|
stack.pop().isErr()
|
|
stack.push(1'u).isOk()
|
|
stack.pop().isOk()
|
|
|
|
test "swap correct":
|
|
privateAccess(EvmStack)
|
|
var stack = EvmStack.init()
|
|
defer: stack.dispose()
|
|
for z in 0 ..< 5:
|
|
check stack.push(z.uint).isOk
|
|
check(toSeq(stack.items()) == @[0.u256, 1.u256, 2.u256, 3.u256, 4.u256])
|
|
check stack.swap(3).isOk
|
|
check(toSeq(stack.items()) == @[0.u256, 4.u256, 2.u256, 3.u256, 1.u256])
|
|
check stack.swap(1).isOk
|
|
check(toSeq(stack.items()) == @[0.u256, 4.u256, 2.u256, 1.u256, 3.u256])
|
|
|
|
test "dup correct":
|
|
privateAccess(EvmStack)
|
|
var stack = EvmStack.init()
|
|
defer: stack.dispose()
|
|
for z in 0 ..< 5:
|
|
check stack.push(z.uint).isOk
|
|
check(toSeq(stack.items()) == @[0.u256, 1.u256, 2.u256, 3.u256, 4.u256])
|
|
check stack.dup(1).isOk
|
|
check(toSeq(stack.items()) == @[0.u256, 1.u256, 2.u256, 3.u256, 4.u256, 4.u256])
|
|
check stack.dup(5).isOk
|
|
check(toSeq(stack.items()) == @[0.u256, 1.u256, 2.u256, 3.u256, 4.u256, 4.u256, 1.u256])
|
|
|
|
test "pop raises InsufficientStack appropriately":
|
|
var stack = EvmStack.init()
|
|
defer: stack.dispose()
|
|
check stack.popInt().error.code == EvmErrorCode.StackInsufficient
|
|
|
|
test "swap raises InsufficientStack appropriately":
|
|
var stack = EvmStack.init()
|
|
defer: stack.dispose()
|
|
check stack.swap(0).error.code == EvmErrorCode.StackInsufficient
|
|
|
|
test "dup raises InsufficientStack appropriately":
|
|
var stack = EvmStack.init()
|
|
defer: stack.dispose()
|
|
check stack.dup(0).error.code == EvmErrorCode.StackInsufficient
|
|
|
|
test "binary operations raises InsufficientStack appropriately":
|
|
# https://github.com/status-im/nimbus/issues/31
|
|
# ./tests/fixtures/VMTests/vmArithmeticTest/mulUnderFlow.json
|
|
|
|
var stack = EvmStack.init()
|
|
defer: stack.dispose()
|
|
check stack.push(123).isOk
|
|
check stack.binaryOp(`+`).error.code == EvmErrorCode.StackInsufficient
|
|
|
|
proc memory32: EvmMemory =
|
|
result = EvmMemory.init(32)
|
|
|
|
proc memory128: EvmMemory =
|
|
result = EvmMemory.init(123)
|
|
|
|
proc runMemoryTests() =
|
|
suite "Memory tests":
|
|
test "write":
|
|
var mem = memory32()
|
|
# Test that write creates 32byte string == value padded with zeros
|
|
check mem.write(startPos = 0, value = @[1.byte, 0.byte, 1.byte, 0.byte]).isOk
|
|
check(mem.bytes == @[1.byte, 0.byte, 1.byte, 0.byte].concat(repeat(0.byte, 28)))
|
|
|
|
test "write rejects values beyond memory size":
|
|
var mem = memory128()
|
|
check mem.write(startPos = 128, value = @[1.byte, 0.byte, 1.byte, 0.byte]).error.code == EvmErrorCode.MemoryFull
|
|
check mem.write(startPos = 128, value = 1.byte).error.code == EvmErrorCode.MemoryFull
|
|
|
|
test "extends appropriately extends memory":
|
|
var mem = EvmMemory.init()
|
|
# Test extends to 32 byte array: 0 < (start_position + size) <= 32
|
|
mem.extend(startPos = 0, size = 10)
|
|
check(mem.bytes == repeat(0.byte, 32))
|
|
# Test will extend past length if params require: 32 < (start_position + size) <= 64
|
|
mem.extend(startPos = 28, size = 32)
|
|
check(mem.bytes == repeat(0.byte, 64))
|
|
# Test won't extend past length unless params require: 32 < (start_position + size) <= 64
|
|
mem.extend(startPos = 48, size = 10)
|
|
check(mem.bytes == repeat(0.byte, 64))
|
|
|
|
test "read returns correct bytes":
|
|
var mem = memory32()
|
|
check mem.write(startPos = 5, value = @[1.byte, 0.byte, 1.byte, 0.byte]).isOk
|
|
check(@(mem.read(startPos = 5, size = 4)) == @[1.byte, 0.byte, 1.byte, 0.byte])
|
|
check(@(mem.read(startPos = 6, size = 4)) == @[0.byte, 1.byte, 0.byte, 0.byte])
|
|
check(@(mem.read(startPos = 1, size = 3)) == @[0.byte, 0.byte, 0.byte])
|
|
|
|
proc runCodeStreamTests() =
|
|
suite "Codestream tests":
|
|
test "accepts bytes":
|
|
let codeStream = CodeStream.init("\x01")
|
|
check(codeStream.len == 1)
|
|
|
|
test "next returns the correct opcode":
|
|
var codeStream = CodeStream.init("\x01\x02\x30")
|
|
check(codeStream.next == Op.ADD)
|
|
check(codeStream.next == Op.MUL)
|
|
check(codeStream.next == Op.ADDRESS)
|
|
|
|
test "peek returns next opcode without changing location":
|
|
var codeStream = CodeStream.init("\x01\x02\x30")
|
|
check(codeStream.pc == 0)
|
|
check(codeStream.peek == Op.ADD)
|
|
check(codeStream.pc == 0)
|
|
check(codeStream.next == Op.ADD)
|
|
check(codeStream.pc == 1)
|
|
check(codeStream.peek == Op.MUL)
|
|
check(codeStream.pc == 1)
|
|
|
|
test "stop opcode is returned when end reached":
|
|
var codeStream = CodeStream.init("\x01\x02")
|
|
discard codeStream.next
|
|
discard codeStream.next
|
|
check(codeStream.next == Op.STOP)
|
|
|
|
test "[] returns opcode":
|
|
let codeStream = CodeStream.init("\x01\x02\x30")
|
|
check(codeStream[0] == Op.ADD)
|
|
check(codeStream[1] == Op.MUL)
|
|
check(codeStream[2] == Op.ADDRESS)
|
|
|
|
test "isValidOpcode invalidates after PUSHXX":
|
|
var codeStream = CodeStream.init("\x02\x60\x02\x04")
|
|
check(codeStream.isValidOpcode(0))
|
|
check(codeStream.isValidOpcode(1))
|
|
check(not codeStream.isValidOpcode(2))
|
|
check(codeStream.isValidOpcode(3))
|
|
check(not codeStream.isValidOpcode(4))
|
|
|
|
test "isValidOpcode 0":
|
|
var codeStream = CodeStream.init(@[2.byte, 3.byte, 0x72.byte].concat(repeat(4.byte, 32)).concat(@[5.byte]))
|
|
# valid: 0 - 2 :: 22 - 35
|
|
# invalid: 3-21 (PUSH19) :: 36+ (too long)
|
|
check(codeStream.isValidOpcode(0))
|
|
check(codeStream.isValidOpcode(1))
|
|
check(codeStream.isValidOpcode(2))
|
|
check(not codeStream.isValidOpcode(3))
|
|
check(not codeStream.isValidOpcode(21))
|
|
check(codeStream.isValidOpcode(22))
|
|
check(codeStream.isValidOpcode(35))
|
|
check(not codeStream.isValidOpcode(36))
|
|
|
|
test "isValidOpcode 1":
|
|
let test = @[2.byte, 3.byte, 0x7d.byte].concat(repeat(4.byte, 32)).concat(@[5.byte, 0x7e.byte]).concat(repeat(4.byte, 35)).concat(@[1.byte, 0x61.byte, 1.byte, 1.byte, 1.byte])
|
|
var codeStream = CodeStream.init(test)
|
|
# valid: 0 - 2 :: 33 - 36 :: 68 - 73 :: 76
|
|
# invalid: 3 - 32 (PUSH30) :: 37 - 67 (PUSH31) :: 74, 75 (PUSH2) :: 77+ (too long)
|
|
check(codeStream.isValidOpcode(0))
|
|
check(codeStream.isValidOpcode(1))
|
|
check(codeStream.isValidOpcode(2))
|
|
check(not codeStream.isValidOpcode(3))
|
|
check(not codeStream.isValidOpcode(32))
|
|
check(codeStream.isValidOpcode(33))
|
|
check(codeStream.isValidOpcode(36))
|
|
check(not codeStream.isValidOpcode(37))
|
|
check(not codeStream.isValidOpcode(67))
|
|
check(codeStream.isValidOpcode(68))
|
|
check(codeStream.isValidOpcode(71))
|
|
check(codeStream.isValidOpcode(72))
|
|
check(codeStream.isValidOpcode(73))
|
|
check(not codeStream.isValidOpcode(74))
|
|
check(not codeStream.isValidOpcode(75))
|
|
check(codeStream.isValidOpcode(76))
|
|
check(not codeStream.isValidOpcode(77))
|
|
|
|
test "right number of bytes invalidates":
|
|
var codeStream = CodeStream.init("\x02\x03\x60\x02\x02")
|
|
check(codeStream.isValidOpcode(0))
|
|
check(codeStream.isValidOpcode(1))
|
|
check(codeStream.isValidOpcode(2))
|
|
check(not codeStream.isValidOpcode(3))
|
|
check(codeStream.isValidOpcode(4))
|
|
check(not codeStream.isValidOpcode(5))
|
|
|
|
|
|
proc initGasMeter(startGas: GasInt): GasMeter = result.init(startGas)
|
|
|
|
proc gasMeters: seq[GasMeter] =
|
|
@[initGasMeter(10), initGasMeter(100), initGasMeter(999)]
|
|
|
|
template runTest(body: untyped) =
|
|
var res = gasMeters()
|
|
for gasMeter {.inject.} in res.mitems:
|
|
let StartGas {.inject.} = gasMeter.gasRemaining
|
|
body
|
|
|
|
proc runGasMeterTests() =
|
|
suite "GasMeter tests":
|
|
test "consume spends":
|
|
runTest:
|
|
check(gasMeter.gasRemaining == StartGas)
|
|
let consume = StartGas
|
|
check gasMeter.consumeGas(consume, "0").isOk
|
|
check(gasMeter.gasRemaining - (StartGas - consume) == 0)
|
|
|
|
test "consume errors":
|
|
runTest:
|
|
check(gasMeter.gasRemaining == StartGas)
|
|
check gasMeter.consumeGas(StartGas + 1, "").error.code == EvmErrorCode.OutOfGas
|
|
|
|
test "return refund works correctly":
|
|
runTest:
|
|
check(gasMeter.gasRemaining == StartGas)
|
|
check(gasMeter.gasRefunded == 0)
|
|
check gasMeter.consumeGas(5, "").isOk
|
|
check(gasMeter.gasRemaining == StartGas - 5)
|
|
gasMeter.returnGas(5)
|
|
check(gasMeter.gasRemaining == StartGas)
|
|
gasMeter.refundGas(5)
|
|
check(gasMeter.gasRefunded == 5)
|
|
|
|
proc runMiscTests() =
|
|
suite "Misc test suite":
|
|
test "calcGasLimitEIP1559":
|
|
type
|
|
GLT = object
|
|
limit: GasInt
|
|
max : GasInt
|
|
min : GasInt
|
|
|
|
const testData = [
|
|
GLT(limit: 20000000, max: 20019530, min: 19980470),
|
|
GLT(limit: 40000000, max: 40039061, min: 39960939)
|
|
]
|
|
|
|
for x in testData:
|
|
# Increase
|
|
var have = calcGasLimit1559(x.limit, 2*x.limit)
|
|
var want = x.max
|
|
check have == want
|
|
|
|
# Decrease
|
|
have = calcGasLimit1559(x.limit, 0)
|
|
want = x.min
|
|
check have == want
|
|
|
|
# Small decrease
|
|
have = calcGasLimit1559(x.limit, x.limit-1)
|
|
want = x.limit-1
|
|
check have == want
|
|
|
|
# Small increase
|
|
have = calcGasLimit1559(x.limit, x.limit+1)
|
|
want = x.limit+1
|
|
check have == want
|
|
|
|
# No change
|
|
have = calcGasLimit1559(x.limit, x.limit)
|
|
want = x.limit
|
|
check have == want
|
|
|
|
const
|
|
data = [0x5b.uint8, 0x5a, 0x5a, 0x30, 0x30, 0x30, 0x30, 0x72, 0x00, 0x00, 0x00, 0x58,
|
|
0x58, 0x24, 0x58, 0x58, 0x3a, 0x19, 0x75, 0x75, 0x2e, 0x2e, 0x2e, 0x2e,
|
|
0xec, 0x9f, 0x69, 0x67, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x6c, 0x5a, 0x32,
|
|
0x07, 0xf4, 0x75, 0x75, 0xf5, 0x75, 0x75, 0x75, 0x7f, 0x5b, 0xd9, 0x32,
|
|
0x5a, 0x07, 0x19, 0x34, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e,
|
|
0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e,
|
|
0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0x2e, 0xec,
|
|
0x9f, 0x69, 0x67, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x6c, 0xfc, 0xf7, 0xfc,
|
|
0xfc, 0xfc, 0xfc, 0xf4, 0x03, 0x03, 0x81, 0x81, 0x81, 0xfb, 0x7a, 0x30,
|
|
0x80, 0x3d, 0x59, 0x59, 0x59, 0x59, 0x81, 0x00, 0x59, 0x2f, 0x45, 0x30,
|
|
0x32, 0xf4, 0x5d, 0x5b, 0x37, 0x19]
|
|
|
|
codeAddress = address"000000000000000000000000636f6e7472616374"
|
|
coinbase = address"4444588443C3a91288c5002483449Aba1054192b"
|
|
|
|
proc runTestOverflow() =
|
|
test "GasCall unhandled overflow":
|
|
let header = Header(
|
|
stateRoot: EMPTY_ROOT_HASH,
|
|
number: 1150000'u64,
|
|
coinBase: coinbase,
|
|
gasLimit: 30000000,
|
|
timeStamp: EthTime(123456),
|
|
)
|
|
|
|
let com = CommonRef.new(
|
|
newCoreDbRef(DefaultDbMemory),
|
|
nil,
|
|
config = chainConfigForNetwork(MainNet)
|
|
)
|
|
|
|
let s = BaseVMState.new(
|
|
header,
|
|
header,
|
|
com,
|
|
com.db.baseTxFrame()
|
|
)
|
|
|
|
s.ledger.setCode(codeAddress, @data)
|
|
let unsignedTx = Transaction(
|
|
txType: TxLegacy,
|
|
nonce: 0,
|
|
chainId: MainNet.ChainId,
|
|
gasPrice: 0.GasInt,
|
|
gasLimit: 30000000,
|
|
to: Opt.some codeAddress,
|
|
value: 0.u256,
|
|
payload: @data
|
|
)
|
|
|
|
let privateKey = PrivateKey.fromHex("0000000000000000000000000000000000000000000000000000001000000000")[]
|
|
let tx = signTransaction(unsignedTx, privateKey, false)
|
|
let res = testCallEvm(tx, tx.recoverSender().expect("valid signature"), s)
|
|
|
|
when defined(evmc_enabled):
|
|
check res.error == "EVMC_FAILURE"
|
|
else:
|
|
# After gasCall values always on positive, this test become OOG
|
|
check res.error == "Opcode Dispatch Error: OutOfGas, depth=1"
|
|
|
|
proc evmSupportMain*() =
|
|
runStackTests()
|
|
runMemoryTests()
|
|
runCodeStreamTests()
|
|
runGasMeterTests()
|
|
runMiscTests()
|
|
runTestOverflow()
|
|
|
|
when isMainModule:
|
|
evmSupportMain()
|