Skip to content

Commit 3a75409

Browse files
authored
feat: state.SnapshotTree interface for drop-in replacement (#77)
## Why this should be merged Allows for a drop-in replacement of `snapshot.Tree` (i.e. one that uses block hashes instead of state roots). This is intended as a temporary solution while we investigate having the state root affected by the block hash to remove path ambiguity. ## How this works Introduction of: 1. `state.SnapshotTree` interface to match methods required on `snapshot.Tree` as used by `state.StateDB`; and 2. `stateconf` package for variadic options plumbed by `StateDB.Commit()` through to `SnapshotTree.Update()`. Although variadic (to maintain function call-signature compatibility) only the `stateconf.WithUpdatePayload(any)` is expected to be used. Recipients of the options can access the payload with `stateconf.ExtractUpdatePayload()`. ## How this was tested Unit test demonstrating propagation of `stateconf.UpdateOption` payload.
1 parent 4feb960 commit 3a75409

File tree

5 files changed

+189
-5
lines changed

5 files changed

+189
-5
lines changed

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/statedb.go

Lines changed: 6 additions & 4 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
@@ -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+
}

core/state/statedb.libevm_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
24+
25+
"github.com/ava-labs/libevm/common"
26+
"github.com/ava-labs/libevm/core/rawdb"
27+
"github.com/ava-labs/libevm/core/state/snapshot"
28+
"github.com/ava-labs/libevm/core/types"
29+
"github.com/ava-labs/libevm/libevm/stateconf"
30+
)
31+
32+
func TestStateDBCommitPropagatesOptions(t *testing.T) {
33+
var rec snapTreeRecorder
34+
sdb, err := New(types.EmptyRootHash, NewDatabase(rawdb.NewMemoryDatabase()), &rec)
35+
require.NoError(t, err, "New()")
36+
37+
// Ensures that rec.Update() will be called.
38+
sdb.SetNonce(common.Address{}, 42)
39+
40+
const payload = "hello world"
41+
opt := stateconf.WithUpdatePayload(payload)
42+
_, err = sdb.Commit(0, false, opt)
43+
require.NoErrorf(t, err, "%T.Commit(..., %T)", sdb, opt)
44+
45+
assert.Equalf(t, payload, rec.gotPayload, "%T payload propagated via %T.Commit() to %T.Update()", opt, sdb, rec)
46+
}
47+
48+
type snapTreeRecorder struct {
49+
SnapshotTree
50+
gotPayload any
51+
}
52+
53+
func (*snapTreeRecorder) Cap(common.Hash, int) error {
54+
return nil
55+
}
56+
57+
func (r *snapTreeRecorder) Update(
58+
_, _ common.Hash,
59+
_ map[common.Hash]struct{}, _ map[common.Hash][]byte, _ map[common.Hash]map[common.Hash][]byte,
60+
opts ...stateconf.SnapshotUpdateOption,
61+
) error {
62+
r.gotPayload = stateconf.ExtractUpdatePayload(opts...)
63+
return nil
64+
}
65+
66+
func (*snapTreeRecorder) Snapshot(common.Hash) snapshot.Snapshot {
67+
return snapshotStub{}
68+
}
69+
70+
type snapshotStub struct {
71+
snapshot.Snapshot
72+
}
73+
74+
func (snapshotStub) Account(common.Hash) (*types.SlimAccount, error) {
75+
return &types.SlimAccount{}, nil
76+
}
77+
78+
func (snapshotStub) Root() common.Hash {
79+
return common.Hash{}
80+
}

libevm/stateconf/conf.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 stateconf configures state management.
18+
package stateconf
19+
20+
import "github.com/ava-labs/libevm/libevm/options"
21+
22+
// A SnapshotUpdateOption configures the behaviour of
23+
// state.SnapshotTree.Update() implementations. This will be removed along with
24+
// state.SnapshotTree.
25+
type SnapshotUpdateOption = options.Option[snapshotUpdateConfig]
26+
27+
type snapshotUpdateConfig struct {
28+
payload any
29+
}
30+
31+
// WithUpdatePayload returns a SnapshotUpdateOption carrying an arbitrary
32+
// payload. It acts only as a carrier to exploit existing function plumbing and
33+
// the effect on behaviour is left to the implementation receiving it.
34+
func WithUpdatePayload(p any) SnapshotUpdateOption {
35+
return options.Func[snapshotUpdateConfig](func(c *snapshotUpdateConfig) {
36+
c.payload = p
37+
})
38+
}
39+
40+
// ExtractUpdatePayload returns the payload carried by a [WithUpdatePayload]
41+
// option. Only one such option can be used at once; behaviour is otherwise
42+
// undefined.
43+
func ExtractUpdatePayload(opts ...SnapshotUpdateOption) any {
44+
return options.As(opts...).payload
45+
}

0 commit comments

Comments
 (0)