Skip to content

Commit cf50026

Browse files
core/state: introduce the TransitionState object (verkle transition part 1) (#31634)
This is the first part of #31532 It maintains a series of conversion maker which are to be updated by the conversion code (in a follow-up PR, this is a breakdown of a larger PR to make things easier to review). They can be used in this way: - During the conversion, by storing the conversion markers when the block has been processed. This is meant to be written in a function that isn't currently present, hence [this TODO](https://github.com/ethereum/go-ethereum/pull/31634/files#diff-89272f61e115723833d498a0acbe59fa2286e3dc7276a676a7f7816f21e248b7R384). Part of #31583 --------- Signed-off-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Co-authored-by: Gary Rong <garyrong0905@gmail.com>
1 parent 073e7ec commit cf50026

File tree

8 files changed

+171
-13
lines changed

8 files changed

+171
-13
lines changed

core/chain_makers.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,10 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
540540
return block, b.receipts
541541
}
542542

543+
sdb := state.NewDatabase(trdb, nil)
544+
543545
for i := 0; i < n; i++ {
544-
statedb, err := state.New(parent.Root(), state.NewDatabase(trdb, nil))
546+
statedb, err := state.New(parent.Root(), sdb)
545547
if err != nil {
546548
panic(err)
547549
}

core/overlay/state_transition.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 (
20+
"bytes"
21+
"encoding/gob"
22+
23+
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/core/rawdb"
25+
"github.com/ethereum/go-ethereum/ethdb"
26+
"github.com/ethereum/go-ethereum/log"
27+
)
28+
29+
// TransitionState is a structure that holds the progress markers of the
30+
// translation process.
31+
type TransitionState struct {
32+
CurrentAccountAddress *common.Address // addresss of the last translated account
33+
CurrentSlotHash common.Hash // hash of the last translated storage slot
34+
CurrentPreimageOffset int64 // next byte to read from the preimage file
35+
Started, Ended bool
36+
37+
// Mark whether the storage for an account has been processed. This is useful if the
38+
// maximum number of leaves of the conversion is reached before the whole storage is
39+
// processed.
40+
StorageProcessed bool
41+
42+
BaseRoot common.Hash // hash of the last read-only MPT base tree
43+
}
44+
45+
// InTransition returns true if the translation process is in progress.
46+
func (ts *TransitionState) InTransition() bool {
47+
return ts != nil && ts.Started && !ts.Ended
48+
}
49+
50+
// Transitioned returns true if the translation process has been completed.
51+
func (ts *TransitionState) Transitioned() bool {
52+
return ts != nil && ts.Ended
53+
}
54+
55+
// Copy returns a deep copy of the TransitionState object.
56+
func (ts *TransitionState) Copy() *TransitionState {
57+
ret := &TransitionState{
58+
Started: ts.Started,
59+
Ended: ts.Ended,
60+
CurrentSlotHash: ts.CurrentSlotHash,
61+
CurrentPreimageOffset: ts.CurrentPreimageOffset,
62+
StorageProcessed: ts.StorageProcessed,
63+
}
64+
if ts.CurrentAccountAddress != nil {
65+
addr := *ts.CurrentAccountAddress
66+
ret.CurrentAccountAddress = &addr
67+
}
68+
return ret
69+
}
70+
71+
// LoadTransitionState retrieves the Verkle transition state associated with
72+
// the given state root hash from the database.
73+
func LoadTransitionState(db ethdb.KeyValueReader, root common.Hash, isVerkle bool) *TransitionState {
74+
var ts *TransitionState
75+
76+
data, _ := rawdb.ReadVerkleTransitionState(db, root)
77+
78+
// if a state could be read from the db, attempt to decode it
79+
if len(data) > 0 {
80+
var (
81+
newts TransitionState
82+
buf = bytes.NewBuffer(data[:])
83+
dec = gob.NewDecoder(buf)
84+
)
85+
// Decode transition state
86+
err := dec.Decode(&newts)
87+
if err != nil {
88+
log.Error("failed to decode transition state", "err", err)
89+
return nil
90+
}
91+
ts = &newts
92+
}
93+
94+
// Fallback that should only happen before the transition
95+
if ts == nil {
96+
// Initialize the first transition state, with the "ended"
97+
// field set to true if the database was created
98+
// as a verkle database.
99+
log.Debug("no transition state found, starting fresh", "is verkle", db)
100+
101+
// Start with a fresh state
102+
ts = &TransitionState{Ended: isVerkle}
103+
}
104+
return ts
105+
}

core/rawdb/accessors_overlay.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 rawdb
18+
19+
import (
20+
"github.com/ethereum/go-ethereum/common"
21+
"github.com/ethereum/go-ethereum/ethdb"
22+
)
23+
24+
func ReadVerkleTransitionState(db ethdb.KeyValueReader, hash common.Hash) ([]byte, error) {
25+
return db.Get(transitionStateKey(hash))
26+
}
27+
28+
func WriteVerkleTransitionState(db ethdb.KeyValueWriter, hash common.Hash, state []byte) error {
29+
return db.Put(transitionStateKey(hash), state)
30+
}

core/rawdb/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ var knownMetadataKeys = [][]byte{
604604
snapshotGeneratorKey, snapshotRecoveryKey, txIndexTailKey, fastTxLookupLimitKey,
605605
uncleanShutdownKey, badBlockKey, transitionStatusKey, skeletonSyncStatusKey,
606606
persistentStateIDKey, trieJournalKey, snapshotSyncStatusKey, snapSyncStatusFlagKey,
607-
filterMapsRangeKey, headStateHistoryIndexKey,
607+
filterMapsRangeKey, headStateHistoryIndexKey, VerkleTransitionStatePrefix,
608608
}
609609

610610
// printChainMetadata prints out chain metadata to stderr.

core/rawdb/schema.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ var (
158158
preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
159159
preimageHitsCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
160160
preimageMissCounter = metrics.NewRegisteredCounter("db/preimage/miss", nil)
161+
162+
// Verkle transition information
163+
VerkleTransitionStatePrefix = []byte("verkle-transition-state-")
161164
)
162165

163166
// LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
@@ -397,3 +400,8 @@ func storageHistoryIndexBlockKey(addressHash common.Hash, storageHash common.Has
397400
binary.BigEndian.PutUint32(buf[:], blockID)
398401
return append(append(append(StateHistoryStorageBlockPrefix, addressHash.Bytes()...), storageHash.Bytes()...), buf[:]...)
399402
}
403+
404+
// transitionStateKey = transitionStatusKey + hash
405+
func transitionStateKey(hash common.Hash) []byte {
406+
return append(VerkleTransitionStatePrefix, hash.Bytes()...)
407+
}

core/state/database.go

Lines changed: 18 additions & 10 deletions
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/state/snapshot"
2627
"github.com/ethereum/go-ethereum/core/types"
@@ -151,17 +152,21 @@ type CachingDB struct {
151152
codeCache *lru.SizeConstrainedCache[common.Hash, []byte]
152153
codeSizeCache *lru.Cache[common.Hash, int]
153154
pointCache *utils.PointCache
155+
156+
// Transition-specific fields
157+
TransitionStatePerRoot *lru.Cache[common.Hash, *overlay.TransitionState]
154158
}
155159

156160
// NewDatabase creates a state database with the provided data sources.
157161
func NewDatabase(triedb *triedb.Database, snap *snapshot.Tree) *CachingDB {
158162
return &CachingDB{
159-
disk: triedb.Disk(),
160-
triedb: triedb,
161-
snap: snap,
162-
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
163-
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
164-
pointCache: utils.NewPointCache(pointCacheSize),
163+
disk: triedb.Disk(),
164+
triedb: triedb,
165+
snap: snap,
166+
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
167+
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
168+
pointCache: utils.NewPointCache(pointCacheSize),
169+
TransitionStatePerRoot: lru.NewCache[common.Hash, *overlay.TransitionState](1000),
165170
}
166171
}
167172

@@ -224,7 +229,13 @@ func (db *CachingDB) ReadersWithCacheStats(stateRoot common.Hash) (ReaderWithSta
224229
// OpenTrie opens the main account trie at a specific root hash.
225230
func (db *CachingDB) OpenTrie(root common.Hash) (Trie, error) {
226231
if db.triedb.IsVerkle() {
227-
return trie.NewVerkleTrie(root, db.triedb, db.pointCache)
232+
ts := overlay.LoadTransitionState(db.TrieDB().Disk(), root, db.triedb.IsVerkle())
233+
if ts.InTransition() {
234+
panic("transition isn't supported yet")
235+
}
236+
if ts.Transitioned() {
237+
return trie.NewVerkleTrie(root, db.triedb, db.pointCache)
238+
}
228239
}
229240
tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb)
230241
if err != nil {
@@ -235,9 +246,6 @@ func (db *CachingDB) OpenTrie(root common.Hash) (Trie, error) {
235246

236247
// OpenStorageTrie opens the storage trie of an account.
237248
func (db *CachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash, self Trie) (Trie, error) {
238-
// In the verkle case, there is only one tree. But the two-tree structure
239-
// is hardcoded in the codebase. So we need to return the same trie in this
240-
// case.
241249
if db.triedb.IsVerkle() {
242250
return self, nil
243251
}

core/state/reader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ func newTrieReader(root common.Hash, db *triedb.Database, cache *utils.PointCach
241241
if !db.IsVerkle() {
242242
tr, err = trie.NewStateTrie(trie.StateTrieID(root), db)
243243
} else {
244+
// TODO @gballet determine the trie type (verkle or overlay) by transition state
244245
tr, err = trie.NewVerkleTrie(root, db, cache)
245246
}
246247
if err != nil {

core/verkle_witness_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"encoding/binary"
2222
"encoding/hex"
23+
"fmt"
2324
"math/big"
2425
"slices"
2526
"testing"
@@ -202,12 +203,15 @@ func TestProcessVerkle(t *testing.T) {
202203

203204
t.Log("verified verkle proof, inserting blocks into the chain")
204205

206+
for i, b := range chain {
207+
fmt.Printf("%d %x\n", i, b.Root())
208+
}
205209
endnum, err := blockchain.InsertChain(chain)
206210
if err != nil {
207211
t.Fatalf("block %d imported with error: %v", endnum, err)
208212
}
209213

210-
for i := 0; i < 2; i++ {
214+
for i := range 2 {
211215
b := blockchain.GetBlockByNumber(uint64(i) + 1)
212216
if b == nil {
213217
t.Fatalf("expected block %d to be present in chain", i+1)

0 commit comments

Comments
 (0)