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

patches for subnet-evm #11

Closed
wants to merge 10 commits into from
64 changes: 64 additions & 0 deletions core/state/state.libevm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2024 the libevm authors.
//
// The libevm additions to go-ethereum are free software: you can redistribute
// them and/or modify them under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The libevm additions are distributed in the hope that they will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see
// <http://www.gnu.org/licenses/>.

package state

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

// GetExtra returns the extra payload from the [types.StateAccount] associated
// with the address, or a zero-value `SA` if not found. The
// [types.ExtraPayloads] MUST be sourced from [types.RegisterExtras].
func GetExtra[SA any](s *StateDB, p types.ExtraPayloads[SA], addr common.Address) SA {
stateObject := s.getStateObject(addr)
if stateObject != nil {
return p.FromStateAccount(&stateObject.data)
}
var zero SA
return zero
}

// SetExtra sets the extra payload for the address. See [GetExtra] for details.
func SetExtra[SA any](s *StateDB, p types.ExtraPayloads[SA], addr common.Address, extra SA) {
stateObject := s.getOrNewStateObject(addr)
if stateObject != nil {
setExtraOnObject(stateObject, p, addr, extra)
}
}

func setExtraOnObject[SA any](s *stateObject, p types.ExtraPayloads[SA], addr common.Address, extra SA) {
s.db.journal.append(extraChange[SA]{
payloads: p,
account: &addr,
prev: p.FromStateAccount(&s.data),
})
p.SetOnStateAccount(&s.data, extra)
}

// extraChange is a [journalEntry] for [SetExtra] / [setExtraOnObject].
type extraChange[SA any] struct {
payloads types.ExtraPayloads[SA]
account *common.Address
prev SA
}

func (e extraChange[SA]) dirtied() *common.Address { return e.account }

func (e extraChange[SA]) revert(s *StateDB) {
e.payloads.SetOnStateAccount(&s.getStateObject(*e.account).data, e.prev)
}
172 changes: 172 additions & 0 deletions core/state/state.libevm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// Copyright 2024 the libevm authors.
//
// The libevm additions to go-ethereum are free software: you can redistribute
// them and/or modify them under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The libevm additions are distributed in the hope that they will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see
// <http://www.gnu.org/licenses/>.

package state_test

import (
"fmt"
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/state/snapshot"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb/memorydb"
"github.com/ethereum/go-ethereum/libevm/ethtest"
"github.com/ethereum/go-ethereum/triedb"
)

func TestGetSetExtra(t *testing.T) {
type accountExtra struct {
// Data is a pointer to test deep copying.
Data *[]byte // MUST be exported; I spent 20 minutes investigating failing tests because I'm an idiot
}

types.TestOnlyClearRegisteredExtras()
t.Cleanup(types.TestOnlyClearRegisteredExtras)
// Just as its Data field is a pointer, the registered type is a pointer to
// test deep copying.
payloads := types.RegisterExtras[*accountExtra]()

rng := ethtest.NewPseudoRand(42)
addr := rng.Address()
nonce := rng.Uint64()
balance := rng.Uint256()
buf := rng.Bytes(8)
extra := &accountExtra{Data: &buf}

views := newWithSnaps(t)
stateDB := views.newStateDB(t, types.EmptyRootHash)

assert.Nilf(t, state.GetExtra(stateDB, payloads, addr), "state.GetExtra() returns zero-value %T if before account creation", extra)
stateDB.CreateAccount(addr)
stateDB.SetNonce(addr, nonce)
stateDB.SetBalance(addr, balance)
assert.Nilf(t, state.GetExtra(stateDB, payloads, addr), "state.GetExtra() returns zero-value %T if after account creation but before SetExtra()", extra)
state.SetExtra(stateDB, payloads, addr, extra)
require.Equal(t, extra, state.GetExtra(stateDB, payloads, addr), "state.GetExtra() immediately after SetExtra()")

root, err := stateDB.Commit(1, false) // arbitrary block number
require.NoErrorf(t, err, "%T.Commit(1, false)", stateDB)
require.NotEqualf(t, types.EmptyRootHash, root, "root hash returned by %T.Commit() is not the empty root", stateDB)

t.Run(fmt.Sprintf("retrieve from %T", views.snaps), func(t *testing.T) {
iter, err := views.snaps.AccountIterator(root, common.Hash{})
require.NoErrorf(t, err, "%T.AccountIterator(...)", views.snaps)
defer iter.Release()

require.Truef(t, iter.Next(), "%T.Next() (i.e. at least one account)", iter)
require.NoErrorf(t, iter.Error(), "%T.Error()", iter)

t.Run("types.FullAccount()", func(t *testing.T) {
got, err := types.FullAccount(iter.Account())
require.NoErrorf(t, err, "types.FullAccount(%T.Account())", iter)

want := &types.StateAccount{
Nonce: nonce,
Balance: balance,
Root: types.EmptyRootHash,
CodeHash: types.EmptyCodeHash[:],
}
payloads.SetOnStateAccount(want, extra)

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("types.FullAccount(%T.Account()) diff (-want +got):\n%s", iter, diff)
}
})

require.Falsef(t, iter.Next(), "%T.Next() after first account (i.e. only one)", iter)
})

