mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-02-26 07:37:20 +00:00
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 https://github.com/ethereum/go-ethereum/issues/31583 --------- Signed-off-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Co-authored-by: Gary Rong <garyrong0905@gmail.com>
This commit is contained in:
parent
073e7ec4b0
commit
cf50026466
8 changed files with 171 additions and 13 deletions
|
|
@ -540,8 +540,10 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
|
|||
return block, b.receipts
|
||||
}
|
||||
|
||||
sdb := state.NewDatabase(trdb, nil)
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
statedb, err := state.New(parent.Root(), state.NewDatabase(trdb, nil))
|
||||
statedb, err := state.New(parent.Root(), sdb)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
|||
105
core/overlay/state_transition.go
Normal file
105
core/overlay/state_transition.go
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
// Copyright 2025 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package overlay
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
)
|
||||
|
||||
// TransitionState is a structure that holds the progress markers of the
|
||||
// translation process.
|
||||
type TransitionState struct {
|
||||
CurrentAccountAddress *common.Address // addresss of the last translated account
|
||||
CurrentSlotHash common.Hash // hash of the last translated storage slot
|
||||
CurrentPreimageOffset int64 // next byte to read from the preimage file
|
||||
Started, Ended bool
|
||||
|
||||
// Mark whether the storage for an account has been processed. This is useful if the
|
||||
// maximum number of leaves of the conversion is reached before the whole storage is
|
||||
// processed.
|
||||
StorageProcessed bool
|
||||
|
||||
BaseRoot common.Hash // hash of the last read-only MPT base tree
|
||||
}
|
||||
|
||||
// InTransition returns true if the translation process is in progress.
|
||||
func (ts *TransitionState) InTransition() bool {
|
||||
return ts != nil && ts.Started && !ts.Ended
|
||||
}
|
||||
|
||||
// Transitioned returns true if the translation process has been completed.
|
||||
func (ts *TransitionState) Transitioned() bool {
|
||||
return ts != nil && ts.Ended
|
||||
}
|
||||
|
||||
// Copy returns a deep copy of the TransitionState object.
|
||||
func (ts *TransitionState) Copy() *TransitionState {
|
||||
ret := &TransitionState{
|
||||
Started: ts.Started,
|
||||
Ended: ts.Ended,
|
||||
CurrentSlotHash: ts.CurrentSlotHash,
|
||||
CurrentPreimageOffset: ts.CurrentPreimageOffset,
|
||||
StorageProcessed: ts.StorageProcessed,
|
||||
}
|
||||
if ts.CurrentAccountAddress != nil {
|
||||
addr := *ts.CurrentAccountAddress
|
||||
ret.CurrentAccountAddress = &addr
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// LoadTransitionState retrieves the Verkle transition state associated with
|
||||
// the given state root hash from the database.
|
||||
func LoadTransitionState(db ethdb.KeyValueReader, root common.Hash, isVerkle bool) *TransitionState {
|
||||
var ts *TransitionState
|
||||
|
||||
data, _ := rawdb.ReadVerkleTransitionState(db, root)
|
||||
|
||||
// if a state could be read from the db, attempt to decode it
|
||||
if len(data) > 0 {
|
||||
var (
|
||||
newts TransitionState
|
||||
buf = bytes.NewBuffer(data[:])
|
||||
dec = gob.NewDecoder(buf)
|
||||
)
|
||||
// Decode transition state
|
||||
err := dec.Decode(&newts)
|
||||
if err != nil {
|
||||
log.Error("failed to decode transition state", "err", err)
|
||||
return nil
|
||||
}
|
||||
ts = &newts
|
||||
}
|
||||
|
||||
// Fallback that should only happen before the transition
|
||||
if ts == nil {
|
||||
// Initialize the first transition state, with the "ended"
|
||||
// field set to true if the database was created
|
||||
// as a verkle database.
|
||||
log.Debug("no transition state found, starting fresh", "is verkle", db)
|
||||
|
||||
// Start with a fresh state
|
||||
ts = &TransitionState{Ended: isVerkle}
|
||||
}
|
||||
return ts
|
||||
}
|
||||
30
core/rawdb/accessors_overlay.go
Normal file
30
core/rawdb/accessors_overlay.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
// Copyright 2025 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package rawdb
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
)
|
||||
|
||||
func ReadVerkleTransitionState(db ethdb.KeyValueReader, hash common.Hash) ([]byte, error) {
|
||||
return db.Get(transitionStateKey(hash))
|
||||
}
|
||||
|
||||
func WriteVerkleTransitionState(db ethdb.KeyValueWriter, hash common.Hash, state []byte) error {
|
||||
return db.Put(transitionStateKey(hash), state)
|
||||
}
|
||||
|
|
@ -604,7 +604,7 @@ var knownMetadataKeys = [][]byte{
|
|||
snapshotGeneratorKey, snapshotRecoveryKey, txIndexTailKey, fastTxLookupLimitKey,
|
||||
uncleanShutdownKey, badBlockKey, transitionStatusKey, skeletonSyncStatusKey,
|
||||
persistentStateIDKey, trieJournalKey, snapshotSyncStatusKey, snapSyncStatusFlagKey,
|
||||
filterMapsRangeKey, headStateHistoryIndexKey,
|
||||
filterMapsRangeKey, headStateHistoryIndexKey, VerkleTransitionStatePrefix,
|
||||
}
|
||||
|
||||
// printChainMetadata prints out chain metadata to stderr.
|
||||
|
|
|
|||
|
|
@ -158,6 +158,9 @@ var (
|
|||
preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
|
||||
preimageHitsCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
|
||||
preimageMissCounter = metrics.NewRegisteredCounter("db/preimage/miss", nil)
|
||||
|
||||
// Verkle transition information
|
||||
VerkleTransitionStatePrefix = []byte("verkle-transition-state-")
|
||||
)
|
||||
|
||||
// LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
|
||||
|
|
@ -397,3 +400,8 @@ func storageHistoryIndexBlockKey(addressHash common.Hash, storageHash common.Has
|
|||
binary.BigEndian.PutUint32(buf[:], blockID)
|
||||
return append(append(append(StateHistoryStorageBlockPrefix, addressHash.Bytes()...), storageHash.Bytes()...), buf[:]...)
|
||||
}
|
||||
|
||||
// transitionStateKey = transitionStatusKey + hash
|
||||
func transitionStateKey(hash common.Hash) []byte {
|
||||
return append(VerkleTransitionStatePrefix, hash.Bytes()...)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ import (
|
|||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/common/lru"
|
||||
"github.com/ethereum/go-ethereum/core/overlay"
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
"github.com/ethereum/go-ethereum/core/state/snapshot"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
|
|
@ -151,17 +152,21 @@ type CachingDB struct {
|
|||
codeCache *lru.SizeConstrainedCache[common.Hash, []byte]
|
||||
codeSizeCache *lru.Cache[common.Hash, int]
|
||||
pointCache *utils.PointCache
|
||||
|
||||
// Transition-specific fields
|
||||
TransitionStatePerRoot *lru.Cache[common.Hash, *overlay.TransitionState]
|
||||
}
|
||||
|
||||
// NewDatabase creates a state database with the provided data sources.
|
||||
func NewDatabase(triedb *triedb.Database, snap *snapshot.Tree) *CachingDB {
|
||||
return &CachingDB{
|
||||
disk: triedb.Disk(),
|
||||
triedb: triedb,
|
||||
snap: snap,
|
||||
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
|
||||
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
|
||||
pointCache: utils.NewPointCache(pointCacheSize),
|
||||
disk: triedb.Disk(),
|
||||
triedb: triedb,
|
||||
snap: snap,
|
||||
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
|
||||
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
|
||||
pointCache: utils.NewPointCache(pointCacheSize),
|
||||
TransitionStatePerRoot: lru.NewCache[common.Hash, *overlay.TransitionState](1000),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -224,7 +229,13 @@ func (db *CachingDB) ReadersWithCacheStats(stateRoot common.Hash) (ReaderWithSta
|
|||
// OpenTrie opens the main account trie at a specific root hash.
|
||||
func (db *CachingDB) OpenTrie(root common.Hash) (Trie, error) {
|
||||
if db.triedb.IsVerkle() {
|
||||
return trie.NewVerkleTrie(root, db.triedb, db.pointCache)
|
||||
ts := overlay.LoadTransitionState(db.TrieDB().Disk(), root, db.triedb.IsVerkle())
|
||||
if ts.InTransition() {
|
||||
panic("transition isn't supported yet")
|
||||
}
|
||||
if ts.Transitioned() {
|
||||
return trie.NewVerkleTrie(root, db.triedb, db.pointCache)
|
||||
}
|
||||
}
|
||||
tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb)
|
||||
if err != nil {
|
||||
|
|
@ -235,9 +246,6 @@ func (db *CachingDB) OpenTrie(root common.Hash) (Trie, error) {
|
|||
|
||||
// OpenStorageTrie opens the storage trie of an account.
|
||||
func (db *CachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash, self Trie) (Trie, error) {
|
||||
// In the verkle case, there is only one tree. But the two-tree structure
|
||||
// is hardcoded in the codebase. So we need to return the same trie in this
|
||||
// case.
|
||||
if db.triedb.IsVerkle() {
|
||||
return self, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -241,6 +241,7 @@ func newTrieReader(root common.Hash, db *triedb.Database, cache *utils.PointCach
|
|||
if !db.IsVerkle() {
|
||||
tr, err = trie.NewStateTrie(trie.StateTrieID(root), db)
|
||||
} else {
|
||||
// TODO @gballet determine the trie type (verkle or overlay) by transition state
|
||||
tr, err = trie.NewVerkleTrie(root, db, cache)
|
||||
}
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import (
|
|||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"slices"
|
||||
"testing"
|
||||
|
|
@ -202,12 +203,15 @@ func TestProcessVerkle(t *testing.T) {
|
|||
|
||||
t.Log("verified verkle proof, inserting blocks into the chain")
|
||||
|
||||
for i, b := range chain {
|
||||
fmt.Printf("%d %x\n", i, b.Root())
|
||||
}
|
||||
endnum, err := blockchain.InsertChain(chain)
|
||||
if err != nil {
|
||||
t.Fatalf("block %d imported with error: %v", endnum, err)
|
||||
}
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
for i := range 2 {
|
||||
b := blockchain.GetBlockByNumber(uint64(i) + 1)
|
||||
if b == nil {
|
||||
t.Fatalf("expected block %d to be present in chain", i+1)
|
||||
|
|
|
|||
Loading…
Reference in a new issue