Skip to content

Commit fab5066

Browse files
committed
Merge branch 'main' into arr4n/precompilegen
2 parents e841e39 + 44f23c8 commit fab5066

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1874
-364
lines changed

.github/workflows/go.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010
jobs:
1111
go_test_short:
1212
env:
13-
FLAKY_REGEX: 'ava-labs/libevm/(eth|eth/tracers/js|eth/tracers/logger|accounts/abi/bind|accounts/keystore|eth/downloader|miner|ethclient|ethclient/gethclient|eth/catalyst)$'
13+
FLAKY_REGEX: 'ava-labs/libevm/(triedb/pathdb|eth|eth/tracers/js|eth/tracers/logger|accounts/abi/bind|accounts/keystore|eth/downloader|miner|ethclient|ethclient/gethclient|eth/catalyst)$'
1414
runs-on: ubuntu-latest
1515
steps:
1616
- uses: actions/checkout@v4
@@ -24,3 +24,20 @@ jobs:
2424
- name: Run non-flaky tests concurrently
2525
run: |
2626
go test -short $(go list ./... | grep -Pv "${FLAKY_REGEX}");
27+
28+
go_generate:
29+
env:
30+
EXCLUDE_REGEX: 'ava-labs/libevm/(accounts/usbwallet/trezor)$'
31+
runs-on: ubuntu-latest
32+
steps:
33+
- uses: actions/checkout@v4
34+
- name: Set up Go
35+
uses: actions/setup-go@v5
36+
with:
37+
go-version: 1.21.4
38+
39+
- name: Run `go generate`
40+
run: go list ./... | grep -Pv "${EXCLUDE_REGEX}" | xargs go generate;
41+
42+
- name: git diff
43+
run: git diff --exit-code