t.Run(fmt.Sprintf("retrieve from new %T", stateDB), func(t *testing.T) {
s := views.newStateDB(t, root)
assert.Equalf(t, nonce, s.GetNonce(addr), "%T.GetNonce()", s)
assert.Equalf(t, balance, s.GetBalance(addr), "%T.GetBalance()", s)
assert.Equal(t, extra, state.GetExtra(s, payloads, addr), "state.GetExtra()")
})

t.Run("reverting to snapshot", func(t *testing.T) {
s := views.newStateDB(t, root)
snap := s.Snapshot()

oldExtra := extra
buf := append(*oldExtra.Data, rng.Bytes(8)...)
newExtra := &accountExtra{Data: &buf}

state.SetExtra(s, payloads, addr, newExtra)
assert.Equalf(t, newExtra, state.GetExtra(s, payloads, addr), "state.GetExtra() after overwriting with new value")
s.RevertToSnapshot(snap)
assert.Equalf(t, oldExtra, state.GetExtra(s, payloads, addr), "state.GetExtra() after reverting to snapshot")
})

t.Run(fmt.Sprintf("%T.Copy()", stateDB), func(t *testing.T) {
require.Equalf(t, reflect.Pointer, reflect.TypeOf(extra).Kind(), "extra-payload type")
require.Equalf(t, reflect.Pointer, reflect.TypeOf(extra.Data).Kind(), "extra-payload field")

orig := views.newStateDB(t, root)
cp := orig.Copy()

oldExtra := extra
buf := append(*oldExtra.Data, rng.Bytes(8)...)
newExtra := &accountExtra{Data: &buf}

assert.Equalf(t, oldExtra, state.GetExtra(orig, payloads, addr), "GetExtra([original %T]) before setting", orig)
assert.Equalf(t, oldExtra, state.GetExtra(cp, payloads, addr), "GetExtra([copy of %T]) returns the same payload", orig)
state.SetExtra(orig, payloads, addr, newExtra)
assert.Equalf(t, newExtra, state.GetExtra(orig, payloads, addr), "GetExtra([original %T]) returns overwritten payload", orig)
assert.Equalf(t, oldExtra, state.GetExtra(cp, payloads, addr), "GetExtra([copy of %T]) returns original payload despite overwriting on original", orig)
})
}

// stateViews are different ways to access the same data.
type stateViews struct {
snaps *snapshot.Tree
database state.Database
}

func (v stateViews) newStateDB(t *testing.T, root common.Hash) *state.StateDB {
t.Helper()
s, err := state.New(root, v.database, v.snaps)
require.NoError(t, err, "state.New()")
return s
}

