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

Fix getOpcodesForHF for when HF > istanbul #647

Merged
merged 2 commits into from
Jan 8, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/evm/opcodes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Common from 'ethereumjs-common'

export interface Opcode {
name: string
fee: number
Expand Down Expand Up @@ -180,8 +182,8 @@ const istanbulOpcodes: OpcodeList = {
0x54: { name: 'SLOAD', fee: 800, isAsync: true },
}

export function getOpcodesForHF(hf: string) {
if (hf === 'istanbul') {
export function getOpcodesForHF(common: Common) {
if (common.gteHardfork('istanbul')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups. Yeah, well. 🙃

return { ...opcodes, ...istanbulOpcodes }
} else {
return { ...opcodes }
Expand Down
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default class VM extends AsyncEventEmitter {
}

// Set list of opcodes based on HF
this._opcodes = getOpcodesForHF(this._common.hardfork()!)
this._opcodes = getOpcodesForHF(this._common)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for fixing this, @s1na :)

if (opts.stateManager) {
this.stateManager = opts.stateManager
Expand Down
2 changes: 1 addition & 1 deletion tests/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ tape('VM with default blockchain', (t) => {
})

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

const vm = new VM({ common })
st.equal(vm._common, common)
Expand Down
26 changes: 26 additions & 0 deletions tests/api/opcodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const tape = require('tape')
const { getOpcodesForHF } = require('../../dist/evm/opcodes')
const Common = require('ethereumjs-common').default

const CHAINID = 0x46

tape('getOpcodesForHF', (t) => {
t.test('shouldnt apply istanbul opcode changes for petersburg', (st) => {
const c = new Common('mainnet', 'petersburg')
const opcodes = getOpcodesForHF(c)
st.assert(opcodes[CHAINID] === undefined)
st.end()
})

t.test('should correctly apply istanbul opcode when hf >= istanbul', (st) => {
let c = new Common('mainnet', 'istanbul')
let opcodes = getOpcodesForHF(c)
st.equal(opcodes[CHAINID].name, 'CHAINID')

c = new Common('mainnet', 'muirGlacier')
opcodes = getOpcodesForHF(c)
st.equal(opcodes[CHAINID].name, 'CHAINID')

st.end()
})
})