Skip to content

Commit

Permalink
vm/evm: move gasLeft as property of Interpreter into RunState
Browse files Browse the repository at this point in the history
  • Loading branch information
jochem-brouwer committed Jun 6, 2022
1 parent 2732e54 commit 1acc189
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/vm/src/evm/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ export default class EVM extends AsyncEventEmitter {
const interpreterRes = await interpreter.run(message.code as Buffer, opts)

let result = interpreter._result
let gasUsed = message.gasLimit - interpreter._gasLeft
let gasUsed = message.gasLimit - interpreterRes.runState!.gasLeft
if (interpreterRes.exceptionError) {
if (
interpreterRes.exceptionError.error !== ERROR.REVERT &&
Expand All @@ -719,7 +719,7 @@ export default class EVM extends AsyncEventEmitter {
...interpreter._env,
},
exceptionError: interpreterRes.exceptionError,
gas: interpreter._gasLeft,
gas: interpreterRes.runState?.gasLeft,
gasUsed,
gasRefund: interpreterRes.runState!.gasRefund,
returnValue: result.returnValue ? result.returnValue : Buffer.alloc(0),
Expand Down
23 changes: 10 additions & 13 deletions packages/vm/src/evm/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ export interface RunState {
messageGasLimit?: bigint // Cache value from `gas.ts` to save gas limit for a message call
interpreter: Interpreter
gasRefund: bigint // Tracks the current refund
gasLeft: bigint // Current gas left
auth?: Address /** EIP-3074 AUTH parameter */
returnBuffer: Buffer /* Current bytes in the return buffer. Cleared each time a CALL/CREATE is made in the current frame. */
}

export interface InterpreterResult {
runState?: RunState
runState: RunState
exceptionError?: VmError
}

Expand Down Expand Up @@ -113,10 +114,6 @@ export default class Interpreter {
_evm: EVM
_env: Env

// Current gas left
// TODO: should be moved into Env?
_gasLeft: bigint

// Keep track of this Interpreter run result
// TODO move into Env?
_result: RunResult
Expand Down Expand Up @@ -147,10 +144,10 @@ export default class Interpreter {
shouldDoJumpAnalysis: true,
interpreter: this,
gasRefund: env.gasRefund,
gasLeft,
returnBuffer: Buffer.alloc(0),
}
this._env = env
this._gasLeft = gasLeft
this._result = {
logs: [],
returnValue: undefined,
Expand Down Expand Up @@ -403,12 +400,12 @@ export default class Interpreter {
* @throws if out of gas
*/
useGas(amount: bigint, context?: string): void {
this._gasLeft -= amount
this._runState.gasLeft -= amount
if (this._evm.DEBUG) {
debugGas(`${context ? context + ': ' : ''}used ${amount} gas (-> ${this._gasLeft})`)
debugGas(`${context ? context + ': ' : ''}used ${amount} gas (-> ${this._runState.gasLeft})`)
}
if (this._gasLeft < BigInt(0)) {
this._gasLeft = BigInt(0)
if (this._runState.gasLeft < BigInt(0)) {
this._runState.gasLeft = BigInt(0)
trap(ERROR.OUT_OF_GAS)
}
}
Expand Down Expand Up @@ -451,9 +448,9 @@ export default class Interpreter {
*/
addStipend(amount: bigint): void {
if (this._evm.DEBUG) {
debugGas(`add stipend ${amount} (-> ${this._gasLeft})`)
debugGas(`add stipend ${amount} (-> ${this._runState.gasLeft})`)
}
this._gasLeft += amount
this._runState.gasLeft += amount
}

/**
Expand Down Expand Up @@ -587,7 +584,7 @@ export default class Interpreter {
* Returns the current gasCounter.
*/
getGasLeft(): bigint {
return this._gasLeft
return this._runState.gasLeft
}

/**
Expand Down

0 comments on commit 1acc189

Please sign in to comment.