Skip to content

Commit

Permalink
Add package w3vm for simulating EVM message execution (#58)
Browse files Browse the repository at this point in the history
* added multiEVMLogger

* added `defaultChainConfig`

* added `Receipt`

* switching to `go-ethereum` head

* added `VM` API

* added `Fetcher` interface

* added `Option`'s

* added `nilToZero` util

* moved and added zero values

* state: added `RPCFetcher`

* state: added `noopKeyValueStore`

* state: added `noopNodeIterator` and fixed `noopBatch`

* state: added `DB`

* state: renamed `account2` -> `account`

* added `defaultBlockContext`

* internal/mod: init

* state: added `Account` marshalling test

* state: added `forkState`

* state: added `NewTestingRPCFetcher`

* added `TestVMApply_Integration`

* added `VM` implementation

* w3types: using `types.EmptyCodeHash`

* upgraded `go-ethereum`

* filter nil-EVMLoggers in multiEVMLogger

* refactored db + drop all db storage logic

* moved `w3.RandA()` to `w3vm.RandA()`

* state: drop unused noop structs

* added testdata

* added `.gitattributes`

* try change selector

* move

* fixed name

* changed selector

* added `VM.Apply` tests

* added WETH storage slot utils

* added `VM` tests

* added `VM` Benchmark

* not always revert to zero, but take a snapshot in the before the call/apply instead

* disable BaseFee for calls

* fixing storage overwriting

* adding support for `WithFork`

* dependency upgrade

* added more integration test blocks

* ignore nil-Option's

* fixed repetitive state read

* state: fixed forkState change detection

* reverted dependency update

* updated comment

* upgraded to Go1.21

* dependency update

* dropped `Returner` interface for `CallFuncFactory` struct

* upgraded ´go-ethereum`

* return fetch error in vm state access methods

* refactored `VM`

* fixed `WithNoBaseFee` option

* doc update

* added support for pending block number

* state: changed API visibility

* merged package `w3vm/state` and `w3vm`

* return `ErrRevert` for revert errors

* improved doc

* updated `defaultBlockContext()`

* exposed `Slot` and `Slot2`

* added `WETHBalanceSlot` example

* doc update

* fixed fetcher setup in `New`

* doc update

---------

Co-authored-by: lmittmann <lmittmann@users.noreply.github.com>
  • Loading branch information
lmittmann and lmittmann authored Sep 3, 2023
1 parent dc84e45 commit 344bb21
Show file tree
Hide file tree
Showing 62 changed files with 101,889 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata/ linguist-generated=true
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'
- name: go fmt
run: |
gofmt -s -d . > fmt.out
Expand All @@ -27,7 +27,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'
- name: go test
run: go test -covermode atomic -coverprofile=covprofile ./...
- name: install goveralls
Expand Down
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func NewClient(client *rpc.Client, opts ...Option) *Client {
client: client,
}
for _, opt := range opts {
if opt == nil {
continue
}
opt(c)
}
return c
Expand Down
16 changes: 9 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module github.com/lmittmann/w3

go 1.18
go 1.21

require (
github.com/ethereum/go-ethereum v1.12.2
github.com/google/go-cmp v0.5.9
github.com/holiman/uint256 v1.2.3
golang.org/x/sync v0.3.0
golang.org/x/time v0.3.0
)

Expand All @@ -28,35 +29,36 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/c-kzg-4844 v0.3.1 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rivo/uniseg v0.4.2 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/exp v0.0.0-20230810033253-352e893a4cad // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
59 changes: 48 additions & 11 deletions go.sum

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions internal/mod/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mod

import (
"os/exec"
"strings"
)

// Root contains the absolute path of the module root. Its value is empty if
// used outside of a module.
var Root string

func init() {
stdout, _ := exec.Command("go", "env", "GOMOD").Output()

var ok bool
Root, ok = strings.CutSuffix(strings.TrimSpace(string(stdout)), "/go.mod")
if !ok {
Root = ""
}
}
14 changes: 14 additions & 0 deletions internal/mod/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mod_test

import (
"strings"
"testing"

"github.com/lmittmann/w3/internal/mod"
)

func TestModRoot(t *testing.T) {
if !strings.HasSuffix(mod.Root, "w3") {
t.Fatalf("Unexpected module root: %q", mod.Root)
}
}
6 changes: 6 additions & 0 deletions internal/module/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ func BlockNumberArg(blockNumber *big.Int) string {
if blockNumber == nil {
return "latest"
}

switch blockNumber.Int64() {
case -1:
return "pending"
}

return hexutil.EncodeBig(blockNumber)
}
7 changes: 0 additions & 7 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package w3

import (
"crypto/rand"
"encoding/hex"
"fmt"
"math/big"
Expand Down Expand Up @@ -50,12 +49,6 @@ func APtr(hexAddress string) *common.Address {
return &addr
}

// RandA returns a random address.
func RandA() (addr common.Address) {
rand.Read(addr[:])
return addr
}

// B returns a byte slice from a hexstring or panics if the hexstring does not
// represent a valid byte slice.
//
Expand Down
5 changes: 2 additions & 3 deletions w3types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/lmittmann/w3/internal/crypto"
)

var emptyCodeHash = crypto.Keccak256Hash(nil)

type State map[common.Address]*Account

// SetGenesisAlloc copies the given [core.GenesisAlloc] to the state and
Expand Down Expand Up @@ -44,7 +43,7 @@ func (acc *Account) CodeHash() common.Hash {
return *acc.codeHash
}
if len(acc.Code) == 0 {
return emptyCodeHash
return types.EmptyCodeHash
}

h := crypto.Keccak256Hash(acc.Code)
Expand Down
141 changes: 141 additions & 0 deletions w3vm/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package w3vm

import (
"errors"
"math/big"

"github.com/ethereum/go-ethereum/common"
gethState "github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/trienode"
"github.com/lmittmann/w3/internal/crypto"
)

var errNotFound = errors.New("not found")

// db implements the [state.Database] and [state.Trie] interfaces.
type db struct {
fetcher Fetcher
}

func newDB(fetcher Fetcher) *db {
return &db{
fetcher: fetcher,
}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// state.Database methods //////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

func (db *db) OpenTrie(root common.Hash) (gethState.Trie, error) { return db, nil }

func (db *db) OpenStorageTrie(stateRoot common.Hash, addr common.Address, root common.Hash) (gethState.Trie, error) {
return db, nil
}

func (*db) CopyTrie(gethState.Trie) gethState.Trie { panic("not implemented") }

func (db *db) ContractCode(addr common.Address, codeHash common.Hash) ([]byte, error) {
if db.fetcher == nil {
return []byte{}, nil
}

code, err := db.fetcher.Code(addr)
if err != nil {
return nil, errNotFound
}
return code, nil
}

func (db *db) ContractCodeSize(addr common.Address, codeHash common.Hash) (int, error) {
code, err := db.ContractCode(addr, codeHash)
if err != nil {
return 0, err
}
return len(code), nil
}

func (*db) DiskDB() ethdb.KeyValueStore { panic("not implemented") }

func (*db) TrieDB() *trie.Database { panic("not implemented") }

////////////////////////////////////////////////////////////////////////////////////////////////////
// state.Trie methods //////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

func (*db) GetKey([]byte) []byte { panic("not implemented") }

func (db *db) GetStorage(addr common.Address, key []byte) ([]byte, error) {
if db.fetcher == nil {
return []byte{}, nil
}

storageKey := common.BytesToHash(key)
storageVal, err := db.fetcher.StorageAt(addr, storageKey)
if err != nil {
return nil, err
}
return storageVal.Bytes(), nil
}

func (db *db) GetAccount(addr common.Address) (*types.StateAccount, error) {
if db.fetcher == nil {
return &types.StateAccount{
Balance: new(big.Int),
CodeHash: types.EmptyCodeHash[:],
}, nil
}

nonce, err := db.fetcher.Nonce(addr)
if err != nil {
return nil, err
}
balance, err := db.fetcher.Balance(addr)
if err != nil {
return nil, err
}
code, err := db.fetcher.Code(addr)
if err != nil {
return nil, err
}

var codeHash []byte
if len(code) == 0 {
codeHash = types.EmptyCodeHash[:]
} else {
codeHash = crypto.Keccak256(code)
}

return &types.StateAccount{
Nonce: nonce,
Balance: balance,
CodeHash: codeHash,
}, nil
}

func (*db) UpdateStorage(addr common.Address, key, value []byte) error { panic("not implemented") }

func (*db) UpdateAccount(addr common.Address, acc *types.StateAccount) error {
panic("not implemented")
}

func (*db) UpdateContractCode(addr common.Address, codeHash common.Hash, code []byte) error {
panic("not implemented")
}

func (*db) DeleteStorage(addr common.Address, key []byte) error { panic("not implemented") }

func (*db) DeleteAccount(addr common.Address) error { panic("not implemented") }

func (*db) Hash() common.Hash { panic("not implemented") }

func (*db) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error) {
panic("not implemented")
}

func (*db) NodeIterator(startKey []byte) (trie.NodeIterator, error) { panic("not implemented") }

func (*db) Prove(key []byte, proofDb ethdb.KeyValueWriter) error { panic("not implemented") }
Loading

0 comments on commit 344bb21

Please sign in to comment.