Skip to content

Commit 2d9c923

Browse files
authored
Merge pull request ethereumjs#647 from ethereumjs/fix/getOpcodesForHF
Fix getOpcodesForHF for when HF > istanbul
2 parents d5475fa + 57f2915 commit 2d9c923

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

lib/evm/opcodes.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Common from 'ethereumjs-common'
2+
13
export interface Opcode {
24
name: string
35
fee: number
@@ -180,8 +182,8 @@ const istanbulOpcodes: OpcodeList = {
180182
0x54: { name: 'SLOAD', fee: 800, isAsync: true },
181183
}
182184

183-
export function getOpcodesForHF(hf: string) {
184-
if (hf === 'istanbul') {
185+
export function getOpcodesForHF(common: Common) {
186+
if (common.gteHardfork('istanbul')) {
185187
return { ...opcodes, ...istanbulOpcodes }
186188
} else {
187189
return { ...opcodes }

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export default class VM extends AsyncEventEmitter {
110110
}
111111

112112
// Set list of opcodes based on HF
113-
this._opcodes = getOpcodesForHF(this._common.hardfork()!)
113+
this._opcodes = getOpcodesForHF(this._common)
114114

115115
if (opts.stateManager) {
116116
this.stateManager = opts.stateManager

tests/api/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ tape('VM with default blockchain', (t) => {
4343
})
4444

4545
t.test('should accept a common object as option', (st) => {
46-
const common = new Common('mainnet')
46+
const common = new Common('mainnet', 'istanbul')
4747

4848
const vm = new VM({ common })
4949
st.equal(vm._common, common)

tests/api/opcodes.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const tape = require('tape')
2+
const { getOpcodesForHF } = require('../../dist/evm/opcodes')
3+
const Common = require('ethereumjs-common').default
4+
5+
const CHAINID = 0x46
6+
7+
tape('getOpcodesForHF', (t) => {
8+
t.test('shouldnt apply istanbul opcode changes for petersburg', (st) => {
9+
const c = new Common('mainnet', 'petersburg')
10+
const opcodes = getOpcodesForHF(c)
11+
st.assert(opcodes[CHAINID] === undefined)
12+
st.end()
13+
})
14+
15+
t.test('should correctly apply istanbul opcode when hf >= istanbul', (st) => {
16+
let c = new Common('mainnet', 'istanbul')
17+
let opcodes = getOpcodesForHF(c)
18+
st.equal(opcodes[CHAINID].name, 'CHAINID')
19+
20+
c = new Common('mainnet', 'muirGlacier')
21+
opcodes = getOpcodesForHF(c)
22+
st.equal(opcodes[CHAINID].name, 'CHAINID')
23+
24+
st.end()
25+
})
26+
})

0 commit comments

Comments
 (0)