Skip to content

Commit

Permalink
added Option's
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann committed Jul 16, 2023
1 parent ad833ba commit fd96f0d
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions w3vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@ package w3vm

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/params"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/w3types"
"github.com/lmittmann/w3/w3vm/state"
)

type VM struct{}
type VM struct {
opts *vmOptions
}

type vmOptions struct {
chainConfig *params.ChainConfig
preState w3types.State
blockCtx *vm.BlockContext
header *types.Header
forkClient *w3.Client
forkBlockNumber *big.Int
fetcher state.Fetcher
tb testing.TB
}

func New(opts ...Option) *VM {
c := &VM{}
c := &VM{opts: new(vmOptions)}
for _, opt := range opts {
opt(c)
}
Expand Down Expand Up @@ -78,14 +95,37 @@ func (vm *VM) StorageAt(addr common.Address, slot common.Hash) common.Hash {
// An Option configures a VM.
type Option func(*VM)

// WithChainConfig configures the VM's chain config.
func WithChainConfig(cfg *params.ChainConfig) Option {
return func(vm *VM) {}
return func(vm *VM) { vm.opts.chainConfig = cfg }
}

// WithBlockContext configures the VM's block context.
func WithBlockContext(ctx *vm.BlockContext) Option {
return func(vm *VM) {}
return func(vm *VM) { vm.opts.blockCtx = ctx }
}

// WithState sets the VM's pre state.
func WithState(state w3types.State) Option {
return func(vm *VM) { vm.opts.preState = state }
}

func WithFork(client *w3.Client, blockNumber *big.Int) Option {
return func(vm *VM) {
vm.opts.forkClient = client
vm.opts.forkBlockNumber = blockNumber
}
}

// WithHeader configures the VM's block context based on the given header.
func WithHeader(header *types.Header) Option {
return func(vm *VM) { vm.opts.header = header }
}

func WithFetcher(fetcher state.Fetcher) Option {
return func(vm *VM) { vm.opts.fetcher = fetcher }
}

func WithState(s w3types.State) Option {
return func(vm *VM) {}
func WithTB(tb testing.TB) Option {
return func(vm *VM) { vm.opts.tb = tb }
}

0 comments on commit fd96f0d

Please sign in to comment.