Skip to content

Commit

Permalink
EIP-6: Rename SUICIDE opcode (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
gasolin authored and cdetrio committed Jul 27, 2017
1 parent 51a6a0c commit 7f12684
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -102,7 +102,7 @@ Runs EVM code
- `gas` - the amount of gas left as a `bignum`
- `gasUsed` - the amount of gas as a `bignum` the code used to run.
- `gasRefund` - a `Bignum` containing the amount of gas to refund from deleting storage values
- `suicides` - an `Object` with keys for accounts that have suicided and values for balance transfer recipient accounts.
- `selfdestruct` - an `Object` with keys for accounts that have selfdestructed and values for balance transfer recipient accounts.
- `logs` - an `Array` of logs that the contract emitted.
- `exception` - `0` if the contract encountered an exception, `1` otherwise.
- `exceptionError` - a `String` describing the exception if there was one.
Expand Down
18 changes: 9 additions & 9 deletions lib/opFns.js
Expand Up @@ -682,21 +682,21 @@ module.exports = {
runState.returnValue = memLoad(runState, offset, length)
},
// '0x70', range - other
SUICIDE: function (suicideToAddress, runState, cb) {
SELFDESTRUCT: function (selfdestructToAddress, runState, cb) {
var stateManager = runState.stateManager
var contract = runState.contract
var contractAddress = runState.address
var zeroBalance = new BN(0)
suicideToAddress = utils.setLengthLeft(suicideToAddress, 20)
selfdestructToAddress = utils.setLengthLeft(selfdestructToAddress, 20)

stateManager.getAccount(suicideToAddress, function (err, toAccount) {
stateManager.getAccount(selfdestructToAddress, function (err, toAccount) {
// update balances
if (err) {
cb(err)
return
}

stateManager.accountIsEmpty(suicideToAddress, function (error, empty) {
stateManager.accountIsEmpty(selfdestructToAddress, function (error, empty) {
if (error) {
cb(error)
return
Expand All @@ -713,16 +713,16 @@ module.exports = {
}
}

// only add to refund if this is the first suicide for the address
if (!runState.suicides[contractAddress.toString('hex')]) {
// only add to refund if this is the first selfdestruct for the address
if (!runState.selfdestruct[contractAddress.toString('hex')]) {
runState.gasRefund = runState.gasRefund.add(new BN(fees.suicideRefundGas.v))
}
runState.suicides[contractAddress.toString('hex')] = suicideToAddress
runState.selfdestruct[contractAddress.toString('hex')] = selfdestructToAddress
runState.stopped = true

var newBalance = new Buffer(new BN(contract.balance).add(new BN(toAccount.balance)).toArray())
async.series([
stateManager.putAccountBalance.bind(stateManager, suicideToAddress, newBalance),
stateManager.putAccountBalance.bind(stateManager, selfdestructToAddress, newBalance),
stateManager.putAccountBalance.bind(stateManager, contractAddress, new BN(0))
], cb)
})
Expand Down Expand Up @@ -861,7 +861,7 @@ function makeCall (runState, callOptions, localOpts, cb) {
callOptions.gasPrice = runState.gasPrice
callOptions.block = runState.block
callOptions.populateCache = false
callOptions.suicides = runState.suicides
callOptions.selfdestruct = runState.selfdestruct

// increment the runState.depth
callOptions.depth = runState.depth + 1
Expand Down
2 changes: 1 addition & 1 deletion lib/opcodes.js
Expand Up @@ -149,7 +149,7 @@ const codes = {
0xf4: ['DELEGATECALL', 700, 6, 1, true],

// '0x70', range - other
0xff: ['SUICIDE', 5000, 1, 0, false]
0xff: ['SELFDESTRUCT', 5000, 1, 0, false]
}

module.exports = function (op, full) {
Expand Down
5 changes: 3 additions & 2 deletions lib/runCall.js
Expand Up @@ -41,7 +41,8 @@ module.exports = function (opts, cb) {
var origin = opts.origin
var isCompiled = opts.compiled
var depth = opts.depth
var suicides = opts.suicides
// opts.suicides is kept for backward compatiblity with pre-EIP6 syntax
var selfdestruct = opts.selfdestruct || opts.suicides
var delegatecall = opts.delegatecall || false

txValue = new BN(txValue)
Expand Down Expand Up @@ -139,7 +140,7 @@ module.exports = function (opts, cb) {
value: new Buffer(txValue.toArray()),
block: block,
depth: depth,
suicides: suicides,
selfdestruct: selfdestruct,
populateCache: false
}

Expand Down
7 changes: 4 additions & 3 deletions lib/runCode.js
Expand Up @@ -62,7 +62,8 @@ module.exports = function (opts, cb) {
gasRefund: new BN(0),
highestMemCost: new BN(0),
depth: opts.depth || 0,
suicides: opts.suicides || {},
// opts.suicides is kept for backward compatiblity with pre-EIP6 syntax
selfdestruct: opts.selfdestruct || opts.suicides || {},
block: block,
callValue: opts.value || new BN(0),
address: opts.address || utils.zeros(32),
Expand Down Expand Up @@ -212,7 +213,7 @@ module.exports = function (opts, cb) {

var results = {
runState: runState,
suicides: runState.suicides,
selfdestruct: runState.selfdestruct,
gasRefund: runState.gasRefund,
exception: err ? 0 : 1,
exceptionError: err,
Expand All @@ -223,7 +224,7 @@ module.exports = function (opts, cb) {

if (results.exceptionError) {
delete results.gasRefund
delete results.suicides
delete results.selfdestruct
self.stateManager.touched = []
}

Expand Down
6 changes: 3 additions & 3 deletions lib/runTx.js
Expand Up @@ -170,11 +170,11 @@ module.exports = function (opts, cb) {
self.stateManager.cache.put(block.header.coinbase, minerAccount)
}

if (!results.vm.suicides) {
results.vm.suicides = {}
if (!results.vm.selfdestruct) {
results.vm.selfdestruct = {}
}

var keys = Object.keys(results.vm.suicides)
var keys = Object.keys(results.vm.selfdestruct)

keys.forEach(function (s) {
self.stateManager.cache.del(new Buffer(s, 'hex'))
Expand Down

0 comments on commit 7f12684

Please sign in to comment.