|
| 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 | +} |
0 commit comments