Skip to content

Commit

Permalink
Merge pull request #582 from ethereumjs/group-opcodes-by-hf
Browse files Browse the repository at this point in the history
Maintain separate list of opcodes for Istanbul
  • Loading branch information
holgerd77 authored Aug 28, 2019
2 parents 205f830 + eccc8bf commit edffcde
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
49 changes: 28 additions & 21 deletions lib/evm/opcodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ export interface OpInfo {
name: string
opcode: number
fee: number
dynamic: boolean
isAsync: boolean
}

const codes: any = {
let codes: any = {
// 0x0 range - arithmetic ops
// name, baseCost, dynamic, async
// name, baseCost, async
0x00: ['STOP', 0, false],
0x01: ['ADD', 3, false],
0x02: ['MUL', 5, false],
Expand Down Expand Up @@ -43,7 +42,7 @@ const codes: any = {

// 0x30 range - closure state
0x30: ['ADDRESS', 2, true],
0x31: ['BALANCE', 400, true, true],
0x31: ['BALANCE', 400, true],
0x32: ['ORIGIN', 2, true],
0x33: ['CALLER', 2, true],
0x34: ['CALLVALUE', 2, true],
Expand All @@ -53,28 +52,27 @@ const codes: any = {
0x38: ['CODESIZE', 2, false],
0x39: ['CODECOPY', 3, false],
0x3a: ['GASPRICE', 2, false],
0x3b: ['EXTCODESIZE', 700, true, true],
0x3c: ['EXTCODECOPY', 700, true, true],
0x3b: ['EXTCODESIZE', 700, true],
0x3c: ['EXTCODECOPY', 700, true],
0x3d: ['RETURNDATASIZE', 2, true],
0x3e: ['RETURNDATACOPY', 3, true],
0x3f: ['EXTCODEHASH', 400, true, true],
0x3f: ['EXTCODEHASH', 400, true],

// '0x40' range - block operations
0x40: ['BLOCKHASH', 20, true, true],
0x40: ['BLOCKHASH', 20, true],
0x41: ['COINBASE', 2, true],
0x42: ['TIMESTAMP', 2, true],
0x43: ['NUMBER', 2, true],
0x44: ['DIFFICULTY', 2, true],
0x45: ['GASLIMIT', 2, true],
0x46: ['CHAINID', 2, false],

// 0x50 range - 'storage' and execution
0x50: ['POP', 2, false],
0x51: ['MLOAD', 3, false],
0x52: ['MSTORE', 3, false],
0x53: ['MSTORE8', 3, false],
0x54: ['SLOAD', 200, true, true],
0x55: ['SSTORE', 0, true, true],
0x54: ['SLOAD', 200, true],
0x55: ['SSTORE', 0, true],
0x56: ['JUMP', 8, false],
0x57: ['JUMPI', 10, false],
0x58: ['PC', 2, false],
Expand Down Expand Up @@ -157,22 +155,32 @@ const codes: any = {
0xa4: ['LOG', 375, false],

// '0xf0' range - closures
0xf0: ['CREATE', 32000, true, true],
0xf1: ['CALL', 700, true, true],
0xf2: ['CALLCODE', 700, true, true],
0xf0: ['CREATE', 32000, true],
0xf1: ['CALL', 700, true],
0xf2: ['CALLCODE', 700, true],
0xf3: ['RETURN', 0, false],
0xf4: ['DELEGATECALL', 700, true, true],
0xf5: ['CREATE2', 32000, true, true],
0xfa: ['STATICCALL', 700, true, true],
0xf4: ['DELEGATECALL', 700, true],
0xf5: ['CREATE2', 32000, true],
0xfa: ['STATICCALL', 700, true],
0xfd: ['REVERT', 0, false],

// '0x70', range - other
0xfe: ['INVALID', 0, false],
0xff: ['SELFDESTRUCT', 5000, false, true],
0xff: ['SELFDESTRUCT', 5000, true],
}

const istanbulOpcodes: any = {
0x46: ['CHAINID', 2, false],
}

export function setOpcodes(hf: string) {
if (hf === 'istanbul') {
codes = { ...codes, ...istanbulOpcodes }
}
}

export function lookupOpInfo(op: number, full: boolean = false): OpInfo {
const code = codes[op] ? codes[op] : ['INVALID', 0, false, false]
const code = codes[op] ? codes[op] : ['INVALID', 0, false]
let opcode = code[0]

if (full) {
Expand All @@ -197,7 +205,6 @@ export function lookupOpInfo(op: number, full: boolean = false): OpInfo {
name: opcode,
opcode: op,
fee: code[1],
dynamic: code[2],
isAsync: code[3],
isAsync: code[2],
}
}
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { default as runCall, RunCallOpts } from './runCall'
import { default as runTx, RunTxOpts, RunTxResult } from './runTx'
import { default as runBlock, RunBlockOpts, RunBlockResult } from './runBlock'
import { EVMResult, ExecResult } from './evm/evm'
import { setOpcodes } from './evm/opcodes'
import runBlockchain from './runBlockchain'
const promisify = require('util.promisify')
const AsyncEventEmitter = require('async-eventemitter')
Expand Down Expand Up @@ -109,6 +110,9 @@ export default class VM extends AsyncEventEmitter {

this.allowUnlimitedContractSize =
opts.allowUnlimitedContractSize === undefined ? false : opts.allowUnlimitedContractSize

// Set list of opcodes based on HF
setOpcodes(this._common.hardfork()!)
}

/**
Expand Down

0 comments on commit edffcde

Please sign in to comment.