Commit a7368ae614730b38e21a3792049310c00d65a725
Kernel: implement selfdestruct
Alex Beregszaszi committed on 8/28/2016, 9:18:56 PMParent: 6d77ad101453e8f18c589971f99e9d718ba6a243
Files changed
environment.js | changed |
index.js | changed |
interface.js | changed |
environment.js | ||
---|---|---|
@@ -21,8 +21,9 @@ | ||
21 | 21 | // the ROM |
22 | 22 | code: new Uint8Array(), // the current running code |
23 | 23 | // output calls |
24 | 24 | logs: [], |
25 | + selfDestruct: false, | |
25 | 26 | selfDestructAddress: new Address('0x0000000000000000000000000000000000000000'), |
26 | 27 | // more output calls |
27 | 28 | returnValue: new Uint8Array() |
28 | 29 | } |
index.js | ||
---|---|---|
@@ -119,16 +119,25 @@ | ||
119 | 119 | |
120 | 120 | const kernel = new Kernel(environment) |
121 | 121 | kernel.codeHandler(code, new Interface(environment)) |
122 | 122 | |
123 | + // self destructed | |
124 | + if (environment.selfDestruct) { | |
125 | + const balance = this.state.get(call.to.toString()).get('balance') | |
126 | + const beneficiary = this.state.get(environment.selfDestructAddress) | |
127 | + beneficiary.set('balance', beneficiary.get('balance').add(balance)) | |
128 | + this.state.delete(call.to.toString()) | |
129 | + } | |
130 | + | |
123 | 131 | // generate new stateroot |
124 | 132 | // this.environment.state.set(address, { stateRoot: stateRoot }) |
125 | 133 | |
126 | 134 | return { |
127 | 135 | executionOutcome: 1, // success |
128 | 136 | gasLeft: new U256(environment.gasLeft), |
129 | 137 | gasRefund: new U256(environment.gasRefund), |
130 | 138 | returnValue: environment.returnValue, |
139 | + selfDestruct: environment.selfDestruct, | |
131 | 140 | selfDestructAddress: environment.selfDestructAddress, |
132 | 141 | logs: environment.logs |
133 | 142 | } |
134 | 143 | } |
@@ -152,8 +161,10 @@ | ||
152 | 161 | // Run code and take return value as contract code |
153 | 162 | // FIXME: decide if these are the right values here: value: 0, data: '' |
154 | 163 | code = this.callHandler({ from: create.from, to: address, gasLimit: create.gasLimit, value: new U256(0), data: new Uint8Array() }).returnValue |
155 | 164 | |
165 | + // FIXME: special handling for selfdestruct | |
166 | + | |
156 | 167 | this.environment.state.get(address.toString()).set('code', code) |
157 | 168 | |
158 | 169 | return { |
159 | 170 | executionOutcome: 1, // success |
interface.js | ||
---|---|---|
@@ -392,9 +392,9 @@ | ||
392 | 392 | |
393 | 393 | // Special case for non-zero value |
394 | 394 | if (!value.isZero()) { |
395 | 395 | this.takeGas(9000) |
396 | - gas += 2300; | |
396 | + gas += 2300 | |
397 | 397 | } |
398 | 398 | |
399 | 399 | const [errorCode, result] = this.environment.call(gas, address, value, data) |
400 | 400 | this.setMemory(resultOffset, resultLength, result) |
@@ -504,9 +504,10 @@ | ||
504 | 504 | * balance to an address path |
505 | 505 | * @param {integer} offset the offset to load the address from |
506 | 506 | */ |
507 | 507 | selfDestruct (addressOffset) { |
508 | - this.environment.suicideAddress = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES)) | |
508 | + this.environment.selfDestruct = true | |
509 | + this.environment.selfDestructAddress = Address.fromMemory(this.getMemory(addressOffset, ADDRESS_SIZE_BYTES)) | |
509 | 510 | this.environment.gasRefund += 24000 |
510 | 511 | } |
511 | 512 | |
512 | 513 | getMemory (offset, length) { |
Built with git-ssb-web