Skip to content

Commit e03fa09

Browse files
committed
fix: use specific type for triedb option
1 parent 46a8274 commit e03fa09

File tree

4 files changed

+38
-32
lines changed

4 files changed

+38
-32
lines changed

core/state/statedb.libevm_test.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,28 @@ func TestStateDBCommitPropagatesOptions(t *testing.T) {
4646
},
4747
},
4848
)
49-
var rec snapTreeRecorder
50-
sdb, err := New(types.EmptyRootHash, NewDatabaseWithNodeDB(memdb, triedb), &rec)
49+
trieRec, _ := triedb.Backend().(*triedbRecorder)
50+
var snapRec snapTreeRecorder
51+
sdb, err := New(types.EmptyRootHash, NewDatabaseWithNodeDB(memdb, triedb), &snapRec)
5152
require.NoError(t, err, "New()")
5253

5354
// Ensures that rec.Update() will be called.
5455
sdb.SetNonce(common.Address{}, 42)
5556

5657
const snapshotPayload = "hello world"
57-
const trieDBPayload = "goodbye world"
58+
var (
59+
parentHash = common.HexToHash("0x0102030405060708090a0b0c0d0e0f1011121314151617181920212223242526")
60+
currentHash = common.HexToHash("0x1234567890123456789012345678901234567890123456789012345678901234")
61+
)
5862
snapshotOpt := stateconf.WithSnapshotUpdatePayload(snapshotPayload)
59-
triedbOpt := stateconf.WithTrieDBUpdatePayload(trieDBPayload)
63+
triedbOpt := stateconf.WithTrieDBUpdatePayload(parentHash, currentHash)
6064
_, err = sdb.Commit(0, false, stateconf.WithSnapshotUpdateOpts(snapshotOpt), stateconf.WithTrieDBUpdateOpts(triedbOpt))
61-
require.NoErrorf(t, err, "%T.Commit(..., %T, %T)", sdb, snapshotOpt, triedbOpt)
6265

63-
assert.Equalf(t, snapshotPayload, rec.gotPayload, "%T payload propagated via %T.Commit() to %T.Update()", snapshotOpt, sdb, rec)
64-
innerTrieDB, ok := triedb.Backend().(*triedbRecorder)
65-
if !ok {
66-
t.Fatalf("expected %T to be a *triedbRecorder", triedb.Backend())
67-
}
68-
assert.Equalf(t, trieDBPayload, innerTrieDB.gotPayload, "%T payload propagated via %T.Commit() to %T.Update()", triedbOpt, sdb, rec)
66+
require.NoErrorf(t, err, "%T.Commit(..., %T, %T)", sdb, snapshotOpt, triedbOpt)
67+
assert.Equalf(t, snapshotPayload, snapRec.gotPayload, "%T payload propagated via %T.Commit() to %T.Update()", snapshotOpt, sdb, snapRec)
68+
assert.Truef(t, trieRec.exists, "%T exists propagated via %T.Commit() to %T.Update()", triedbOpt, sdb, trieRec)
69+
assert.Equalf(t, parentHash, trieRec.parentBlockHash, "%T parentHash propagated via %T.Commit() to %T.Update()", triedbOpt, sdb, trieRec)
70+
assert.Equalf(t, currentHash, trieRec.currentBlockHash, "%T currentHash propagated via %T.Commit() to %T.Update()", triedbOpt, sdb, trieRec)
6971
}
7072

