nimbus-eth1/tests/test_helpers.nim

164 lines
6.0 KiB
Nim
Raw Permalink Normal View History

# Nimbus
# Copyright (c) 2018-2024 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.
2018-01-17 11:24:09 +00:00
import
std/[os, macros, json, strformat, strutils, tables],
stew/byteutils, net, eth/[keys, p2p], unittest2,
testutils/markdown_reports,
2022-12-02 04:39:12 +00:00
../nimbus/[constants, config, transaction, errors],
../nimbus/db/ledger,
2022-12-02 04:39:12 +00:00
../nimbus/common/[context, common]
2018-01-17 11:24:09 +00:00
2022-12-02 04:39:12 +00:00
func revmap(x: Table[EVMFork, string]): Table[string, EVMFork] =
result = Table[string, EVMFork]()
for k, v in x:
result[v] = k
const
# from https://ethereum-tests.readthedocs.io/en/latest/test_types/state_tests.html
forkNames* = {
FkFrontier: "Frontier",
FkHomestead: "Homestead",
FkTangerine: "EIP150",
FkSpurious: "EIP158",
FkByzantium: "Byzantium",
2020-03-04 09:32:30 +00:00
FkConstantinople: "Constantinople",
FkPetersburg: "ConstantinopleFix",
FkIstanbul: "Istanbul",
FkBerlin: "Berlin",
FkLondon: "London",
FkParis: "Merge",
FkPrague: "Prague",
}.toTable
nameToFork* = revmap(forkNames)
func skipNothing*(folder: string, name: string): bool = false
var status = initOrderedTable[string, OrderedTable[string, Status]]()
proc jsonTestImpl*(inputFolder, outputName: string, handler, skipTest: NimNode): NimNode {.compileTime.} =
let
testStatusIMPL = ident("testStatusIMPL")
# workaround for strformat in quote do: https://github.com/nim-lang/Nim/issues/8220
2020-04-15 11:09:49 +00:00
symbol {.used.} = newIdentNode"symbol"
final {.used.} = newIdentNode"final"
name {.used.} = newIdentNode"name"
formatted {.used.} = newStrLitNode"{symbol[final]} {name:<64}{$final}{'\n'}"
2018-02-13 17:18:08 +00:00
result = quote:
var filenames: seq[string] = @[]
2022-12-02 04:39:12 +00:00
let inputPath = "tests" / "fixtures" / `inputFolder`
for filename in walkDirRec(inputPath):
if not filename.endsWith(".json"):
continue
var (folder, name) = filename.splitPath()
let last = folder.splitPath().tail
if not status.hasKey(last):
status[last] = initOrderedTable[string, Status]()
status[last][name] = Status.Skip
if `skipTest`(last, name):
continue
filenames.add(filename)
doAssert(filenames.len > 0)
for fname in filenames:
let filename = fname
2022-12-02 04:39:12 +00:00
test fname.subStr(inputPath.len + 1):
{.gcsafe.}:
let
(folder, name) = filename.splitPath()
last = folder.splitPath().tail
# we set this here because exceptions might be raised in the handler:
status[last][name] = Status.Fail
2022-04-08 04:54:11 +00:00
let fixtures = parseJson(readFile(filename))
2022-12-02 04:39:12 +00:00
`handler`(fixtures, `testStatusIMPL`)
if `testStatusIMPL` == OK:
status[last][name] = Status.OK
elif `testStatusIMPL` == SKIPPED:
status[last][name] = Status.Skip
suiteTeardown:
status.sort do (a: (string, OrderedTable[string, Status]),
b: (string, OrderedTable[string, Status])) -> int: cmp(a[0], b[0])
generateReport(`outputName`, status)
status.clear()
2018-01-17 11:24:09 +00:00
macro jsonTest*(inputFolder, outputName: static[string], handler: untyped, skipTest: untyped = skipNothing): untyped =
result = jsonTestImpl(inputFolder, outputName, handler, skipTest)
macro jsonTest*(inputFolder: static[string], handler: untyped, skipTest: untyped = skipNothing): untyped =
result = jsonTestImpl(inputFolder, inputFolder, handler, skipTest)
func ethAddressFromHex*(s: string): EthAddress = hexToByteArray(s, result)
2018-05-30 16:11:15 +00:00
func safeHexToSeqByte*(hexStr: string): seq[byte] =
if hexStr == "":
@[]
else:
hexStr.hexToSeqByte
func getHexadecimalInt*(j: JsonNode): int64 =
# parseutils.parseHex works with int which will overflow in 32 bit
2022-04-08 04:54:11 +00:00
var data: StUint[64]
data = fromHex(StUint[64], j.getStr)
result = cast[int64](data)
2018-12-31 03:28:12 +00:00
proc verifyStateDB*(wantedState: JsonNode, stateDB: ReadOnlyStateDB) =
for ac, accountData in wantedState:
let account = ethAddressFromHex(ac)
for slot, value in accountData{"storage"}:
let
slotId = UInt256.fromHex slot
wantedValue = UInt256.fromHex value.getStr
2020-05-30 03:14:59 +00:00
let actualValue = stateDB.getStorage(account, slotId)
#if not found:
# raise newException(ValidationError, "account not found: " & ac)
2019-09-23 15:50:13 +00:00
if actualValue != wantedValue:
2019-09-25 13:05:33 +00:00
raise newException(ValidationError, &"{ac} storageDiff: [{slot}] {actualValue.toHex} != {wantedValue.toHex}")
let
wantedCode = hexToSeqByte(accountData{"code"}.getStr)
wantedBalance = UInt256.fromHex accountData{"balance"}.getStr
2019-09-23 15:50:13 +00:00
wantedNonce = accountData{"nonce"}.getHexadecimalInt.AccountNonce
actualCode = stateDB.getCode(account)
actualBalance = stateDB.getBalance(account)
actualNonce = stateDB.getNonce(account)
2019-09-23 15:50:13 +00:00
if wantedCode != actualCode:
2021-01-13 11:17:38 +00:00
raise newException(ValidationError, &"{ac} codeDiff {wantedCode.toHex} != {actualCode.toHex}")
2019-09-23 15:50:13 +00:00
if wantedBalance != actualBalance:
2019-09-25 13:05:33 +00:00
raise newException(ValidationError, &"{ac} balanceDiff {wantedBalance.toHex} != {actualBalance.toHex}")
2019-09-23 15:50:13 +00:00
if wantedNonce != actualNonce:
2019-09-25 13:05:33 +00:00
raise newException(ValidationError, &"{ac} nonceDiff {wantedNonce.toHex} != {actualNonce.toHex}")
proc setupEthNode*(
conf: NimbusConf, ctx: EthContext,
capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode =
let keypair = ctx.getNetKeys(conf.netKey, conf.dataDir.string).tryGet()
let srvAddress = Address(
ip: conf.listenAddress, tcpPort: conf.tcpPort, udpPort: conf.udpPort)
var node = newEthereumNode(
keypair, srvAddress,
conf.networkId,
conf.agentString,
addAllCapabilities = false,
bindUdpPort = conf.udpPort, bindTcpPort = conf.tcpPort)
2019-04-24 20:45:29 +00:00
for capability in capabilities:
node.addCapability capability
node
proc makeTestConfig*(): NimbusConf =
# commandLineParams() will not works inside all_tests
makeConfig(@[])