git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit a7368ae614730b38e21a3792049310c00d65a725

Kernel: implement selfdestruct

Alex Beregszaszi committed on 8/28/2016, 9:18:56 PM
Parent: 6d77ad101453e8f18c589971f99e9d718ba6a243

Files changed

environment.jschanged
index.jschanged
interface.jschanged
environment.jsView
@@ -21,8 +21,9 @@
2121 // the ROM
2222 code: new Uint8Array(), // the current running code
2323 // output calls
2424 logs: [],
25+ selfDestruct: false,
2526 selfDestructAddress: new Address('0x0000000000000000000000000000000000000000'),
2627 // more output calls
2728 returnValue: new Uint8Array()
2829 }
index.jsView
@@ -119,16 +119,25 @@
119119
120120 const kernel = new Kernel(environment)
121121 kernel.codeHandler(code, new Interface(environment))
122122
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+
123131 // generate new stateroot
124132 // this.environment.state.set(address, { stateRoot: stateRoot })
125133
126134 return {
127135 executionOutcome: 1, // success
128136 gasLeft: new U256(environment.gasLeft),
129137 gasRefund: new U256(environment.gasRefund),
130138 returnValue: environment.returnValue,
139+ selfDestruct: environment.selfDestruct,
131140 selfDestructAddress: environment.selfDestructAddress,
132141 logs: environment.logs
133142 }
134143 }
@@ -152,8 +161,10 @@
152161 // Run code and take return value as contract code
153162 // FIXME: decide if these are the right values here: value: 0, data: ''
154163 code = this.callHandler({ from: create.from, to: address, gasLimit: create.gasLimit, value: new U256(0), data: new Uint8Array() }).returnValue
155164
165+ // FIXME: special handling for selfdestruct
166+
156167 this.environment.state.get(address.toString()).set('code', code)
157168
158169 return {
159170 executionOutcome: 1, // success
interface.jsView
@@ -392,9 +392,9 @@
392392
393393 // Special case for non-zero value
394394 if (!value.isZero()) {
395395 this.takeGas(9000)
396- gas += 2300;
396+ gas += 2300
397397 }
398398
399399 const [errorCode, result] = this.environment.call(gas, address, value, data)
400400 this.setMemory(resultOffset, resultLength, result)
@@ -504,9 +504,10 @@
504504 * balance to an address path
505505 * @param {integer} offset the offset to load the address from
506506 */
507507 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))
509510 this.environment.gasRefund += 24000
510511 }
511512
512513 getMemory (offset, length) {

Built with git-ssb-web