Skip to content

Commit d2edc07

Browse files
committed
add check if nonce or code at address
1 parent cf9589f commit d2edc07

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

lib/opFns.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ module.exports = {
519519
checkOutOfGas(runState, options)
520520
makeCall(runState, options, localOpts, done)
521521
},
522-
CREATE2: function (value, offset, length, runState, done) {
522+
CREATE2: function (value, offset, length, salt, runState, done) {
523523
if (!runState._common.gteHardfork('constantinople')) {
524524
trap(ERROR.INVALID_OPCODE)
525525
}
@@ -533,17 +533,20 @@ module.exports = {
533533
// set up config
534534
var options = {
535535
value: value,
536-
data: data
536+
data: data,
537+
salt: salt.toBuffer('be', 32)
537538
}
538539

539540
var localOpts = {
540541
inOffset: offset,
541542
inLength: length,
542543
outOffset: new BN(0),
543-
outLength: new BN(0),
544-
salt: salt
544+
outLength: new BN(0)
545545
}
546546

547+
// Deduct gas costs for hashing
548+
// TODO this is not yet part of the tests
549+
// subGas(runState, new BN(runState._common.param('gasPrices', 'sha3Word')).imul(length.divCeil(new BN(32))))
547550
checkCallMemCost(runState, options, localOpts)
548551
checkOutOfGas(runState, options)
549552
makeCall(runState, options, localOpts, done)

lib/runCall.js

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const BN = ethUtil.BN
55
const exceptions = require('./exceptions.js')
66

77
const ERROR = exceptions.ERROR
8+
const EMPTY_CODE_HASH = ethUtil.keccak256()
89

910
/**
1011
* runs a CALL operation
@@ -69,39 +70,53 @@ module.exports = function (opts, cb) {
6970
code = txData
7071
txData = undefined
7172
var newNonce = new BN(account.nonce).subn(1)
72-
73+
7374
if (salt) {
7475
createdAddress = toAddress = ethUtil.generateAddress2(caller, salt, code)
7576
} else {
7677
createdAddress = toAddress = ethUtil.generateAddress(caller, newNonce.toArray())
7778
}
7879

79-
stateManager.clearContractStorage(createdAddress, function (err) {
80+
stateManager.getAccount(createdAddress, function (err, account) {
8081
if (err) {
8182
done(err)
83+
return
8284
}
8385

84-
async.series([
85-
newContractEvent,
86-
getAccount
87-
], done)
88-
89-
function newContractEvent (callback) {
90-
self.emit('newContract', {
91-
address: createdAddress,
92-
code: code
93-
}, callback)
86+
if ((account.nonce && new BN(account.nonce) > 0) || account.codeHash.compare(EMPTY_CODE_HASH) != 0) {
87+
done(ERROR.INVALID_OPCODE)
88+
return
9489
}
9590

96-
function getAccount (callback) {
97-
stateManager.getAccount(createdAddress, function (err, account) {
98-
toAccount = account
99-
const NONCE_OFFSET = 1
100-
toAccount.nonce = new BN(toAccount.nonce).addn(NONCE_OFFSET).toArrayLike(Buffer)
101-
callback(err)
102-
})
103-
}
91+
stateManager.clearContractStorage(createdAddress, function (err) {
92+
if (err) {
93+
done(err)
94+
return
95+
}
96+
97+
async.series([
98+
newContractEvent,
99+
getAccount
100+
], done)
101+
102+
function newContractEvent (callback) {
103+
self.emit('newContract', {
104+
address: createdAddress,
105+
code: code
106+
}, callback)
107+
}
108+
109+
function getAccount (callback) {
110+
stateManager.getAccount(createdAddress, function (err, account) {
111+
toAccount = account
112+
const NONCE_OFFSET = 1
113+
toAccount.nonce = new BN(toAccount.nonce).addn(NONCE_OFFSET).toArrayLike(Buffer)
114+
callback(err)
115+
})
116+
}
117+
})
104118
})
119+
105120
} else {
106121
// else load the `to` account
107122
toAccount = stateManager.cache.get(toAddress)

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"ethereumjs-account": "^2.0.3",
3838
"ethereumjs-block": "~1.7.0",
3939
"ethereumjs-common": "~0.4.0",
40-
"ethereumjs-util": "git+https://github.com/ethereumjs/ethereumjs-util#create2",
40+
"ethereumjs-util": "git+https://github.com/ethereumjs/ethereumjs-util.git#create2",
4141
"fake-merkle-patricia-tree": "^1.0.1",
4242
"functional-red-black-tree": "^1.0.1",
4343
"merkle-patricia-tree": "^2.1.2",
@@ -49,7 +49,7 @@
4949
"babel-preset-env": "^1.6.1",
5050
"coveralls": "^3.0.0",
5151
"ethereumjs-blockchain": "~2.1.0",
52-
"ethereumjs-testing": "git+https://github.com/ethereumjs/ethereumjs-testing.git#v1.1.1",
52+
"ethereumjs-testing": "git+https://github.com/ethereumjs/ethereumjs-testing.git#v1.2.2",
5353
"ethereumjs-tx": "1.3.3",
5454
"istanbul": "^0.4.5",
5555
"level": "^1.4.0",

0 commit comments

Comments
 (0)