-
Notifications
You must be signed in to change notification settings - Fork 759
/
GeneralStateTestsRunner.js
165 lines (150 loc) · 5.01 KB
/
GeneralStateTestsRunner.js
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
const async = require('async')
const testUtil = require('./util')
const Trie = require('merkle-patricia-tree/secure')
const ethUtil = require('ethereumjs-util')
const Account = require('ethereumjs-account').default
const BN = ethUtil.BN
const { getRequiredForkConfigAlias } = require('./util')
function parseTestCases (forkConfig, testData, data, gasLimit, value) {
let testCases = []
if (testData['post'][forkConfig]) {
testCases = testData['post'][forkConfig].map(testCase => {
let testIndexes = testCase['indexes']
let tx = { ...testData.transaction }
if (data !== undefined && testIndexes['data'] !== data) {
return null
}
if (value !== undefined && testIndexes['value'] !== value) {
return null
}
if (gasLimit !== undefined && testIndexes['gas'] !== gasLimit) {
return null
}
tx.data = testData.transaction.data[testIndexes['data']]
tx.gasLimit = testData.transaction.gasLimit[testIndexes['gas']]
tx.value = testData.transaction.value[testIndexes['value']]
return {
'transaction': tx,
'postStateRoot': testCase['hash'],
'env': testData['env'],
'pre': testData['pre']
}
})
}
testCases = testCases.filter(testCase => {
return testCase != null
})
return testCases
}
function runTestCase (options, testData, t, cb) {
const state = new Trie()
let block, vm
async.series([
function (done) {
var VM
if (options.dist) {
VM = require('../dist/index.js').default
} else {
VM = require('../lib/index').default
}
vm = new VM({
state: state,
hardfork: options.forkConfig.toLowerCase()
})
testUtil.setupPreConditions(state, testData, done)
},
function (done) {
var tx = testUtil.makeTx(testData.transaction)
block = testUtil.makeBlockFromEnv(testData.env)
tx._homestead = true
tx.enableHomestead = true
block.isHomestead = function () {
return true
}
if (!tx.validate()) {
return done()
}
if (options.jsontrace) {
vm.on('step', function (e) {
let hexStack = []
hexStack = e.stack.map(item => {
return '0x' + new BN(item).toString(16, 0)
})
var opTrace = {
'pc': e.pc,
'op': e.opcode.opcode,
'gas': '0x' + e.gasLeft.toString('hex'),
'gasCost': '0x' + e.opcode.fee.toString(16),
'stack': hexStack,
'depth': e.depth,
'opName': e.opcode.name
}
t.comment(JSON.stringify(opTrace))
})
vm.on('afterTx', function (results) {
let stateRoot = {
'stateRoot': vm.stateManager._trie.root.toString('hex')
}
t.comment(JSON.stringify(stateRoot))
})
}
vm.runTx({ tx: tx, block: block })
.then(() => done())
.catch((err) => {
// If tx is invalid and coinbase is empty, the test harness
// expects the coinbase account to be deleted from state.
// Without this ecmul_0-3_5616_28000_96 would fail.
vm.stateManager.getAccount(block.header.coinbase, function (err, account) {
if (err) {
done()
return
}
if (new BN(account.balance).isZero()) {
async.series([
(cb) => vm.stateManager.putAccount(block.header.coinbase, new Account(), cb),
(cb) => vm.stateManager.cleanupTouchedAccounts(cb),
(cb) => vm.stateManager._cache.flush(cb)
], (err) => {
err = null
done()
})
} else {
done()
}
})
})
},
function (done) {
if (testData.postStateRoot.substr(0, 2) === '0x') {
testData.postStateRoot = testData.postStateRoot.substr(2)
}
t.equal(state.root.toString('hex'), testData.postStateRoot, 'the state roots should match')
if (state.root.toString('hex') !== testData.postStateRoot.toString('hex')) {
// since General State Tests, postState keys are no longer included in
// the state test format. only postStateRoot, so can't debug expected post conditions
// testUtil.verifyPostConditions(state, testData.post, t, done)
done()
} else {
done()
}
}
], cb)
}
module.exports = function runStateTest (options, testData, t, cb) {
const forkConfig = getRequiredForkConfigAlias(options.forkConfig)
try {
const testCases = parseTestCases(forkConfig, testData, options.data, options.gasLimit, options.value)
if (testCases.length > 0) {
async.eachSeries(testCases,
(testCase, done) => runTestCase(options, testCase, t, done),
cb)
} else {
t.comment(`No ${forkConfig} post state defined, skip test`)
cb()
}
} catch (e) {
t.fail('error running test case for fork: ' + forkConfig)
console.log('error:', e)
cb()
}
}