Skip to content

Commit 0ccb64f

Browse files
alcuadrados1na
authored andcommitted
ExecResult simplification (ethereumjs#551)
* Always use VMError and not directly ERROR * Remove PrecompileResult and move OOGResult * Rename EVMResult#vm to EVMResult#execResult * Replace runValue for return * Fix TxReceipt's status doc * Simplify exceptions handling in results values * Remove unnecessary re-export * Rename return to returnValue
1 parent 4bbb6e3 commit 0ccb64f

File tree

18 files changed

+127
-151
lines changed

18 files changed

+127
-151
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ vm.runCode({
7171
gasLimit: new BN(0xffff),
7272
})
7373
.then(results => {
74-
console.log('Returned : ' + results.return.toString('hex'))
74+
console.log('Returned : ' + results.returnValue.toString('hex'))
7575
console.log('gasUsed : ' + results.gasUsed.toString())
7676
})
7777
.catch(err => console.log('Error : ' + err))

examples/run-solidity-contract/index.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as fs from 'fs'
66
import { promisify } from 'util'
77
import * as util from 'ethereumjs-util'
88
import Account from 'ethereumjs-account'
9-
const Transaction = require('ethereumjs-tx') // Change when https://github.com/ethereumjs/ethereumjs-vm/pull/541 gets merged
9+
import { Transaction } from 'ethereumjs-tx'
1010
const abi = require('ethereumjs-abi')
1111
const solc = require('solc')
1212

@@ -95,9 +95,8 @@ async function deployContract(
9595
const params = abi.rawEncode(['string'], [greeting])
9696

9797
const tx = new Transaction({
98-
to: null,
9998
value: 0,
100-
gas: 2000000, // We assume that 2M is enough,
99+
gasLimit: 2000000, // We assume that 2M is enough,
101100
gasPrice: 1,
102101
data: '0x' + deploymentBytecode + params.toString('hex'),
103102
nonce: await getAccountNonce(vm, senderPrivateKey),
@@ -107,8 +106,8 @@ async function deployContract(
107106

108107
const deploymentResult = await vm.runTx({ tx })
109108

110-
if (deploymentResult.vm.exception === 0) {
111-
throw deploymentResult.vm.exceptionError
109+
if (deploymentResult.execResult.exceptionError) {
110+
throw deploymentResult.execResult.exceptionError
112111
}
113112

114113
return deploymentResult.createdAddress!
@@ -125,7 +124,7 @@ async function setGreeting(
125124
const tx = new Transaction({
126125
to: contractAddress,
127126
value: 0,
128-
gas: 2000000, // We assume that 2M is enough,
127+
gasLimit: 2000000, // We assume that 2M is enough,
129128
gasPrice: 1,
130129
data: '0x' + abi.methodID('setGreeting', ['string']).toString('hex') + params.toString('hex'),
131130
nonce: await getAccountNonce(vm, senderPrivateKey),
@@ -135,8 +134,8 @@ async function setGreeting(
135134

136135
const setGreetingResult = await vm.runTx({ tx })
137136

138-
if (setGreetingResult.vm.exception === 0) {
139-
throw setGreetingResult.vm.exceptionError
137+
if (setGreetingResult.execResult.exceptionError) {
138+
throw setGreetingResult.execResult.exceptionError
140139
}
141140
}
142141

@@ -148,11 +147,11 @@ async function getGreeting(vm: VM, contractAddress: Buffer, caller: Buffer) {
148147
data: abi.methodID('greet', []),
149148
})
150149

151-
if (greetResult.vm.exception === 0) {
152-
throw greetResult.vm.exceptionError
150+
if (greetResult.execResult.exceptionError) {
151+
throw greetResult.execResult.exceptionError
153152
}
154153

155-
const results = abi.rawDecode(['string'], greetResult.vm.return)
154+
const results = abi.rawDecode(['string'], greetResult.execResult.return)
156155

157156
return results[0]
158157
}

examples/run-transactions-complete/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import VM from '../..'
22
import Account from 'ethereumjs-account'
33
import * as utils from 'ethereumjs-util'
44
import PStateManager from '../../lib/state/promisified'
5-
6-
const Transaction = require('ethereumjs-tx') // Change when https://github.com/ethereumjs/ethereumjs-vm/pull/541 gets merged
5+
import { Transaction } from 'ethereumjs-tx'
76

87
async function main() {
98
const vm = new VM()
@@ -62,7 +61,7 @@ async function runTx(vm: VM, rawTx: any, privateKey: Buffer) {
6261
})
6362

6463
console.log('gas used: ' + results.gasUsed.toString())
65-
console.log('returned: ' + results.vm.return.toString('hex'))
64+
console.log('returned: ' + results.execResult.return.toString('hex'))
6665

6766
const createdAddress = results.createdAddress
6867

lib/evm/eei.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Common from 'ethereumjs-common'
66
import PStateManager from '../state/promisified'
77
import { VmError, ERROR } from '../exceptions'
88
import Message from './message'
9-
import EVM from './evm'
9+
import EVM, { EVMResult } from './evm'
1010
const promisify = require('util.promisify')
1111

1212
/**
@@ -460,34 +460,35 @@ export default class EEI {
460460

461461
const results = await this._evm.executeMessage(msg)
462462

463-
if (results.vm.logs) {
464-
this._result.logs = this._result.logs.concat(results.vm.logs)
463+
if (results.execResult.logs) {
464+
this._result.logs = this._result.logs.concat(results.execResult.logs)
465465
}
466466

467467
// add gasRefund
468-
if (results.vm.gasRefund) {
469-
this._result.gasRefund = this._result.gasRefund.add(results.vm.gasRefund)
468+
if (results.execResult.gasRefund) {
469+
this._result.gasRefund = this._result.gasRefund.add(results.execResult.gasRefund)
470470
}
471471

472472
// this should always be safe
473473
this.useGas(results.gasUsed)
474474

475475
// Set return value
476476
if (
477-
results.vm.return &&
478-
(!results.vm.exceptionError || (results.vm.exceptionError as VmError).error === ERROR.REVERT)
477+
results.execResult.returnValue &&
478+
(!results.execResult.exceptionError ||
479+
results.execResult.exceptionError.error === ERROR.REVERT)
479480
) {
480-
this._lastReturned = results.vm.return
481+
this._lastReturned = results.execResult.returnValue
481482
}
482483

483-
if (!results.vm.exceptionError) {
484+
if (!results.execResult.exceptionError) {
484485
Object.assign(this._result.selfdestruct, selfdestruct)
485486
// update stateRoot on current contract
486487
const account = await this._state.getAccount(this._env.address)
487488
this._env.contract = account
488489
}
489490

490-
return new BN(results.vm.exception)
491+
return this._getReturnCode(results)
491492
}
492493

493494
/**
@@ -521,27 +522,27 @@ export default class EEI {
521522

522523
const results = await this._evm.executeMessage(msg)
523524

524-
if (results.vm.logs) {
525-
this._result.logs = this._result.logs.concat(results.vm.logs)
525+
if (results.execResult.logs) {
526+
this._result.logs = this._result.logs.concat(results.execResult.logs)
526527
}
527528

528529
// add gasRefund
529-
if (results.vm.gasRefund) {
530-
this._result.gasRefund = this._result.gasRefund.add(results.vm.gasRefund)
530+
if (results.execResult.gasRefund) {
531+
this._result.gasRefund = this._result.gasRefund.add(results.execResult.gasRefund)
531532
}
532533

533534
// this should always be safe
534535
this.useGas(results.gasUsed)
535536

536537
// Set return buffer in case revert happened
537538
if (
538-
results.vm.exceptionError &&
539-
(results.vm.exceptionError as VmError).error === ERROR.REVERT
539+
results.execResult.exceptionError &&
540+
results.execResult.exceptionError.error === ERROR.REVERT
540541
) {
541-
this._lastReturned = results.vm.return
542+
this._lastReturned = results.execResult.returnValue
542543
}
543544

544-
if (!results.vm.exceptionError) {
545+
if (!results.execResult.exceptionError) {
545546
Object.assign(this._result.selfdestruct, selfdestruct)
546547
// update stateRoot on current contract
547548
const account = await this._state.getAccount(this._env.address)
@@ -552,7 +553,7 @@ export default class EEI {
552553
}
553554
}
554555

555-
return new BN(results.vm.exception)
556+
return this._getReturnCode(results)
556557
}
557558

558559
/**
@@ -570,6 +571,16 @@ export default class EEI {
570571
async isAccountEmpty(address: Buffer): Promise<boolean> {
571572
return this._state.accountIsEmpty(address)
572573
}
574+
575+
private _getReturnCode(results: EVMResult) {
576+
// This preserves the previous logic, but seems to contradict the EEI spec
577+
// https://github.com/ewasm/design/blob/38eeded28765f3e193e12881ea72a6ab807a3371/eth_interface.md
578+
if (results.execResult.exceptionError) {
579+
return new BN(0)
580+
} else {
581+
return new BN(1)
582+
}
583+
}
573584
}
574585

575586
function trap(err: ERROR) {

0 commit comments

Comments
 (0)