Skip to content

Commit

Permalink
refactor: allow constant and dynamic gas simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR4N committed Sep 18, 2024
1 parent 6a5de75 commit 7d054b9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
37 changes: 11 additions & 26 deletions core/vm/jump_table.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,24 @@ func overrideJumpTable(r params.Rules, jt *JumpTable) *JumpTable {
}

// An OperationBuilder is a factory for a new operations to include in a
// [JumpTable]. All of its fields are required.
type OperationBuilder[G interface {
uint64 | func(_ *EVM, _ *Contract, _ *Stack, _ *Memory, requestedMemorySize uint64) (uint64, error)
}] struct {
// [JumpTable].
type OperationBuilder struct {
Execute func(pc *uint64, interpreter *EVMInterpreter, callContext *ScopeContext) ([]byte, error)
Gas G
ConstantGas uint64
DynamicGas func(_ *EVM, _ *Contract, _ *Stack, _ *Memory, requestedMemorySize uint64) (uint64, error)
MinStack, MaxStack int
MemorySize func(s *Stack) (size uint64, overflow bool)
}

type (
// OperationBuilderConstantGas is the constant-gas version of an
// OperationBuilder.
OperationBuilderConstantGas = OperationBuilder[uint64]
// OperationBuilderDynamicGas is the dynamic-gas version of an
// OperationBuilder.
OperationBuilderDynamicGas = OperationBuilder[func(_ *EVM, _ *Contract, _ *Stack, _ *Memory, requestedMemorySize uint64) (uint64, error)]
)

// Build constructs the operation.
func (b OperationBuilder[G]) Build() *operation {
func (b OperationBuilder) Build() *operation {
o := &operation{
execute: b.Execute,
minStack: b.MinStack,
maxStack: b.MaxStack,
memorySize: b.MemorySize,
}

switch g := any(b.Gas).(type) {
case uint64:
o.constantGas = g
case gasFunc:
o.dynamicGas = g
execute: b.Execute,
constantGas: b.ConstantGas,
dynamicGas: b.DynamicGas,
minStack: b.MinStack,
maxStack: b.MaxStack,
memorySize: b.MemorySize,
}
return o
}
Expand Down
6 changes: 3 additions & 3 deletions core/vm/jump_table.libevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func TestOverrideJumpTable(t *testing.T) {

vmHooks := &vmHooksStub{
replacement: &vm.JumpTable{
opcode: vm.OperationBuilderConstantGas{
opcode: vm.OperationBuilder{
Execute: func(pc *uint64, interpreter *vm.EVMInterpreter, callContext *vm.ScopeContext) ([]byte, error) {
executed = true
return nil, nil
},
Gas: gasCost,
ConstantGas: gasCost,
MemorySize: func(s *vm.Stack) (size uint64, overflow bool) {
return 0, false
},
Expand Down Expand Up @@ -102,6 +102,6 @@ func TestOverrideJumpTable(t *testing.T) {
func TestOperationFieldCount(t *testing.T) {
// The libevm OperationBuilder assumes that the 6 struct fields are the only
// ones.
op := vm.OperationBuilderConstantGas{}.Build()
op := vm.OperationBuilder{}.Build()
require.Equalf(t, 6, reflect.TypeOf(*op).NumField(), "number of fields in %T struct", *op)
}

0 comments on commit 7d054b9

Please sign in to comment.