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

imp(jump-table): make JumpTable Operation public #32

Draft
wants to merge 2 commits into
base: release/1.10
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions core/vm/eips.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func enable1884(jt *JumpTable) {
jt[EXTCODEHASH].constantGas = params.ExtcodeHashGasEIP1884

// New opcode
jt[SELFBALANCE] = &operation{
jt[SELFBALANCE] = &Operation{
execute: opSelfBalance,
constantGas: GasFastStep,
minStack: minStack(0, 1),
Expand All @@ -90,7 +90,7 @@ func opSelfBalance(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext)
// - Adds an opcode that returns the current chain’s EIP-155 unique identifier
func enable1344(jt *JumpTable) {
// New opcode
jt[CHAINID] = &operation{
jt[CHAINID] = &Operation{
execute: opChainID,
constantGas: GasQuickStep,
minStack: minStack(0, 1),
Expand Down Expand Up @@ -162,7 +162,7 @@ func enable3529(jt *JumpTable) {
// - Adds an opcode that returns the current block's base fee.
func enable3198(jt *JumpTable) {
// New opcode
jt[BASEFEE] = &operation{
jt[BASEFEE] = &Operation{
execute: opBaseFee,
constantGas: GasQuickStep,
minStack: minStack(0, 1),
Expand All @@ -180,7 +180,7 @@ func opBaseFee(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]
// enable3855 applies EIP-3855 (PUSH0 opcode)
func enable3855(jt *JumpTable) {
// New opcode
jt[PUSH0] = &operation{
jt[PUSH0] = &Operation{
execute: opPush0,
constantGas: GasQuickStep,
minStack: minStack(0, 1),
Expand Down
47 changes: 26 additions & 21 deletions core/vm/jump_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,30 @@ import (
type (
executionFunc func(pc *uint64, interpreter *EVMInterpreter, callContext *ScopeContext) ([]byte, error)
gasFunc func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error)
// memorySizeFunc returns the required size, and whether the operation overflowed a uint64
// memorySizeFunc returns the required size, and whether the Operation overflowed a uint64
memorySizeFunc func(*Stack) (size uint64, overflow bool)
)

type operation struct {
// execute is the operation function
type Operation struct {
// execute is the Operation function
execute executionFunc
constantGas uint64
dynamicGas gasFunc
// minStack tells how many stack items are required
minStack int
// maxStack specifies the max length the stack can have for this operation
// maxStack specifies the max length the stack can have for this Operation
// to not overflow the stack.
maxStack int

// memorySize returns the memory size required for the operation
// memorySize returns the memory size required for the Operation
memorySize memorySizeFunc
}

// UpdateConstantGas updates the constant gas for the Operation.
func (op *Operation) UpdateConstantGas(gas uint64) {
op.constantGas = gas
}

var (
FrontierInstructionSet = newFrontierInstructionSet()
HomesteadInstructionSet = newHomesteadInstructionSet()
Expand All @@ -58,7 +63,7 @@ var (
)

// JumpTable contains the EVM opcodes supported at a given fork.
type JumpTable [256]*operation
type JumpTable [256]*Operation

// DefaultJumpTable defines the default jump table used by the EVM interpreter.
func DefaultJumpTable(rules params.Rules) (jumpTable *JumpTable) {
Expand Down Expand Up @@ -88,7 +93,7 @@ func DefaultJumpTable(rules params.Rules) (jumpTable *JumpTable) {
return jumpTable
}

// Validate checks if all the operations are set and if they are valid according to the
// Validate checks if all the Operations are set and if they are valid according to the
// interpreter assumptions.
func (jt JumpTable) Validate() error {
for i, op := range jt {
Expand All @@ -110,7 +115,7 @@ func (jt JumpTable) Validate() error {
return nil
}

// MustValidate panics if the operations are not valid.
// MustValidate panics if the Operations are not valid.
func (jt JumpTable) MustValidate() {
if err := jt.Validate(); err != nil {
panic(err)
Expand All @@ -119,7 +124,7 @@ func (jt JumpTable) MustValidate() {

func newMergeInstructionSet() JumpTable {
instructionSet := newLondonInstructionSet()
instructionSet[RANDOM] = &operation{
instructionSet[RANDOM] = &Operation{
execute: opRandom,
constantGas: GasQuickStep,
minStack: minStack(0, 1),
Expand Down Expand Up @@ -165,31 +170,31 @@ func newIstanbulInstructionSet() JumpTable {
// byzantium and contantinople instructions.
func newConstantinopleInstructionSet() JumpTable {
instructionSet := newByzantiumInstructionSet()
instructionSet[SHL] = &operation{
instructionSet[SHL] = &Operation{
execute: opSHL,
constantGas: GasFastestStep,
minStack: minStack(2, 1),
maxStack: maxStack(2, 1),
}
instructionSet[SHR] = &operation{
instructionSet[SHR] = &Operation{
execute: opSHR,
constantGas: GasFastestStep,
minStack: minStack(2, 1),
maxStack: maxStack(2, 1),
}
instructionSet[SAR] = &operation{
instructionSet[SAR] = &Operation{
execute: opSAR,
constantGas: GasFastestStep,
minStack: minStack(2, 1),
maxStack: maxStack(2, 1),
}
instructionSet[EXTCODEHASH] = &operation{
instructionSet[EXTCODEHASH] = &Operation{
execute: opExtCodeHash,
constantGas: params.ExtcodeHashGasConstantinople,
minStack: minStack(1, 1),
maxStack: maxStack(1, 1),
}
instructionSet[CREATE2] = &operation{
instructionSet[CREATE2] = &Operation{
execute: opCreate2,
constantGas: params.Create2Gas,
dynamicGas: gasCreate2,
Expand All @@ -205,29 +210,29 @@ func newConstantinopleInstructionSet() JumpTable {
// byzantium instructions.
func newByzantiumInstructionSet() JumpTable {
instructionSet := newSpuriousDragonInstructionSet()
instructionSet[STATICCALL] = &operation{
instructionSet[STATICCALL] = &Operation{
execute: opStaticCall,
constantGas: params.CallGasEIP150,
dynamicGas: gasStaticCall,
minStack: minStack(6, 1),
maxStack: maxStack(6, 1),
memorySize: memoryStaticCall,
}
instructionSet[RETURNDATASIZE] = &operation{
instructionSet[RETURNDATASIZE] = &Operation{
execute: opReturnDataSize,
constantGas: GasQuickStep,
minStack: minStack(0, 1),
maxStack: maxStack(0, 1),
}
instructionSet[RETURNDATACOPY] = &operation{
instructionSet[RETURNDATACOPY] = &Operation{
execute: opReturnDataCopy,
constantGas: GasFastestStep,
dynamicGas: gasReturnDataCopy,
minStack: minStack(3, 0),
maxStack: maxStack(3, 0),
memorySize: memoryReturnDataCopy,
}
instructionSet[REVERT] = &operation{
instructionSet[REVERT] = &Operation{
execute: opRevert,
dynamicGas: gasRevert,
minStack: minStack(2, 0),
Expand Down Expand Up @@ -264,7 +269,7 @@ func newTangerineWhistleInstructionSet() JumpTable {
// instructions that can be executed during the homestead phase.
func newHomesteadInstructionSet() JumpTable {
instructionSet := newFrontierInstructionSet()
instructionSet[DELEGATECALL] = &operation{
instructionSet[DELEGATECALL] = &Operation{
execute: opDelegateCall,
dynamicGas: gasDelegateCall,
constantGas: params.CallGasFrontier,
Expand Down Expand Up @@ -1085,15 +1090,15 @@ func newFrontierInstructionSet() JumpTable {
// Fill all unassigned slots with opUndefined.
for i, entry := range tbl {
if entry == nil {
tbl[i] = &operation{execute: opUndefined, maxStack: maxStack(0, 0)}
tbl[i] = &Operation{execute: opUndefined, maxStack: maxStack(0, 0)}
}
}

tbl.MustValidate()
return tbl
}

// CopyJumpTable creates copy of the operations from the provided source JumpTable.
// CopyJumpTable creates copy of the Operations from the provided source JumpTable.
func CopyJumpTable(source *JumpTable) *JumpTable {
dest := *source
for i, op := range source {
Expand Down
Loading