2018-04-06 14:52:10 +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.
|
|
|
|
|
2018-01-17 11:24:09 +00:00
|
|
|
import
|
2019-11-13 14:49:39 +00:00
|
|
|
os, macros, json, strformat, strutils, parseutils, os, tables,
|
2020-07-21 06:15:06 +00:00
|
|
|
stew/byteutils, net, eth/[common, keys, rlp, p2p], unittest2,
|
2020-03-10 11:30:27 +00:00
|
|
|
testutils/markdown_reports,
|
2021-06-01 10:54:13 +00:00
|
|
|
../nimbus/[constants, config, transaction, utils, errors, forks],
|
2020-07-20 06:50:05 +00:00
|
|
|
../nimbus/db/accounts_cache,
|
2021-09-07 13:45:01 +00:00
|
|
|
../nimbus/context
|
2018-01-17 11:24:09 +00:00
|
|
|
|
2019-08-26 15:19:18 +00:00
|
|
|
func revmap(x: Table[Fork, string]): Table[string, Fork] =
|
|
|
|
result = initTable[string, Fork]()
|
|
|
|
for k, v in x:
|
|
|
|
result[v] = k
|
|
|
|
|
2018-11-28 19:02:21 +00:00
|
|
|
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",
|
2021-01-06 10:02:19 +00:00
|
|
|
FkIstanbul: "Istanbul",
|
2021-06-30 13:41:29 +00:00
|
|
|
FkBerlin: "Berlin",
|
|
|
|
FkLondon: "London"
|
2018-11-28 19:02:21 +00:00
|
|
|
}.toTable
|
|
|
|
|
2019-11-13 11:39:35 +00:00
|
|
|
supportedForks* = {
|
|
|
|
FkFrontier,
|
|
|
|
FkHomestead,
|
|
|
|
FkTangerine,
|
|
|
|
FkSpurious,
|
|
|
|
FkByzantium,
|
|
|
|
FkConstantinople,
|
2020-03-04 09:32:30 +00:00
|
|
|
FkPetersburg,
|
2021-01-06 10:02:19 +00:00
|
|
|
FkIstanbul,
|
2021-06-30 13:41:29 +00:00
|
|
|
FkBerlin,
|
|
|
|
FkLondon}
|
2018-11-28 19:02:21 +00:00
|
|
|
|
2019-08-26 15:19:18 +00:00
|
|
|
nameToFork* = revmap(forkNames)
|
|
|
|
|
2019-11-20 15:25:09 +00:00
|
|
|
func skipNothing*(folder: string, name: string): bool = false
|
2018-02-27 18:10:34 +00:00
|
|
|
|
2019-03-15 15:34:05 +00:00
|
|
|
proc lacksSupportedForks*(fixtures: JsonNode): bool =
|
2019-05-30 16:42:55 +00:00
|
|
|
# XXX: Until Nimbus supports all forks, some of the GeneralStateTests won't work.
|
2019-03-15 15:34:05 +00:00
|
|
|
|
2018-09-12 16:33:53 +00:00
|
|
|
var fixture: JsonNode
|
|
|
|
for label, child in fixtures:
|
|
|
|
fixture = child
|
|
|
|
break
|
|
|
|
|
2018-11-28 19:02:21 +00:00
|
|
|
# not all fixtures make a distinction between forks, so default to accepting
|
|
|
|
# them all, until we find the ones that specify forks in their "post" section
|
|
|
|
result = false
|
2019-03-15 15:34:05 +00:00
|
|
|
if fixture.kind == JObject and fixture.hasKey("transaction") and fixture.hasKey("post"):
|
2018-11-28 19:02:21 +00:00
|
|
|
result = true
|
|
|
|
for fork in supportedForks:
|
2019-03-15 15:34:05 +00:00
|
|
|
if fixture["post"].hasKey(forkNames[fork]):
|
2018-11-28 19:02:21 +00:00
|
|
|
result = false
|
|
|
|
break
|
2018-09-12 16:33:53 +00:00
|
|
|
|
2019-05-30 16:42:55 +00:00
|
|
|
var status = initOrderedTable[string, OrderedTable[string, Status]]()
|
|
|
|
|
2020-04-21 03:44:35 +00:00
|
|
|
proc jsonTestImpl*(inputFolder, outputName: string, handler, skipTest: NimNode): NimNode {.compileTime.} =
|
2018-07-06 15:08:31 +00:00
|
|
|
let
|
|
|
|
testStatusIMPL = ident("testStatusIMPL")
|
2019-05-30 16:42:55 +00:00
|
|
|
testName = ident("testName")
|
2018-07-06 15:08:31 +00:00
|
|
|
# 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-11-28 19:02:21 +00:00
|
|
|
|
2018-02-13 17:18:08 +00:00
|
|
|
result = quote:
|
2019-05-30 16:42:55 +00:00
|
|
|
var filenames: seq[string] = @[]
|
2020-04-21 03:44:35 +00:00
|
|
|
for filename in walkDirRec("tests" / "fixtures" / `inputFolder`):
|
2018-11-28 19:02:21 +00:00
|
|
|
if not filename.endsWith(".json"):
|
|
|
|
continue
|
2018-02-27 18:10:34 +00:00
|
|
|
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
|
2019-11-20 15:25:09 +00:00
|
|
|
if `skipTest`(last, name):
|
|
|
|
continue
|
|
|
|
filenames.add(filename)
|
2021-04-08 09:29:25 +00:00
|
|
|
|
|
|
|
doAssert(filenames.len > 0)
|
2019-05-30 16:42:55 +00:00
|
|
|
for fname in filenames:
|
|
|
|
test fname:
|
|
|
|
{.gcsafe.}:
|
|
|
|
let
|
|
|
|
filename = `testName` # the first argument passed to the `test` template
|
|
|
|
(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))
|
2019-05-30 16:42:55 +00:00
|
|
|
if fixtures.lacksSupportedForks:
|
|
|
|
status[last][name] = Status.Skip
|
|
|
|
skip()
|
|
|
|
else:
|
2020-07-21 06:15:06 +00:00
|
|
|
# when not paralleliseTests:
|
|
|
|
# echo filename
|
2019-05-30 16:42:55 +00:00
|
|
|
`handler`(fixtures, `testStatusIMPL`)
|
|
|
|
if `testStatusIMPL` == OK:
|
|
|
|
status[last][name] = Status.OK
|
2021-05-16 18:23:18 +00:00
|
|
|
elif `testStatusIMPL` == SKIPPED:
|
|
|
|
status[last][name] = Status.Skip
|
2019-05-30 16:42:55 +00:00
|
|
|
|
|
|
|
suiteTeardown:
|
|
|
|
status.sort do (a: (string, OrderedTable[string, Status]),
|
|
|
|
b: (string, OrderedTable[string, Status])) -> int: cmp(a[0], b[0])
|
|
|
|
|
2020-04-21 03:44:35 +00:00
|
|
|
generateReport(`outputName`, status)
|
2019-05-30 16:42:55 +00:00
|
|
|
status.clear()
|
2018-01-17 11:24:09 +00:00
|
|
|
|
2020-04-21 03:44:35 +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)
|
|
|
|
|
2018-09-13 15:00:23 +00:00
|
|
|
func ethAddressFromHex*(s: string): EthAddress = hexToByteArray(s, result)
|
2018-05-30 16:11:15 +00:00
|
|
|
|
2018-09-15 20:39:30 +00:00
|
|
|
func safeHexToSeqByte*(hexStr: string): seq[byte] =
|
|
|
|
if hexStr == "":
|
|
|
|
@[]
|
|
|
|
else:
|
|
|
|
hexStr.hexToSeqByte
|
|
|
|
|
2019-08-17 14:48:45 +00:00
|
|
|
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)
|
2019-08-17 14:48:45 +00:00
|
|
|
result = cast[int64](data)
|
|
|
|
|
2022-01-18 16:19:32 +00:00
|
|
|
proc setupStateDB*(wantedState: JsonNode, stateDB: AccountsCache) =
|
2018-07-05 12:41:01 +00:00
|
|
|
for ac, accountData in wantedState:
|
|
|
|
let account = ethAddressFromHex(ac)
|
2018-02-13 17:18:08 +00:00
|
|
|
for slot, value in accountData{"storage"}:
|
2018-09-04 12:30:15 +00:00
|
|
|
stateDB.setStorage(account, fromHex(UInt256, slot), fromHex(UInt256, value.getStr))
|
|
|
|
|
2019-08-17 14:48:45 +00:00
|
|
|
let nonce = accountData{"nonce"}.getHexadecimalInt.AccountNonce
|
2020-04-20 18:12:44 +00:00
|
|
|
let code = accountData{"code"}.getStr.safeHexToSeqByte
|
2018-07-17 08:57:14 +00:00
|
|
|
let balance = UInt256.fromHex accountData{"balance"}.getStr
|
2018-01-17 11:24:09 +00:00
|
|
|
|
2018-02-13 17:18:08 +00:00
|
|
|
stateDB.setNonce(account, nonce)
|
|
|
|
stateDB.setCode(account, code)
|
|
|
|
stateDB.setBalance(account, balance)
|
2018-02-14 16:38:01 +00:00
|
|
|
|
2018-12-31 03:28:12 +00:00
|
|
|
proc verifyStateDB*(wantedState: JsonNode, stateDB: ReadOnlyStateDB) =
|
2018-07-05 12:41:01 +00:00
|
|
|
for ac, accountData in wantedState:
|
|
|
|
let account = ethAddressFromHex(ac)
|
|
|
|
for slot, value in accountData{"storage"}:
|
|
|
|
let
|
2018-07-17 08:57:14 +00:00
|
|
|
slotId = UInt256.fromHex slot
|
2018-07-05 12:41:01 +00:00
|
|
|
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}")
|
2018-07-05 12:41:01 +00:00
|
|
|
|
|
|
|
let
|
2020-04-20 18:12:44 +00:00
|
|
|
wantedCode = hexToSeqByte(accountData{"code"}.getStr)
|
2018-07-17 08:57:14 +00:00
|
|
|
wantedBalance = UInt256.fromHex accountData{"balance"}.getStr
|
2019-09-23 15:50:13 +00:00
|
|
|
wantedNonce = accountData{"nonce"}.getHexadecimalInt.AccountNonce
|
2018-07-05 12:41:01 +00:00
|
|
|
|
|
|
|
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}")
|
2018-07-05 12:41:01 +00:00
|
|
|
|
2021-05-15 06:37:40 +00:00
|
|
|
proc parseAccessList(n: JsonNode): AccessList =
|
|
|
|
if n.kind == JNull:
|
|
|
|
return
|
|
|
|
|
|
|
|
for x in n:
|
|
|
|
var ap = AccessPair(
|
|
|
|
address: parseAddress(x["address"].getStr)
|
|
|
|
)
|
|
|
|
let sks = x["storageKeys"]
|
|
|
|
for sk in sks:
|
|
|
|
ap.storageKeys.add hexToByteArray[32](sk.getStr())
|
|
|
|
result.add ap
|
|
|
|
|
2018-09-19 16:46:14 +00:00
|
|
|
proc getFixtureTransaction*(j: JsonNode, dataIndex, gasIndex, valueIndex: int): Transaction =
|
2021-06-30 13:41:29 +00:00
|
|
|
let dynamicFeeTx = "gasPrice" notin j
|
2021-09-07 13:45:01 +00:00
|
|
|
let nonce = j["nonce"].getHexadecimalInt.AccountNonce
|
2021-05-15 06:37:40 +00:00
|
|
|
let gasLimit = j["gasLimit"][gasIndex].getHexadecimalInt
|
|
|
|
|
2021-06-27 04:19:43 +00:00
|
|
|
var toAddr: Option[EthAddress]
|
2021-05-17 05:41:58 +00:00
|
|
|
# Fixture transactions with `"to": ""` are contract creations.
|
|
|
|
#
|
|
|
|
# Fixture transactions with `"to": "0x..."` or `"to": "..."` where `...` are
|
|
|
|
# 40 hex digits are call/transfer transactions. Even if the digits are all
|
|
|
|
# zeros, because the all-zeros address is a legitimate account.
|
|
|
|
#
|
|
|
|
# There are no other formats. The number of digits if present is always 40,
|
|
|
|
# "0x" prefix is used in some but not all fixtures, and upper case hex digits
|
|
|
|
# occur in a few.
|
2018-09-11 13:15:59 +00:00
|
|
|
let rawTo = j["to"].getStr
|
2021-06-27 04:19:43 +00:00
|
|
|
if rawTo != "":
|
|
|
|
toAddr = some(rawTo.parseAddress)
|
2021-05-15 06:37:40 +00:00
|
|
|
|
2022-02-10 08:47:31 +00:00
|
|
|
let hexStr = j["value"][valueIndex].getStr
|
|
|
|
# stTransactionTest/ValueOverflow.json
|
|
|
|
# prevent parsing exception and subtitute it with max uint256
|
|
|
|
let value = if ':' in hexStr: high(UInt256) else: fromHex(UInt256, hexStr)
|
2021-05-15 06:37:40 +00:00
|
|
|
let payload = j["data"][dataIndex].getStr.safeHexToSeqByte
|
2018-09-05 23:48:06 +00:00
|
|
|
|
|
|
|
var secretKey = j["secretKey"].getStr
|
|
|
|
removePrefix(secretKey, "0x")
|
2020-04-05 13:12:48 +00:00
|
|
|
let privateKey = PrivateKey.fromHex(secretKey).tryGet()
|
2021-09-07 13:45:01 +00:00
|
|
|
|
2021-06-30 13:41:29 +00:00
|
|
|
if dynamicFeeTx:
|
|
|
|
let accList = j["accessLists"][dataIndex]
|
|
|
|
var tx = Transaction(
|
|
|
|
txType: TxEip1559,
|
|
|
|
nonce: nonce,
|
|
|
|
maxFee: j["maxFeePerGas"].getHexadecimalInt,
|
|
|
|
maxPriorityFee: j["maxPriorityFeePerGas"].getHexadecimalInt,
|
|
|
|
gasLimit: gasLimit,
|
|
|
|
to: toAddr,
|
|
|
|
value: value,
|
|
|
|
payload: payload,
|
|
|
|
accessList: parseAccessList(accList),
|
|
|
|
chainId: ChainId(1)
|
|
|
|
)
|
|
|
|
return signTransaction(tx, privateKey, ChainId(1), false)
|
2021-09-07 13:45:01 +00:00
|
|
|
|
2021-06-30 13:41:29 +00:00
|
|
|
let gasPrice = j["gasPrice"].getHexadecimalInt
|
2021-05-15 06:37:40 +00:00
|
|
|
if j.hasKey("accessLists"):
|
|
|
|
let accList = j["accessLists"][dataIndex]
|
2021-06-27 04:19:43 +00:00
|
|
|
var tx = Transaction(
|
|
|
|
txType: TxEip2930,
|
2021-05-15 06:37:40 +00:00
|
|
|
nonce: nonce,
|
|
|
|
gasPrice: gasPrice,
|
|
|
|
gasLimit: gasLimit,
|
|
|
|
to: toAddr,
|
|
|
|
value: value,
|
|
|
|
payload: payload,
|
|
|
|
accessList: parseAccessList(accList),
|
2021-06-27 04:19:43 +00:00
|
|
|
chainId: ChainId(1)
|
2021-05-15 06:37:40 +00:00
|
|
|
)
|
2021-06-27 04:19:43 +00:00
|
|
|
signTransaction(tx, privateKey, ChainId(1), false)
|
2021-05-15 06:37:40 +00:00
|
|
|
else:
|
2021-06-27 04:19:43 +00:00
|
|
|
var tx = Transaction(
|
|
|
|
txType: TxLegacy,
|
2021-05-15 06:37:40 +00:00
|
|
|
nonce: nonce,
|
|
|
|
gasPrice: gasPrice,
|
|
|
|
gasLimit: gasLimit,
|
|
|
|
to: toAddr,
|
|
|
|
value: value,
|
|
|
|
payload: payload
|
|
|
|
)
|
2021-06-27 04:19:43 +00:00
|
|
|
signTransaction(tx, privateKey, ChainId(1), false)
|
2019-03-18 04:27:32 +00:00
|
|
|
|
2019-03-18 01:55:02 +00:00
|
|
|
proc hashLogEntries*(logs: seq[Log]): string =
|
|
|
|
toLowerAscii("0x" & $keccakHash(rlp.encode(logs)))
|
2019-04-24 20:45:29 +00:00
|
|
|
|
2022-03-16 08:01:35 +00:00
|
|
|
proc setupEthNode*(
|
|
|
|
conf: NimbusConf, ctx: EthContext,
|
|
|
|
capabilities: varargs[ProtocolInfo, `protocolInfo`]): EthereumNode =
|
2021-09-11 14:58:01 +00:00
|
|
|
let keypair = ctx.hexToKeyPair(conf.nodeKeyHex).tryGet()
|
2022-03-16 08:01:35 +00:00
|
|
|
let srvAddress = Address(
|
|
|
|
ip: conf.listenAddress, tcpPort: conf.tcpPort, udpPort: conf.udpPort)
|
|
|
|
|
|
|
|
var node = newEthereumNode(
|
2021-09-11 14:58:01 +00:00
|
|
|
keypair, srvAddress,
|
2021-09-16 15:59:46 +00:00
|
|
|
conf.networkId,
|
2021-09-11 14:58:01 +00:00
|
|
|
nil, conf.agentString,
|
2022-03-16 08:01:35 +00:00
|
|
|
addAllCapabilities = false,
|
|
|
|
bindUdpPort = conf.udpPort, bindTcpPort = conf.tcpPort)
|
|
|
|
|
2019-04-24 20:45:29 +00:00
|
|
|
for capability in capabilities:
|
2022-03-16 08:01:35 +00:00
|
|
|
node.addCapability capability
|
|
|
|
|
|
|
|
node
|
2021-09-16 15:59:46 +00:00
|
|
|
|
|
|
|
proc makeTestConfig*(): NimbusConf =
|
|
|
|
# commandLineParams() will not works inside all_tests
|
|
|
|
makeConfig(@[])
|