Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make list of opcodes property of VM instance #592

Merged
merged 3 commits into from
Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 36 additions & 5 deletions lib/evm/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ERROR, VmError } from '../exceptions'
import Memory from './memory'
import Stack from './stack'
import EEI from './eei'
import { lookupOpInfo, OpInfo } from './opcodes'
import { Opcode } from './opcodes'
import { handlers as opHandlers, OpHandler } from './opFns.js'

export interface InterpreterOpts {
Expand Down Expand Up @@ -102,7 +102,7 @@ export default class Interpreter {
* reducing it's base gas cost, and increments the program counter.
*/
async runStep(): Promise<void> {
const opInfo = lookupOpInfo(this._runState.opCode)
const opInfo = this.lookupOpInfo(this._runState.opCode)
// Check for invalid opcode
if (opInfo.name === 'INVALID') {
throw new VmError(ERROR.INVALID_OPCODE)
Expand All @@ -125,15 +125,46 @@ export default class Interpreter {
/**
* Get the handler function for an opcode.
*/
getOpHandler(opInfo: OpInfo): OpHandler {
getOpHandler(opInfo: Opcode): OpHandler {
return opHandlers[opInfo.name]
}

/**
* Get info for an opcode from VM's list of opcodes.
*/
lookupOpInfo(op: number, full: boolean = false): Opcode {
const opcode = this._vm._opcodes[op]
? this._vm._opcodes[op]
: { name: 'INVALID', fee: 0, isAsync: false }

if (full) {
let name = opcode.name
if (name === 'LOG') {
name += op - 0xa0
}

if (name === 'PUSH') {
name += op - 0x5f
}

if (name === 'DUP') {
name += op - 0x7f
}

if (name === 'SWAP') {
name += op - 0x8f
}
return { ...opcode, ...{ name } }
}

return opcode
}

s1na marked this conversation as resolved.
Show resolved Hide resolved
async _runStepHook(): Promise<void> {
const eventObj = {
pc: this._runState.programCounter,
gasLeft: this._eei.getGasLeft(),
opcode: lookupOpInfo(this._runState.opCode, true),
opcode: this.lookupOpInfo(this._runState.opCode, true),
stack: this._runState.stack._store,
depth: this._eei._env.depth,
address: this._eei._env.address,
Expand Down Expand Up @@ -166,7 +197,7 @@ export default class Interpreter {
const jumps = []

for (let i = 0; i < code.length; i++) {
const curOpCode = lookupOpInfo(code[i]).name
const curOpCode = this.lookupOpInfo(code[i]).name

// no destinations into the middle of PUSH
if (curOpCode === 'PUSH') {
Expand Down