core/gen_genesis.go

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/state/snapshot/snapshot.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/ava-labs/libevm/core/rawdb"
2828
"github.com/ava-labs/libevm/core/types"
2929
"github.com/ava-labs/libevm/ethdb"
30+
"github.com/ava-labs/libevm/libevm/stateconf"
3031
"github.com/ava-labs/libevm/log"
3132
"github.com/ava-labs/libevm/metrics"
3233
"github.com/ava-labs/libevm/rlp"
@@ -348,7 +349,9 @@ func (t *Tree) Snapshots(root common.Hash, limits int, nodisk bool) []Snapshot {
348349

349350
// Update adds a new snapshot into the tree, if that can be linked to an existing
350351
// old parent. It is disallowed to insert a disk layer (the origin of all).
351-
func (t *Tree) 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 {
352+
//
353+
// libevm: Options are ignored and only included to match an interface method.
354+
func (t *Tree) 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, _ ...stateconf.SnapshotUpdateOption) error {
352355
// Reject noop updates to avoid self-loops in the snapshot tree. This is a
353356
// special case that can only happen for Clique networks where empty blocks
354357
// don't modify the state (0 block subsidy).

core/state/state.libevm.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,47 @@ package state
1919
import (
2020
"github.com/ava-labs/libevm/common"
2121
"github.com/ava-labs/libevm/core/types"
22+
"github.com/ava-labs/libevm/libevm/pseudo"
2223
)
2324

2425
// GetExtra returns the extra payload from the [types.StateAccount] associated
25-
// with the address, or a zero-value `SA` if not found. The
26-
// [types.ExtraPayloads] MUST be sourced from [types.RegisterExtras].
27-
func GetExtra[SA any](s *StateDB, p types.ExtraPayloads[SA], addr common.Address) SA {
26+
// with the address, or a zero-value `SA` if not found. The [pseudo.Accessor]
27+
// MUST be sourced from [types.RegisterExtras].
28+
func GetExtra[SA any](s *StateDB, a pseudo.Accessor[types.StateOrSlimAccount, SA], addr common.Address) SA {
2829
stateObject := s.getStateObject(addr)
2930
if stateObject != nil {
30-
return p.FromStateAccount(&stateObject.data)
31+
return a.Get(&stateObject.data)
3132
}
3233
var zero SA
3334
return zero
3435
}
3536

3637
// SetExtra sets the extra payload for the address. See [GetExtra] for details.
37-
func SetExtra[SA any](s *StateDB, p types.ExtraPayloads[SA], addr common.Address, extra SA) {
38+
func SetExtra[SA any](s *StateDB, a pseudo.Accessor[types.StateOrSlimAccount, SA], addr common.Address, extra SA) {
3839
stateObject := s.getOrNewStateObject(addr)
3940
if stateObject != nil {
40-
setExtraOnObject(stateObject, p, addr, extra)
41+
setExtraOnObject(stateObject, a, addr, extra)
4142
}
4243
}
4344

44-
func setExtraOnObject[SA any](s *stateObject, p types.ExtraPayloads[SA], addr common.Address, extra SA) {
45+
func setExtraOnObject[SA any](s *stateObject, a pseudo.Accessor[types.StateOrSlimAccount, SA], addr common.Address, extra SA) {
4546
s.db.journal.append(extraChange[SA]{
46-
payloads: p,
47+
accessor: a,
4748
account: &addr,
48-
prev: p.FromStateAccount(&s.data),
49+
prev: a.Get(&s.data),
4950
})
50-
p.SetOnStateAccount(&s.data, extra)
51+
a.Set(&s.data, extra)
5152
}
5253

5354
// extraChange is a [journalEntry] for [SetExtra] / [setExtraOnObject].
5455
type extraChange[SA any] struct {
55-
payloads types.ExtraPayloads[SA]
56+
accessor pseudo.Accessor[types.StateOrSlimAccount, SA]
5657
account *common.Address
5758
prev SA
5859
}
5960

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

6263
func (e extraChange[SA]) revert(s *StateDB) {
63-
e.payloads.SetOnStateAccount(&s.getStateObject(*e.account).data, e.prev)
64+
e.accessor.Set(&s.getStateObject(*e.account).data, e.prev)
6465
}

core/state/state.libevm_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestGetSetExtra(t *testing.T) {
4545
t.Cleanup(types.TestOnlyClearRegisteredExtras)
4646
// Just as its Data field is a pointer, the registered type is a pointer to
4747
// test deep copying.
48-
payloads := types.RegisterExtras[*accountExtra]()
48+
payloads := types.RegisterExtras[types.NOOPHeaderHooks, *types.NOOPHeaderHooks, *accountExtra]().StateAccount
4949

5050
rng := ethtest.NewPseudoRand(42)
5151
addr := rng.Address()
@@ -87,7 +87,7 @@ func TestGetSetExtra(t *testing.T) {
8787
Root: types.EmptyRootHash,
8888
CodeHash: types.EmptyCodeHash[:],
8989
}
90-
payloads.SetOnStateAccount(want, extra)
90+
payloads.Set(want, extra)
9191

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

core/state/state_object.libevm_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,21 @@ func TestStateObjectEmpty(t *testing.T) {
4646
{
4747
name: "explicit false bool",
4848
registerAndSet: func(acc *types.StateAccount) {
49-
types.RegisterExtras[bool]().SetOnStateAccount(acc, false)
49+
types.RegisterExtras[types.NOOPHeaderHooks, *types.NOOPHeaderHooks, bool]().StateAccount.Set(acc, false)
5050
},
5151
wantEmpty: true,
5252
},
5353
{
5454
name: "implicit false bool",
5555
registerAndSet: func(*types.StateAccount) {
56-
types.RegisterExtras[bool]()
56+
types.RegisterExtras[types.NOOPHeaderHooks, *types.NOOPHeaderHooks, bool]()
5757
},
5858
wantEmpty: true,
5959
},
6060
{
6161
name: "true bool",
6262
registerAndSet: func(acc *types.StateAccount) {
63-
types.RegisterExtras[bool]().SetOnStateAccount(acc, true)
63+
types.RegisterExtras[types.NOOPHeaderHooks, *types.NOOPHeaderHooks, bool]().StateAccount.Set(acc, true)
6464
},
6565
wantEmpty: false,
6666
},

core/state/statedb.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/ava-labs/libevm/core/state/snapshot"
2828
"github.com/ava-labs/libevm/core/types"
2929
"github.com/ava-labs/libevm/crypto"
30+
"github.com/ava-labs/libevm/libevm/stateconf"
3031
"github.com/ava-labs/libevm/log"
3132
"github.com/ava-labs/libevm/metrics"
3233
"github.com/ava-labs/libevm/params"
@@ -63,7 +64,7 @@ type StateDB struct {
6364
prefetcher *triePrefetcher
6465
trie Trie
6566
hasher crypto.KeccakState
66-
snaps *snapshot.Tree // Nil if snapshot is not available
67+
snaps SnapshotTree // Nil if snapshot is not available
6768
snap snapshot.Snapshot // Nil if snapshot is not available
6869

6970
// originalRoot is the pre-state root, before any changes were made.
@@ -141,7 +142,8 @@ type StateDB struct {
141142
}
142143

143144
// New creates a new state from a given trie.
144-
func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
145+
func New(root common.Hash, db Database, snaps SnapshotTree) (*StateDB, error) {
146+
snaps = clearTypedNilPointer(snaps)
145147
tr, err := db.OpenTrie(root)
146148
if err != nil {
147149
return nil, err
@@ -175,13 +177,13 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
175177
// StartPrefetcher initializes a new trie prefetcher to pull in nodes from the
176178
// state trie concurrently while the state is mutated so that when we reach the
177179
// commit phase, most of the needed data is already hot.
178-
func (s *StateDB) StartPrefetcher(namespace string) {
180+
func (s *StateDB) StartPrefetcher(namespace string, opts ...PrefetcherOption) {
179181
if s.prefetcher != nil {
180182
s.prefetcher.close()
181183
s.prefetcher = nil
182184
}
183185
if s.snap != nil {
184-
s.prefetcher = newTriePrefetcher(s.db, s.originalRoot, namespace)
186+
s.prefetcher = newTriePrefetcher(s.db, s.originalRoot, namespace, opts...)
185187
}
186188
}
187189

@@ -1162,7 +1164,7 @@ func (s *StateDB) handleDestruction(nodes *trienode.MergedNodeSet) (map[common.A
11621164
//
11631165
// The associated block number of the state transition is also provided
11641166
// for more chain context.
1165-
func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, error) {
1167+
func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool, opts ...stateconf.SnapshotUpdateOption) (common.Hash, error) {
11661168
// Short circuit in case any database failure occurred earlier.
11671169
if s.dbErr != nil {
11681170
return common.Hash{}, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
@@ -1252,7 +1254,7 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er
12521254
start := time.Now()
12531255
// Only update if there's a state transition (skip empty Clique blocks)
12541256
if parent := s.snap.Root(); parent != root {
1255-
if err := s.snaps.Update(root, parent, s.convertAccountSet(s.stateObjectsDestruct), s.accounts, s.storages); err != nil {
1257+
if err := s.snaps.Update(root, parent, s.convertAccountSet(s.stateObjectsDestruct), s.accounts, s.storages, opts...); err != nil {
12561258
log.Warn("Failed to update snapshot tree", "from", parent, "to", root, "err", err)
12571259
}
12581260
// Keep 128 diff layers in the memory, persistent layer is 129th.

core/state/statedb.libevm.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2024 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package state
18+
19+
import (
20+
"reflect"
21+
22+
"github.com/ava-labs/libevm/common"
23+
"github.com/ava-labs/libevm/core/state/snapshot"
24+
"github.com/ava-labs/libevm/libevm/stateconf"
25+
)
26+
27+
// SnapshotTree mirrors the functionality of a [snapshot.Tree], allowing for
28+
// drop-in replacements. This is intended as a temporary feature as a workaround
29+
// until a standard Tree can be used.
30+
type SnapshotTree interface {
31+
Cap(common.Hash, int) error
32+
Snapshot(common.Hash) snapshot.Snapshot
33+
StorageIterator(root, account, seek common.Hash) (snapshot.StorageIterator, error)
34+
Update(
35+
blockRoot common.Hash,
36+
parentRoot common.Hash,
37+
destructs map[common.Hash]struct{},
38+
accounts map[common.Hash][]byte,
39+
storage map[common.Hash]map[common.Hash][]byte,
40+
opts ...stateconf.SnapshotUpdateOption,
41+
) error
42+
}
43+
44+
var _ SnapshotTree = (*snapshot.Tree)(nil)
45+
46+
// clearTypedNilPointer returns nil if `snaps == nil` or if it holds a nil
47+
// pointer. The default geth behaviour expected a [snapshot.Tree] pointer
48+
// instead of a SnapshotTree interface, which could result in typed-nil bugs.
49+
func clearTypedNilPointer(snaps SnapshotTree) SnapshotTree {
50+
if v := reflect.ValueOf(snaps); v.Kind() == reflect.Pointer && v.IsNil() {
51+
return nil
52+
}
53+
return snaps
54+
}

0 commit comments

Comments
 (0)