-
Notifications
You must be signed in to change notification settings - Fork 107
/
test_generalstate_json.nim
211 lines (184 loc) · 7.59 KB
/
test_generalstate_json.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Nimbus
# Copyright (c) 2018 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
unittest2, strformat, strutils, tables, json, times, os,
stew/ranges/typedranges, nimcrypto, options,
eth/[rlp, common], eth/trie/[db, trie_defs], chronicles,
./test_helpers, ./test_allowed_to_fail,
../nimbus/p2p/executor, test_config,
../nimbus/transaction,
../nimbus/[vm_state, vm_types, vm_state_transactions, utils],
../nimbus/vm/interpreter,
../nimbus/db/[db_chain, state_db]
type
Tester = object
name: string
header: BlockHeader
pre: JsonNode
tx: Transaction
expectedHash: string
expectedLogs: string
fork: Fork
debugMode: bool
trace: bool
index: int
GST_VMState = ref object of BaseVMState
proc toBytes(x: string): seq[byte] =
result = newSeq[byte](x.len)
for i in 0..<x.len: result[i] = x[i].byte
proc newGST_VMState(prevStateRoot: Hash256, header: BlockHeader, chainDB: BaseChainDB, tracerFlags: set[TracerFlags]): GST_VMState =
new result
result.init(prevStateRoot, header, chainDB, tracerFlags)
method getAncestorHash*(vmState: GST_VMState, blockNumber: BlockNumber): Hash256 {.gcsafe.} =
if blockNumber >= vmState.blockNumber:
return
elif blockNumber < 0:
return
elif blockNumber < vmState.blockNumber - 256:
return
else:
return keccakHash(toBytes($blockNumber))
proc dumpAccount(accountDb: ReadOnlyStateDB, address: EthAddress, name: string): JsonNode =
result = %{
"name": %name,
"address": %($address),
"nonce": %toHex(accountDb.getNonce(address)),
"balance": %accountDb.getBalance(address).toHex(),
"codehash": %($accountDb.getCodeHash(address)),
"storageRoot": %($accountDb.getStorageRoot(address))
}
proc dumpDebugData(tester: Tester, vmState: BaseVMState, sender: EthAddress, gasUsed: GasInt, success: bool) =
let recipient = tester.tx.getRecipient()
let miner = tester.header.coinbase
var accounts = newJObject()
accounts[$miner] = dumpAccount(vmState.readOnlyStateDB, miner, "miner")
accounts[$sender] = dumpAccount(vmState.readOnlyStateDB, sender, "sender")
accounts[$recipient] = dumpAccount(vmState.readOnlyStateDB, recipient, "recipient")
let accountList = [sender, miner, recipient]
var i = 0
for ac, _ in tester.pre:
let account = ethAddressFromHex(ac)
if account notin accountList:
accounts[$account] = dumpAccount(vmState.readOnlyStateDB, account, "pre" & $i)
inc i
let tracingResult = if tester.trace: vmState.getTracingResult() else: %[]
let debugData = %{
"gasUsed": %gasUsed,
"structLogs": tracingResult,
"accounts": accounts
}
let status = if success: "_success" else: "_failed"
writeFile("debug_" & tester.name & "_" & $tester.index & status & ".json", debugData.pretty())
proc testFixtureIndexes(tester: Tester, testStatusIMPL: var TestStatus) =
var tracerFlags: set[TracerFlags] = if tester.trace: {TracerFlags.EnableTracing} else : {}
# TODO: implement journalDB in AccountStateDB
# then turn on state trie pruning
var vmState = newGST_VMState(emptyRlpHash, tester.header, newBaseChainDB(newMemoryDb(), false), tracerFlags)
var gasUsed: GasInt
let sender = tester.tx.getSender()
vmState.mutateStateDB:
setupStateDB(tester.pre, db)
vmState.accountDB.updateOriginalRoot()
defer:
let obtainedHash = "0x" & `$`(vmState.readOnlyStateDB.rootHash).toLowerAscii
check obtainedHash == tester.expectedHash
let logEntries = vmState.getAndClearLogEntries()
let actualLogsHash = hashLogEntries(logEntries)
let expectedLogsHash = toLowerAscii(tester.expectedLogs)
check(expectedLogsHash == actualLogsHash)
if tester.debugMode:
let success = expectedLogsHash == actualLogsHash and obtainedHash == tester.expectedHash
tester.dumpDebugData(vmState, sender, gasUsed, success)
if not validateTransaction(vmState, tester.tx, sender, tester.fork):
vmState.mutateStateDB:
# pre-EIP158 (e.g., Byzantium) should ensure currentCoinbase exists
# in later forks, don't create at all
db.addBalance(tester.header.coinbase, 0.u256)
# TODO: this feels not right to be here
# perhaps the entire validateTransaction block
# should be moved into processTransaction
if tester.fork >= FkSpurious:
let miner = tester.header.coinbase
let touchedAccounts = [miner]
for account in touchedAccounts:
debug "state clearing", account
if db.accountExists(account) and db.isEmptyAccount(account):
db.deleteAccount(account)
return
gasUsed = tester.tx.processTransaction(sender, vmState, tester.fork)
proc testFixture(fixtures: JsonNode, testStatusIMPL: var TestStatus,
trace = false, debugMode = false, supportedForks: set[Fork] = supportedForks) =
var tester: Tester
var fixture: JsonNode
for label, child in fixtures:
fixture = child
tester.name = label
break
let fenv = fixture["env"]
tester.header = BlockHeader(
coinbase: fenv["currentCoinbase"].getStr.ethAddressFromHex,
difficulty: fromHex(UInt256, fenv{"currentDifficulty"}.getStr),
blockNumber: fenv{"currentNumber"}.getHexadecimalInt.u256,
gasLimit: fenv{"currentGasLimit"}.getHexadecimalInt.GasInt,
timestamp: fenv{"currentTimestamp"}.getHexadecimalInt.int64.fromUnix,
stateRoot: emptyRlpHash
)
let specifyIndex = getConfiguration().index
tester.trace = trace
tester.debugMode = debugMode
let ftrans = fixture["transaction"]
var testedInFork = false
for fork in supportedForks:
if fixture["post"].hasKey(forkNames[fork]):
for expectation in fixture["post"][forkNames[fork]]:
inc tester.index
if specifyIndex > 0 and tester.index != specifyIndex:
continue
testedInFork = true
tester.expectedHash = expectation["hash"].getStr
tester.expectedLogs = expectation["logs"].getStr
let
indexes = expectation["indexes"]
dataIndex = indexes["data"].getInt
gasIndex = indexes["gas"].getInt
valueIndex = indexes["value"].getInt
tester.tx = ftrans.getFixtureTransaction(dataIndex, gasIndex, valueIndex)
tester.pre = fixture["pre"]
tester.fork = fork
testFixtureIndexes(tester, testStatusIMPL)
if not testedInFork:
echo "test subject '", tester.name, "' not tested in any forks"
proc generalStateJsonMain*(debugMode = false) =
if paramCount() == 0 or not debugMode:
# run all test fixtures
#suite "generalstate json tests":
#jsonTest("GeneralStateTests", testFixture, skipGSTTests)
suite "new generalstate json tests":
jsonTest("newGeneralStateTests", testFixture, skipNewGSTTests)
else:
# execute single test in debug mode
let config = getConfiguration()
if config.testSubject.len == 0:
echo "missing test subject"
quit(QuitFailure)
let path = "tests" / "fixtures" / "newGeneralStateTests"
let n = json.parseFile(path / config.testSubject)
var testStatusIMPL: TestStatus
var forks: set[Fork] = {}
forks.incl config.fork
testFixture(n, testStatusIMPL, config.trace, true, forks)
when isMainModule:
var message: string
## Processing command line arguments
if processArguments(message) != Success:
echo message
quit(QuitFailure)
else:
if len(message) > 0:
echo message
quit(QuitSuccess)
generalStateJsonMain(true)