Commit b3016505 authored by yann300's avatar yann300

fix parsing codes

parent cb47ed68
'use strict' 'use strict'
import Common from '@ethereumjs/common' import Common from '@ethereumjs/common'
import { getOpcodesForHF } from '@ethereumjs/vm/dist/evm/opcodes' import { getOpcodesForHF } from '@ethereumjs/vm/dist/evm/opcodes'
import getOpcodes from './opcodes'
export function nameOpCodes (raw, hardfork) { export function nameOpCodes (raw, hardfork) {
const common = new Common({ chain: 'mainnet', hardfork }) const common = new Common({ chain: 'mainnet', hardfork })
...@@ -34,6 +35,12 @@ export function nameOpCodes (raw, hardfork) { ...@@ -34,6 +35,12 @@ export function nameOpCodes (raw, hardfork) {
return [code, codeMap] return [code, codeMap]
} }
type Opcode = {
name: String,
pushData?: Array<number>
in?: number
out?: number
}
/** /**
* Parses code as a list of integers into a list of objects containing * Parses code as a list of integers into a list of objects containing
* information about the opcode. * information about the opcode.
...@@ -44,15 +51,19 @@ export function parseCode (raw) { ...@@ -44,15 +51,19 @@ export function parseCode (raw) {
const code = [] const code = []
for (let i = 0; i < raw.length; i++) { for (let i = 0; i < raw.length; i++) {
let opcode const opcode: Opcode = { name: 'INVALID' }
try { try {
opcode = opcodes.get(raw[i]).fullName const code = opcodes.get(raw[i])
const opcodeDetails = getOpcodes(raw[i], false)
opcode.in = opcodeDetails.in
opcode.out = opcodeDetails.out
opcode.name = code.fullName
} catch (e) { } catch (e) {
opcode = 'INVALID' opcode.name = 'INVALID'
} }
if (opcode.name.slice(0, 4) === 'PUSH') { if (opcode.name.slice(0, 4) === 'PUSH') {
const length = raw[i] - 0x5f const length = raw[i] - 0x5f
opcode['pushData'] = raw.slice(i + 1, i + length + 1) opcode.pushData = raw.slice(i + 1, i + length + 1)
// in case pushdata extends beyond code // in case pushdata extends beyond code
if (i + 1 + length > raw.length) { if (i + 1 + length > raw.length) {
for (let j = opcode['pushData'].length; j < length; j++) { for (let j = opcode['pushData'].length; j < length; j++) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment