Skip to content

Commit dd707f0

Browse files
committed
add check if nonce or code at address
1 parent e4d08e9 commit dd707f0

File tree

3 files changed

+43
-25
lines changed

3 files changed

+43
-25
lines changed

lib/opFns.js

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

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

548+
// Deduct gas costs for hashing
549+
// TODO this is not yet part of the tests
550+
// subGas(runState, new BN(runState._common.param('gasPrices', 'sha3Word')).imul(length.divCeil(new BN(32))))
548551
checkCallMemCost(runState, options, localOpts)
549552
checkOutOfGas(runState, options)
550553
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"ethereumjs-account": "^2.0.3",
4040
"ethereumjs-block": "~2.0.1",
4141
"ethereumjs-common": "~0.4.0",
42-
"ethereumjs-util": "git+https://github.com/ethereumjs/ethereumjs-util#create2",
42+
"ethereumjs-util": "git+https://github.com/ethereumjs/ethereumjs-util.git#create2",
4343
"fake-merkle-patricia-tree": "^1.0.1",
4444
"functional-red-black-tree": "^1.0.1",
4545
"merkle-patricia-tree": "^2.1.2",

0 commit comments

Comments
 (0)