Skip to content

Commit

Permalink
return fetch error in vm state access methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann committed Aug 16, 2023
1 parent fcc0aff commit cba23b2
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 17 deletions.
54 changes: 42 additions & 12 deletions w3vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package w3vm

import (
"encoding/binary"
"errors"
"fmt"
"math/big"
"testing"
Expand All @@ -24,6 +25,9 @@ import (
var (
big1 = big.NewInt(1)
pendingBlockNumber = big.NewInt(-1)

ErrFetch = errors.New("fetch error")
ErrRevert = errors.New("revert error")
)

type VM struct {
Expand Down Expand Up @@ -52,7 +56,7 @@ type vmOptions struct {
tb testing.TB
}

func New(opts ...Option) *VM {
func New(opts ...Option) (*VM, error) {
v := &VM{opts: new(vmOptions)}
for _, opt := range opts {
if opt == nil {
Expand All @@ -79,7 +83,9 @@ func New(opts ...Option) *VM {
}
}

v.opts.forkClient.Call(calls...) // TODO: check error
if err := v.opts.forkClient.Call(calls...); err != nil {
return nil, fmt.Errorf("%w: failed to fetch header: %v", ErrFetch, err)
}

if latest || v.opts.tb == nil {
v.fetcher = state.NewRPCFetcher(v.opts.forkClient, v.opts.forkBlockNumber)
Expand Down Expand Up @@ -122,14 +128,18 @@ func New(opts ...Option) *VM {
v.db.SetState(addr, slot, val)
}
}
return v
return v, nil
}

func (v *VM) Apply(msg *w3types.Message, tracers ...vm.EVMLogger) (*Receipt, error) {
return v.apply(msg, false, newMultiEVMLogger(tracers))
}

func (v *VM) apply(msg *w3types.Message, isCall bool, tracer vm.EVMLogger) (*Receipt, error) {
if v.db.Error() != nil {
return nil, ErrFetch
}

coreMsg, txCtx, err := v.buildMessage(msg, isCall)
if err != nil {
return nil, err
Expand Down Expand Up @@ -212,29 +222,49 @@ func (cff *CallFuncFactory) Returns(returns ...any) error {
}

// Nonce returns the nonce of Address addr.
func (v *VM) Nonce(addr common.Address) uint64 {
return v.db.GetNonce(addr)
func (v *VM) Nonce(addr common.Address) (uint64, error) {
nonce := v.db.GetNonce(addr)
if v.db.Error() != nil {
return 0, fmt.Errorf("%w: failed to fetch nonce of %s", ErrFetch, addr)
}
return nonce, nil
}

// Balance returns the balance of Address addr.
func (v *VM) Balance(addr common.Address) *big.Int {
return v.db.GetBalance(addr)
func (v *VM) Balance(addr common.Address) (*big.Int, error) {
balance := v.db.GetBalance(addr)
if v.db.Error() != nil {
return nil, fmt.Errorf("%w: failed to fetch balance of %s", ErrFetch, addr)
}
return balance, nil
}

// Code returns the code of Address addr.
func (v *VM) Code(addr common.Address) []byte {
return v.db.GetCode(addr)
func (v *VM) Code(addr common.Address) ([]byte, error) {
code := v.db.GetCode(addr)
if v.db.Error() != nil {
return nil, fmt.Errorf("%w: failed to fetch code of %s", ErrFetch, addr)
}
return code, nil
}

// StorageAt returns the state of Address addr at the give storage Hash slot.
func (v *VM) StorageAt(addr common.Address, slot common.Hash) common.Hash {
return v.db.GetState(addr, slot)
func (v *VM) StorageAt(addr common.Address, slot common.Hash) (common.Hash, error) {
val := v.db.GetState(addr, slot)
if v.db.Error() != nil {
return hash0, fmt.Errorf("%w: failed to fetch storage of %s at %s", ErrFetch, addr, slot)
}
return val, nil
}

func (v *VM) buildMessage(msg *w3types.Message, skipAccChecks bool) (*core.Message, *vm.TxContext, error) {
nonce := msg.Nonce
if nonce == 0 && !skipAccChecks && msg.From != addr0 {
nonce = v.Nonce(msg.From)
var err error
nonce, err = v.Nonce(msg.From)
if err != nil {
return nil, nil, err
}
}

gasLimit := msg.Gas
Expand Down
67 changes: 62 additions & 5 deletions w3vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func TestVMApply(t *testing.T) {

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
vm := w3vm.New(
vm, _ := w3vm.New(
w3vm.WithState(test.PreState),
)
gotReceipt, gotErr := vm.Apply(test.Message)
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestVMCall(t *testing.T) {

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
vm := w3vm.New(
vm, _ := w3vm.New(
w3vm.WithState(test.PreState),
)
gotReceipt, gotErr := vm.Call(test.Message)
Expand All @@ -232,7 +232,7 @@ func TestVMCall(t *testing.T) {
}

func TestVMCallFunc(t *testing.T) {
vm := w3vm.New(
vm, _ := w3vm.New(
w3vm.WithState(w3types.State{
addrWETH: {
Code: codeWETH,
Expand All @@ -254,6 +254,63 @@ func TestVMCallFunc(t *testing.T) {
}
}

func TestVM_Fetcher(t *testing.T) {
f := new(testFetcher)
vm, err := w3vm.New(
w3vm.WithFetcher(f),
)
if err != nil {
t.Fatalf("Failed to create VM: %v", err)
}

_, err = vm.Nonce(addr0)
want := "fetch error: failed to fetch nonce of 0x0000000000000000000000000000000000000000"
if !errors.Is(err, w3vm.ErrFetch) || want != err.Error() {
t.Errorf("Nonce: want %q, got %q", want, err)
}

_, err = vm.Balance(addr0)
want = "fetch error: failed to fetch balance of 0x0000000000000000000000000000000000000000"
if !errors.Is(err, w3vm.ErrFetch) || want != err.Error() {
t.Errorf("Balance: want %q, got %q", want, err)
}

_, err = vm.Code(addr0)
want = "fetch error: failed to fetch code of 0x0000000000000000000000000000000000000000"
if !errors.Is(err, w3vm.ErrFetch) || want != err.Error() {
t.Errorf("Code: want %q, got %q", want, err)
}

_, err = vm.StorageAt(addr0, common.Hash{})
want = "fetch error: failed to fetch storage of 0x0000000000000000000000000000000000000000 at 0x0000000000000000000000000000000000000000000000000000000000000000"
if !errors.Is(err, w3vm.ErrFetch) || want != err.Error() {
t.Errorf("StorageAt: want %q, got %q", want, err)
}
}

type testFetcher struct{}

func (f *testFetcher) Nonce(addr common.Address) (uint64, error) {
return 0, fmt.Errorf("%w: failed to fetch nonce", w3vm.ErrFetch)
}

func (f *testFetcher) Balance(addr common.Address) (*big.Int, error) {
// return nil, fmt.Errorf("%w: failed to fetch balance", w3vm.ErrFetch)
return big.NewInt(1), nil
}

func (f *testFetcher) Code(addr common.Address) ([]byte, error) {
return nil, fmt.Errorf("%w: failed to code", w3vm.ErrFetch)
}

func (f *testFetcher) StorageAt(addr common.Address, key common.Hash) (common.Hash, error) {
return common.Hash{}, fmt.Errorf("%w: failed to fetch storage", w3vm.ErrFetch)
}

func (f *testFetcher) HeaderHash(blockNumber *big.Int) (common.Hash, error) {
return common.Hash{}, fmt.Errorf("%w: failed to fetch code hash", w3vm.ErrFetch)
}

func TestVMApply_Integration(t *testing.T) {
blocks := []*big.Int{
big.NewInt(4_369_998),
Expand Down Expand Up @@ -320,7 +377,7 @@ func TestVMApply_Integration(t *testing.T) {
}

f := state.NewTestingRPCFetcher(t, client, new(big.Int).Sub(number, w3.Big1))
vm := w3vm.New(
vm, _ := w3vm.New(
w3vm.WithFetcher(f),
w3vm.WithHeader(block.Header()),
)
Expand Down Expand Up @@ -406,7 +463,7 @@ func BenchmarkTransferWETH9(b *testing.B) {
}

b.Run("w3vm", func(b *testing.B) {
vm := w3vm.New(
vm, _ := w3vm.New(
w3vm.WithBlockContext(&blockCtx),
w3vm.WithChainConfig(params.AllEthashProtocolChanges),
w3vm.WithState(w3types.State{
Expand Down

0 comments on commit cba23b2

Please sign in to comment.