Skip to content

Commit 86bf1cd

Browse files
committed
add check if nonce or code at address
1 parent 3a86d3e commit 86bf1cd

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
@@ -70,39 +71,53 @@ module.exports = function (opts, cb) {
7071
code = txData
7172
txData = undefined
7273
var newNonce = new BN(account.nonce).subn(1)
73-
74+
7475
if (salt) {
7576
createdAddress = toAddress = ethUtil.generateAddress2(caller, salt, code)
7677
} else {
7778
createdAddress = toAddress = ethUtil.generateAddress(caller, newNonce.toArray())
7879
}
7980

80-
stateManager.clearContractStorage(createdAddress, function (err) {
81+
stateManager.getAccount(createdAddress, function (err, account) {
8182
if (err) {
8283
done(err)
84+
return
8385
}
8486

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

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