Skip to content

Commit

Permalink
Merge pull request #297 from mattdean-digicatapult/update-tests-Rever…
Browse files Browse the repository at this point in the history
…tInCreateInInit

Created contract accounts should have nonce 1 and storage empty
  • Loading branch information
jwasinger authored Apr 28, 2018
2 parents 9cd0665 + 702a07c commit c243d5e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
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

0 comments on commit c243d5e

Please sign in to comment.