|
| 1 | +const tape = require('tape') |
| 2 | +const async = require('async') |
| 3 | +const VM = require('../') |
| 4 | +const Account = require('ethereumjs-account') |
| 5 | +const testUtil = require('./util') |
| 6 | +const Trie = require('merkle-patricia-tree/secure') |
| 7 | +const ethUtil = require('ethereumjs-util') |
| 8 | +const BN = ethUtil.BN |
| 9 | + |
| 10 | +var testCases = [ |
| 11 | + { code: '0x60006000556000600055', usedGas: 412, refund: 0, original: '0x' }, |
| 12 | + { code: '0x60006000556001600055', usedGas: 20212, refund: 0, original: '0x' }, |
| 13 | + { code: '0x60016000556000600055', usedGas: 20212, refund: 19800, original: '0x' }, |
| 14 | + { code: '0x60016000556002600055', usedGas: 20212, refund: 0, original: '0x' }, |
| 15 | + { code: '0x60016000556001600055', usedGas: 20212, refund: 0, original: '0x' }, |
| 16 | + { code: '0x60006000556000600055', usedGas: 5212, refund: 15000, original: '0x01' }, |
| 17 | + { code: '0x60006000556001600055', usedGas: 5212, refund: 4800, original: '0x01' }, |
| 18 | + { code: '0x60006000556002600055', usedGas: 5212, refund: 0, original: '0x01' }, |
| 19 | + { code: '0x60026000556000600055', usedGas: 5212, refund: 15000, original: '0x01' }, |
| 20 | + { code: '0x60026000556003600055', usedGas: 5212, refund: 0, original: '0x01' }, |
| 21 | + { code: '0x60026000556001600055', usedGas: 5212, refund: 4800, original: '0x01' }, |
| 22 | + { code: '0x60026000556002600055', usedGas: 5212, refund: 0, original: '0x01' }, |
| 23 | + { code: '0x60016000556000600055', usedGas: 5212, refund: 15000, original: '0x01' }, |
| 24 | + { code: '0x60016000556002600055', usedGas: 5212, refund: 0, original: '0x01' }, |
| 25 | + { code: '0x60016000556001600055', usedGas: 412, refund: 0, original: '0x01' }, |
| 26 | + { code: '0x600160005560006000556001600055', usedGas: 40218, refund: 19800, original: '0x' }, |
| 27 | + { code: '0x600060005560016000556000600055', usedGas: 10218, refund: 19800, original: '0x01' } |
| 28 | +] |
| 29 | + |
| 30 | +var testData = { |
| 31 | + 'env': { |
| 32 | + 'currentCoinbase': '0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba', |
| 33 | + 'currentDifficulty': '0x0100', |
| 34 | + 'currentGasLimit': '0x0f4240', |
| 35 | + 'currentNumber': '0x00', |
| 36 | + 'currentTimestamp': '0x01' |
| 37 | + }, |
| 38 | + 'exec': { |
| 39 | + 'address': '0x01', |
| 40 | + 'caller': '0xcd1722f3947def4cf144679da39c4c32bdc35681', |
| 41 | + 'code': '0x60006000556000600055', |
| 42 | + 'data': '0x', |
| 43 | + 'gas': '0', |
| 44 | + 'gasPrice': '0x5af3107a4000', |
| 45 | + 'origin': '0xcd1722f3947def4cf144679da39c4c32bdc35681', |
| 46 | + 'value': '0x0de0b6b3a7640000' |
| 47 | + }, |
| 48 | + 'gas': '0', |
| 49 | + 'pre': { |
| 50 | + '0x01': { |
| 51 | + 'balance': '0x152d02c7e14af6800000', |
| 52 | + 'code': '0x', |
| 53 | + 'nonce': '0x00', |
| 54 | + 'storage': { |
| 55 | + '0x': '0' |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +tape('test constantinople SSTORE (eip-1283)', function (t) { |
| 62 | + testCases.forEach(function (params, i) { |
| 63 | + t.test('should correctly run eip-1283 test #' + i, function (st) { |
| 64 | + let state = new Trie() |
| 65 | + let results |
| 66 | + let account |
| 67 | + |
| 68 | + testData.exec.code = params.code |
| 69 | + testData.exec.gas = params.usedGas |
| 70 | + testData.pre['0x01'].storage['0x'] = params.original |
| 71 | + |
| 72 | + async.series([ |
| 73 | + function (done) { |
| 74 | + let acctData = testData.pre[testData.exec.address] |
| 75 | + account = new Account() |
| 76 | + account.nonce = testUtil.format(acctData.nonce) |
| 77 | + account.balance = testUtil.format(acctData.balance) |
| 78 | + testUtil.setupPreConditions(state, testData, done) |
| 79 | + }, |
| 80 | + function (done) { |
| 81 | + state.get(Buffer.from(testData.exec.address, 'hex'), function (err, data) { |
| 82 | + let a = new Account(data) |
| 83 | + account.stateRoot = a.stateRoot |
| 84 | + done(err) |
| 85 | + }) |
| 86 | + }, |
| 87 | + function (done) { |
| 88 | + let block = testUtil.makeBlockFromEnv(testData.env) |
| 89 | + let vm = new VM({state: state, hardfork: 'constantinople'}) |
| 90 | + let runCodeData = testUtil.makeRunCodeData(testData.exec, account, block) |
| 91 | + vm.runCode(runCodeData, function (err, r) { |
| 92 | + if (r) { |
| 93 | + results = r |
| 94 | + } |
| 95 | + done(err) |
| 96 | + }) |
| 97 | + }, |
| 98 | + function (done) { |
| 99 | + if (testData.gas) { |
| 100 | + let actualGas = results.gas.toString() |
| 101 | + let expectedGas = new BN(testUtil.format(testData.gas)).toString() |
| 102 | + t.equal(actualGas, expectedGas, 'valid gas usage') |
| 103 | + t.equals(results.gasRefund.toNumber(), params.refund, 'valid gas refund') |
| 104 | + } |
| 105 | + done() |
| 106 | + } |
| 107 | + ], function (err) { |
| 108 | + t.assert(!err) |
| 109 | + st.end() |
| 110 | + }) |
| 111 | + }) |
| 112 | + }) |
| 113 | +}) |
0 commit comments