Skip to content

Commit ab66019

Browse files
committed
review feedback (with gob still)
1 parent 7352993 commit ab66019

File tree

5 files changed

+78
-59
lines changed

5 files changed

+78
-59
lines changed

consensus/beacon/consensus.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/ethereum/go-ethereum/consensus"
2626
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
2727
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
28+
"github.com/ethereum/go-ethereum/core/overlay"
2829
"github.com/ethereum/go-ethereum/core/state"
2930
"github.com/ethereum/go-ethereum/core/tracing"
3031
"github.com/ethereum/go-ethereum/core/types"
@@ -382,7 +383,7 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
382383
// This step needs to happen as late as possible to catch all access events.
383384
if chain.Config().IsVerkle(header.Number, header.Time) {
384385
// TODO(gballet) move this to the end of the overlay conversion function in a subsequent PR
385-
statedb.Database().(*state.CachingDB).SaveTransitionState(header.Root, &state.TransitionState{Ended: true})
386+
statedb.Database().(*state.CachingDB).SaveTransitionState(header.Root, &overlay.TransitionState{Ended: true})
386387
keys := statedb.AccessEvents().Keys()
387388

388389
// Open the pre-tree to prove the pre-state against

core/genesis.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/ethereum/go-ethereum/common"
2929
"github.com/ethereum/go-ethereum/common/hexutil"
3030
"github.com/ethereum/go-ethereum/common/math"
31+
"github.com/ethereum/go-ethereum/core/overlay"
3132
"github.com/ethereum/go-ethereum/core/rawdb"
3233
"github.com/ethereum/go-ethereum/core/state"
3334
"github.com/ethereum/go-ethereum/core/tracing"
@@ -284,10 +285,10 @@ func (o *ChainOverrides) apply(cfg *params.ChainConfig) error {
284285
// representing a converted state, which is used in devnets that activate
285286
// verkle at genesis.
286287
func saveVerkleTransitionStatusAtVerlkeGenesis(db ethdb.Database) {
287-
saveVerkleTransitionStatus(db, common.Hash{}, &state.TransitionState{Ended: true})
288+
saveVerkleTransitionStatus(db, common.Hash{}, &overlay.TransitionState{Ended: true})
288289
}
289290

290-
func saveVerkleTransitionStatus(db ethdb.Database, root common.Hash, ts *state.TransitionState) {
291+
func saveVerkleTransitionStatus(db ethdb.Database, root common.Hash, ts *overlay.TransitionState) {
291292
var buf bytes.Buffer
292293
enc := gob.NewEncoder(&buf)
293294
err := enc.Encode(ts)
@@ -575,7 +576,7 @@ func (g *Genesis) Commit(db ethdb.Database, triedb *triedb.Database) (*types.Blo
575576
return nil, err
576577
}
577578
if g.IsVerkle() {
578-
saveVerkleTransitionStatus(db, block.Root(), &state.TransitionState{Ended: true})
579+
saveVerkleTransitionStatus(db, block.Root(), &overlay.TransitionState{Ended: true})
579580
}
580581
batch := db.NewBatch()
581582
rawdb.WriteGenesisStateSpec(batch, block.Hash(), blob)

core/overlay/state_transition.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2025 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package overlay
18+
19+
import "github.com/ethereum/go-ethereum/common"
20+
21+
// TransitionState is a structure that holds the progress markers of the
22+
// translation process.
23+
type TransitionState struct {
24+
CurrentAccountAddress *common.Address // addresss of the last translated account
25+
CurrentSlotHash common.Hash // hash of the last translated storage slot
26+
CurrentPreimageOffset int64 // next byte to read from the preimage file
27+
Started, Ended bool
28+
29+
// Mark whether the storage for an account has been processed. This is useful if the
30+
// maximum number of leaves of the conversion is reached before the whole storage is
31+
// processed.
32+
StorageProcessed bool
33+
34+
BaseRoot common.Hash // hash of the last read-only MPT base tree
35+
}
36+
37+
// InTransition returns true if the translation process is in progress.
38+
func (ts *TransitionState) InTransition() bool {
39+
return ts != nil && ts.Started && !ts.Ended
40+
}
41+
42+
// Transitioned returns true if the translation process has been completed.
43+
func (ts *TransitionState) Transitioned() bool {
44+
return ts != nil && ts.Ended
45+
}
46+
47+
// Copy returns a deep copy of the TransitionState object.
48+
func (ts *TransitionState) Copy() *TransitionState {
49+
ret := &TransitionState{
50+
Started: ts.Started,
51+
Ended: ts.Ended,
52+
CurrentSlotHash: ts.CurrentSlotHash,
53+
CurrentPreimageOffset: ts.CurrentPreimageOffset,
54+
StorageProcessed: ts.StorageProcessed,
55+
}
56+
57+
if ts.CurrentAccountAddress != nil {
58+
ret.CurrentAccountAddress = &common.Address{}
59+
copy(ret.CurrentAccountAddress[:], ts.CurrentAccountAddress[:])
60+
}
61+
62+
return ret
63+
}

core/state/database.go

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/ethereum/go-ethereum/common"
2626
"github.com/ethereum/go-ethereum/common/lru"
27+
"github.com/ethereum/go-ethereum/core/overlay"
2728
"github.com/ethereum/go-ethereum/core/rawdb"
2829
"github.com/ethereum/go-ethereum/core/state/snapshot"
2930
"github.com/ethereum/go-ethereum/core/types"
@@ -145,50 +146,6 @@ type Trie interface {
145146
IsVerkle() bool
146147
}
147148

148-
// TransitionState is a structure that holds the progress markers of the
149-
// translation process.
150-
type TransitionState struct {
151-
CurrentAccountAddress *common.Address // addresss of the last translated account
152-
CurrentSlotHash common.Hash // hash of the last translated storage slot
153-
CurrentPreimageOffset int64 // next byte to read from the preimage file
154-
Started, Ended bool
155-
156-
// Mark whether the storage for an account has been processed. This is useful if the
157-
// maximum number of leaves of the conversion is reached before the whole storage is
158-
// processed.
159-
StorageProcessed bool
160-
161-
BaseRoot common.Hash // hash of the last read-only MPT base tree
162-
}
163-
164-
// InTransition returns true if the translation process is in progress.
165-
func (ts *TransitionState) InTransition() bool {
166-
return ts != nil && ts.Started && !ts.Ended
167-
}
168-
169-
// Transitioned returns true if the translation process has been completed.
170-
func (ts *TransitionState) Transitioned() bool {
171-
return ts != nil && ts.Ended
172-
}
173-
174-
// Copy returns a deep copy of the TransitionState object.
175-
func (ts *TransitionState) Copy() *TransitionState {
176-
ret := &TransitionState{
177-
Started: ts.Started,
178-
Ended: ts.Ended,
179-
CurrentSlotHash: ts.CurrentSlotHash,
180-
CurrentPreimageOffset: ts.CurrentPreimageOffset,
181-
StorageProcessed: ts.StorageProcessed,
182-
}
183-
184-
if ts.CurrentAccountAddress != nil {
185-
ret.CurrentAccountAddress = &common.Address{}
186-
copy(ret.CurrentAccountAddress[:], ts.CurrentAccountAddress[:])
187-
}
188-
189-
return ret
190-
}
191-
192149
// CachingDB is an implementation of Database interface. It leverages both trie and
193150
// state snapshot to provide functionalities for state access. It's meant to be a
194151
// long-live object and has a few caches inside for sharing between blocks.
@@ -201,7 +158,7 @@ type CachingDB struct {
201158
pointCache *utils.PointCache
202159

203160
// Transition-specific fields
204-
TransitionStatePerRoot lru.BasicLRU[common.Hash, *TransitionState]
161+
TransitionStatePerRoot lru.BasicLRU[common.Hash, *overlay.TransitionState]
205162
}
206163

207164
// NewDatabase creates a state database with the provided data sources.
@@ -213,7 +170,7 @@ func NewDatabase(triedb *triedb.Database, snap *snapshot.Tree) *CachingDB {
213170
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
214171
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
215172
pointCache: utils.NewPointCache(pointCacheSize),
216-
TransitionStatePerRoot: lru.NewBasicLRU[common.Hash, *TransitionState](1000),
173+
TransitionStatePerRoot: lru.NewBasicLRU[common.Hash, *overlay.TransitionState](1000),
217174
}
218175
}
219176

@@ -338,7 +295,7 @@ func mustCopyTrie(t Trie) Trie {
338295

339296
// SaveTransitionState saves the transition state to the cache and commits
340297
// it to the database if it's not already in the cache.
341-
func (db *CachingDB) SaveTransitionState(root common.Hash, ts *TransitionState) {
298+
func (db *CachingDB) SaveTransitionState(root common.Hash, ts *overlay.TransitionState) {
342299
if ts == nil {
343300
panic("nil transition state")
344301
}
@@ -370,22 +327,18 @@ func (db *CachingDB) SaveTransitionState(root common.Hash, ts *TransitionState)
370327
log.Debug("saving transition state", "storage processed", ts.StorageProcessed, "addr", ts.CurrentAccountAddress, "slot hash", ts.CurrentSlotHash, "root", root, "ended", ts.Ended, "started", ts.Started)
371328
}
372329

373-
func (db *CachingDB) LoadTransitionState(root common.Hash) *TransitionState {
330+
func (db *CachingDB) LoadTransitionState(root common.Hash) *overlay.TransitionState {
374331
// Try to get the transition state from the cache and
375332
// the DB if it's not there.
376333
ts, ok := db.TransitionStatePerRoot.Get(root)
377334
if !ok {
378335
// Not in the cache, try getting it from the DB
379336
data, _ := rawdb.ReadVerkleTransitionState(db.TrieDB().Disk(), root)
380-
// if err != nil && errors.Is(err, triedb.ErrNotFound) {
381-
// log.Error("failed to read transition state", "err", err)
382-
// return nil
383-
// }
384337

385338
// if a state could be read from the db, attempt to decode it
386339
if len(data) > 0 {
387340
var (
388-
newts TransitionState
341+
newts overlay.TransitionState
389342
buf = bytes.NewBuffer(data[:])
390343
dec = gob.NewDecoder(buf)
391344
)
@@ -405,7 +358,7 @@ func (db *CachingDB) LoadTransitionState(root common.Hash) *TransitionState {
405358
// as a verkle database.
406359
log.Debug("no transition state found, starting fresh", "is verkle", db.triedb.IsVerkle())
407360
// Start with a fresh state
408-
ts = &TransitionState{Ended: db.triedb.IsVerkle()}
361+
ts = &overlay.TransitionState{Ended: db.triedb.IsVerkle()}
409362
}
410363

411364
db.TransitionStatePerRoot.Add(root, ts)

core/state/reader.go

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

2222
"github.com/ethereum/go-ethereum/common"
2323
"github.com/ethereum/go-ethereum/common/lru"
24+
"github.com/ethereum/go-ethereum/core/overlay"
2425
"github.com/ethereum/go-ethereum/core/rawdb"
2526
"github.com/ethereum/go-ethereum/core/types"
2627
"github.com/ethereum/go-ethereum/crypto"
@@ -207,7 +208,7 @@ type trieReader struct {
207208

208209
// trieReader constructs a trie reader of the specific state. An error will be
209210
// returned if the associated trie specified by root is not existent.
210-
func newTrieReader(root common.Hash, db *triedb.Database, cache *utils.PointCache, ts *TransitionState) (*trieReader, error) {
211+
func newTrieReader(root common.Hash, db *triedb.Database, cache *utils.PointCache, ts *overlay.TransitionState) (*trieReader, error) {
211212
var (
212213
tr Trie
213214
err error

0 commit comments

Comments
 (0)