Skip to content

Commit b3f43c8

Browse files
authored
core/vm: expose jumptable constructors (#26880)
When interacting with geth as a library to e.g. produce state tests, it is desirable to obtain the consensus-correct jumptable definition for a given fork. This changes adds accessors so the instructionset can be obtained and characteristics about opcodes can be inspected.
1 parent 905a723 commit b3f43c8

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

core/vm/jump_table_export.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2023 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package vm
18+
19+
import (
20+
"errors"
21+
22+
"github.com/ethereum/go-ethereum/params"
23+
)
24+
25+
// LookupInstructionSet returns the instructionset for the fork configured by
26+
// the rules.
27+
func LookupInstructionSet(rules params.Rules) (JumpTable, error) {
28+
switch {
29+
case rules.IsPrague:
30+
return newShanghaiInstructionSet(), errors.New("prague-fork not defined yet")
31+
case rules.IsCancun:
32+
return newShanghaiInstructionSet(), errors.New("cancun-fork not defined yet")
33+
case rules.IsShanghai:
34+
return newShanghaiInstructionSet(), nil
35+
case rules.IsMerge:
36+
return newMergeInstructionSet(), nil
37+
case rules.IsLondon:
38+
return newLondonInstructionSet(), nil
39+
case rules.IsBerlin:
40+
return newBerlinInstructionSet(), nil
41+
case rules.IsIstanbul:
42+
return newIstanbulInstructionSet(), nil
43+
case rules.IsConstantinople:
44+
return newConstantinopleInstructionSet(), nil
45+
case rules.IsByzantium:
46+
return newByzantiumInstructionSet(), nil
47+
case rules.IsEIP158:
48+
return newSpuriousDragonInstructionSet(), nil
49+
case rules.IsEIP150:
50+
return newTangerineWhistleInstructionSet(), nil
51+
case rules.IsHomestead:
52+
return newHomesteadInstructionSet(), nil
53+
}
54+
return newFrontierInstructionSet(), nil
55+
}
56+
57+
// Stack returns the mininum and maximum stack requirements.
58+
func (op *operation) Stack() (int, int) {
59+
return op.minStack, op.maxStack
60+
}
61+
62+
// HasCost returns true if the opcode has a cost. Opcodes which do _not_ have
63+
// a cost assigned are one of two things:
64+
// - undefined, a.k.a invalid opcodes,
65+
// - the STOP opcode.
66+
// This method can thus be used to check if an opcode is "Invalid (or STOP)".
67+
func (op *operation) HasCost() bool {
68+
// Ideally, we'd check this:
69+
// return op.execute == opUndefined
70+
// However, go-lang does now allow that. So we'll just check some other
71+
// 'indicators' that this is an invalid op. Alas, STOP is impossible to
72+
// filter out
73+
return op.dynamicGas != nil || op.constantGas != 0
74+
}

params/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ type Rules struct {
956956
IsHomestead, IsEIP150, IsEIP155, IsEIP158 bool
957957
IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool
958958
IsBerlin, IsLondon bool
959-
IsMerge, IsShanghai, isCancun, isPrague bool
959+
IsMerge, IsShanghai, IsCancun, IsPrague bool
960960
}
961961

962962
// Rules ensures c's ChainID is not nil.
@@ -979,7 +979,7 @@ func (c *ChainConfig) Rules(num *big.Int, isMerge bool, timestamp uint64) Rules
979979
IsLondon: c.IsLondon(num),
980980
IsMerge: isMerge,
981981
IsShanghai: c.IsShanghai(timestamp),
982-
isCancun: c.IsCancun(timestamp),
983-
isPrague: c.IsPrague(timestamp),
982+
IsCancun: c.IsCancun(timestamp),
983+
IsPrague: c.IsPrague(timestamp),
984984
}
985985
}

0 commit comments

Comments
 (0)