Skip to content

Commit

Permalink
Merge pull request #272 from ethereumjs/update-tests
Browse files Browse the repository at this point in the history
[WIP] update to latest testing repo
  • Loading branch information
holgerd77 authored Jun 22, 2018
2 parents 4f1d427 + ed4022e commit 1394a2e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 43 deletions.
11 changes: 8 additions & 3 deletions lib/precompiled/05-modexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ function multComplexity (x) {
}

function getAdjustedExponentLength (data) {
var baseLen = new BN(data.slice(0, 32)).toNumber()
var expBytesStart
try {
var baseLen = new BN(data.slice(0, 32)).toNumber()
expBytesStart = 96 + baseLen // 96 for base length, then exponent length, and modulus length, then baseLen for the base data, then exponent bytes start
} catch (e) {
expBytesStart = Number.MAX_SAFE_INTEGER - 32
}
var expLen = new BN(data.slice(32, 64))
var expBytesStart = 96 + baseLen // 96 for base length, then exponent length, and modulus length, then baseLen for the base data, then exponent bytes start
var firstExpBytes = Buffer.from(data.slice(expBytesStart, expBytesStart + 32)) // first word of the exponent data
firstExpBytes = utils.setLengthRight(firstExpBytes, 32) // reading past the data reads virtual zeros
firstExpBytes = new BN(firstExpBytes)
Expand Down Expand Up @@ -105,7 +110,7 @@ module.exports = function (opts) {
}

if (mLen.isZero()) {
results.return = Buffer.from([0])
results.return = Buffer.alloc(0)
results.exception = 1
return results
}
Expand Down
15 changes: 10 additions & 5 deletions lib/runCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@ module.exports = function (opts, cb) {
txData = undefined
var newNonce = new BN(account.nonce).subn(1)
createdAddress = toAddress = ethUtil.generateAddress(caller, newNonce.toArray())
stateManager.getAccount(createdAddress, function (err, account) {
toAccount = account
const NONCE_OFFSET = 1
toAccount.nonce = new BN(toAccount.nonce).addn(NONCE_OFFSET).toArrayLike(Buffer)
done(err)
stateManager.clearContractStorage(createdAddress, function (err) {
if (err) {
done(err)
}

stateManager.getAccount(createdAddress, function (err, account) {
toAccount = account
toAccount.nonce = new BN(1).toArrayLike(Buffer)
done(err)
})
})
} else {
// else load the `to` account
Expand Down
33 changes: 24 additions & 9 deletions lib/stateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,14 @@ proto.getContractStorage = function (address, key, cb) {
})
}

proto.putContractStorage = function (address, key, value, cb) {
proto._modifyContractStorage = function (address, modifyTrie, cb) {
var self = this
self._getStorageTrie(address, function (err, storageTrie) {
if (err) {
return cb(err)
}

if (value && value.length) {
// format input
var encodedValue = rlp.encode(value)
storageTrie.put(key, encodedValue, finalize)
} else {
// deleting a value
storageTrie.del(key, finalize)
}
modifyTrie(storageTrie, finalize)

function finalize (err) {
if (err) return cb(err)
Expand All @@ -189,6 +182,28 @@ proto.putContractStorage = function (address, key, value, cb) {
})
}

proto.putContractStorage = function (address, key, value, cb) {
var self = this
self._modifyContractStorage(address, function (storageTrie, done) {
if (value && value.length) {
// format input
var encodedValue = rlp.encode(value)
storageTrie.put(key, encodedValue, done)
} else {
// deleting a value
storageTrie.del(key, done)
}
}, cb)
}

proto.clearContractStorage = function (address, cb) {
var self = this
self._modifyContractStorage(address, function (storageTrie, done) {
storageTrie.root = storageTrie.EMPTY_TRIE_ROOT
done()
}, cb)
}

proto.commitContracts = function (cb) {
var self = this
async.each(Object.keys(self._storageTries), function (address, cb) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"babel-preset-env": "^1.6.1",
"coveralls": "^3.0.0",
"ethereumjs-blockchain": "~2.1.0",
"ethereumjs-testing": "https://github.com/ethereumjs/ethereumjs-testing",
"ethereumjs-testing": "git+https://github.com/ethereumjs/ethereumjs-testing.git#v1.1.1",
"ethereumjs-tx": "1.3.3",
"istanbul": "^0.4.5",
"level": "^1.4.0",
Expand Down
58 changes: 33 additions & 25 deletions tests/GeneralStateTestsRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,34 @@ const ethUtil = require('ethereumjs-util')
const BN = ethUtil.BN

function parseTestCases (forkConfig, testData, data, gasLimit, value) {
let testCases = testData['post'][forkConfig].map(testCase => {
let testIndexes = testCase['indexes']
let tx = Object.assign({}, testData.transaction)
if (data !== undefined && testIndexes['data'] !== data) {
return null
}
let testCases = []
if (testData['post'][forkConfig]) {
testCases = testData['post'][forkConfig].map(testCase => {
let testIndexes = testCase['indexes']
let tx = Object.assign({}, testData.transaction)
if (data !== undefined && testIndexes['data'] !== data) {
return null
}

if (value !== undefined && testIndexes['value'] !== value) {
return null
}
if (value !== undefined && testIndexes['value'] !== value) {
return null
}

if (gasLimit !== undefined && testIndexes['gas'] !== gasLimit) {
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']
}
})
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
Expand Down Expand Up @@ -123,9 +126,14 @@ function runTestCase (options, testData, t, cb) {
module.exports = function runStateTest (options, testData, t, cb) {
try {
const testCases = parseTestCases(options.forkConfig, testData, options.data, options.gasLimit, options.value)
async.eachSeries(testCases,
(testCase, done) => runTestCase(options, testCase, t, done),
cb)
if (testCases.length > 0) {
async.eachSeries(testCases,
(testCase, done) => runTestCase(options, testCase, t, done),
cb)
} else {
t.comment(`No ${options.forkConfig} post state defined, skip test`)
cb()
}
} catch (e) {
t.fail('error running test case for fork: ' + options.forkConfig)
console.log('error:', e)
Expand Down

0 comments on commit 1394a2e

Please sign in to comment.