-
Notifications
You must be signed in to change notification settings - Fork 106
/
test_helpers.nim
163 lines (137 loc) · 5.98 KB
/
test_helpers.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Nimbus
# Copyright (c) 2018-2023 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http:https://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http:https://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import
std/[os, macros, json, strformat, strutils, tables],
stew/byteutils, net, eth/[keys, p2p], unittest2,
testutils/markdown_reports,
../nimbus/[constants, config, transaction, errors],
../nimbus/db/ledger,
../nimbus/common/[context, common]
func revmap(x: Table[EVMFork, string]): Table[string, EVMFork] =
result = initTable[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",
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
symbol {.used.} = newIdentNode"symbol"
final {.used.} = newIdentNode"final"
name {.used.} = newIdentNode"name"
formatted {.used.} = newStrLitNode"{symbol[final]} {name:<64}{$final}{'\n'}"
result = quote:
var filenames: seq[string] = @[]
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
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
let fixtures = parseJson(readFile(filename))
`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()
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)
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
var data: StUint[64]
data = fromHex(StUint[64], j.getStr)
result = cast[int64](data)
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
let actualValue = stateDB.getStorage(account, slotId)
#if not found:
# raise newException(ValidationError, "account not found: " & ac)
if actualValue != wantedValue:
raise newException(ValidationError, &"{ac} storageDiff: [{slot}] {actualValue.toHex} != {wantedValue.toHex}")
let
wantedCode = hexToSeqByte(accountData{"code"}.getStr)
wantedBalance = UInt256.fromHex accountData{"balance"}.getStr
wantedNonce = accountData{"nonce"}.getHexadecimalInt.AccountNonce
actualCode = stateDB.getCode(account)
actualBalance = stateDB.getBalance(account)
actualNonce = stateDB.getNonce(account)
if wantedCode != actualCode:
raise newException(ValidationError, &"{ac} codeDiff {wantedCode.toHex} != {actualCode.toHex}")
if wantedBalance != actualBalance:
raise newException(ValidationError, &"{ac} balanceDiff {wantedBalance.toHex} != {actualBalance.toHex}")
if wantedNonce != actualNonce:
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)
for capability in capabilities:
node.addCapability capability
node
proc makeTestConfig*(): NimbusConf =
# commandLineParams() will not works inside all_tests
makeConfig(@[])