Skip to content

Commit 15b7b85

Browse files
committed
feat: allow additional options to triedb update
1 parent 0bfe4a0 commit 15b7b85

File tree

8 files changed

+19
-16
lines changed

8 files changed

+19
-16
lines changed

core/state/snapshot/snapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func (t *Tree) Snapshots(root common.Hash, limits int, nodisk bool) []Snapshot {
351351
// old parent. It is disallowed to insert a disk layer (the origin of all).
352352
//
353353
// 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 {
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.StateUpdateOption) error {
355355
// Reject noop updates to avoid self-loops in the snapshot tree. This is a
356356
// special case that can only happen for Clique networks where empty blocks
357357
// don't modify the state (0 block subsidy).

core/state/statedb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ func (s *StateDB) handleDestruction(nodes *trienode.MergedNodeSet) (map[common.A
11521152
//
11531153
// The associated block number of the state transition is also provided
11541154
// for more chain context.
1155-
func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool, opts ...stateconf.SnapshotUpdateOption) (common.Hash, error) {
1155+
func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool, opts ...stateconf.StateUpdateOption) (common.Hash, error) {
11561156
// Short circuit in case any database failure occurred earlier.
11571157
if s.dbErr != nil {
11581158
return common.Hash{}, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
@@ -1268,7 +1268,7 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool, opts ...statecon
12681268
if root != origin {
12691269
start := time.Now()
12701270
set := triestate.New(s.accountsOrigin, s.storagesOrigin, incomplete)
1271-
if err := s.db.TrieDB().Update(root, origin, block, nodes, set); err != nil {
1271+
if err := s.db.TrieDB().Update(root, origin, block, nodes, set, opts...); err != nil {
12721272
return common.Hash{}, err
12731273
}
12741274
s.originalRoot = root

core/state/statedb.libevm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type SnapshotTree interface {
3737
destructs map[common.Hash]struct{},
3838
accounts map[common.Hash][]byte,
3939
storage map[common.Hash]map[common.Hash][]byte,
40-
opts ...stateconf.SnapshotUpdateOption,
40+
opts ...stateconf.StateUpdateOption,
4141
) error
4242
}
4343

core/state/statedb.libevm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (*snapTreeRecorder) Cap(common.Hash, int) error {
5757
func (r *snapTreeRecorder) Update(
5858
_, _ common.Hash,
5959
_ map[common.Hash]struct{}, _ map[common.Hash][]byte, _ map[common.Hash]map[common.Hash][]byte,
60-
opts ...stateconf.SnapshotUpdateOption,
60+
opts ...stateconf.StateUpdateOption,
6161
) error {
6262
r.gotPayload = stateconf.ExtractUpdatePayload(opts...)
6363
return nil

libevm/stateconf/conf.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,27 @@ package stateconf
1919

2020
import "github.com/ava-labs/libevm/libevm/options"
2121

22-
// A SnapshotUpdateOption configures the behaviour of
22+
// A StateUpdateOption configures the behaviour of
2323
// state.SnapshotTree.Update() implementations. This will be removed along with
2424
// state.SnapshotTree.
25-
type SnapshotUpdateOption = options.Option[snapshotUpdateConfig]
25+
type StateUpdateOption = options.Option[stateUpdateOption]
2626

27-
type snapshotUpdateConfig struct {
27+
type stateUpdateOption struct {
2828
payload any
2929
}
3030

3131
// WithUpdatePayload returns a SnapshotUpdateOption carrying an arbitrary
3232
// payload. It acts only as a carrier to exploit existing function plumbing and
3333
// 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) {
34+
func WithUpdatePayload(p any) StateUpdateOption {
35+
return options.Func[stateUpdateOption](func(c *stateUpdateOption) {
3636
c.payload = p
3737
})
3838
}
3939

4040
// ExtractUpdatePayload returns the payload carried by a [WithUpdatePayload]
4141
// option. Only one such option can be used at once; behaviour is otherwise
4242
// undefined.
43-
func ExtractUpdatePayload(opts ...SnapshotUpdateOption) any {
43+
func ExtractUpdatePayload(opts ...StateUpdateOption) any {
4444
return options.As(opts...).payload
4545
}

triedb/database.go

Lines changed: 4 additions & 3 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/ethdb"
24+
"github.com/ava-labs/libevm/libevm/stateconf"
2425
"github.com/ava-labs/libevm/log"
2526
"github.com/ava-labs/libevm/trie"
2627
"github.com/ava-labs/libevm/trie/trienode"
@@ -70,7 +71,7 @@ type backend interface {
7071
//
7172
// The passed in maps(nodes, states) will be retained to avoid copying
7273
// everything. Therefore, these maps must not be changed afterwards.
73-
Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set) error
74+
Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set, opts ...stateconf.StateUpdateOption) error
7475

7576
// Commit writes all relevant trie nodes belonging to the specified state
7677
// to disk. Report specifies whether logs will be displayed in info level.
@@ -148,11 +149,11 @@ func (db *Database) Reader(blockRoot common.Hash) (database.Reader, error) {
148149
//
149150
// The passed in maps(nodes, states) will be retained to avoid copying everything.
150151
// Therefore, these maps must not be changed afterwards.
151-
func (db *Database) Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set) error {
152+
func (db *Database) Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set, opts ...stateconf.StateUpdateOption) error {
152153
if db.preimages != nil {
153154
db.preimages.commit(false)
154155
}
155-
return db.backend.Update(root, parent, block, nodes, states)
156+
return db.backend.Update(root, parent, block, nodes, states, opts...)
156157
}
157158

158159
// Commit iterates over all the children of a particular node, writes them out

triedb/hashdb/database.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ava-labs/libevm/core/rawdb"
2929
"github.com/ava-labs/libevm/core/types"
3030
"github.com/ava-labs/libevm/ethdb"
31+
"github.com/ava-labs/libevm/libevm/stateconf"
3132
"github.com/ava-labs/libevm/log"
3233
"github.com/ava-labs/libevm/metrics"
3334
"github.com/ava-labs/libevm/rlp"
@@ -548,7 +549,7 @@ func (db *Database) Initialized(genesisRoot common.Hash) bool {
548549

549550
// Update inserts the dirty nodes in provided nodeset into database and link the
550551
// account trie with multiple storage tries if necessary.
551-
func (db *Database) Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set) error {
552+
func (db *Database) Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set, opts ...stateconf.StateUpdateOption) error {
552553
// Ensure the parent state is present and signal a warning if not.
553554
if parent != types.EmptyRootHash {
554555
if blob, _ := db.node(parent); len(blob) == 0 {

triedb/pathdb/database.go

Lines changed: 2 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/params"
3233
"github.com/ava-labs/libevm/trie/trienode"
@@ -223,7 +224,7 @@ func (db *Database) Reader(root common.Hash) (layer, error) {
223224
//
224225
// The passed in maps(nodes, states) will be retained to avoid copying everything.
225226
// Therefore, these maps must not be changed afterwards.
226-
func (db *Database) Update(root common.Hash, parentRoot common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set) error {
227+
func (db *Database) Update(root common.Hash, parentRoot common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set, opts ...stateconf.StateUpdateOption) error {
227228
// Hold the lock to prevent concurrent mutations.
228229
db.lock.Lock()
229230
defer db.lock.Unlock()

0 commit comments

Comments
 (0)