Skip to content

Commit 28caab5

Browse files
committed
vm: added dedicated EIP opcode activation list, added EIP-2315 to the list of supported opcodes
1 parent 03d00fd commit 28caab5

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

packages/vm/lib/evm/opcodes/codes.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,15 @@ const hardforkOpcodes = [
240240
0x47: { name: 'SELFBALANCE', isAsync: false }, // EIP 1884
241241
},
242242
},
243+
]
244+
245+
const eipOpcodes = [
243246
{
244-
hardforkName: 'berlin',
247+
eip: 2315,
245248
opcodes: {
246-
0x5c: { name: 'BEGINSUB', isAsync: false }, // EIP 2315
247-
0x5d: { name: 'RETURNSUB', isAsync: false }, // EIP 2315
248-
0x5e: { name: 'JUMPSUB', isAsync: false }, // EIP 2315
249+
0x5c: { name: 'BEGINSUB', isAsync: false },
250+
0x5d: { name: 'RETURNSUB', isAsync: false },
251+
0x5e: { name: 'JUMPSUB', isAsync: false },
249252
},
250253
},
251254
]
@@ -288,6 +291,11 @@ export function getOpcodesForHF(common: Common): OpcodeList {
288291
opcodeBuilder = { ...opcodeBuilder, ...hardforkOpcodes[fork].opcodes }
289292
}
290293
}
294+
for (const eipOps of eipOpcodes) {
295+
if (common.isActivatedEIP(eipOps.eip)) {
296+
opcodeBuilder = { ...opcodeBuilder, ...eipOps.opcodes }
297+
}
298+
}
291299

292300
/* eslint-disable-next-line no-restricted-syntax */
293301
for (const key in opcodeBuilder) {

packages/vm/lib/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ export interface VMOpts {
4040
*
4141
* ### Supported EIPs
4242
*
43+
* - [EIP-2315](https://eips.ethereum.org/EIPS/eip-2315) - VM simple subroutines
4344
* - [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537) (`experimental`) - BLS12-381 precompiles
44-
* - [EIP-2929](https://eips.ethereum.org/EIPS/eip-2929) (`experimental`) - Gas cost increases for state access opcodes
45+
* - [EIP-2929](https://eips.ethereum.org/EIPS/eip-2929) - Gas cost increases for state access opcodes
4546
*
4647
* *Annotations:*
4748
*
@@ -163,7 +164,7 @@ export default class VM extends AsyncEventEmitter {
163164

164165
if (opts.common) {
165166
//EIPs
166-
const supportedEIPs = [2537, 2565, 2718, 2929, 2930]
167+
const supportedEIPs = [2315, 2537, 2565, 2718, 2929, 2930]
167168
for (const eip of opts.common.eips()) {
168169
if (!supportedEIPs.includes(eip)) {
169170
throw new Error(`${eip} is not supported by the VM`)

packages/vm/tests/api/opcodes.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import VM from '../../lib'
44

55
tape('VM -> getActiveOpcodes()', (t) => {
66
const CHAINID = 0x46 //istanbul opcode
7+
const BEGINSUB = 0x5c // EIP-2315 opcode
78

89
t.test('should not expose opcodes from a follow-up HF (istanbul -> petersburg)', (st) => {
910
const common = new Common({ chain: 'mainnet', hardfork: 'petersburg' })
@@ -36,6 +37,26 @@ tape('VM -> getActiveOpcodes()', (t) => {
3637
st.end()
3738
})
3839

40+
t.test('should expose opcodes when EIP is active', (st) => {
41+
let common = new Common({ chain: 'mainnet', hardfork: 'istanbul', eips: [2315] })
42+
let vm = new VM({ common })
43+
st.equal(
44+
vm.getActiveOpcodes().get(BEGINSUB)!.name,
45+
'BEGINSUB',
46+
'EIP-2315 opcode BEGINSUB exposed (EIP-2315 activated)'
47+
)
48+
49+
common = new Common({ chain: 'mainnet', hardfork: 'istanbul' })
50+
vm = new VM({ common })
51+
st.equal(
52+
vm.getActiveOpcodes().get(BEGINSUB),
53+
undefined,
54+
'EIP-2315 opcode BEGINSUB not exposed (EIP-2315 not activated)'
55+
)
56+
57+
st.end()
58+
})
59+
3960
t.test('should update opcodes on a hardfork change', async (st) => {
4061
const common = new Common({ chain: 'mainnet', hardfork: 'istanbul' })
4162
const vm = new VM({ common })

0 commit comments

Comments
 (0)