Skip to content

Commit 910d572

Browse files
committed
feat: stateconf.SnapshotUpdateOption
1 parent 2abdcff commit 910d572

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
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: 3 additions & 2 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"
@@ -1163,7 +1164,7 @@ func (s *StateDB) handleDestruction(nodes *trienode.MergedNodeSet) (map[common.A
11631164
//
11641165
// The associated block number of the state transition is also provided
11651166
// for more chain context.
1166-
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) {
11671168
// Short circuit in case any database failure occurred earlier.
11681169
if s.dbErr != nil {
11691170
return common.Hash{}, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
@@ -1253,7 +1254,7 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er
12531254
start := time.Now()
12541255
// Only update if there's a state transition (skip empty Clique blocks)
12551256
if parent := s.snap.Root(); parent != root {
1256-
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 {
12571258
log.Warn("Failed to update snapshot tree", "from", parent, "to", root, "err", err)
12581259
}
12591260
// Keep 128 diff layers in the memory, persistent layer is 129th.

core/state/statedb.libevm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/ava-labs/libevm/common"
2323
"github.com/ava-labs/libevm/core/state/snapshot"
24+
"github.com/ava-labs/libevm/libevm/stateconf"
2425
)
2526

2627
// SnapshotTree mirrors the functionality of a [snapshot.Tree], allowing for
@@ -35,6 +36,7 @@ type SnapshotTree interface {
3536
destructs map[common.Hash]struct{},
3637
accounts map[common.Hash][]byte,
3738
storage map[common.Hash]map[common.Hash][]byte,
39+
opts ...stateconf.SnapshotUpdateOption,
3840
) error
3941
}
4042

libevm/stateconf/conf.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.
24+
type SnapshotUpdateOption = options.Option[snapshotUpdateConfig]
25+
26+
type snapshotUpdateConfig struct {
27+
payload any
28+
}
29+
30+
// WithUpdatePayload returns a SnapshotUpdateOption carrying an arbitrary
31+
// payload. It acts only as a carrier to exploit existing function plumbing and
32+
// the effect on behaviour is left to the implementation receiving it.
33+
func WithUpdatePayload(p any) SnapshotUpdateOption {
34+
return options.Func[snapshotUpdateConfig](func(c *snapshotUpdateConfig) {
35+
c.payload = p
36+
})
37+
}
38+
39+
// ExtractUpdatePayload returns the payload carried by a [WithUpdatePayload]
40+
// option. Only one such option can be used at once; behaviour is otherwise
41+
// undefined.
42+
func ExtractUpdatePayload(opts ...SnapshotUpdateOption) any {
43+
return options.As(opts...).payload
44+
}

0 commit comments

Comments
 (0)