func newWithSnaps(t *testing.T) stateViews {
t.Helper()
empty := types.EmptyRootHash
kvStore := memorydb.New()
ethDB := rawdb.NewDatabase(kvStore)
snaps, err := snapshot.New(
snapshot.Config{
CacheSize: 16, // Mb (arbitrary but non-zero)
},
kvStore,
triedb.NewDatabase(ethDB, nil),
empty,
)
require.NoError(t, err, "snapshot.New()")

return stateViews{
snaps: snaps,
database: state.NewDatabase(ethDB),
}
}
2 changes: 1 addition & 1 deletion core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type stateObject struct {

// empty returns whether the account is considered empty.
func (s *stateObject) empty() bool {
return s.data.Nonce == 0 && s.data.Balance.IsZero() && bytes.Equal(s.data.CodeHash, types.EmptyCodeHash.Bytes())
return s.data.Nonce == 0 && s.data.Balance.IsZero() && bytes.Equal(s.data.CodeHash, types.EmptyCodeHash.Bytes()) && s.data.Extra.IsZero()
}

// newObject creates a state object.
Expand Down
26 changes: 24 additions & 2 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package state

import (
"fmt"
"reflect"
"sort"
"time"

Expand Down Expand Up @@ -47,6 +48,19 @@ type revision struct {
journalIndex int
}

type snapshotTree interface {
Snapshot(root common.Hash) snapshot.Snapshot
Update(
blockRoot common.Hash,
parentRoot common.Hash,
destructs map[common.Hash]struct{},
accounts map[common.Hash][]byte,
storage map[common.Hash]map[common.Hash][]byte,
) error
StorageIterator(root common.Hash, account common.Hash, seek common.Hash) (snapshot.StorageIterator, error)
Cap(root common.Hash, layers int) error
}

// StateDB structs within the ethereum protocol are used to store anything
// within the merkle trie. StateDBs take care of caching and storing
// nested states. It's the general query interface to retrieve:
Expand All @@ -63,7 +77,7 @@ type StateDB struct {
prefetcher *triePrefetcher
trie Trie
hasher crypto.KeccakState
snaps *snapshot.Tree // Nil if snapshot is not available
snaps snapshotTree // Nil if snapshot is not available
snap snapshot.Snapshot // Nil if snapshot is not available

// originalRoot is the pre-state root, before any changes were made.
Expand Down Expand Up @@ -141,7 +155,15 @@ type StateDB struct {
}

// New creates a new state from a given trie.
func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
func New(root common.Hash, db Database, snaps snapshotTree) (*StateDB, error) {
if snaps != nil {
// XXX: Make sure we treat incoming `nil` ptrs as `nil` values, not an
// interface to a nil ptr
v := reflect.ValueOf(snaps)
if v.Kind() == reflect.Ptr && v.IsNil() {
snaps = nil
}
}
tr, err := db.OpenTrie(root)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions core/types/rlp_payload.libevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,7 @@ func (e *StateAccountExtra) Format(s fmt.State, verb rune) {
}
_, _ = s.Write([]byte(out))
}

func (e *StateAccountExtra) IsZero() bool {
return e == nil || e.t == nil || e.t.IsZero()
}
10 changes: 10 additions & 0 deletions eth/tracers/native/prestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ func (t *prestateTracer) lookupAccount(addr common.Address) {
// it to the prestate of the given contract. It assumes `lookupAccount`
// has been performed on the contract before.
func (t *prestateTracer) lookupStorage(addr common.Address, key common.Hash) {
// TODO: Refactor coreth test outside of eth/tracers/internal
// See https://github.com/ava-labs/coreth/pull/286 for context.
// lookupStorage assumes that lookupAccount has already been called.
// This assumption is violated for some historical blocks by the NativeAssetCall
// precompile. To fix this, we perform an extra call to lookupAccount here to ensure
// that the pre-state account is populated before attempting to read from the Storage
// map. When the invariant is maintained properly (since de-activation of the precompile),
// lookupAccount is a no-op. When the invariant is broken by the precompile, this avoids
// the panic and correctly captures the account prestate before the next opcode is executed.
t.lookupAccount(addr)
if _, ok := t.pre[addr].Storage[key]; ok {
return
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang/protobuf v1.5.3
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb
github.com/google/go-cmp v0.5.9
github.com/google/gofuzz v1.2.0
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.4.2
Expand Down Expand Up @@ -106,7 +107,6 @@ require (
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down
12 changes: 10 additions & 2 deletions params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ func newTimestampCompatError(what string, storedtime, newtime *uint64) *ConfigCo
NewTime: newtime,
RewindToTime: 0,
}
if rew != nil {
if rew != nil && *rew != 0 {
err.RewindToTime = *rew - 1
}
return err
Expand All @@ -890,7 +890,15 @@ func (err *ConfigCompatError) Error() string {
if err.StoredBlock != nil {
return fmt.Sprintf("mismatching %s in database (have block %d, want block %d, rewindto block %d)", err.What, err.StoredBlock, err.NewBlock, err.RewindToBlock)
}
return fmt.Sprintf("mismatching %s in database (have timestamp %d, want timestamp %d, rewindto timestamp %d)", err.What, err.StoredTime, err.NewTime, err.RewindToTime)

if err.StoredTime == nil && err.NewTime == nil {
return ""
} else if err.StoredTime == nil && err.NewTime != nil {
return fmt.Sprintf("mismatching %s in database (have timestamp nil, want timestamp %d, rewindto timestamp %d)", err.What, *err.NewTime, err.RewindToTime)
} else if err.StoredTime != nil && err.NewTime == nil {
return fmt.Sprintf("mismatching %s in database (have timestamp %d, want timestamp nil, rewindto timestamp %d)", err.What, *err.StoredTime, err.RewindToTime)
}
return fmt.Sprintf("mismatching %s in database (have timestamp %d, want timestamp %d, rewindto timestamp %d)", err.What, *err.StoredTime, *err.NewTime, err.RewindToTime)
}

// Rules wraps ChainConfig and is merely syntactic sugar or can be used for functions
Expand Down
18 changes: 18 additions & 0 deletions params/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/ethereum/go-ethereum/common/math"
"github.com/stretchr/testify/require"
)

func TestCheckCompatible(t *testing.T) {
Expand Down Expand Up @@ -137,3 +138,20 @@ func TestConfigRules(t *testing.T) {
t.Errorf("expected %v to be shanghai", stamp)
}
}

func TestTimestampCompatError(t *testing.T) {
require.Equal(t, new(ConfigCompatError).Error(), "")

errWhat := "Shanghai fork timestamp"
require.Equal(t, newTimestampCompatError(errWhat, nil, newUint64(1681338455)).Error(),
"mismatching Shanghai fork timestamp in database (have timestamp nil, want timestamp 1681338455, rewindto timestamp 1681338454)")

require.Equal(t, newTimestampCompatError(errWhat, newUint64(1681338455), nil).Error(),
"mismatching Shanghai fork timestamp in database (have timestamp 1681338455, want timestamp nil, rewindto timestamp 1681338454)")

require.Equal(t, newTimestampCompatError(errWhat, newUint64(1681338455), newUint64(600624000)).Error(),
"mismatching Shanghai fork timestamp in database (have timestamp 1681338455, want timestamp 600624000, rewindto timestamp 600623999)")

require.Equal(t, newTimestampCompatError(errWhat, newUint64(0), newUint64(1681338455)).Error(),
"mismatching Shanghai fork timestamp in database (have timestamp 0, want timestamp 1681338455, rewindto timestamp 0)")
}
Loading