Skip to content

Commit 3a0af90

Browse files
committed
update StateTestsRunner for GeneralStateTests format
1 parent 4547a14 commit 3a0af90

File tree

4 files changed

+86
-62
lines changed

4 files changed

+86
-62
lines changed

tests/GeneralStateTestsRunner.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const async = require('async')
2+
const VM = require('../index.js')
3+
const testUtil = require('./util')
4+
const Trie = require('merkle-patricia-tree/secure')
5+
6+
function parseTestCases (forkConfig, testData) {
7+
const testCases = testData['post'][forkConfig].map(testCase => {
8+
let testIndexes = testCase['indexes']
9+
let tx = Object.assign({}, testData.transaction)
10+
tx.data = testData.transaction.data[testIndexes['data']]
11+
tx.gasLimit = testData.transaction.gasLimit[testIndexes['gas']]
12+
tx.value = testData.transaction.value[testIndexes['value']]
13+
return {
14+
'transaction': tx,
15+
'postStateRoot': testCase['hash'],
16+
'env': testData['env'],
17+
'pre': testData['pre']
18+
}
19+
})
20+
21+
return testCases
22+
}
23+
24+
function runTestCase (testData, t, cb) {
25+
const state = new Trie()
26+
let block, vm
27+
28+
async.series([
29+
function (done) {
30+
vm = new VM({
31+
state: state,
32+
enableHomestead: true
33+
})
34+
testUtil.setupPreConditions(state, testData, done)
35+
},
36+
function (done) {
37+
var tx = testUtil.makeTx(testData.transaction)
38+
block = testUtil.makeBlockFromEnv(testData.env)
39+
if (!block.isHomestead() && !testData.homestead) {
40+
tx._homestead = false
41+
} else {
42+
block.isHomestead = function () {
43+
return true
44+
}
45+
}
46+
47+
if (tx.validate()) {
48+
vm.runTx({
49+
tx: tx,
50+
block: block
51+
}, function (err, r) {
52+
err = null
53+
done()
54+
})
55+
} else {
56+
done()
57+
}
58+
},
59+
function (done) {
60+
if (testData.postStateRoot.substr(0, 2) === '0x') {
61+
testData.postStateRoot = testData.postStateRoot.substr(2)
62+
}
63+
t.equal(state.root.toString('hex'), testData.postStateRoot, 'the state roots should match')
64+
65+
if (state.root.toString('hex') !== testData.postStateRoot.toString('hex')) {
66+
// since General State Tests, postState keys are no longer included in
67+
// the state test format. only postStateRoot, so can't debug expected post conditions
68+
// testUtil.verifyPostConditions(state, testData.post, t, done)
69+
done()
70+
} else {
71+
done()
72+
}
73+
}
74+
], cb)
75+
}
76+
77+
module.exports = function runStateTest (options, testData, t, cb) {
78+
const testCases = parseTestCases(options.forkConfig, testData)
79+
async.eachSeries(testCases,
80+
(testCase, done) => runTestCase(testCase, t, done),
81+
cb)
82+
}

tests/StateTestsRunner.js

Lines changed: 0 additions & 58 deletions
This file was deleted.

tests/tester.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const skipVM = [
8282
if (argv.r) {
8383
randomized(argv.r, argv.v)
8484
} else if (argv.s) {
85-
runTests('StateTests', argv)
85+
runTests('GeneralStateTests', argv)
8686
} else if (argv.v) {
8787
runTests('VMTests', argv)
8888
} else if (argv.b) {
@@ -156,7 +156,7 @@ function runAll () {
156156
require('./genesishashes.js')
157157
async.series([
158158
runTests.bind(this, 'VMTests', {}),
159-
//runTests.bind(this, 'StateTests', {}), // TODO: update StateTestsRunnner for GeneralStateTests
159+
runTests.bind(this, 'GeneralStateTests', {}),
160160
runTests.bind(this, 'BlockchainTests', {})
161161
])
162162
}

tests/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ exports.makeTx = function (txData) {
6262
tx.value = format(txData.value)
6363
tx.data = format(txData.data, false, true) // slice off 0x
6464
if (txData.secretKey) {
65-
var privKey = new Buffer(txData.secretKey, 'hex')
65+
var privKey = format(txData.secretKey, false, true)
6666
tx.sign(privKey)
6767
} else {
6868
tx.v = new Buffer(txData.v.slice(2), 'hex')
@@ -254,7 +254,7 @@ exports.makeBlockHeader = function (data) {
254254
header.timestamp = format(data.currentTimestamp)
255255
header.gasLimit = format(data.currentGasLimit)
256256
if (data.previousHash) {
257-
header.parentHash = new Buffer(data.previousHash, 'hex')
257+
header.parentHash = format(data.previousHash, false, true)
258258
}
259259
header.coinbase = utils.setLength(format(data.currentCoinbase, false, true), 20)
260260
header.difficulty = format(data.currentDifficulty)

0 commit comments

Comments
 (0)