7173
type snapTreeRecorder struct {
@@ -104,7 +106,9 @@ func (snapshotStub) Root() common.Hash {
104106

105107
type triedbRecorder struct {
106108
*hashdb.Database
107-
gotPayload any
109+
parentBlockHash common.Hash
110+
currentBlockHash common.Hash
111+
exists bool
108112
}
109113

110114
func (r *triedbRecorder) Update(
@@ -115,7 +119,7 @@ func (r *triedbRecorder) Update(
115119
states *triestate.Set,
116120
opts ...stateconf.TrieDBUpdateOption,
117121
) error {
118-
r.gotPayload = stateconf.ExtractTrieDBUpdatePayload(opts...)
122+
r.parentBlockHash, r.currentBlockHash, r.exists = stateconf.ExtractTrieDBUpdatePayload(opts...)
119123
return r.Database.Update(root, parent, block, nodes, states)
120124
}
121125

libevm/stateconf/conf.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
package stateconf
1919

2020
import (
21+
"github.com/ava-labs/libevm/common"
2122
"github.com/ava-labs/libevm/libevm/options"
2223
)
2324

24-
// A StateDBCommitOption configures the behaviour of all update calls within the
25-
// state.StateDB.Commit() implementations.
26-
// This is provided in two distinct types to allow customizability to both
27-
// snapshot and trie database updates, but are ignored in libevm implementations.
25+
// A StateDBCommitOption configures the behaviour of state.StateDB.Commit()
2826
type StateDBCommitOption = options.Option[stateDBCommitConfig]
2927

3028
type stateDBCommitConfig struct {
@@ -34,10 +32,9 @@ type stateDBCommitConfig struct {
3432

3533
// WithSnapshotUpdateOpts returns a StateDBCommitOption carrying a list of
3634
// SnapshotUpdateOptions.
37-
// If the list is not of length 1, the last option in the list is used.
35+
// If multiple such options are used, only the last will be applied as they overwrite each other.
3836
func WithSnapshotUpdateOpts(opts ...SnapshotUpdateOption) StateDBCommitOption {
3937
return options.Func[stateDBCommitConfig](func(c *stateDBCommitConfig) {
40-
// I don't like append() because there's no way to remove options, but that's a weakly held opinion
4138
c.snapshotOpts = opts
4239
})
4340
}
@@ -49,11 +46,10 @@ func ExtractSnapshotUpdateOpts(opts ...StateDBCommitOption) []SnapshotUpdateOpti
4946
}
5047

5148
// WithTrieDBUpdateOpts returns a StateDBCommitOption carrying a list of
52-
// TrieDBUpdateOptions. If the list is not of length 1, the last option in the
53-
// list is used.
49+
// TrieDBUpdateOptions. If multiple such options are used, only the last will be
50+
// applied as they overwrite each other.
5451
func WithTrieDBUpdateOpts(opts ...TrieDBUpdateOption) StateDBCommitOption {
5552
return options.Func[stateDBCommitConfig](func(c *stateDBCommitConfig) {
56-
// I don't like append() because there's no way to remove options, but that's a weakly held opinion
5753
c.triedbOpts = opts
5854
})
5955
}
@@ -93,21 +89,27 @@ func ExtractSnapshotUpdatePayload(opts ...SnapshotUpdateOption) any {
9389
type TrieDBUpdateOption = options.Option[triedbUpdateConfig]
9490

9591
type triedbUpdateConfig struct {
96-
payload any
92+
parentBlockHash *common.Hash
93+
currentBlockHash *common.Hash
9794
}
9895

99-
// WithTrieDBUpdatePayload returns a TrieDBUpdateOption carrying an arbitrary
100-
// payload. It acts only as a carrier to exploit existing function plumbing and
96+
// WithTrieDBUpdatePayload returns a TrieDBUpdateOption carrying two block hashes.
97+
// It acts only as a carrier to exploit existing function plumbing and
10198
// the effect on behaviour is left to the implementation receiving it.
102-
func WithTrieDBUpdatePayload(p any) TrieDBUpdateOption {
99+
func WithTrieDBUpdatePayload(parent common.Hash, current common.Hash) TrieDBUpdateOption {
103100
return options.Func[triedbUpdateConfig](func(c *triedbUpdateConfig) {
104-
c.payload = p
101+
c.parentBlockHash = &parent
102+
c.currentBlockHash = &current
105103
})
106104
}
107105

108-
// ExtractTrieDBUpdatePayload returns the payload carried by a [WithSnapshotUpdatePayload]
106+
// ExtractTrieDBUpdatePayload returns the payload carried by a [WithTrieDBUpdatePayload]
109107
// option. Only one such option can be used at once; behaviour is otherwise
110108
// undefined.
111-
func ExtractTrieDBUpdatePayload(opts ...TrieDBUpdateOption) any {
112-
return options.As(opts...).payload
109+
func ExtractTrieDBUpdatePayload(opts ...TrieDBUpdateOption) (common.Hash, common.Hash, bool) {
110+
conf := options.As(opts...)
111+
if conf.parentBlockHash == nil && conf.currentBlockHash == nil {
112+
return common.Hash{}, common.Hash{}, false
113+
}
114+
return *conf.parentBlockHash, *conf.currentBlockHash, true
113115
}

triedb/hashdb/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ func (db *Database) Initialized(genesisRoot common.Hash) bool {
549549

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

triedb/pathdb/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (db *Database) Reader(root common.Hash) (layer, error) {
224224
//
225225
// The passed in maps(nodes, states) will be retained to avoid copying everything.
226226
// Therefore, these maps must not be changed afterwards.
227-
func (db *Database) Update(root common.Hash, parentRoot common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set, opts ...stateconf.TrieDBUpdateOption) error {
227+
func (db *Database) Update(root common.Hash, parentRoot common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set, _ ...stateconf.TrieDBUpdateOption) error {
228228
// Hold the lock to prevent concurrent mutations.
229229
db.lock.Lock()
230230
defer db.lock.Unlock()

0 commit comments

Comments
 (0)