From c6f269872637c90daa411e793f1699dfbf7ef243 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 8 Nov 2023 10:51:02 +0100 Subject: [PATCH 01/21] post-genesis transition --- core/blockchain.go | 6 +-- core/overlay_transition.go | 42 +++++++++--------- core/state/database.go | 89 ++++++++++++++++++++++---------------- core/state/statedb.go | 9 ++-- core/state_processor.go | 5 ++- eth/catalyst/api.go | 2 +- light/trie.go | 24 +++++----- miner/worker.go | 4 +- 8 files changed, 98 insertions(+), 83 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 0d43cb989074..173613e2c5c2 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1746,7 +1746,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) } if parent.Number.Uint64() == conversionBlock { - bc.StartVerkleTransition(parent.Root, emptyVerkleRoot, bc.Config(), &parent.Time) + bc.StartVerkleTransition(parent.Root, emptyVerkleRoot, bc.Config(), &parent.Time, parent.Root) bc.stateCache.SetLastMerkleRoot(parent.Root) } statedb, err := state.New(parent.Root, bc.stateCache, bc.snaps) @@ -2532,8 +2532,8 @@ func (bc *BlockChain) GetTrieFlushInterval() time.Duration { return time.Duration(bc.flushInterval.Load()) } -func (bc *BlockChain) StartVerkleTransition(originalRoot, translatedRoot common.Hash, chainConfig *params.ChainConfig, pragueTime *uint64) { - bc.stateCache.StartVerkleTransition(originalRoot, translatedRoot, chainConfig, pragueTime) +func (bc *BlockChain) StartVerkleTransition(originalRoot, translatedRoot common.Hash, chainConfig *params.ChainConfig, pragueTime *uint64, root common.Hash) { + bc.stateCache.StartVerkleTransition(originalRoot, translatedRoot, chainConfig, pragueTime, root) } func (bc *BlockChain) ReorgThroughVerkleTransition() { bc.stateCache.ReorgThroughVerkleTransition() diff --git a/core/overlay_transition.go b/core/overlay_transition.go index 35c09d22d938..24bb7d5e6c02 100644 --- a/core/overlay_transition.go +++ b/core/overlay_transition.go @@ -35,7 +35,7 @@ import ( ) // OverlayVerkleTransition contains the overlay conversion logic -func OverlayVerkleTransition(statedb *state.StateDB) error { +func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { migrdb := statedb.Database() // verkle transition: if the conversion process is in progress, move @@ -47,7 +47,7 @@ func OverlayVerkleTransition(statedb *state.StateDB) error { mpt = tt.Base() vkt = tt.Overlay() hasPreimagesBin = false - preimageSeek = migrdb.GetCurrentPreimageOffset() + preimageSeek = migrdb.GetCurrentPreimageOffset(root) fpreimages *bufio.Reader ) @@ -65,7 +65,7 @@ func OverlayVerkleTransition(statedb *state.StateDB) error { hasPreimagesBin = true } - accIt, err := statedb.Snaps().AccountIterator(mpt.Hash(), migrdb.GetCurrentAccountHash()) + accIt, err := statedb.Snaps().AccountIterator(mpt.Hash(), migrdb.GetCurrentAccountHash(root)) if err != nil { return err } @@ -73,7 +73,7 @@ func OverlayVerkleTransition(statedb *state.StateDB) error { accIt.Next() // If we're about to start with the migration process, we have to read the first account hash preimage. - if migrdb.GetCurrentAccountAddress() == nil { + if migrdb.GetCurrentAccountAddress(root) == nil { var addr common.Address if hasPreimagesBin { if _, err := io.ReadFull(fpreimages, addr[:]); err != nil { @@ -85,8 +85,8 @@ func OverlayVerkleTransition(statedb *state.StateDB) error { return fmt.Errorf("addr len is zero is not 32: %d", len(addr)) } } - migrdb.SetCurrentAccountAddress(addr) - if migrdb.GetCurrentAccountHash() != accIt.Hash() { + migrdb.SetCurrentAccountAddress(addr, root) + if migrdb.GetCurrentAccountHash(root) != accIt.Hash() { return fmt.Errorf("preimage file does not match account hash: %s != %s", crypto.Keccak256Hash(addr[:]), accIt.Hash()) } preimageSeek += int64(len(addr)) @@ -108,7 +108,7 @@ func OverlayVerkleTransition(statedb *state.StateDB) error { log.Error("Invalid account encountered during traversal", "error", err) return err } - vkt.SetStorageRootConversion(*migrdb.GetCurrentAccountAddress(), acc.Root) + vkt.SetStorageRootConversion(*migrdb.GetCurrentAccountAddress(root), acc.Root) // Start with processing the storage, because once the account is // converted, the `stateRoot` field loses its meaning. Which means @@ -120,7 +120,7 @@ func OverlayVerkleTransition(statedb *state.StateDB) error { // to during normal block execution. A mitigation strategy has been // introduced with the `*StorageRootConversion` fields in VerkleDB. if acc.HasStorage() { - stIt, err := statedb.Snaps().StorageIterator(mpt.Hash(), accIt.Hash(), migrdb.GetCurrentSlotHash()) + stIt, err := statedb.Snaps().StorageIterator(mpt.Hash(), accIt.Hash(), migrdb.GetCurrentSlotHash(root)) if err != nil { return err } @@ -132,7 +132,7 @@ func OverlayVerkleTransition(statedb *state.StateDB) error { // processing the storage for that account where we left off. // If the entire storage was processed, then the iterator was // created in vain, but it's ok as this will not happen often. - for ; !migrdb.GetStorageProcessed() && count < maxMovedCount; count++ { + for ; !migrdb.GetStorageProcessed(root) && count < maxMovedCount; count++ { var ( value []byte // slot value after RLP decoding safeValue [32]byte // 32-byte aligned value @@ -160,12 +160,12 @@ func OverlayVerkleTransition(statedb *state.StateDB) error { } preimageSeek += int64(len(slotnr)) - mkv.addStorageSlot(migrdb.GetCurrentAccountAddress().Bytes(), slotnr, safeValue[:]) + mkv.addStorageSlot(migrdb.GetCurrentAccountAddress(root).Bytes(), slotnr, safeValue[:]) // advance the storage iterator - migrdb.SetStorageProcessed(!stIt.Next()) - if !migrdb.GetStorageProcessed() { - migrdb.SetCurrentSlotHash(stIt.Hash()) + migrdb.SetStorageProcessed(!stIt.Next(), root) + if !migrdb.GetStorageProcessed(root) { + migrdb.SetCurrentSlotHash(stIt.Hash(), root) } } stIt.Release() @@ -178,20 +178,20 @@ func OverlayVerkleTransition(statedb *state.StateDB) error { if count < maxMovedCount { count++ // count increase for the account itself - mkv.addAccount(migrdb.GetCurrentAccountAddress().Bytes(), acc) - vkt.ClearStrorageRootConversion(*migrdb.GetCurrentAccountAddress()) + mkv.addAccount(migrdb.GetCurrentAccountAddress(root).Bytes(), acc) + vkt.ClearStrorageRootConversion(*migrdb.GetCurrentAccountAddress(root)) // Store the account code if present if !bytes.Equal(acc.CodeHash, types.EmptyCodeHash[:]) { code := rawdb.ReadCode(statedb.Database().DiskDB(), common.BytesToHash(acc.CodeHash)) chunks := trie.ChunkifyCode(code) - mkv.addAccountCode(migrdb.GetCurrentAccountAddress().Bytes(), uint64(len(code)), chunks) + mkv.addAccountCode(migrdb.GetCurrentAccountAddress(root).Bytes(), uint64(len(code)), chunks) } // reset storage iterator marker for next account - migrdb.SetStorageProcessed(false) - migrdb.SetCurrentSlotHash(common.Hash{}) + migrdb.SetStorageProcessed(false, root) + migrdb.SetCurrentSlotHash(common.Hash{}, root) // Move to the next account, if available - or end // the transition otherwise. @@ -212,7 +212,7 @@ func OverlayVerkleTransition(statedb *state.StateDB) error { return fmt.Errorf("preimage file does not match account hash: %s != %s", crypto.Keccak256Hash(addr[:]), accIt.Hash()) } preimageSeek += int64(len(addr)) - migrdb.SetCurrentAccountAddress(addr) + migrdb.SetCurrentAccountAddress(addr, root) } else { // case when the account iterator has // reached the end but count < maxCount @@ -221,9 +221,9 @@ func OverlayVerkleTransition(statedb *state.StateDB) error { } } } - migrdb.SetCurrentPreimageOffset(preimageSeek) + migrdb.SetCurrentPreimageOffset(preimageSeek, root) - log.Info("Collected key values from base tree", "count", count, "duration", time.Since(now), "last account", statedb.Database().GetCurrentAccountHash()) + log.Info("Collected key values from base tree", "count", count, "duration", time.Since(now), "last account", statedb.Database().GetCurrentAccountHash(root)) // Take all the collected key-values and prepare the new leaf values. // This fires a background routine that will start doing the work that diff --git a/core/state/database.go b/core/state/database.go index af8cc1a36e30..03e33b4f273a 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -64,7 +64,7 @@ type Database interface { // TrieDB retrieves the low level trie database used for data storage. TrieDB() *trie.Database - StartVerkleTransition(originalRoot, translatedRoot common.Hash, chainConfig *params.ChainConfig, cancunTime *uint64) + StartVerkleTransition(originalRoot, translatedRoot common.Hash, chainConfig *params.ChainConfig, cancunTime *uint64, root common.Hash) ReorgThroughVerkleTransition() @@ -74,27 +74,27 @@ type Database interface { Transitioned() bool - SetCurrentSlotHash(hash common.Hash) + SetCurrentSlotHash(common.Hash, common.Hash) - GetCurrentAccountAddress() *common.Address + GetCurrentAccountAddress(common.Hash) *common.Address - SetCurrentAccountAddress(common.Address) + SetCurrentAccountAddress(common.Address, common.Hash) - GetCurrentAccountHash() common.Hash + GetCurrentAccountHash(common.Hash) common.Hash - GetCurrentSlotHash() common.Hash + GetCurrentSlotHash(common.Hash) common.Hash - SetStorageProcessed(bool) + SetStorageProcessed(bool, common.Hash) - GetStorageProcessed() bool + GetStorageProcessed(common.Hash) bool - GetCurrentPreimageOffset() int64 + GetCurrentPreimageOffset(common.Hash) int64 - SetCurrentPreimageOffset(int64) + SetCurrentPreimageOffset(int64, common.Hash) AddRootTranslation(originalRoot, translatedRoot common.Hash) - SetLastMerkleRoot(root common.Hash) + SetLastMerkleRoot(common.Hash) } // Trie is a Ethereum Merkle Patricia trie. @@ -187,6 +187,10 @@ func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database { codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize), triedb: trie.NewDatabaseWithConfig(db, config), addrToPoint: utils.NewPointCache(), + StorageProcessed: map[common.Hash]bool{}, + CurrentAccountAddress: map[common.Hash]*common.Address{}, + CurrentSlotHash: map[common.Hash]common.Hash{}, + CurrentPreimageOffset: map[common.Hash]int64{}, } } @@ -199,6 +203,10 @@ func NewDatabaseWithNodeDB(db ethdb.Database, triedb *trie.Database) Database { triedb: triedb, addrToPoint: utils.NewPointCache(), ended: triedb.IsVerkle(), + StorageProcessed: map[common.Hash]bool{}, + CurrentAccountAddress: map[common.Hash]*common.Address{}, + CurrentSlotHash: map[common.Hash]common.Hash{}, + CurrentPreimageOffset: map[common.Hash]int64{}, } } @@ -211,7 +219,7 @@ func (db *cachingDB) Transitioned() bool { } // Fork implements the fork -func (db *cachingDB) StartVerkleTransition(originalRoot, translatedRoot common.Hash, chainConfig *params.ChainConfig, pragueTime *uint64) { +func (db *cachingDB) StartVerkleTransition(originalRoot, translatedRoot common.Hash, chainConfig *params.ChainConfig, pragueTime *uint64, root common.Hash) { fmt.Println(` __________.__ .__ .__ __ .__ .__ ____ \__ ___| |__ ____ ____ | | ____ ______ | |__ _____ _____/ |_ | |__ _____ ______ __ _ _|__| ____ / ___\ ______ @@ -224,7 +232,12 @@ func (db *cachingDB) StartVerkleTransition(originalRoot, translatedRoot common.H // db.AddTranslation(originalRoot, translatedRoot) db.baseRoot = originalRoot // initialize so that the first storage-less accounts are processed - db.StorageProcessed = true + db.StorageProcessed[root] = true + + // Reinitialize values in case of a reorg + db.CurrentAccountAddress[root] = &(common.Address{}) + db.CurrentSlotHash[root] = common.Hash{} + db.CurrentPreimageOffset[root] = 0 if pragueTime != nil { chainConfig.PragueTime = pragueTime } @@ -263,14 +276,14 @@ type cachingDB struct { addrToPoint *utils.PointCache baseRoot common.Hash // hash of the read-only base tree - 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 + CurrentAccountAddress map[common.Hash]*common.Address // addresss of the last translated account + CurrentSlotHash map[common.Hash]common.Hash // hash of the last translated storage slot + CurrentPreimageOffset map[common.Hash]int64 // next byte to read from the preimage file // 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 + StorageProcessed map[common.Hash]bool } func (db *cachingDB) openMPTTrie(root common.Hash) (Trie, error) { @@ -450,49 +463,49 @@ func (db *cachingDB) GetTreeKeyHeader(addr []byte) *verkle.Point { return db.addrToPoint.GetTreeKeyHeader(addr) } -func (db *cachingDB) SetCurrentAccountAddress(addr common.Address) { - db.CurrentAccountAddress = &addr +func (db *cachingDB) SetCurrentAccountAddress(addr common.Address, root common.Hash) { + db.CurrentAccountAddress[root] = &addr } -func (db *cachingDB) GetCurrentAccountHash() common.Hash { +func (db *cachingDB) GetCurrentAccountHash(root common.Hash) common.Hash { var addrHash common.Hash - if db.CurrentAccountAddress != nil { - addrHash = crypto.Keccak256Hash(db.CurrentAccountAddress[:]) + if db.CurrentAccountAddress[root] != nil { + addrHash = crypto.Keccak256Hash(db.CurrentAccountAddress[root][:]) } return addrHash } -func (db *cachingDB) GetCurrentAccountAddress() *common.Address { - return db.CurrentAccountAddress +func (db *cachingDB) GetCurrentAccountAddress(root common.Hash) *common.Address { + return db.CurrentAccountAddress[root] } -func (db *cachingDB) GetCurrentPreimageOffset() int64 { - return db.CurrentPreimageOffset +func (db *cachingDB) GetCurrentPreimageOffset(root common.Hash) int64 { + return db.CurrentPreimageOffset[root] } -func (db *cachingDB) SetCurrentPreimageOffset(offset int64) { - db.CurrentPreimageOffset = offset +func (db *cachingDB) SetCurrentPreimageOffset(offset int64, root common.Hash) { + db.CurrentPreimageOffset[root] = offset } -func (db *cachingDB) SetCurrentSlotHash(hash common.Hash) { - db.CurrentSlotHash = hash +func (db *cachingDB) SetCurrentSlotHash(hash common.Hash, root common.Hash) { + db.CurrentSlotHash[root] = hash } -func (db *cachingDB) GetCurrentSlotHash() common.Hash { - return db.CurrentSlotHash +func (db *cachingDB) GetCurrentSlotHash(root common.Hash) common.Hash { + return db.CurrentSlotHash[root] } -func (db *cachingDB) SetStorageProcessed(processed bool) { - db.StorageProcessed = processed +func (db *cachingDB) SetStorageProcessed(processed bool, root common.Hash) { + db.StorageProcessed[root] = processed } -func (db *cachingDB) GetStorageProcessed() bool { - return db.StorageProcessed +func (db *cachingDB) GetStorageProcessed(root common.Hash) bool { + return db.StorageProcessed[root] } func (db *cachingDB) AddRootTranslation(originalRoot, translatedRoot common.Hash) { } -func (db *cachingDB) SetLastMerkleRoot(root common.Hash) { - db.LastMerkleRoot = root +func (db *cachingDB) SetLastMerkleRoot(merkleRoot common.Hash) { + db.LastMerkleRoot = merkleRoot } diff --git a/core/state/statedb.go b/core/state/statedb.go index 442527a1e906..49fa246593ae 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -176,10 +176,11 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) if tr.IsVerkle() { sdb.witness = sdb.NewAccessWitness() } - // if sdb.snaps != nil { - // if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap == nil { - // } - // } + if sdb.snaps != nil { + // if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap == nil { + // } + sdb.snap = sdb.snaps.Snapshot(root) + } return sdb, nil } diff --git a/core/state_processor.go b/core/state_processor.go index 5d10bceb1817..47261e8a2111 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -106,7 +106,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg } // Perform the overlay transition, if relevant - if err := OverlayVerkleTransition(statedb); err != nil { + parent := p.bc.GetHeaderByHash(header.ParentHash) + if err := OverlayVerkleTransition(statedb, parent.Root); err != nil { return nil, nil, 0, fmt.Errorf("error performing verkle overlay transition: %w", err) } @@ -320,7 +321,7 @@ func (kvm *keyValueMigrator) prepare() { var currAddr common.Address var currPoint *verkle.Point for i := range batch { - if batch[i].branchKey.addr != currAddr { + if batch[i].branchKey.addr != currAddr || currAddr == (common.Address{}) { currAddr = batch[i].branchKey.addr currPoint = tutils.EvaluateAddressPoint(currAddr[:]) } diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index 63079415fc14..864b3efe84f5 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -532,7 +532,7 @@ func (api *ConsensusAPI) newPayload(params engine.ExecutableData, versionedHashe if api.eth.BlockChain().Config().IsPrague(block.Number(), block.Time()) && !api.eth.BlockChain().Config().IsPrague(parent.Number(), parent.Time()) { parent := api.eth.BlockChain().GetHeaderByNumber(block.NumberU64() - 1) if !api.eth.BlockChain().Config().IsPrague(parent.Number, parent.Time) { - api.eth.BlockChain().StartVerkleTransition(parent.Root, common.Hash{}, api.eth.BlockChain().Config(), nil) + api.eth.BlockChain().StartVerkleTransition(parent.Root, common.Hash{}, api.eth.BlockChain().Config(), nil, parent.Root) } } // Reset db merge state in case of a reorg diff --git a/light/trie.go b/light/trie.go index 53d54615d909..6d0c654ff111 100644 --- a/light/trie.go +++ b/light/trie.go @@ -101,7 +101,7 @@ func (db *odrDatabase) DiskDB() ethdb.KeyValueStore { panic("not implemented") } -func (db *odrDatabase) StartVerkleTransition(originalRoot common.Hash, translatedRoot common.Hash, chainConfig *params.ChainConfig, _ *uint64) { +func (db *odrDatabase) StartVerkleTransition(originalRoot common.Hash, translatedRoot common.Hash, chainConfig *params.ChainConfig, _ *uint64, _ common.Hash) { panic("not implemented") // TODO: Implement } @@ -121,47 +121,47 @@ func (db *odrDatabase) Transitioned() bool { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) SetCurrentSlotHash(hash common.Hash) { +func (db *odrDatabase) SetCurrentSlotHash(common.Hash, common.Hash) { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) GetCurrentAccountAddress() *common.Address { +func (db *odrDatabase) GetCurrentAccountAddress(common.Hash) *common.Address { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) SetCurrentAccountAddress(_ common.Address) { +func (db *odrDatabase) SetCurrentAccountAddress(common.Address, common.Hash) { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) GetCurrentAccountHash() common.Hash { +func (db *odrDatabase) GetCurrentAccountHash(common.Hash) common.Hash { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) GetCurrentSlotHash() common.Hash { +func (db *odrDatabase) GetCurrentSlotHash(common.Hash) common.Hash { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) SetStorageProcessed(_ bool) { +func (db *odrDatabase) SetStorageProcessed(bool, common.Hash) { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) GetStorageProcessed() bool { +func (db *odrDatabase) GetStorageProcessed(common.Hash) bool { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) GetCurrentPreimageOffset() int64 { +func (db *odrDatabase) GetCurrentPreimageOffset(common.Hash) int64 { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) SetCurrentPreimageOffset(_ int64) { +func (db *odrDatabase) SetCurrentPreimageOffset(int64, common.Hash) { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) AddRootTranslation(originalRoot common.Hash, translatedRoot common.Hash) { +func (db *odrDatabase) AddRootTranslation(common.Hash, common.Hash) { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) SetLastMerkleRoot(root common.Hash) { +func (db *odrDatabase) SetLastMerkleRoot(common.Hash) { panic("not implemented") // TODO: Implement } diff --git a/miner/worker.go b/miner/worker.go index 124c93212262..e64fde49f08a 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -894,7 +894,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) { if w.chain.Config().IsPrague(header.Number, header.Time) { parent := w.chain.GetHeaderByNumber(header.Number.Uint64() - 1) if !w.chain.Config().IsPrague(parent.Number, parent.Time) { - w.chain.StartVerkleTransition(parent.Root, common.Hash{}, w.chain.Config(), nil) + w.chain.StartVerkleTransition(parent.Root, common.Hash{}, w.chain.Config(), w.chain.Config().PragueTime, parent.Root) } } @@ -905,7 +905,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) { return nil, err } if w.chain.Config().IsPrague(header.Number, header.Time) { - core.OverlayVerkleTransition(state) + core.OverlayVerkleTransition(state, parent.Root) } // Run the consensus preparation with the default or customized consensus engine. if err := w.engine.Prepare(w.chain, header); err != nil { From 581d77dbf9a2f209abe30e6f70c0df368dc824c6 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 8 Nov 2023 11:05:08 +0100 Subject: [PATCH 02/21] quell linter issue --- core/state/database.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/core/state/database.go b/core/state/database.go index 03e33b4f273a..378517c486b0 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -182,11 +182,11 @@ func NewDatabase(db ethdb.Database) Database { // large memory cache. func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database { return &cachingDB{ - disk: db, - codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize), - codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize), - triedb: trie.NewDatabaseWithConfig(db, config), - addrToPoint: utils.NewPointCache(), + disk: db, + codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize), + codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize), + triedb: trie.NewDatabaseWithConfig(db, config), + addrToPoint: utils.NewPointCache(), StorageProcessed: map[common.Hash]bool{}, CurrentAccountAddress: map[common.Hash]*common.Address{}, CurrentSlotHash: map[common.Hash]common.Hash{}, @@ -197,12 +197,12 @@ func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database { // NewDatabaseWithNodeDB creates a state database with an already initialized node database. func NewDatabaseWithNodeDB(db ethdb.Database, triedb *trie.Database) Database { return &cachingDB{ - disk: db, - codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize), - codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize), - triedb: triedb, - addrToPoint: utils.NewPointCache(), - ended: triedb.IsVerkle(), + disk: db, + codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize), + codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize), + triedb: triedb, + addrToPoint: utils.NewPointCache(), + ended: triedb.IsVerkle(), StorageProcessed: map[common.Hash]bool{}, CurrentAccountAddress: map[common.Hash]*common.Address{}, CurrentSlotHash: map[common.Hash]common.Hash{}, @@ -275,7 +275,7 @@ type cachingDB struct { addrToPoint *utils.PointCache - baseRoot common.Hash // hash of the read-only base tree + baseRoot common.Hash // hash of the read-only base tree CurrentAccountAddress map[common.Hash]*common.Address // addresss of the last translated account CurrentSlotHash map[common.Hash]common.Hash // hash of the last translated storage slot CurrentPreimageOffset map[common.Hash]int64 // next byte to read from the preimage file From 94cf1bbc5f08902ae841f18b8acbde353e9d6b19 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 8 Nov 2023 15:04:10 +0100 Subject: [PATCH 03/21] "unforce" verkle at genesis --- cmd/geth/chaincmd.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 55c22f7322f3..9ab6335205f5 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -214,7 +214,6 @@ func initGenesis(ctx *cli.Context) error { } triedb := trie.NewDatabaseWithConfig(chaindb, &trie.Config{ Preimages: ctx.Bool(utils.CachePreimagesFlag.Name), - Verkle: true, }) _, hash, err := core.SetupGenesisBlockWithOverride(chaindb, triedb, genesis, &overrides) if err != nil { From 21cebf02da3545be0fc2b1d879d3171dfbb35ee7 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 8 Nov 2023 17:33:28 +0100 Subject: [PATCH 04/21] support Transition post tree in conversion --- consensus/beacon/consensus.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go index 2ca701c42f56..1c0399be86e2 100644 --- a/consensus/beacon/consensus.go +++ b/consensus/beacon/consensus.go @@ -415,7 +415,19 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea switch pre := preTrie.(type) { case *trie.VerkleTrie: vtrpre, okpre = preTrie.(*trie.VerkleTrie) - vtrpost, okpost = state.GetTrie().(*trie.VerkleTrie) + switch tr := state.GetTrie().(type) { + case *trie.VerkleTrie: + vtrpost = tr + okpost = true + // This is to handle a situation right at the start of the conversion: + // the post trie is a transition tree when the pre tree is an empty + // verkle tree. + case *trie.TransitionTrie: + vtrpost = tr.Overlay() + okpost = true + default: + okpost = false + } case *trie.TransitionTrie: vtrpre = pre.Overlay() okpre = true From 1d80ebd0553aa6f7ac62e67ce9acd5d0cb4ed9c9 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Thu, 30 Nov 2023 14:45:58 +0100 Subject: [PATCH 05/21] Refactor transition post genesis (#311) * rewrite per-block conversion pointer management * remove unused method * fix: a branch that can verge at genesis or post genesis (#314) * fix: import cycle in conversion refactor (#315) * fix shadowfork panic in OpenStorageTrie --- cmd/geth/chaincmd.go | 1 + consensus/beacon/consensus.go | 18 +- core/blockchain.go | 6 + core/chain_makers.go | 14 +- core/genesis.go | 27 ++- .../conversion.go} | 224 ++++++++++++++++-- core/state/database.go | 206 ++++++++++------ core/state_processor.go | 188 --------------- light/trie.go | 30 ++- miner/worker.go | 3 - 10 files changed, 403 insertions(+), 314 deletions(-) rename core/{overlay_transition.go => overlay/conversion.go} (53%) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 9ab6335205f5..2179b61032b6 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -214,6 +214,7 @@ func initGenesis(ctx *cli.Context) error { } triedb := trie.NewDatabaseWithConfig(chaindb, &trie.Config{ Preimages: ctx.Bool(utils.CachePreimagesFlag.Name), + Verkle: genesis.IsVerkle(), }) _, hash, err := core.SetupGenesisBlockWithOverride(chaindb, triedb, genesis, &overrides) if err != nil { diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go index 1c0399be86e2..6582a7bfb90b 100644 --- a/consensus/beacon/consensus.go +++ b/consensus/beacon/consensus.go @@ -25,6 +25,7 @@ import ( "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/misc/eip1559" "github.com/ethereum/go-ethereum/consensus/misc/eip4844" + "github.com/ethereum/go-ethereum/core/overlay" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" @@ -368,6 +369,12 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types. state.Witness().TouchAddressOnWriteAndComputeGas(header.Coinbase[:], uint256.Int{}, utils.NonceLeafKey) state.Witness().TouchAddressOnWriteAndComputeGas(header.Coinbase[:], uint256.Int{}, utils.CodeKeccakLeafKey) state.Witness().TouchAddressOnWriteAndComputeGas(header.Coinbase[:], uint256.Int{}, utils.CodeSizeLeafKey) + + if chain.Config().IsPrague(header.Number, header.Time) { + fmt.Println("at block", header.Number, "performing transition?", state.Database().InTransition()) + parent := chain.GetHeaderByHash(header.ParentHash) + overlay.OverlayVerkleTransition(state, parent.Root) + } } // FinalizeAndAssemble implements consensus.Engine, setting the final state and @@ -392,6 +399,7 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea // Assign the final state root to header. header.Root = state.IntermediateRoot(true) + state.Database().SaveTransitionState(header.Root) var ( p *verkle.VerkleProof @@ -405,6 +413,7 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea return nil, fmt.Errorf("nil parent header for block %d", header.Number) } + state.Database().LoadTransitionState(parent.Root) preTrie, err := state.Database().OpenTrie(parent.Root) if err != nil { return nil, fmt.Errorf("error opening pre-state tree root: %w", err) @@ -435,7 +444,14 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea vtrpost = post.Overlay() okpost = true default: - panic("invalid tree type") + // This should only happen for the first block, + // so the previous tree is a merkle tree. Logically, + // the "previous" verkle tree is an empty tree. + okpre = true + vtrpre = trie.NewVerkleTrie(verkle.New(), state.Database().TrieDB(), utils.NewPointCache(), false) + post := state.GetTrie().(*trie.TransitionTrie) + vtrpost = post.Overlay() + okpost = true } if okpre && okpost { if len(keys) > 0 { diff --git a/core/blockchain.go b/core/blockchain.go index 173613e2c5c2..e39cccad5b79 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -312,6 +312,12 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis // Declare the end of the verkle transition if need be if bc.chainConfig.Rules(head.Number, false /* XXX */, head.Time).IsPrague { + // TODO this only works when resuming a chain that has already gone + // through the conversion. All pointers should be saved to the DB + // for it to be able to recover if interrupted during the transition + // but that's left out to a later PR since there's not really a need + // right now. + bc.stateCache.InitTransitionStatus(true, true) bc.stateCache.EndVerkleTransition() } diff --git a/core/chain_makers.go b/core/chain_makers.go index 5d8ade8a0fb0..ec12f2d4ed21 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -425,13 +425,15 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine return nil, nil } var snaps *snapshot.Tree + triedb := state.NewDatabaseWithConfig(db, nil) + triedb.StartVerkleTransition(common.Hash{}, common.Hash{}, config, config.PragueTime, common.Hash{}) + triedb.EndVerkleTransition() + statedb, err := state.New(parent.Root(), triedb, snaps) + if err != nil { + panic(fmt.Sprintf("could not find state for block %d: err=%v, parent root=%x", parent.NumberU64(), err, parent.Root())) + } + statedb.Database().SaveTransitionState(parent.Root()) for i := 0; i < n; i++ { - triedb := state.NewDatabaseWithConfig(db, nil) - triedb.EndVerkleTransition() - statedb, err := state.New(parent.Root(), triedb, snaps) - if err != nil { - panic(fmt.Sprintf("could not find state for block %d: err=%v, parent root=%x", i, err, parent.Root())) - } block, receipt := genblock(i, parent, statedb) blocks[i] = block receipts[i] = receipt diff --git a/core/genesis.go b/core/genesis.go index 74294afb7272..efa339024c7e 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -126,6 +126,7 @@ func (ga *GenesisAlloc) deriveHash(cfg *params.ChainConfig, timestamp uint64) (c // all the derived states will be discarded to not pollute disk. db := state.NewDatabase(rawdb.NewMemoryDatabase()) if cfg.IsPrague(big.NewInt(int64(0)), timestamp) { + db.StartVerkleTransition(common.Hash{}, common.Hash{}, cfg, ×tamp, common.Hash{}) db.EndVerkleTransition() } statedb, err := state.New(types.EmptyRootHash, db, nil) @@ -146,15 +147,17 @@ func (ga *GenesisAlloc) deriveHash(cfg *params.ChainConfig, timestamp uint64) (c // flush is very similar with deriveHash, but the main difference is // all the generated states will be persisted into the given database. // Also, the genesis state specification will be flushed as well. -func (ga *GenesisAlloc) flush(db ethdb.Database, triedb *trie.Database, blockhash common.Hash, cfg *params.ChainConfig) error { - statedb, err := state.New(types.EmptyRootHash, state.NewDatabaseWithNodeDB(db, triedb), nil) - if err != nil { - return err +func (ga *GenesisAlloc) flush(db ethdb.Database, triedb *trie.Database, blockhash common.Hash, cfg *params.ChainConfig, timestamp *uint64) error { + database := state.NewDatabaseWithNodeDB(db, triedb) + // End the verkle conversion at genesis if the fork block is 0 + if timestamp != nil && cfg.IsPrague(big.NewInt(int64(0)), *timestamp) { + database.StartVerkleTransition(common.Hash{}, common.Hash{}, cfg, timestamp, common.Hash{}) + database.EndVerkleTransition() } - // End the verkle conversion at genesis if the fork block is 0 - if triedb.IsVerkle() { - statedb.Database().EndVerkleTransition() + statedb, err := state.New(types.EmptyRootHash, database, nil) + if err != nil { + return err } for addr, account := range *ga { @@ -221,7 +224,7 @@ func CommitGenesisState(db ethdb.Database, triedb *trie.Database, blockhash comm return errors.New("not found") } } - return alloc.flush(db, triedb, blockhash, config) + return alloc.flush(db, triedb, blockhash, config, nil) } // GenesisAccount is an account in the state of the genesis block. @@ -456,6 +459,12 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig { } } +// IsVerkle indicates whether the state is already stored in a verkle +// tree at genesis time. +func (g *Genesis) IsVerkle() bool { + return g.Config.IsPrague(new(big.Int).SetUint64(g.Number), g.Timestamp) +} + // ToBlock returns the genesis block according to genesis specification. func (g *Genesis) ToBlock() *types.Block { root, err := g.Alloc.deriveHash(g.Config, g.Timestamp) @@ -530,7 +539,7 @@ func (g *Genesis) Commit(db ethdb.Database, triedb *trie.Database) (*types.Block // All the checks has passed, flush the states derived from the genesis // specification as well as the specification itself into the provided // database. - if err := g.Alloc.flush(db, triedb, block.Hash(), g.Config); err != nil { + if err := g.Alloc.flush(db, triedb, block.Hash(), g.Config, &g.Timestamp); err != nil { return nil, err } rawdb.WriteTd(db, block.Hash(), block.NumberU64(), block.Difficulty()) diff --git a/core/overlay_transition.go b/core/overlay/conversion.go similarity index 53% rename from core/overlay_transition.go rename to core/overlay/conversion.go index 24bb7d5e6c02..e76aa5900173 100644 --- a/core/overlay_transition.go +++ b/core/overlay/conversion.go @@ -14,14 +14,17 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package core +package overlay import ( "bufio" "bytes" + "encoding/binary" "fmt" "io" "os" + "runtime" + "sync" "time" "github.com/ethereum/go-ethereum/common" @@ -32,8 +35,187 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/trie/utils" + "github.com/gballet/go-verkle" + "github.com/holiman/uint256" ) +var zeroTreeIndex uint256.Int + +// keyValueMigrator is a helper module that collects key-values from the overlay-tree migration for Verkle Trees. +// It assumes that the walk of the base tree is done in address-order, so it exploit that fact to +// collect the key-values in a way that is efficient. +type keyValueMigrator struct { + // leafData contains the values for the future leaf for a particular VKT branch. + leafData []migratedKeyValue + + // When prepare() is called, it will start a background routine that will process the leafData + // saving the result in newLeaves to be used by migrateCollectedKeyValues(). The background + // routine signals that it is done by closing processingReady. + processingReady chan struct{} + newLeaves []verkle.LeafNode + prepareErr error +} + +func newKeyValueMigrator() *keyValueMigrator { + // We do initialize the VKT config since prepare() might indirectly make multiple GetConfig() calls + // in different goroutines when we never called GetConfig() before, causing a race considering the way + // that `config` is designed in go-verkle. + // TODO: jsign as a fix for this in the PR where we move to a file-less precomp, since it allows safe + // concurrent calls to GetConfig(). When that gets merged, we can remove this line. + _ = verkle.GetConfig() + return &keyValueMigrator{ + processingReady: make(chan struct{}), + leafData: make([]migratedKeyValue, 0, 10_000), + } +} + +type migratedKeyValue struct { + branchKey branchKey + leafNodeData verkle.BatchNewLeafNodeData +} +type branchKey struct { + addr common.Address + treeIndex uint256.Int +} + +func newBranchKey(addr []byte, treeIndex *uint256.Int) branchKey { + var sk branchKey + copy(sk.addr[:], addr) + sk.treeIndex = *treeIndex + return sk +} + +func (kvm *keyValueMigrator) addStorageSlot(addr []byte, slotNumber []byte, slotValue []byte) { + treeIndex, subIndex := utils.GetTreeKeyStorageSlotTreeIndexes(slotNumber) + leafNodeData := kvm.getOrInitLeafNodeData(newBranchKey(addr, treeIndex)) + leafNodeData.Values[subIndex] = slotValue +} + +func (kvm *keyValueMigrator) addAccount(addr []byte, acc *types.StateAccount) { + leafNodeData := kvm.getOrInitLeafNodeData(newBranchKey(addr, &zeroTreeIndex)) + + var version [verkle.LeafValueSize]byte + leafNodeData.Values[utils.VersionLeafKey] = version[:] + + var balance [verkle.LeafValueSize]byte + for i, b := range acc.Balance.Bytes() { + balance[len(acc.Balance.Bytes())-1-i] = b + } + leafNodeData.Values[utils.BalanceLeafKey] = balance[:] + + var nonce [verkle.LeafValueSize]byte + binary.LittleEndian.PutUint64(nonce[:8], acc.Nonce) + leafNodeData.Values[utils.NonceLeafKey] = nonce[:] + + leafNodeData.Values[utils.CodeKeccakLeafKey] = acc.CodeHash[:] +} + +func (kvm *keyValueMigrator) addAccountCode(addr []byte, codeSize uint64, chunks []byte) { + leafNodeData := kvm.getOrInitLeafNodeData(newBranchKey(addr, &zeroTreeIndex)) + + // Save the code size. + var codeSizeBytes [verkle.LeafValueSize]byte + binary.LittleEndian.PutUint64(codeSizeBytes[:8], codeSize) + leafNodeData.Values[utils.CodeSizeLeafKey] = codeSizeBytes[:] + + // The first 128 chunks are stored in the account header leaf. + for i := 0; i < 128 && i < len(chunks)/32; i++ { + leafNodeData.Values[byte(128+i)] = chunks[32*i : 32*(i+1)] + } + + // Potential further chunks, have their own leaf nodes. + for i := 128; i < len(chunks)/32; { + treeIndex, _ := utils.GetTreeKeyCodeChunkIndices(uint256.NewInt(uint64(i))) + leafNodeData := kvm.getOrInitLeafNodeData(newBranchKey(addr, treeIndex)) + + j := i + for ; (j-i) < 256 && j < len(chunks)/32; j++ { + leafNodeData.Values[byte((j-128)%256)] = chunks[32*j : 32*(j+1)] + } + i = j + } +} + +func (kvm *keyValueMigrator) getOrInitLeafNodeData(bk branchKey) *verkle.BatchNewLeafNodeData { + // Remember that keyValueMigration receives actions ordered by (address, subtreeIndex). + // This means that we can assume that the last element of leafData is the one that we + // are looking for, or that we need to create a new one. + if len(kvm.leafData) == 0 || kvm.leafData[len(kvm.leafData)-1].branchKey != bk { + kvm.leafData = append(kvm.leafData, migratedKeyValue{ + branchKey: bk, + leafNodeData: verkle.BatchNewLeafNodeData{ + Stem: nil, // It will be calculated in the prepare() phase, since it's CPU heavy. + Values: make(map[byte][]byte), + }, + }) + } + return &kvm.leafData[len(kvm.leafData)-1].leafNodeData +} + +func (kvm *keyValueMigrator) prepare() { + // We fire a background routine to process the leafData and save the result in newLeaves. + // The background routine signals that it is done by closing processingReady. + go func() { + // Step 1: We split kvm.leafData in numBatches batches, and we process each batch in a separate goroutine. + // This fills each leafNodeData.Stem with the correct value. + var wg sync.WaitGroup + batchNum := runtime.NumCPU() + batchSize := (len(kvm.leafData) + batchNum - 1) / batchNum + for i := 0; i < len(kvm.leafData); i += batchSize { + start := i + end := i + batchSize + if end > len(kvm.leafData) { + end = len(kvm.leafData) + } + wg.Add(1) + + batch := kvm.leafData[start:end] + go func() { + defer wg.Done() + var currAddr common.Address + var currPoint *verkle.Point + for i := range batch { + if batch[i].branchKey.addr != currAddr || currAddr == (common.Address{}) { + currAddr = batch[i].branchKey.addr + currPoint = utils.EvaluateAddressPoint(currAddr[:]) + } + stem := utils.GetTreeKeyWithEvaluatedAddess(currPoint, &batch[i].branchKey.treeIndex, 0) + stem = stem[:verkle.StemSize] + batch[i].leafNodeData.Stem = stem + } + }() + } + wg.Wait() + + // Step 2: Now that we have all stems (i.e: tree keys) calculated, we can create the new leaves. + nodeValues := make([]verkle.BatchNewLeafNodeData, len(kvm.leafData)) + for i := range kvm.leafData { + nodeValues[i] = kvm.leafData[i].leafNodeData + } + + // Create all leaves in batch mode so we can optimize cryptography operations. + kvm.newLeaves, kvm.prepareErr = verkle.BatchNewLeafNode(nodeValues) + close(kvm.processingReady) + }() +} + +func (kvm *keyValueMigrator) migrateCollectedKeyValues(tree *trie.VerkleTrie) error { + now := time.Now() + <-kvm.processingReady + if kvm.prepareErr != nil { + return fmt.Errorf("failed to prepare key values: %w", kvm.prepareErr) + } + log.Info("Prepared key values from base tree", "duration", time.Since(now)) + + // Insert into the tree. + if err := tree.InsertMigratedLeaves(kvm.newLeaves); err != nil { + return fmt.Errorf("failed to insert migrated leaves: %w", err) + } + + return nil +} + // OverlayVerkleTransition contains the overlay conversion logic func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { migrdb := statedb.Database() @@ -47,7 +229,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { mpt = tt.Base() vkt = tt.Overlay() hasPreimagesBin = false - preimageSeek = migrdb.GetCurrentPreimageOffset(root) + preimageSeek = migrdb.GetCurrentPreimageOffset() fpreimages *bufio.Reader ) @@ -65,7 +247,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { hasPreimagesBin = true } - accIt, err := statedb.Snaps().AccountIterator(mpt.Hash(), migrdb.GetCurrentAccountHash(root)) + accIt, err := statedb.Snaps().AccountIterator(mpt.Hash(), migrdb.GetCurrentAccountHash()) if err != nil { return err } @@ -73,7 +255,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { accIt.Next() // If we're about to start with the migration process, we have to read the first account hash preimage. - if migrdb.GetCurrentAccountAddress(root) == nil { + if migrdb.GetCurrentAccountAddress() == nil { var addr common.Address if hasPreimagesBin { if _, err := io.ReadFull(fpreimages, addr[:]); err != nil { @@ -85,8 +267,8 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { return fmt.Errorf("addr len is zero is not 32: %d", len(addr)) } } - migrdb.SetCurrentAccountAddress(addr, root) - if migrdb.GetCurrentAccountHash(root) != accIt.Hash() { + migrdb.SetCurrentAccountAddress(addr) + if migrdb.GetCurrentAccountHash() != accIt.Hash() { return fmt.Errorf("preimage file does not match account hash: %s != %s", crypto.Keccak256Hash(addr[:]), accIt.Hash()) } preimageSeek += int64(len(addr)) @@ -108,7 +290,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { log.Error("Invalid account encountered during traversal", "error", err) return err } - vkt.SetStorageRootConversion(*migrdb.GetCurrentAccountAddress(root), acc.Root) + vkt.SetStorageRootConversion(*migrdb.GetCurrentAccountAddress(), acc.Root) // Start with processing the storage, because once the account is // converted, the `stateRoot` field loses its meaning. Which means @@ -120,7 +302,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { // to during normal block execution. A mitigation strategy has been // introduced with the `*StorageRootConversion` fields in VerkleDB. if acc.HasStorage() { - stIt, err := statedb.Snaps().StorageIterator(mpt.Hash(), accIt.Hash(), migrdb.GetCurrentSlotHash(root)) + stIt, err := statedb.Snaps().StorageIterator(mpt.Hash(), accIt.Hash(), migrdb.GetCurrentSlotHash()) if err != nil { return err } @@ -132,7 +314,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { // processing the storage for that account where we left off. // If the entire storage was processed, then the iterator was // created in vain, but it's ok as this will not happen often. - for ; !migrdb.GetStorageProcessed(root) && count < maxMovedCount; count++ { + for ; !migrdb.GetStorageProcessed() && count < maxMovedCount; count++ { var ( value []byte // slot value after RLP decoding safeValue [32]byte // 32-byte aligned value @@ -160,12 +342,12 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { } preimageSeek += int64(len(slotnr)) - mkv.addStorageSlot(migrdb.GetCurrentAccountAddress(root).Bytes(), slotnr, safeValue[:]) + mkv.addStorageSlot(migrdb.GetCurrentAccountAddress().Bytes(), slotnr, safeValue[:]) // advance the storage iterator - migrdb.SetStorageProcessed(!stIt.Next(), root) - if !migrdb.GetStorageProcessed(root) { - migrdb.SetCurrentSlotHash(stIt.Hash(), root) + migrdb.SetStorageProcessed(!stIt.Next()) + if !migrdb.GetStorageProcessed() { + migrdb.SetCurrentSlotHash(stIt.Hash()) } } stIt.Release() @@ -178,20 +360,20 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { if count < maxMovedCount { count++ // count increase for the account itself - mkv.addAccount(migrdb.GetCurrentAccountAddress(root).Bytes(), acc) - vkt.ClearStrorageRootConversion(*migrdb.GetCurrentAccountAddress(root)) + mkv.addAccount(migrdb.GetCurrentAccountAddress().Bytes(), acc) + vkt.ClearStrorageRootConversion(*migrdb.GetCurrentAccountAddress()) // Store the account code if present if !bytes.Equal(acc.CodeHash, types.EmptyCodeHash[:]) { code := rawdb.ReadCode(statedb.Database().DiskDB(), common.BytesToHash(acc.CodeHash)) chunks := trie.ChunkifyCode(code) - mkv.addAccountCode(migrdb.GetCurrentAccountAddress(root).Bytes(), uint64(len(code)), chunks) + mkv.addAccountCode(migrdb.GetCurrentAccountAddress().Bytes(), uint64(len(code)), chunks) } // reset storage iterator marker for next account - migrdb.SetStorageProcessed(false, root) - migrdb.SetCurrentSlotHash(common.Hash{}, root) + migrdb.SetStorageProcessed(false) + migrdb.SetCurrentSlotHash(common.Hash{}) // Move to the next account, if available - or end // the transition otherwise. @@ -212,7 +394,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { return fmt.Errorf("preimage file does not match account hash: %s != %s", crypto.Keccak256Hash(addr[:]), accIt.Hash()) } preimageSeek += int64(len(addr)) - migrdb.SetCurrentAccountAddress(addr, root) + migrdb.SetCurrentAccountAddress(addr) } else { // case when the account iterator has // reached the end but count < maxCount @@ -221,9 +403,9 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { } } } - migrdb.SetCurrentPreimageOffset(preimageSeek, root) + migrdb.SetCurrentPreimageOffset(preimageSeek) - log.Info("Collected key values from base tree", "count", count, "duration", time.Since(now), "last account", statedb.Database().GetCurrentAccountHash(root)) + log.Info("Collected key values from base tree", "count", count, "duration", time.Since(now), "last account", statedb.Database().GetCurrentAccountHash()) // Take all the collected key-values and prepare the new leaf values. // This fires a background routine that will start doing the work that diff --git a/core/state/database.go b/core/state/database.go index 378517c486b0..2ff85a4c4724 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -26,6 +26,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie/trienode" @@ -74,27 +75,33 @@ type Database interface { Transitioned() bool - SetCurrentSlotHash(common.Hash, common.Hash) + InitTransitionStatus(bool, bool) - GetCurrentAccountAddress(common.Hash) *common.Address + SetCurrentSlotHash(common.Hash) - SetCurrentAccountAddress(common.Address, common.Hash) + GetCurrentAccountAddress() *common.Address - GetCurrentAccountHash(common.Hash) common.Hash + SetCurrentAccountAddress(common.Address) - GetCurrentSlotHash(common.Hash) common.Hash + GetCurrentAccountHash() common.Hash - SetStorageProcessed(bool, common.Hash) + GetCurrentSlotHash() common.Hash - GetStorageProcessed(common.Hash) bool + SetStorageProcessed(bool) - GetCurrentPreimageOffset(common.Hash) int64 + GetStorageProcessed() bool - SetCurrentPreimageOffset(int64, common.Hash) + GetCurrentPreimageOffset() int64 + + SetCurrentPreimageOffset(int64) AddRootTranslation(originalRoot, translatedRoot common.Hash) SetLastMerkleRoot(common.Hash) + + SaveTransitionState(common.Hash) + + LoadTransitionState(common.Hash) } // Trie is a Ethereum Merkle Patricia trie. @@ -182,40 +189,31 @@ func NewDatabase(db ethdb.Database) Database { // large memory cache. func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database { return &cachingDB{ - disk: db, - codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize), - codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize), - triedb: trie.NewDatabaseWithConfig(db, config), - addrToPoint: utils.NewPointCache(), - StorageProcessed: map[common.Hash]bool{}, - CurrentAccountAddress: map[common.Hash]*common.Address{}, - CurrentSlotHash: map[common.Hash]common.Hash{}, - CurrentPreimageOffset: map[common.Hash]int64{}, + disk: db, + codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize), + codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize), + triedb: trie.NewDatabaseWithConfig(db, config), + addrToPoint: utils.NewPointCache(), } } // NewDatabaseWithNodeDB creates a state database with an already initialized node database. func NewDatabaseWithNodeDB(db ethdb.Database, triedb *trie.Database) Database { return &cachingDB{ - disk: db, - codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize), - codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize), - triedb: triedb, - addrToPoint: utils.NewPointCache(), - ended: triedb.IsVerkle(), - StorageProcessed: map[common.Hash]bool{}, - CurrentAccountAddress: map[common.Hash]*common.Address{}, - CurrentSlotHash: map[common.Hash]common.Hash{}, - CurrentPreimageOffset: map[common.Hash]int64{}, + disk: db, + codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize), + codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize), + triedb: triedb, + addrToPoint: utils.NewPointCache(), } } func (db *cachingDB) InTransition() bool { - return db.started && !db.ended + return db.CurrentTransitionState != nil && db.CurrentTransitionState.started && !db.CurrentTransitionState.ended } func (db *cachingDB) Transitioned() bool { - return db.ended + return db.CurrentTransitionState != nil && db.CurrentTransitionState.ended } // Fork implements the fork @@ -227,29 +225,35 @@ func (db *cachingDB) StartVerkleTransition(originalRoot, translatedRoot common.H | | | Y \ ___/ \ ___/| |_\ ___/| |_> | Y \/ __ \| | | | | Y \/ __ \_\___ \ \ /| | | \\___ /\___ \ |____| |___| /\___ \___ |____/\___ | __/|___| (____ |___| |__| |___| (____ /_____/ \/\_/ |__|___| /_____//_____/ |__|`) - db.started = true - db.ended = false + db.CurrentTransitionState = &TransitionState{ + started: true, + // initialize so that the first storage-less accounts are processed + StorageProcessed: true, + } // db.AddTranslation(originalRoot, translatedRoot) db.baseRoot = originalRoot - // initialize so that the first storage-less accounts are processed - db.StorageProcessed[root] = true // Reinitialize values in case of a reorg - db.CurrentAccountAddress[root] = &(common.Address{}) - db.CurrentSlotHash[root] = common.Hash{} - db.CurrentPreimageOffset[root] = 0 if pragueTime != nil { chainConfig.PragueTime = pragueTime } } func (db *cachingDB) ReorgThroughVerkleTransition() { - db.ended, db.started = false, false + log.Warn("trying to reorg through the transition, which makes no sense at this point") +} + +func (db *cachingDB) InitTransitionStatus(started, ended bool) { + db.CurrentTransitionState = &TransitionState{ + ended: ended, + started: started, + // TODO add other fields when we handle mid-transition interrupts + } } func (db *cachingDB) EndVerkleTransition() { - if !db.started { - db.started = true + if !db.CurrentTransitionState.started { + db.CurrentTransitionState.started = true } fmt.Println(` @@ -259,7 +263,35 @@ func (db *cachingDB) EndVerkleTransition() { | | | Y \ ___/ \ ___/| |_\ ___/| |_> | Y \/ __ \| | | | | Y \/ __ \_\___ \ | |__/ __ \| | / /_/ \ ___// /_/ | |____| |___| /\___ \___ |____/\___ | __/|___| (____ |___| |__| |___| (____ /_____/ |____(____ |___| \____ |\___ \____ | |__|`) - db.ended = true + db.CurrentTransitionState.ended = true +} + +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 +} + +func (ts *TransitionState) Copy() *TransitionState { + ret := &TransitionState{ + started: ts.started, + ended: ts.ended, + CurrentSlotHash: ts.CurrentSlotHash, + CurrentPreimageOffset: ts.CurrentPreimageOffset, + } + + if ts.CurrentAccountAddress != nil { + ret.CurrentAccountAddress = &common.Address{} + copy(ret.CurrentAccountAddress[:], ts.CurrentAccountAddress[:]) + } + + return ret } type cachingDB struct { @@ -268,22 +300,16 @@ type cachingDB struct { codeCache *lru.SizeConstrainedCache[common.Hash, []byte] triedb *trie.Database - // Verkle specific fields + // Transition-specific fields // TODO ensure that this info is in the DB - started, ended bool - LastMerkleRoot common.Hash // root hash of the read-only base tree + LastMerkleRoot common.Hash // root hash of the read-only base tree + CurrentTransitionState *TransitionState + TransitionStatePerRoot map[common.Hash]*TransitionState addrToPoint *utils.PointCache - baseRoot common.Hash // hash of the read-only base tree - CurrentAccountAddress map[common.Hash]*common.Address // addresss of the last translated account - CurrentSlotHash map[common.Hash]common.Hash // hash of the last translated storage slot - CurrentPreimageOffset map[common.Hash]int64 // next byte to read from the preimage file + baseRoot common.Hash // hash of the read-only base tree - // 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 map[common.Hash]bool } func (db *cachingDB) openMPTTrie(root common.Hash) (Trie, error) { @@ -297,14 +323,14 @@ func (db *cachingDB) openMPTTrie(root common.Hash) (Trie, error) { func (db *cachingDB) openVKTrie(root common.Hash) (Trie, error) { payload, err := db.DiskDB().Get(trie.FlatDBVerkleNodeKeyPrefix) if err != nil { - return trie.NewVerkleTrie(verkle.New(), db.triedb, db.addrToPoint, db.ended), nil + return trie.NewVerkleTrie(verkle.New(), db.triedb, db.addrToPoint, db.CurrentTransitionState.ended), nil } r, err := verkle.ParseNode(payload, 0) if err != nil { panic(err) } - return trie.NewVerkleTrie(r, db.triedb, db.addrToPoint, db.ended), err + return trie.NewVerkleTrie(r, db.triedb, db.addrToPoint, db.CurrentTransitionState.ended), err } // OpenTrie opens the main account trie at a specific root hash. @@ -316,7 +342,7 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) { // TODO separate both cases when I can be certain that it won't // find a Verkle trie where is expects a Transitoion trie. - if db.started || db.ended { + if db.CurrentTransitionState != nil && (db.CurrentTransitionState.started || db.CurrentTransitionState.ended) { // NOTE this is a kaustinen-only change, it will break replay vkt, err := db.openVKTrie(root) if err != nil { @@ -325,7 +351,7 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) { // If the verkle conversion has ended, return a single // verkle trie. - if db.ended { + if db.CurrentTransitionState.ended { return vkt, nil } @@ -358,7 +384,7 @@ func (db *cachingDB) openStorageMPTrie(stateRoot common.Hash, address common.Add // 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) { // TODO this should only return a verkle tree - if db.ended { + if db.Transitioned() { mpt, err := db.openStorageMPTrie(types.EmptyRootHash, address, common.Hash{}, self) if err != nil { return nil, err @@ -374,7 +400,7 @@ func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Addre panic("unexpected trie type") } } - if db.started { + if db.InTransition() { mpt, err := db.openStorageMPTrie(db.LastMerkleRoot, address, root, nil) if err != nil { return nil, err @@ -463,44 +489,44 @@ func (db *cachingDB) GetTreeKeyHeader(addr []byte) *verkle.Point { return db.addrToPoint.GetTreeKeyHeader(addr) } -func (db *cachingDB) SetCurrentAccountAddress(addr common.Address, root common.Hash) { - db.CurrentAccountAddress[root] = &addr +func (db *cachingDB) SetCurrentAccountAddress(addr common.Address) { + db.CurrentTransitionState.CurrentAccountAddress = &addr } -func (db *cachingDB) GetCurrentAccountHash(root common.Hash) common.Hash { +func (db *cachingDB) GetCurrentAccountHash() common.Hash { var addrHash common.Hash - if db.CurrentAccountAddress[root] != nil { - addrHash = crypto.Keccak256Hash(db.CurrentAccountAddress[root][:]) + if db.CurrentTransitionState.CurrentAccountAddress != nil { + addrHash = crypto.Keccak256Hash(db.CurrentTransitionState.CurrentAccountAddress[:]) } return addrHash } -func (db *cachingDB) GetCurrentAccountAddress(root common.Hash) *common.Address { - return db.CurrentAccountAddress[root] +func (db *cachingDB) GetCurrentAccountAddress() *common.Address { + return db.CurrentTransitionState.CurrentAccountAddress } -func (db *cachingDB) GetCurrentPreimageOffset(root common.Hash) int64 { - return db.CurrentPreimageOffset[root] +func (db *cachingDB) GetCurrentPreimageOffset() int64 { + return db.CurrentTransitionState.CurrentPreimageOffset } -func (db *cachingDB) SetCurrentPreimageOffset(offset int64, root common.Hash) { - db.CurrentPreimageOffset[root] = offset +func (db *cachingDB) SetCurrentPreimageOffset(offset int64) { + db.CurrentTransitionState.CurrentPreimageOffset = offset } -func (db *cachingDB) SetCurrentSlotHash(hash common.Hash, root common.Hash) { - db.CurrentSlotHash[root] = hash +func (db *cachingDB) SetCurrentSlotHash(hash common.Hash) { + db.CurrentTransitionState.CurrentSlotHash = hash } -func (db *cachingDB) GetCurrentSlotHash(root common.Hash) common.Hash { - return db.CurrentSlotHash[root] +func (db *cachingDB) GetCurrentSlotHash() common.Hash { + return db.CurrentTransitionState.CurrentSlotHash } -func (db *cachingDB) SetStorageProcessed(processed bool, root common.Hash) { - db.StorageProcessed[root] = processed +func (db *cachingDB) SetStorageProcessed(processed bool) { + db.CurrentTransitionState.StorageProcessed = processed } -func (db *cachingDB) GetStorageProcessed(root common.Hash) bool { - return db.StorageProcessed[root] +func (db *cachingDB) GetStorageProcessed() bool { + return db.CurrentTransitionState.StorageProcessed } func (db *cachingDB) AddRootTranslation(originalRoot, translatedRoot common.Hash) { @@ -509,3 +535,29 @@ func (db *cachingDB) AddRootTranslation(originalRoot, translatedRoot common.Hash func (db *cachingDB) SetLastMerkleRoot(merkleRoot common.Hash) { db.LastMerkleRoot = merkleRoot } + +func (db *cachingDB) SaveTransitionState(root common.Hash) { + if db.TransitionStatePerRoot == nil { + db.TransitionStatePerRoot = make(map[common.Hash]*TransitionState) + } + + db.TransitionStatePerRoot[root] = db.CurrentTransitionState +} + +func (db *cachingDB) LoadTransitionState(root common.Hash) { + if db.TransitionStatePerRoot == nil { + db.TransitionStatePerRoot = make(map[common.Hash]*TransitionState) + } + + ts, ok := db.TransitionStatePerRoot[root] + if !ok || ts == nil { + // Start with a fresh state + ts = &TransitionState{ended: db.triedb.IsVerkle()} + } + + db.CurrentTransitionState = ts.Copy() + + if db.CurrentTransitionState != nil { + fmt.Println("address", db.CurrentTransitionState.CurrentAccountAddress) + } +} diff --git a/core/state_processor.go b/core/state_processor.go index 47261e8a2111..33640decfedb 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -17,13 +17,9 @@ package core import ( - "encoding/binary" "errors" "fmt" "math/big" - "runtime" - "sync" - "time" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus" @@ -34,10 +30,6 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" - "github.com/ethereum/go-ethereum/trie" - tutils "github.com/ethereum/go-ethereum/trie/utils" - "github.com/gballet/go-verkle" - "github.com/holiman/uint256" ) // StateProcessor is a basic Processor, which takes care of transitioning @@ -106,10 +98,6 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg } // Perform the overlay transition, if relevant - parent := p.bc.GetHeaderByHash(header.ParentHash) - if err := OverlayVerkleTransition(statedb, parent.Root); err != nil { - return nil, nil, 0, fmt.Errorf("error performing verkle overlay transition: %w", err) - } // Finalize the block, applying any consensus engine specific extras (e.g. block rewards) p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles(), withdrawals) @@ -184,179 +172,3 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo vmenv := vm.NewEVM(blockContext, vm.TxContext{BlobHashes: tx.BlobHashes()}, statedb, config, cfg) return applyTransaction(msg, config, gp, statedb, header.Number, header.Hash(), tx, usedGas, vmenv) } - -var zeroTreeIndex uint256.Int - -// keyValueMigrator is a helper module that collects key-values from the overlay-tree migration for Verkle Trees. -// It assumes that the walk of the base tree is done in address-order, so it exploit that fact to -// collect the key-values in a way that is efficient. -type keyValueMigrator struct { - // leafData contains the values for the future leaf for a particular VKT branch. - leafData []migratedKeyValue - - // When prepare() is called, it will start a background routine that will process the leafData - // saving the result in newLeaves to be used by migrateCollectedKeyValues(). The background - // routine signals that it is done by closing processingReady. - processingReady chan struct{} - newLeaves []verkle.LeafNode - prepareErr error -} - -func newKeyValueMigrator() *keyValueMigrator { - // We do initialize the VKT config since prepare() might indirectly make multiple GetConfig() calls - // in different goroutines when we never called GetConfig() before, causing a race considering the way - // that `config` is designed in go-verkle. - // TODO: jsign as a fix for this in the PR where we move to a file-less precomp, since it allows safe - // concurrent calls to GetConfig(). When that gets merged, we can remove this line. - _ = verkle.GetConfig() - return &keyValueMigrator{ - processingReady: make(chan struct{}), - leafData: make([]migratedKeyValue, 0, 10_000), - } -} - -type migratedKeyValue struct { - branchKey branchKey - leafNodeData verkle.BatchNewLeafNodeData -} -type branchKey struct { - addr common.Address - treeIndex uint256.Int -} - -func newBranchKey(addr []byte, treeIndex *uint256.Int) branchKey { - var sk branchKey - copy(sk.addr[:], addr) - sk.treeIndex = *treeIndex - return sk -} - -func (kvm *keyValueMigrator) addStorageSlot(addr []byte, slotNumber []byte, slotValue []byte) { - treeIndex, subIndex := tutils.GetTreeKeyStorageSlotTreeIndexes(slotNumber) - leafNodeData := kvm.getOrInitLeafNodeData(newBranchKey(addr, treeIndex)) - leafNodeData.Values[subIndex] = slotValue -} - -func (kvm *keyValueMigrator) addAccount(addr []byte, acc *types.StateAccount) { - leafNodeData := kvm.getOrInitLeafNodeData(newBranchKey(addr, &zeroTreeIndex)) - - var version [verkle.LeafValueSize]byte - leafNodeData.Values[tutils.VersionLeafKey] = version[:] - - var balance [verkle.LeafValueSize]byte - for i, b := range acc.Balance.Bytes() { - balance[len(acc.Balance.Bytes())-1-i] = b - } - leafNodeData.Values[tutils.BalanceLeafKey] = balance[:] - - var nonce [verkle.LeafValueSize]byte - binary.LittleEndian.PutUint64(nonce[:8], acc.Nonce) - leafNodeData.Values[tutils.NonceLeafKey] = nonce[:] - - leafNodeData.Values[tutils.CodeKeccakLeafKey] = acc.CodeHash[:] -} - -func (kvm *keyValueMigrator) addAccountCode(addr []byte, codeSize uint64, chunks []byte) { - leafNodeData := kvm.getOrInitLeafNodeData(newBranchKey(addr, &zeroTreeIndex)) - - // Save the code size. - var codeSizeBytes [verkle.LeafValueSize]byte - binary.LittleEndian.PutUint64(codeSizeBytes[:8], codeSize) - leafNodeData.Values[tutils.CodeSizeLeafKey] = codeSizeBytes[:] - - // The first 128 chunks are stored in the account header leaf. - for i := 0; i < 128 && i < len(chunks)/32; i++ { - leafNodeData.Values[byte(128+i)] = chunks[32*i : 32*(i+1)] - } - - // Potential further chunks, have their own leaf nodes. - for i := 128; i < len(chunks)/32; { - treeIndex, _ := tutils.GetTreeKeyCodeChunkIndices(uint256.NewInt(uint64(i))) - leafNodeData := kvm.getOrInitLeafNodeData(newBranchKey(addr, treeIndex)) - - j := i - for ; (j-i) < 256 && j < len(chunks)/32; j++ { - leafNodeData.Values[byte((j-128)%256)] = chunks[32*j : 32*(j+1)] - } - i = j - } -} - -func (kvm *keyValueMigrator) getOrInitLeafNodeData(bk branchKey) *verkle.BatchNewLeafNodeData { - // Remember that keyValueMigration receives actions ordered by (address, subtreeIndex). - // This means that we can assume that the last element of leafData is the one that we - // are looking for, or that we need to create a new one. - if len(kvm.leafData) == 0 || kvm.leafData[len(kvm.leafData)-1].branchKey != bk { - kvm.leafData = append(kvm.leafData, migratedKeyValue{ - branchKey: bk, - leafNodeData: verkle.BatchNewLeafNodeData{ - Stem: nil, // It will be calculated in the prepare() phase, since it's CPU heavy. - Values: make(map[byte][]byte), - }, - }) - } - return &kvm.leafData[len(kvm.leafData)-1].leafNodeData -} - -func (kvm *keyValueMigrator) prepare() { - // We fire a background routine to process the leafData and save the result in newLeaves. - // The background routine signals that it is done by closing processingReady. - go func() { - // Step 1: We split kvm.leafData in numBatches batches, and we process each batch in a separate goroutine. - // This fills each leafNodeData.Stem with the correct value. - var wg sync.WaitGroup - batchNum := runtime.NumCPU() - batchSize := (len(kvm.leafData) + batchNum - 1) / batchNum - for i := 0; i < len(kvm.leafData); i += batchSize { - start := i - end := i + batchSize - if end > len(kvm.leafData) { - end = len(kvm.leafData) - } - wg.Add(1) - - batch := kvm.leafData[start:end] - go func() { - defer wg.Done() - var currAddr common.Address - var currPoint *verkle.Point - for i := range batch { - if batch[i].branchKey.addr != currAddr || currAddr == (common.Address{}) { - currAddr = batch[i].branchKey.addr - currPoint = tutils.EvaluateAddressPoint(currAddr[:]) - } - stem := tutils.GetTreeKeyWithEvaluatedAddess(currPoint, &batch[i].branchKey.treeIndex, 0) - stem = stem[:verkle.StemSize] - batch[i].leafNodeData.Stem = stem - } - }() - } - wg.Wait() - - // Step 2: Now that we have all stems (i.e: tree keys) calculated, we can create the new leaves. - nodeValues := make([]verkle.BatchNewLeafNodeData, len(kvm.leafData)) - for i := range kvm.leafData { - nodeValues[i] = kvm.leafData[i].leafNodeData - } - - // Create all leaves in batch mode so we can optimize cryptography operations. - kvm.newLeaves, kvm.prepareErr = verkle.BatchNewLeafNode(nodeValues) - close(kvm.processingReady) - }() -} - -func (kvm *keyValueMigrator) migrateCollectedKeyValues(tree *trie.VerkleTrie) error { - now := time.Now() - <-kvm.processingReady - if kvm.prepareErr != nil { - return fmt.Errorf("failed to prepare key values: %w", kvm.prepareErr) - } - log.Info("Prepared key values from base tree", "duration", time.Since(now)) - - // Insert into the tree. - if err := tree.InsertMigratedLeaves(kvm.newLeaves); err != nil { - return fmt.Errorf("failed to insert migrated leaves: %w", err) - } - - return nil -} diff --git a/light/trie.go b/light/trie.go index 6d0c654ff111..df300c8c6ed2 100644 --- a/light/trie.go +++ b/light/trie.go @@ -121,39 +121,43 @@ func (db *odrDatabase) Transitioned() bool { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) SetCurrentSlotHash(common.Hash, common.Hash) { +func (db *odrDatabase) InitTransitionStatus(bool, bool) { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) GetCurrentAccountAddress(common.Hash) *common.Address { +func (db *odrDatabase) SetCurrentSlotHash(common.Hash) { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) SetCurrentAccountAddress(common.Address, common.Hash) { +func (db *odrDatabase) GetCurrentAccountAddress() *common.Address { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) GetCurrentAccountHash(common.Hash) common.Hash { +func (db *odrDatabase) SetCurrentAccountAddress(common.Address) { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) GetCurrentSlotHash(common.Hash) common.Hash { +func (db *odrDatabase) GetCurrentAccountHash() common.Hash { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) SetStorageProcessed(bool, common.Hash) { +func (db *odrDatabase) GetCurrentSlotHash() common.Hash { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) GetStorageProcessed(common.Hash) bool { +func (db *odrDatabase) SetStorageProcessed(bool) { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) GetCurrentPreimageOffset(common.Hash) int64 { +func (db *odrDatabase) GetStorageProcessed() bool { panic("not implemented") // TODO: Implement } -func (db *odrDatabase) SetCurrentPreimageOffset(int64, common.Hash) { +func (db *odrDatabase) GetCurrentPreimageOffset() int64 { + panic("not implemented") // TODO: Implement +} + +func (db *odrDatabase) SetCurrentPreimageOffset(int64) { panic("not implemented") // TODO: Implement } @@ -165,6 +169,14 @@ func (db *odrDatabase) SetLastMerkleRoot(common.Hash) { panic("not implemented") // TODO: Implement } +func (db *odrDatabase) SaveTransitionState(common.Hash) { + panic("not implemented") // TODO: Implement +} + +func (db *odrDatabase) LoadTransitionState(common.Hash) { + panic("not implemented") // TODO: Implement +} + type odrTrie struct { db *odrDatabase id *TrieID diff --git a/miner/worker.go b/miner/worker.go index e64fde49f08a..6eb4a4ed2a8a 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -904,9 +904,6 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) { if err != nil { return nil, err } - if w.chain.Config().IsPrague(header.Number, header.Time) { - core.OverlayVerkleTransition(state, parent.Root) - } // Run the consensus preparation with the default or customized consensus engine. if err := w.engine.Prepare(w.chain, header); err != nil { log.Error("Failed to prepare header for sealing", "err", err) From 532a3700092fb49f3b2300c5ed8e336854cade75 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Fri, 1 Dec 2023 10:00:24 +0100 Subject: [PATCH 06/21] fix: return error if witness costs OOG in instructions (#318) --- core/vm/instructions.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 84edacb1b9bc..a7860f0fde82 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -347,7 +347,10 @@ func opExtCodeSize(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) cs := uint64(interpreter.evm.StateDB.GetCodeSize(slot.Bytes20())) if interpreter.evm.chainRules.IsPrague { statelessGas := interpreter.evm.Accesses.TouchAddressOnReadAndComputeGas(slot.Bytes(), uint256.Int{}, trieUtils.CodeSizeLeafKey) - scope.Contract.UseGas(statelessGas) + if !scope.Contract.UseGas(statelessGas) { + scope.Contract.Gas = 0 + return nil, ErrOutOfGas + } } slot.SetUint64(cs) return nil, nil @@ -374,7 +377,11 @@ func opCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([ contractAddr := scope.Contract.Address() paddedCodeCopy, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(scope.Contract.Code, uint64CodeOffset, length.Uint64()) if interpreter.evm.chainRules.IsPrague { - scope.Contract.UseGas(touchCodeChunksRangeOnReadAndChargeGas(contractAddr[:], copyOffset, nonPaddedCopyLength, uint64(len(scope.Contract.Code)), interpreter.evm.Accesses)) + statelessGas := touchCodeChunksRangeOnReadAndChargeGas(contractAddr[:], copyOffset, nonPaddedCopyLength, uint64(len(scope.Contract.Code)), interpreter.evm.Accesses) + if !scope.Contract.UseGas(statelessGas) { + scope.Contract.Gas = 0 + return nil, ErrOutOfGas + } } scope.Memory.Set(memOffset.Uint64(), uint64(len(paddedCodeCopy)), paddedCodeCopy) return nil, nil @@ -433,8 +440,11 @@ func opExtCodeCopy(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) self: AccountRef(addr), } paddedCodeCopy, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(code, uint64CodeOffset, length.Uint64()) - gas := touchCodeChunksRangeOnReadAndChargeGas(addr[:], copyOffset, nonPaddedCopyLength, uint64(len(contract.Code)), interpreter.evm.Accesses) - scope.Contract.UseGas(gas) + statelessGas := touchCodeChunksRangeOnReadAndChargeGas(addr[:], copyOffset, nonPaddedCopyLength, uint64(len(contract.Code)), interpreter.evm.Accesses) + if !scope.Contract.UseGas(statelessGas) { + scope.Contract.Gas = 0 + return nil, ErrOutOfGas + } scope.Memory.Set(memOffset.Uint64(), length.Uint64(), paddedCodeCopy) } else { codeCopy := getData(interpreter.evm.StateDB.GetCode(addr), uint64CodeOffset, length.Uint64()) @@ -964,7 +974,10 @@ func opPush1(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]by // advanced past this boundary. contractAddr := scope.Contract.Address() statelessGas := touchCodeChunksRangeOnReadAndChargeGas(contractAddr[:], *pc+1, uint64(1), uint64(len(scope.Contract.Code)), interpreter.evm.Accesses) - scope.Contract.UseGas(statelessGas) + if !scope.Contract.UseGas(statelessGas) { + scope.Contract.Gas = 0 + return nil, ErrOutOfGas + } } } else { scope.Stack.push(integer.Clear()) @@ -990,7 +1003,10 @@ func makePush(size uint64, pushByteSize int) executionFunc { if interpreter.evm.chainRules.IsPrague { contractAddr := scope.Contract.Address() statelessGas := touchCodeChunksRangeOnReadAndChargeGas(contractAddr[:], uint64(startMin), uint64(pushByteSize), uint64(len(scope.Contract.Code)), interpreter.evm.Accesses) - scope.Contract.UseGas(statelessGas) + if !scope.Contract.UseGas(statelessGas) { + scope.Contract.Gas = 0 + return nil, ErrOutOfGas + } } integer := new(uint256.Int) From 48c6e62e3a6ee6a6805fa4e81af2e790dacbf77a Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 5 Dec 2023 16:53:46 +0100 Subject: [PATCH 07/21] fix OpenStorageTrie: return an error instead of panicking if the account tree not a verkle tree (#321) --- core/state/database.go | 2 +- core/state/trie_prefetcher.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/state/database.go b/core/state/database.go index 2ff85a4c4724..c7e6456af369 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -413,7 +413,7 @@ func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Addre case *trie.TransitionTrie: return trie.NewTransitionTree(mpt.(*trie.SecureTrie), self.Overlay(), true), nil default: - panic("unexpected trie type") + return nil, errors.New("expected a verkle account tree, and found another type") } } mpt, err := db.openStorageMPTrie(stateRoot, address, root, nil) diff --git a/core/state/trie_prefetcher.go b/core/state/trie_prefetcher.go index 6c5c158cc239..4521736c7beb 100644 --- a/core/state/trie_prefetcher.go +++ b/core/state/trie_prefetcher.go @@ -302,7 +302,7 @@ func (sf *subfetcher) loop() { } sf.trie = trie } else { - trie, err := sf.db.OpenStorageTrie(sf.state, sf.addr, sf.root, nil /* safe to set to nil for now, as there is no prefetcher for verkle */) + trie, err := sf.db.OpenStorageTrie(sf.state, sf.addr, sf.root, sf.trie) if err != nil { log.Warn("Trie prefetcher failed opening trie", "root", sf.root, "err", err) return From dcb0a9230382ecbd48c5a806664a82b75e18602f Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 5 Dec 2023 17:19:11 +0100 Subject: [PATCH 08/21] add a switch to force proof in blocks (#322) * add a switch to force proof in blocks * activate switch * fix switch type --- cmd/geth/config.go | 4 ++++ cmd/geth/main.go | 1 + cmd/utils/flags.go | 5 +++++ core/blockchain.go | 7 +++++-- core/genesis.go | 5 +++-- eth/backend.go | 3 +++ eth/ethconfig/config.go | 3 +++ 7 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cmd/geth/config.go b/cmd/geth/config.go index bf01c6f91857..4e861d75350b 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -175,6 +175,10 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { v := ctx.Uint64(utils.OverridePrague.Name) cfg.Eth.OverridePrague = &v } + if ctx.IsSet(utils.OverrideProofInBlock.Name) { + v := ctx.Bool(utils.OverrideProofInBlock.Name) + cfg.Eth.OverrideProofInBlock = &v + } backend, eth := utils.RegisterEthService(stack, &cfg.Eth) // Configure log filter RPC API. diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 38fb755b4b5a..2945a3c4b1f9 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -69,6 +69,7 @@ var ( utils.SmartCardDaemonPathFlag, utils.OverrideCancun, utils.OverridePrague, + utils.OverrideProofInBlock, utils.EnablePersonal, utils.TxPoolLocalsFlag, utils.TxPoolNoLocalsFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index b927d0f94f83..5f071d0a7479 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -273,6 +273,11 @@ var ( Usage: "Manually specify the Verkle fork timestamp, overriding the bundled setting", Category: flags.EthCategory, } + OverrideProofInBlock = &cli.BoolFlag{ + Name: "override.blockproof", + Usage: "Manually specify the proof-in-block setting", + Category: flags.EthCategory, + } // Light server and client settings LightServeFlag = &cli.IntFlag{ Name: "light.serve", diff --git a/core/blockchain.go b/core/blockchain.go index e39cccad5b79..462eba746938 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -250,6 +250,9 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok { return nil, genesisErr } + if overrides.OverrideProofInBlock != nil { + chainConfig.ProofInBlocks = *overrides.OverrideProofInBlock + } log.Info("") log.Info(strings.Repeat("-", 153)) for _, line := range strings.Split(chainConfig.Description(), "\n") { @@ -315,8 +318,8 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis // TODO this only works when resuming a chain that has already gone // through the conversion. All pointers should be saved to the DB // for it to be able to recover if interrupted during the transition - // but that's left out to a later PR since there's not really a need - // right now. + // but that's left out to a later PR since there's not really a need + // right now. bc.stateCache.InitTransitionStatus(true, true) bc.stateCache.EndVerkleTransition() } diff --git a/core/genesis.go b/core/genesis.go index efa339024c7e..1a63062c308a 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -291,8 +291,9 @@ func (e *GenesisMismatchError) Error() string { // ChainOverrides contains the changes to chain config. type ChainOverrides struct { - OverrideCancun *uint64 - OverridePrague *uint64 + OverrideCancun *uint64 + OverridePrague *uint64 + OverrideProofInBlock *bool } // SetupGenesisBlock writes or updates the genesis block in db. diff --git a/eth/backend.go b/eth/backend.go index a6c80159077d..c5be460c0d91 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -201,6 +201,9 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { if config.OverridePrague != nil { overrides.OverridePrague = config.OverridePrague } + if config.OverrideProofInBlock != nil { + overrides.OverrideProofInBlock = config.OverrideProofInBlock + } eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, config.Genesis, &overrides, eth.engine, vmConfig, eth.shouldPreserve, &config.TxLookupLimit) if err != nil { return nil, err diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index 4606b60408dd..b9660f5c60b3 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -158,6 +158,9 @@ type Config struct { // OverrideVerkle (TODO: remove after the fork) OverridePrague *uint64 `toml:",omitempty"` + + // OverrideProofInBlock + OverrideProofInBlock *bool `toml:",omitempty"` } // CreateConsensusEngine creates a consensus engine for the given chain config. From e842fd4cdfa81549b4efb03343534f141efcf7ee Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 5 Dec 2023 17:44:28 +0100 Subject: [PATCH 09/21] add switch to override the stride of the overlay conversion (#323) * add switch to override the stride of the overlay conversion * set a default stride of 10k --- cmd/geth/config.go | 4 ++++ cmd/geth/main.go | 1 + cmd/utils/flags.go | 6 ++++++ consensus/beacon/consensus.go | 2 +- core/blockchain.go | 3 +++ core/genesis.go | 7 ++++--- core/overlay/conversion.go | 5 ++--- eth/ethconfig/config.go | 3 +++ params/config.go | 3 ++- 9 files changed, 26 insertions(+), 8 deletions(-) diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 4e861d75350b..cdd893d2a8ec 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -179,6 +179,10 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { v := ctx.Bool(utils.OverrideProofInBlock.Name) cfg.Eth.OverrideProofInBlock = &v } + if ctx.IsSet(utils.OverrideOverlayStride.Name) { + v := ctx.Uint64(utils.OverrideOverlayStride.Name) + cfg.Eth.OverrideOverlayStride = &v + } backend, eth := utils.RegisterEthService(stack, &cfg.Eth) // Configure log filter RPC API. diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 2945a3c4b1f9..e2e4ba25fe06 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -67,6 +67,7 @@ var ( utils.NoUSBFlag, utils.USBFlag, utils.SmartCardDaemonPathFlag, + utils.OverrideOverlayStride, utils.OverrideCancun, utils.OverridePrague, utils.OverrideProofInBlock, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 5f071d0a7479..0dd311706abb 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -263,6 +263,12 @@ var ( Value: 2048, Category: flags.EthCategory, } + OverrideOverlayStride = &cli.Uint64Flag{ + Name: "override.overlay-stride", + Usage: "Manually specify the stride of the overlay transition, overriding the bundled setting", + Value: 10000, + Category: flags.EthCategory, + } OverrideCancun = &cli.Uint64Flag{ Name: "override.cancun", Usage: "Manually specify the Cancun fork timestamp, overriding the bundled setting", diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go index 6582a7bfb90b..7f22d6369a60 100644 --- a/consensus/beacon/consensus.go +++ b/consensus/beacon/consensus.go @@ -373,7 +373,7 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types. if chain.Config().IsPrague(header.Number, header.Time) { fmt.Println("at block", header.Number, "performing transition?", state.Database().InTransition()) parent := chain.GetHeaderByHash(header.ParentHash) - overlay.OverlayVerkleTransition(state, parent.Root) + overlay.OverlayVerkleTransition(state, parent.Root, chain.Config().OverlayStride) } } diff --git a/core/blockchain.go b/core/blockchain.go index 462eba746938..39485505bb05 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -253,6 +253,9 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis if overrides.OverrideProofInBlock != nil { chainConfig.ProofInBlocks = *overrides.OverrideProofInBlock } + if overrides.OverrideOverlayStride != nil { + chainConfig.OverlayStride = *overrides.OverrideOverlayStride + } log.Info("") log.Info(strings.Repeat("-", 153)) for _, line := range strings.Split(chainConfig.Description(), "\n") { diff --git a/core/genesis.go b/core/genesis.go index 1a63062c308a..9a022f64f335 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -291,9 +291,10 @@ func (e *GenesisMismatchError) Error() string { // ChainOverrides contains the changes to chain config. type ChainOverrides struct { - OverrideCancun *uint64 - OverridePrague *uint64 - OverrideProofInBlock *bool + OverrideCancun *uint64 + OverridePrague *uint64 + OverrideProofInBlock *bool + OverrideOverlayStride *uint64 } // SetupGenesisBlock writes or updates the genesis block in db. diff --git a/core/overlay/conversion.go b/core/overlay/conversion.go index e76aa5900173..0e9047920a0b 100644 --- a/core/overlay/conversion.go +++ b/core/overlay/conversion.go @@ -217,7 +217,7 @@ func (kvm *keyValueMigrator) migrateCollectedKeyValues(tree *trie.VerkleTrie) er } // OverlayVerkleTransition contains the overlay conversion logic -func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { +func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedCount uint64) error { migrdb := statedb.Database() // verkle transition: if the conversion process is in progress, move @@ -274,14 +274,13 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash) error { preimageSeek += int64(len(addr)) } - const maxMovedCount = 10000 // mkv will be assiting in the collection of up to maxMovedCount key values to be migrated to the VKT. // It has internal caches to do efficient MPT->VKT key calculations, which will be discarded after // this function. mkv := newKeyValueMigrator() // move maxCount accounts into the verkle tree, starting with the // slots from the previous account. - count := 0 + count := uint64(0) // if less than maxCount slots were moved, move to the next account for count < maxMovedCount { diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index b9660f5c60b3..fc9550147bcc 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -161,6 +161,9 @@ type Config struct { // OverrideProofInBlock OverrideProofInBlock *bool `toml:",omitempty"` + + // OverrideOverlayStride + OverrideOverlayStride *uint64 `toml:",omitempty"` } // CreateConsensusEngine creates a consensus engine for the given chain config. diff --git a/params/config.go b/params/config.go index 19e633a71def..a2df06893a22 100644 --- a/params/config.go +++ b/params/config.go @@ -301,7 +301,8 @@ type ChainConfig struct { IsDevMode bool `json:"isDev,omitempty"` // Proof in block - ProofInBlocks bool `json:"proofInBlocks,omitempty"` + ProofInBlocks bool `json:"proofInBlocks,omitempty"` + OverlayStride uint64 `json:"overlayStride,omitempty"` } // EthashConfig is the consensus engine configs for proof-of-work based sealing. From bdd68f599cadccc0214daf3a69d77abd24e365cd Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 5 Dec 2023 17:55:39 +0100 Subject: [PATCH 10/21] add a few traces for relaunch --- core/overlay/conversion.go | 1 + core/state/database.go | 1 + 2 files changed, 2 insertions(+) diff --git a/core/overlay/conversion.go b/core/overlay/conversion.go index 0e9047920a0b..1a987ab69250 100644 --- a/core/overlay/conversion.go +++ b/core/overlay/conversion.go @@ -223,6 +223,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC // verkle transition: if the conversion process is in progress, move // N values from the MPT into the verkle tree. if migrdb.InTransition() { + fmt.Printf("Processing verkle conversion starting at %x %x, building on top of %x\n", migrdb.GetCurrentAccountHash(), migrdb.GetCurrentSlotHash(), root) var ( now = time.Now() tt = statedb.GetTrie().(*trie.TransitionTrie) diff --git a/core/state/database.go b/core/state/database.go index c7e6456af369..43e0569c638b 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -401,6 +401,7 @@ func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Addre } } if db.InTransition() { + fmt.Printf("OpenStorageTrie during transition, state root=%x root=%x\n", stateRoot, root) mpt, err := db.openStorageMPTrie(db.LastMerkleRoot, address, root, nil) if err != nil { return nil, err From 2c5d36c8f7552473ef0a46c2c8e113793fdb7ba8 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Wed, 6 Dec 2023 11:42:53 +0100 Subject: [PATCH 11/21] add missing step in the chain of setting the override for overlay stride --- eth/backend.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/eth/backend.go b/eth/backend.go index c5be460c0d91..c47bc6b5bb35 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -204,6 +204,9 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { if config.OverrideProofInBlock != nil { overrides.OverrideProofInBlock = config.OverrideProofInBlock } + if config.OverrideOverlayStride != nil { + overrides.OverrideOverlayStride = config.OverrideOverlayStride + } eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, config.Genesis, &overrides, eth.engine, vmConfig, eth.shouldPreserve, &config.TxLookupLimit) if err != nil { return nil, err From 1e2d2ade8420d126c8f726a418b962a4c0a8bf24 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Fri, 8 Dec 2023 15:02:38 +0100 Subject: [PATCH 12/21] fix: save and load transition state for block processing (#324) * fix: save and load transition state for block processing * log whether the tree is verkle in LoadTransitionState * fix: ensure the transition is marked as started in insertChain * dump saved address * fix nil pointer panic * remove stacktrace that is no longer useful * fix a panic * fix build * check: copy current account address BEFORE it's saved * mandatory panic fix * Remove debug fmt.Println * more cleanup + comments --- core/block_validator.go | 1 + core/blockchain.go | 9 +++++++++ core/state/database.go | 14 ++++++++------ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/core/block_validator.go b/core/block_validator.go index b1ceab9d5c6c..d977c1d63d96 100644 --- a/core/block_validator.go +++ b/core/block_validator.go @@ -131,6 +131,7 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root { return fmt.Errorf("invalid merkle root (remote: %x local: %x) dberr: %w", header.Root, root, statedb.Error()) } + statedb.Database().SaveTransitionState(header.Root) return nil } diff --git a/core/blockchain.go b/core/blockchain.go index 39485505bb05..ec12f57fc1ee 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1757,6 +1757,15 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1) } + if bc.Config().IsPrague(block.Number(), block.Time()) { + bc.stateCache.LoadTransitionState(parent.Root) + + // pragueTime has been reached. If the transition isn't active, it means this + // is the fork block and that the conversion needs to be marked at started. + if !bc.stateCache.InTransition() && !bc.stateCache.Transitioned() { + bc.stateCache.StartVerkleTransition(parent.Root, emptyVerkleRoot, bc.Config(), bc.Config().PragueTime, parent.Root) + } + } if parent.Number.Uint64() == conversionBlock { bc.StartVerkleTransition(parent.Root, emptyVerkleRoot, bc.Config(), &parent.Time, parent.Root) bc.stateCache.SetLastMerkleRoot(parent.Root) diff --git a/core/state/database.go b/core/state/database.go index 43e0569c638b..21280359794e 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -342,7 +342,7 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) { // TODO separate both cases when I can be certain that it won't // find a Verkle trie where is expects a Transitoion trie. - if db.CurrentTransitionState != nil && (db.CurrentTransitionState.started || db.CurrentTransitionState.ended) { + if db.InTransition() || db.Transitioned() { // NOTE this is a kaustinen-only change, it will break replay vkt, err := db.openVKTrie(root) if err != nil { @@ -542,7 +542,11 @@ func (db *cachingDB) SaveTransitionState(root common.Hash) { db.TransitionStatePerRoot = make(map[common.Hash]*TransitionState) } - db.TransitionStatePerRoot[root] = db.CurrentTransitionState + if db.CurrentTransitionState != nil { + // Copy so that the address pointer isn't updated after + // it has been saved. + db.TransitionStatePerRoot[root] = db.CurrentTransitionState.Copy() + } } func (db *cachingDB) LoadTransitionState(root common.Hash) { @@ -556,9 +560,7 @@ func (db *cachingDB) LoadTransitionState(root common.Hash) { ts = &TransitionState{ended: db.triedb.IsVerkle()} } + // Copy so that the CurrentAddress pointer in the map + // doesn't get overwritten. db.CurrentTransitionState = ts.Copy() - - if db.CurrentTransitionState != nil { - fmt.Println("address", db.CurrentTransitionState.CurrentAccountAddress) - } } From f68cc4ec65994df9178cab382b14443fd67d908d Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Sun, 10 Dec 2023 20:34:08 +0100 Subject: [PATCH 13/21] fix: ensure StorageProcessed is properly recovered --- core/overlay/conversion.go | 2 +- core/state/database.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/core/overlay/conversion.go b/core/overlay/conversion.go index 1a987ab69250..929357203380 100644 --- a/core/overlay/conversion.go +++ b/core/overlay/conversion.go @@ -405,7 +405,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC } migrdb.SetCurrentPreimageOffset(preimageSeek) - log.Info("Collected key values from base tree", "count", count, "duration", time.Since(now), "last account", statedb.Database().GetCurrentAccountHash()) + log.Info("Collected key values from base tree", "count", count, "duration", time.Since(now), "last account", statedb.Database().GetCurrentAccountHash(), "storage processed", statedb.Database().GetStorageProcessed(), "last storage", statedb.Database().GetCurrentSlotHash()) // Take all the collected key-values and prepare the new leaf values. // This fires a background routine that will start doing the work that diff --git a/core/state/database.go b/core/state/database.go index 21280359794e..181173bdf5d9 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -284,6 +284,7 @@ func (ts *TransitionState) Copy() *TransitionState { ended: ts.ended, CurrentSlotHash: ts.CurrentSlotHash, CurrentPreimageOffset: ts.CurrentPreimageOffset, + StorageProcessed: ts.StorageProcessed, } if ts.CurrentAccountAddress != nil { @@ -563,4 +564,6 @@ func (db *cachingDB) LoadTransitionState(root common.Hash) { // Copy so that the CurrentAddress pointer in the map // doesn't get overwritten. db.CurrentTransitionState = ts.Copy() + + fmt.Println("loaded transition state", db.CurrentTransitionState.StorageProcessed) } From 3c84e23bfa0bf8c5951df6b94f423ca717a2b3ff Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Mon, 11 Dec 2023 16:26:20 +0100 Subject: [PATCH 14/21] add traces for gas pool overflow --- core/state_transition.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/state_transition.go b/core/state_transition.go index 2bdfef1c0552..6a04a9fa7fb9 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -497,6 +497,10 @@ func (st *StateTransition) refundGas(refundQuotient uint64) { remaining := new(big.Int).Mul(new(big.Int).SetUint64(st.gasRemaining), st.msg.GasPrice) st.state.AddBalance(st.msg.From, remaining) + if st.gp.Gas() > math.MaxUint64-st.gasRemaining { + fmt.Println(st.gp.Gas(), refund, refundQuotient, st.gasUsed(), st.initialGas, st.gasUsed(), st.evm.Accesses, st.msg) + } + // Also return remaining gas to the block gas counter so it is // available for the next transaction. st.gp.AddGas(st.gasRemaining) From 95ab71640b48a86d9d67a1de99d60d78630f2383 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Fri, 26 Jan 2024 17:57:55 +0100 Subject: [PATCH 15/21] cmd/{geth, utils}: add a command to clear verkle costs (#326) * cmd/{geth, utils}: add a command to clear verkle costs fix: boolean issue fix: load finalization state in FinalizeAndAssemble (#340) * Conversion and TransitionTrie fixes (#346) * fixes Signed-off-by: Ignacio Hagopian * remove old comment Signed-off-by: Ignacio Hagopian --------- Signed-off-by: Ignacio Hagopian * trace cleanup --------- Signed-off-by: Ignacio Hagopian Co-authored-by: Ignacio Hagopian --- cmd/geth/config.go | 3 ++ cmd/geth/main.go | 1 + cmd/utils/flags.go | 5 ++ consensus/beacon/consensus.go | 86 +++++++++++++++++++---------------- core/blockchain.go | 7 +-- core/overlay/conversion.go | 47 +++++++++++-------- core/state/database.go | 11 ++++- core/state/statedb.go | 2 +- miner/worker.go | 2 +- trie/transition.go | 8 +++- 10 files changed, 107 insertions(+), 65 deletions(-) diff --git a/cmd/geth/config.go b/cmd/geth/config.go index cdd893d2a8ec..4184290e86c6 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -183,6 +183,9 @@ func makeFullNode(ctx *cli.Context) (*node.Node, ethapi.Backend) { v := ctx.Uint64(utils.OverrideOverlayStride.Name) cfg.Eth.OverrideOverlayStride = &v } + if ctx.IsSet(utils.ClearVerkleCosts.Name) { + params.ClearVerkleWitnessCosts() + } backend, eth := utils.RegisterEthService(stack, &cfg.Eth) // Configure log filter RPC API. diff --git a/cmd/geth/main.go b/cmd/geth/main.go index e2e4ba25fe06..5cb1580df3d1 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -71,6 +71,7 @@ var ( utils.OverrideCancun, utils.OverridePrague, utils.OverrideProofInBlock, + utils.ClearVerkleCosts, utils.EnablePersonal, utils.TxPoolLocalsFlag, utils.TxPoolNoLocalsFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 0dd311706abb..3395eebca866 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -284,6 +284,11 @@ var ( Usage: "Manually specify the proof-in-block setting", Category: flags.EthCategory, } + ClearVerkleCosts = &cli.BoolFlag{ + Name: "clear.verkle.costs", + Usage: "Clear verkle costs (for shadow forks)", + Category: flags.EthCategory, + } // Light server and client settings LightServeFlag = &cli.IntFlag{ Name: "light.serve", diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go index 7f22d6369a60..0faecd2e7073 100644 --- a/consensus/beacon/consensus.go +++ b/consensus/beacon/consensus.go @@ -28,6 +28,7 @@ import ( "github.com/ethereum/go-ethereum/core/overlay" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/trie" @@ -373,7 +374,9 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types. if chain.Config().IsPrague(header.Number, header.Time) { fmt.Println("at block", header.Number, "performing transition?", state.Database().InTransition()) parent := chain.GetHeaderByHash(header.ParentHash) - overlay.OverlayVerkleTransition(state, parent.Root, chain.Config().OverlayStride) + if err := overlay.OverlayVerkleTransition(state, parent.Root, chain.Config().OverlayStride); err != nil { + log.Error("error performing the transition", "err", err) + } } } @@ -406,7 +409,7 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea k verkle.StateDiff keys = state.Witness().Keys() ) - if chain.Config().IsPrague(header.Number, header.Time) && chain.Config().ProofInBlocks { + if chain.Config().IsPrague(header.Number, header.Time) { // Open the pre-tree to prove the pre-state against parent := chain.GetHeaderByNumber(header.Number.Uint64() - 1) if parent == nil { @@ -414,50 +417,53 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea } state.Database().LoadTransitionState(parent.Root) - preTrie, err := state.Database().OpenTrie(parent.Root) - if err != nil { - return nil, fmt.Errorf("error opening pre-state tree root: %w", err) - } - var okpre, okpost bool - var vtrpre, vtrpost *trie.VerkleTrie - switch pre := preTrie.(type) { - case *trie.VerkleTrie: - vtrpre, okpre = preTrie.(*trie.VerkleTrie) - switch tr := state.GetTrie().(type) { + if chain.Config().ProofInBlocks { + preTrie, err := state.Database().OpenTrie(parent.Root) + if err != nil { + return nil, fmt.Errorf("error opening pre-state tree root: %w", err) + } + + var okpre, okpost bool + var vtrpre, vtrpost *trie.VerkleTrie + switch pre := preTrie.(type) { case *trie.VerkleTrie: - vtrpost = tr - okpost = true - // This is to handle a situation right at the start of the conversion: - // the post trie is a transition tree when the pre tree is an empty - // verkle tree. + vtrpre, okpre = preTrie.(*trie.VerkleTrie) + switch tr := state.GetTrie().(type) { + case *trie.VerkleTrie: + vtrpost = tr + okpost = true + // This is to handle a situation right at the start of the conversion: + // the post trie is a transition tree when the pre tree is an empty + // verkle tree. + case *trie.TransitionTrie: + vtrpost = tr.Overlay() + okpost = true + default: + okpost = false + } case *trie.TransitionTrie: - vtrpost = tr.Overlay() + vtrpre = pre.Overlay() + okpre = true + post, _ := state.GetTrie().(*trie.TransitionTrie) + vtrpost = post.Overlay() okpost = true default: - okpost = false + // This should only happen for the first block, + // so the previous tree is a merkle tree. Logically, + // the "previous" verkle tree is an empty tree. + okpre = true + vtrpre = trie.NewVerkleTrie(verkle.New(), state.Database().TrieDB(), utils.NewPointCache(), false) + post := state.GetTrie().(*trie.TransitionTrie) + vtrpost = post.Overlay() + okpost = true } - case *trie.TransitionTrie: - vtrpre = pre.Overlay() - okpre = true - post, _ := state.GetTrie().(*trie.TransitionTrie) - vtrpost = post.Overlay() - okpost = true - default: - // This should only happen for the first block, - // so the previous tree is a merkle tree. Logically, - // the "previous" verkle tree is an empty tree. - okpre = true - vtrpre = trie.NewVerkleTrie(verkle.New(), state.Database().TrieDB(), utils.NewPointCache(), false) - post := state.GetTrie().(*trie.TransitionTrie) - vtrpost = post.Overlay() - okpost = true - } - if okpre && okpost { - if len(keys) > 0 { - p, k, err = trie.ProveAndSerialize(vtrpre, vtrpost, keys, vtrpre.FlatdbNodeResolver) - if err != nil { - return nil, fmt.Errorf("error generating verkle proof for block %d: %w", header.Number, err) + if okpre && okpost { + if len(keys) > 0 { + p, k, err = trie.ProveAndSerialize(vtrpre, vtrpost, keys, vtrpre.FlatdbNodeResolver) + if err != nil { + return nil, fmt.Errorf("error generating verkle proof for block %d: %w", header.Number, err) + } } } } diff --git a/core/blockchain.go b/core/blockchain.go index ec12f57fc1ee..d99437ba9bee 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -103,7 +103,7 @@ const ( txLookupCacheLimit = 1024 maxFutureBlocks = 256 maxTimeFutureBlocks = 30 - TriesInMemory = 128 + TriesInMemory = 8192 // BlockChainVersion ensures that an incompatible database forces a resync from scratch. // @@ -2009,7 +2009,7 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (i parent = bc.GetHeader(parent.ParentHash, parent.Number.Uint64()-1) } if parent == nil { - return it.index, errors.New("missing parent") + return it.index, fmt.Errorf("missing parent: hash=%x, number=%d", current.Hash(), current.Number) } // Import all the pruned blocks to make the state available var ( @@ -2070,7 +2070,7 @@ func (bc *BlockChain) recoverAncestors(block *types.Block) (common.Hash, error) } } if parent == nil { - return common.Hash{}, errors.New("missing parent") + return common.Hash{}, fmt.Errorf("missing parent during ancestor recovery: hash=%x, number=%d", block.ParentHash(), block.Number()) } // Import all the pruned blocks to make the state available for i := len(hashes) - 1; i >= 0; i-- { @@ -2308,6 +2308,7 @@ func (bc *BlockChain) SetCanonical(head *types.Block) (common.Hash, error) { defer bc.chainmu.Unlock() // Re-execute the reorged chain in case the head state is missing. + log.Trace("looking for state", "root", head.Root(), "has state", bc.HasState(head.Root())) if !bc.HasState(head.Root()) { if latestValidHash, err := bc.recoverAncestors(head); err != nil { return latestValidHash, err diff --git a/core/overlay/conversion.go b/core/overlay/conversion.go index 929357203380..e538c58bcce0 100644 --- a/core/overlay/conversion.go +++ b/core/overlay/conversion.go @@ -47,7 +47,7 @@ var zeroTreeIndex uint256.Int // collect the key-values in a way that is efficient. type keyValueMigrator struct { // leafData contains the values for the future leaf for a particular VKT branch. - leafData []migratedKeyValue + leafData map[branchKey]*migratedKeyValue // When prepare() is called, it will start a background routine that will process the leafData // saving the result in newLeaves to be used by migrateCollectedKeyValues(). The background @@ -66,7 +66,7 @@ func newKeyValueMigrator() *keyValueMigrator { _ = verkle.GetConfig() return &keyValueMigrator{ processingReady: make(chan struct{}), - leafData: make([]migratedKeyValue, 0, 10_000), + leafData: make(map[branchKey]*migratedKeyValue, 10_000), } } @@ -138,19 +138,17 @@ func (kvm *keyValueMigrator) addAccountCode(addr []byte, codeSize uint64, chunks } func (kvm *keyValueMigrator) getOrInitLeafNodeData(bk branchKey) *verkle.BatchNewLeafNodeData { - // Remember that keyValueMigration receives actions ordered by (address, subtreeIndex). - // This means that we can assume that the last element of leafData is the one that we - // are looking for, or that we need to create a new one. - if len(kvm.leafData) == 0 || kvm.leafData[len(kvm.leafData)-1].branchKey != bk { - kvm.leafData = append(kvm.leafData, migratedKeyValue{ - branchKey: bk, - leafNodeData: verkle.BatchNewLeafNodeData{ - Stem: nil, // It will be calculated in the prepare() phase, since it's CPU heavy. - Values: make(map[byte][]byte), - }, - }) + if ld, ok := kvm.leafData[bk]; ok { + return &ld.leafNodeData } - return &kvm.leafData[len(kvm.leafData)-1].leafNodeData + kvm.leafData[bk] = &migratedKeyValue{ + branchKey: bk, + leafNodeData: verkle.BatchNewLeafNodeData{ + Stem: nil, // It will be calculated in the prepare() phase, since it's CPU heavy. + Values: make(map[byte][]byte, 256), + }, + } + return &kvm.leafData[bk].leafNodeData } func (kvm *keyValueMigrator) prepare() { @@ -159,6 +157,10 @@ func (kvm *keyValueMigrator) prepare() { go func() { // Step 1: We split kvm.leafData in numBatches batches, and we process each batch in a separate goroutine. // This fills each leafNodeData.Stem with the correct value. + leafData := make([]migratedKeyValue, 0, len(kvm.leafData)) + for _, v := range kvm.leafData { + leafData = append(leafData, *v) + } var wg sync.WaitGroup batchNum := runtime.NumCPU() batchSize := (len(kvm.leafData) + batchNum - 1) / batchNum @@ -170,7 +172,7 @@ func (kvm *keyValueMigrator) prepare() { } wg.Add(1) - batch := kvm.leafData[start:end] + batch := leafData[start:end] go func() { defer wg.Done() var currAddr common.Address @@ -190,8 +192,8 @@ func (kvm *keyValueMigrator) prepare() { // Step 2: Now that we have all stems (i.e: tree keys) calculated, we can create the new leaves. nodeValues := make([]verkle.BatchNewLeafNodeData, len(kvm.leafData)) - for i := range kvm.leafData { - nodeValues[i] = kvm.leafData[i].leafNodeData + for i := range leafData { + nodeValues[i] = leafData[i].leafNodeData } // Create all leaves in batch mode so we can optimize cryptography operations. @@ -306,7 +308,12 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC if err != nil { return err } - stIt.Next() + processed := stIt.Next() + if processed { + log.Debug("account has storage and a next item") + } else { + log.Debug("account has storage and NO next item") + } // fdb.StorageProcessed will be initialized to `true` if the // entire storage for an account was not entirely processed @@ -315,6 +322,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC // If the entire storage was processed, then the iterator was // created in vain, but it's ok as this will not happen often. for ; !migrdb.GetStorageProcessed() && count < maxMovedCount; count++ { + log.Trace("Processing storage", "count", count, "slot", stIt.Slot(), "storage processed", migrdb.GetStorageProcessed(), "current account", migrdb.GetCurrentAccountAddress(), "current account hash", migrdb.GetCurrentAccountHash()) var ( value []byte // slot value after RLP decoding safeValue [32]byte // 32-byte aligned value @@ -337,6 +345,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC return fmt.Errorf("slotnr len is zero is not 32: %d", len(slotnr)) } } + log.Trace("found slot number", "number", slotnr) if crypto.Keccak256Hash(slotnr[:]) != stIt.Hash() { return fmt.Errorf("preimage file does not match storage hash: %s!=%s", crypto.Keccak256Hash(slotnr), stIt.Hash()) } @@ -378,6 +387,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC // Move to the next account, if available - or end // the transition otherwise. if accIt.Next() { + log.Trace("Found another account to convert", "hash", accIt.Hash()) var addr common.Address if hasPreimagesBin { if _, err := io.ReadFull(fpreimages, addr[:]); err != nil { @@ -393,6 +403,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC if crypto.Keccak256Hash(addr[:]) != accIt.Hash() { return fmt.Errorf("preimage file does not match account hash: %s != %s", crypto.Keccak256Hash(addr[:]), accIt.Hash()) } + log.Trace("Converting account address", "hash", accIt.Hash(), "addr", addr) preimageSeek += int64(len(addr)) migrdb.SetCurrentAccountAddress(addr) } else { diff --git a/core/state/database.go b/core/state/database.go index 181173bdf5d9..588dbbb3d403 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -340,6 +340,7 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) { mpt Trie err error ) + fmt.Printf("opening trie with root %x, %v %v\n", root, db.InTransition(), db.Transitioned()) // TODO separate both cases when I can be certain that it won't // find a Verkle trie where is expects a Transitoion trie. @@ -347,12 +348,14 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) { // NOTE this is a kaustinen-only change, it will break replay vkt, err := db.openVKTrie(root) if err != nil { + log.Error("failed to open the vkt", "err", err) return nil, err } // If the verkle conversion has ended, return a single // verkle trie. if db.CurrentTransitionState.ended { + log.Debug("transition ended, returning a simple verkle tree") return vkt, nil } @@ -360,6 +363,7 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) { // trie and an overlay, verkle trie. mpt, err = db.openMPTTrie(db.baseRoot) if err != nil { + log.Error("failed to open the mpt", "err", err, "root", db.baseRoot) return nil, err } @@ -547,6 +551,8 @@ func (db *cachingDB) SaveTransitionState(root common.Hash) { // Copy so that the address pointer isn't updated after // it has been saved. db.TransitionStatePerRoot[root] = db.CurrentTransitionState.Copy() + + log.Debug("saving transition state", "storage processed", db.CurrentTransitionState.StorageProcessed, "addr", db.CurrentTransitionState.CurrentAccountAddress, "slot hash", db.CurrentTransitionState.CurrentSlotHash, "root", root, "ended", db.CurrentTransitionState.ended, "started", db.CurrentTransitionState.started) } } @@ -555,6 +561,9 @@ func (db *cachingDB) LoadTransitionState(root common.Hash) { db.TransitionStatePerRoot = make(map[common.Hash]*TransitionState) } + // Initialize the first transition state, with the "ended" + // field set to true if the database was created + // as a verkle database. ts, ok := db.TransitionStatePerRoot[root] if !ok || ts == nil { // Start with a fresh state @@ -565,5 +574,5 @@ func (db *cachingDB) LoadTransitionState(root common.Hash) { // doesn't get overwritten. db.CurrentTransitionState = ts.Copy() - fmt.Println("loaded transition state", db.CurrentTransitionState.StorageProcessed) + log.Debug("loaded transition state", "storage processed", db.CurrentTransitionState.StorageProcessed, "addr", db.CurrentTransitionState.CurrentAccountAddress, "slot hash", db.CurrentTransitionState.CurrentSlotHash, "root", root, "ended", db.CurrentTransitionState.ended, "started", db.CurrentTransitionState.started) } diff --git a/core/state/statedb.go b/core/state/statedb.go index 49fa246593ae..bbf05bfbb3fb 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1310,7 +1310,7 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool) (common.Hash, er // - head layer is paired with HEAD state // - head-1 layer is paired with HEAD-1 state // - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state - if err := s.snaps.Cap(root, 128); err != nil { + if err := s.snaps.Cap(root, 8192); err != nil { log.Warn("Failed to cap snapshot tree", "root", root, "layers", 128, "err", err) } } diff --git a/miner/worker.go b/miner/worker.go index 6eb4a4ed2a8a..621c38cb3d9c 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -852,7 +852,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) { if genParams.parentHash != (common.Hash{}) { block := w.chain.GetBlockByHash(genParams.parentHash) if block == nil { - return nil, fmt.Errorf("missing parent") + return nil, fmt.Errorf("missing parent: %x", genParams.parentHash) } parent = block.Header() } diff --git a/trie/transition.go b/trie/transition.go index 083aa57db1ac..1cfbbcf8f47e 100644 --- a/trie/transition.go +++ b/trie/transition.go @@ -17,6 +17,8 @@ package trie import ( + "fmt" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" @@ -62,7 +64,11 @@ func (t *TransitionTrie) GetKey(key []byte) []byte { // not be modified by the caller. If a node was not found in the database, a // trie.MissingNodeError is returned. func (t *TransitionTrie) GetStorage(addr common.Address, key []byte) ([]byte, error) { - if val, err := t.overlay.GetStorage(addr, key); len(val) != 0 || err != nil { + val, err := t.overlay.GetStorage(addr, key) + if err != nil { + return nil, fmt.Errorf("get storage from overlay: %s", err) + } + if len(val) != 0 { return val, nil } // TODO also insert value into overlay From 9813f39fce61f944d04feef2becc49469ed2bc2c Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Tue, 23 Jan 2024 19:07:19 -0300 Subject: [PATCH 16/21] replay changes Signed-off-by: Ignacio Hagopian --- cmd/geth/conversion.txt | 1 + cmd/utils/cmd.go | 4 ++++ cmd/utils/flags.go | 23 ++++++++++++++++++++++- core/block_validator.go | 34 +++++++++++++++++----------------- core/state_processor.go | 10 +++++++++- core/vm/evm.go | 1 - core/vm/interpreter.go | 3 ++- params/config.go | 3 ++- 8 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 cmd/geth/conversion.txt diff --git a/cmd/geth/conversion.txt b/cmd/geth/conversion.txt new file mode 100644 index 000000000000..ec044355baed --- /dev/null +++ b/cmd/geth/conversion.txt @@ -0,0 +1 @@ +4702177 diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index de25fd1a146d..9e8f5ffaa5f4 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -170,6 +170,10 @@ func ImportChain(chain *core.BlockChain, fn string) error { } defer fh.Close() + if _, err := fh.Seek(18224628422, 0); err != nil { + panic(err) + } + var reader io.Reader = fh if strings.HasSuffix(fn, ".gz") { if reader, err = gzip.NewReader(reader); err != nil { diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 3395eebca866..03dd05546ba5 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -2156,8 +2156,29 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockCh } vmcfg := vm.Config{EnablePreimageRecording: ctx.Bool(VMEnableDebugFlag.Name)} + // Override the chain config with provided settings. + var overrides core.ChainOverrides + if ctx.IsSet(OverrideCancun.Name) { + v := ctx.Uint64(OverrideCancun.Name) + overrides.OverrideCancun = &v + } + if ctx.IsSet(OverridePrague.Name) { + v := ctx.Uint64(OverridePrague.Name) + overrides.OverridePrague = &v + } + if ctx.IsSet(OverrideProofInBlock.Name) { + v := ctx.Bool(OverrideProofInBlock.Name) + overrides.OverrideProofInBlock = &v + } + if ctx.IsSet(OverrideOverlayStride.Name) { + v := ctx.Uint64(OverrideOverlayStride.Name) + overrides.OverrideOverlayStride = &v + } + if ctx.IsSet(ClearVerkleCosts.Name) { + params.ClearVerkleWitnessCosts() + } // Disable transaction indexing/unindexing by default. - chain, err := core.NewBlockChain(chainDb, cache, gspec, nil, engine, vmcfg, nil, nil) + chain, err := core.NewBlockChain(chainDb, cache, gspec, &overrides, engine, vmcfg, nil, nil) if err != nil { Fatalf("Can't create BlockChain: %v", err) } diff --git a/core/block_validator.go b/core/block_validator.go index d977c1d63d96..d84363e926ea 100644 --- a/core/block_validator.go +++ b/core/block_validator.go @@ -98,13 +98,13 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error { return errors.New("data blobs present in block body") } } - if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) { - if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) { - return consensus.ErrUnknownAncestor - } - fmt.Println("failure here") - return consensus.ErrPrunedAncestor - } + // if !v.bc.HasBlockAndState(block.ParentHash(), block.NumberU64()-1) { + // if !v.bc.HasBlock(block.ParentHash(), block.NumberU64()-1) { + // return consensus.ErrUnknownAncestor + // } + // fmt.Println("failure here") + // return consensus.ErrPrunedAncestor + // } return nil } @@ -121,16 +121,16 @@ func (v *BlockValidator) ValidateState(block *types.Block, statedb *state.StateD if rbloom != header.Bloom { return fmt.Errorf("invalid bloom (remote: %x local: %x)", header.Bloom, rbloom) } - // Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, Rn]])) - receiptSha := types.DeriveSha(receipts, trie.NewStackTrie(nil)) - if receiptSha != header.ReceiptHash { - return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha) - } - // Validate the state root against the received state root and throw - // an error if they don't match. - if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root { - return fmt.Errorf("invalid merkle root (remote: %x local: %x) dberr: %w", header.Root, root, statedb.Error()) - } + // // Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, Rn]])) + // receiptSha := types.DeriveSha(receipts, trie.NewStackTrie(nil)) + // if receiptSha != header.ReceiptHash { + // return fmt.Errorf("invalid receipt root hash (remote: %x local: %x)", header.ReceiptHash, receiptSha) + // } + // // Validate the state root against the received state root and throw + // // an error if they don't match. + // if root := statedb.IntermediateRoot(v.config.IsEIP158(header.Number)); header.Root != root { + // return fmt.Errorf("invalid merkle root (remote: %x local: %x) dberr: %w", header.Root, root, statedb.Error()) + // } statedb.Database().SaveTransitionState(header.Root) return nil } diff --git a/core/state_processor.go b/core/state_processor.go index 33640decfedb..637e29d0aa7b 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/misc" + "github.com/ethereum/go-ethereum/core/overlay" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" @@ -97,7 +98,14 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg return nil, nil, 0, errors.New("withdrawals before shanghai") } - // Perform the overlay transition, if relevant + state, err := p.bc.State() + if err != nil { + return nil, nil, 0, fmt.Errorf("get statedb: %s", err) + } + parent := p.bc.GetHeaderByHash(header.ParentHash) + if err := overlay.OverlayVerkleTransition(state, parent.Root, p.config.OverlayStride); err != nil { + log.Error("error performing the transition", "err", err) + } // Finalize the block, applying any consensus engine specific extras (e.g. block rewards) p.engine.Finalize(p.bc, header, statedb, block.Transactions(), block.Uncles(), withdrawals) diff --git a/core/vm/evm.go b/core/vm/evm.go index a04258b0df22..d066ed6dbe00 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -248,7 +248,6 @@ func (evm *EVM) Call(caller ContractRef, addr common.Address, input []byte, gas }(gas) } } - if isPrecompile { ret, gas, err = RunPrecompiledContract(p, input, gas) } else { diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index 1bc0e80dfc44..02829a662edf 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -58,7 +58,8 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter { switch { case evm.chainRules.IsPrague: // TODO replace with prooper instruction set when fork is specified - table = &shanghaiInstructionSet + // table = &shanghaiInstructionSet + table = &byzantiumInstructionSet case evm.chainRules.IsCancun: table = &cancunInstructionSet case evm.chainRules.IsShanghai: diff --git a/params/config.go b/params/config.go index a2df06893a22..866830ec113e 100644 --- a/params/config.go +++ b/params/config.go @@ -505,7 +505,8 @@ func (c *ChainConfig) IsCancun(num *big.Int, time uint64) bool { // IsPrague returns whether num is either equal to the Prague fork time or greater. func (c *ChainConfig) IsPrague(num *big.Int, time uint64) bool { - return c.IsLondon(num) && isTimestampForked(c.PragueTime, time) + return num.Uint64() >= 4702177 + // return c.IsLondon(num) && isTimestampForked(c.PragueTime, time) } // CheckCompatible checks whether scheduled fork transitions have been imported From 0db0f60403867794cf12c1933d99849b361eec83 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Wed, 24 Jan 2024 10:25:22 -0300 Subject: [PATCH 17/21] use timesetamp for forking and simplify api Signed-off-by: Ignacio Hagopian --- cmd/geth/conversion.txt | 1 - core/blockchain.go | 44 ++++------------------------------------- core/chain_makers.go | 2 +- core/genesis.go | 4 ++-- core/state/database.go | 8 ++++---- eth/catalyst/api.go | 2 +- light/trie.go | 2 +- miner/worker.go | 2 +- params/config.go | 3 +-- 9 files changed, 15 insertions(+), 53 deletions(-) delete mode 100644 cmd/geth/conversion.txt diff --git a/cmd/geth/conversion.txt b/cmd/geth/conversion.txt deleted file mode 100644 index ec044355baed..000000000000 --- a/cmd/geth/conversion.txt +++ /dev/null @@ -1 +0,0 @@ -4702177 diff --git a/core/blockchain.go b/core/blockchain.go index d99437ba9bee..8ec1482c2415 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -18,15 +18,11 @@ package core import ( - "bufio" "errors" "fmt" "io" - "math" "math/big" - "os" "runtime" - "strconv" "strings" "sync" "sync/atomic" @@ -1532,30 +1528,6 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) { return bc.insertChain(chain, true) } -func findVerkleConversionBlock() (uint64, error) { - if _, err := os.Stat("conversion.txt"); os.IsNotExist(err) { - return math.MaxUint64, nil - } - - f, err := os.Open("conversion.txt") - if err != nil { - log.Error("Failed to open conversion.txt", "err", err) - return 0, err - } - defer f.Close() - - scanner := bufio.NewScanner(f) - scanner.Scan() - conversionBlock, err := strconv.ParseUint(scanner.Text(), 10, 64) - if err != nil { - log.Error("Failed to parse conversionBlock", "err", err) - return 0, err - } - log.Info("Found conversion block info", "conversionBlock", conversionBlock) - - return conversionBlock, nil -} - // insertChain is the internal implementation of InsertChain, which assumes that // 1) chains are contiguous, and 2) The chain mutex is held. // @@ -1570,11 +1542,6 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) return 0, nil } - conversionBlock, err := findVerkleConversionBlock() - if err != nil { - return 0, err - } - // Start a parallel signature recovery (signer will fluke on fork transition, minimal perf loss) SenderCacher.RecoverFromBlocks(types.MakeSigner(bc.chainConfig, chain[0].Number(), chain[0].Time()), chain) @@ -1763,13 +1730,10 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) // pragueTime has been reached. If the transition isn't active, it means this // is the fork block and that the conversion needs to be marked at started. if !bc.stateCache.InTransition() && !bc.stateCache.Transitioned() { - bc.stateCache.StartVerkleTransition(parent.Root, emptyVerkleRoot, bc.Config(), bc.Config().PragueTime, parent.Root) + bc.stateCache.StartVerkleTransition(parent.Root, emptyVerkleRoot, bc.Config(), bc.Config().PragueTime) + bc.stateCache.SetLastMerkleRoot(parent.Root) } } - if parent.Number.Uint64() == conversionBlock { - bc.StartVerkleTransition(parent.Root, emptyVerkleRoot, bc.Config(), &parent.Time, parent.Root) - bc.stateCache.SetLastMerkleRoot(parent.Root) - } statedb, err := state.New(parent.Root, bc.stateCache, bc.snaps) if err != nil { return it.index, err @@ -2554,8 +2518,8 @@ func (bc *BlockChain) GetTrieFlushInterval() time.Duration { return time.Duration(bc.flushInterval.Load()) } -func (bc *BlockChain) StartVerkleTransition(originalRoot, translatedRoot common.Hash, chainConfig *params.ChainConfig, pragueTime *uint64, root common.Hash) { - bc.stateCache.StartVerkleTransition(originalRoot, translatedRoot, chainConfig, pragueTime, root) +func (bc *BlockChain) StartVerkleTransition(originalRoot, translatedRoot common.Hash, chainConfig *params.ChainConfig, pragueTime *uint64) { + bc.stateCache.StartVerkleTransition(originalRoot, translatedRoot, chainConfig, pragueTime) } func (bc *BlockChain) ReorgThroughVerkleTransition() { bc.stateCache.ReorgThroughVerkleTransition() diff --git a/core/chain_makers.go b/core/chain_makers.go index ec12f2d4ed21..dc6641c22175 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -426,7 +426,7 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine } var snaps *snapshot.Tree triedb := state.NewDatabaseWithConfig(db, nil) - triedb.StartVerkleTransition(common.Hash{}, common.Hash{}, config, config.PragueTime, common.Hash{}) + triedb.StartVerkleTransition(common.Hash{}, common.Hash{}, config, config.PragueTime) triedb.EndVerkleTransition() statedb, err := state.New(parent.Root(), triedb, snaps) if err != nil { diff --git a/core/genesis.go b/core/genesis.go index 9a022f64f335..13d9048ce8a6 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -126,7 +126,7 @@ func (ga *GenesisAlloc) deriveHash(cfg *params.ChainConfig, timestamp uint64) (c // all the derived states will be discarded to not pollute disk. db := state.NewDatabase(rawdb.NewMemoryDatabase()) if cfg.IsPrague(big.NewInt(int64(0)), timestamp) { - db.StartVerkleTransition(common.Hash{}, common.Hash{}, cfg, ×tamp, common.Hash{}) + db.StartVerkleTransition(common.Hash{}, common.Hash{}, cfg, ×tamp) db.EndVerkleTransition() } statedb, err := state.New(types.EmptyRootHash, db, nil) @@ -151,7 +151,7 @@ func (ga *GenesisAlloc) flush(db ethdb.Database, triedb *trie.Database, blockhas database := state.NewDatabaseWithNodeDB(db, triedb) // End the verkle conversion at genesis if the fork block is 0 if timestamp != nil && cfg.IsPrague(big.NewInt(int64(0)), *timestamp) { - database.StartVerkleTransition(common.Hash{}, common.Hash{}, cfg, timestamp, common.Hash{}) + database.StartVerkleTransition(common.Hash{}, common.Hash{}, cfg, timestamp) database.EndVerkleTransition() } diff --git a/core/state/database.go b/core/state/database.go index 588dbbb3d403..0981f0a194df 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -65,7 +65,7 @@ type Database interface { // TrieDB retrieves the low level trie database used for data storage. TrieDB() *trie.Database - StartVerkleTransition(originalRoot, translatedRoot common.Hash, chainConfig *params.ChainConfig, cancunTime *uint64, root common.Hash) + StartVerkleTransition(originalRoot, translatedRoot common.Hash, chainConfig *params.ChainConfig, pragueTime *uint64) ReorgThroughVerkleTransition() @@ -217,7 +217,7 @@ func (db *cachingDB) Transitioned() bool { } // Fork implements the fork -func (db *cachingDB) StartVerkleTransition(originalRoot, translatedRoot common.Hash, chainConfig *params.ChainConfig, pragueTime *uint64, root common.Hash) { +func (db *cachingDB) StartVerkleTransition(originalRoot, translatedRoot common.Hash, chainConfig *params.ChainConfig, pragueTime *uint64) { fmt.Println(` __________.__ .__ .__ __ .__ .__ ____ \__ ___| |__ ____ ____ | | ____ ______ | |__ _____ _____/ |_ | |__ _____ ______ __ _ _|__| ____ / ___\ ______ @@ -340,7 +340,7 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) { mpt Trie err error ) - fmt.Printf("opening trie with root %x, %v %v\n", root, db.InTransition(), db.Transitioned()) + // fmt.Printf("opening trie with root %x, %v %v\n", root, db.InTransition(), db.Transitioned()) // TODO separate both cases when I can be certain that it won't // find a Verkle trie where is expects a Transitoion trie. @@ -406,7 +406,7 @@ func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Addre } } if db.InTransition() { - fmt.Printf("OpenStorageTrie during transition, state root=%x root=%x\n", stateRoot, root) + // fmt.Printf("OpenStorageTrie during transition, state root=%x root=%x\n", stateRoot, root) mpt, err := db.openStorageMPTrie(db.LastMerkleRoot, address, root, nil) if err != nil { return nil, err diff --git a/eth/catalyst/api.go b/eth/catalyst/api.go index 864b3efe84f5..63079415fc14 100644 --- a/eth/catalyst/api.go +++ b/eth/catalyst/api.go @@ -532,7 +532,7 @@ func (api *ConsensusAPI) newPayload(params engine.ExecutableData, versionedHashe if api.eth.BlockChain().Config().IsPrague(block.Number(), block.Time()) && !api.eth.BlockChain().Config().IsPrague(parent.Number(), parent.Time()) { parent := api.eth.BlockChain().GetHeaderByNumber(block.NumberU64() - 1) if !api.eth.BlockChain().Config().IsPrague(parent.Number, parent.Time) { - api.eth.BlockChain().StartVerkleTransition(parent.Root, common.Hash{}, api.eth.BlockChain().Config(), nil, parent.Root) + api.eth.BlockChain().StartVerkleTransition(parent.Root, common.Hash{}, api.eth.BlockChain().Config(), nil) } } // Reset db merge state in case of a reorg diff --git a/light/trie.go b/light/trie.go index df300c8c6ed2..d81eafedcaad 100644 --- a/light/trie.go +++ b/light/trie.go @@ -101,7 +101,7 @@ func (db *odrDatabase) DiskDB() ethdb.KeyValueStore { panic("not implemented") } -func (db *odrDatabase) StartVerkleTransition(originalRoot common.Hash, translatedRoot common.Hash, chainConfig *params.ChainConfig, _ *uint64, _ common.Hash) { +func (db *odrDatabase) StartVerkleTransition(originalRoot common.Hash, translatedRoot common.Hash, chainConfig *params.ChainConfig, _ *uint64) { panic("not implemented") // TODO: Implement } diff --git a/miner/worker.go b/miner/worker.go index 621c38cb3d9c..a1a429f6d206 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -894,7 +894,7 @@ func (w *worker) prepareWork(genParams *generateParams) (*environment, error) { if w.chain.Config().IsPrague(header.Number, header.Time) { parent := w.chain.GetHeaderByNumber(header.Number.Uint64() - 1) if !w.chain.Config().IsPrague(parent.Number, parent.Time) { - w.chain.StartVerkleTransition(parent.Root, common.Hash{}, w.chain.Config(), w.chain.Config().PragueTime, parent.Root) + w.chain.StartVerkleTransition(parent.Root, common.Hash{}, w.chain.Config(), w.chain.Config().PragueTime) } } diff --git a/params/config.go b/params/config.go index 866830ec113e..15c9966b72f8 100644 --- a/params/config.go +++ b/params/config.go @@ -505,8 +505,7 @@ func (c *ChainConfig) IsCancun(num *big.Int, time uint64) bool { // IsPrague returns whether num is either equal to the Prague fork time or greater. func (c *ChainConfig) IsPrague(num *big.Int, time uint64) bool { - return num.Uint64() >= 4702177 - // return c.IsLondon(num) && isTimestampForked(c.PragueTime, time) + return c.IsByzantium(num) && isTimestampForked(c.PragueTime, time) } // CheckCompatible checks whether scheduled fork transitions have been imported From 7b2e9c059a33b9260545f00215ca1e1a5a70128b Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Wed, 24 Jan 2024 19:37:36 -0300 Subject: [PATCH 18/21] fix typos Signed-off-by: Ignacio Hagopian --- core/overlay/conversion.go | 2 +- core/state/database.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/overlay/conversion.go b/core/overlay/conversion.go index e538c58bcce0..89012c95dce1 100644 --- a/core/overlay/conversion.go +++ b/core/overlay/conversion.go @@ -315,7 +315,7 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC log.Debug("account has storage and NO next item") } - // fdb.StorageProcessed will be initialized to `true` if the + // fdb.StorageProcessed will be initialized to `false` if the // entire storage for an account was not entirely processed // by the previous block. This is used as a signal to resume // processing the storage for that account where we left off. diff --git a/core/state/database.go b/core/state/database.go index 0981f0a194df..e722226f98be 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -267,7 +267,7 @@ func (db *cachingDB) EndVerkleTransition() { } type TransitionState struct { - CurrentAccountAddress *common.Address // addresss of the last translated account + CurrentAccountAddress *common.Address // address 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 From 9fab31f02c8017a2af34e9d94bd05cb41655c618 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 26 Jan 2024 14:08:14 -0300 Subject: [PATCH 19/21] rebase fix Signed-off-by: Ignacio Hagopian --- core/state_processor.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/core/state_processor.go b/core/state_processor.go index 637e29d0aa7b..d481c2d7f121 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -98,12 +98,8 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg return nil, nil, 0, errors.New("withdrawals before shanghai") } - state, err := p.bc.State() - if err != nil { - return nil, nil, 0, fmt.Errorf("get statedb: %s", err) - } parent := p.bc.GetHeaderByHash(header.ParentHash) - if err := overlay.OverlayVerkleTransition(state, parent.Root, p.config.OverlayStride); err != nil { + if err := overlay.OverlayVerkleTransition(statedb, parent.Root, p.config.OverlayStride); err != nil { log.Error("error performing the transition", "err", err) } From 9fadea95dfbf6381fdea28910bf4e9c1c3056347 Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Sat, 27 Jan 2024 09:06:36 -0300 Subject: [PATCH 20/21] params: fix fork ordering Signed-off-by: Ignacio Hagopian --- params/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/params/config.go b/params/config.go index 15c9966b72f8..1dc807042a8e 100644 --- a/params/config.go +++ b/params/config.go @@ -550,6 +550,7 @@ func (c *ChainConfig) CheckConfigForkOrder() error { {name: "eip155Block", block: c.EIP155Block}, {name: "eip158Block", block: c.EIP158Block}, {name: "byzantiumBlock", block: c.ByzantiumBlock}, + {name: "pragueTime", timestamp: c.PragueTime, optional: true}, {name: "constantinopleBlock", block: c.ConstantinopleBlock}, {name: "petersburgBlock", block: c.PetersburgBlock}, {name: "istanbulBlock", block: c.IstanbulBlock}, @@ -561,7 +562,6 @@ func (c *ChainConfig) CheckConfigForkOrder() error { {name: "mergeNetsplitBlock", block: c.MergeNetsplitBlock, optional: true}, {name: "shanghaiTime", timestamp: c.ShanghaiTime}, {name: "cancunTime", timestamp: c.CancunTime, optional: true}, - {name: "pragueTime", timestamp: c.PragueTime, optional: true}, } { if lastFork.name != "" { switch { From cdda01c67cb118fd994bb2c90c92d111b26d10cb Mon Sep 17 00:00:00 2001 From: Ignacio Hagopian Date: Fri, 23 Feb 2024 11:23:36 -0300 Subject: [PATCH 21/21] Resumable replays & fix precompile fork ordering (#373) * fix precompiles configuration for replay Prague Signed-off-by: Ignacio Hagopian * fixes for resumable replay after conversion Signed-off-by: Ignacio Hagopian * extra fixes Signed-off-by: Ignacio Hagopian * more fixes Signed-off-by: Ignacio Hagopian * remove blank line Signed-off-by: Ignacio Hagopian * fix underflow bug Signed-off-by: Ignacio Hagopian * fix fork ordering for constantinople Signed-off-by: Ignacio Hagopian * fix precompile switch order Signed-off-by: Ignacio Hagopian --------- Signed-off-by: Ignacio Hagopian --- core/blockchain.go | 3 +-- core/overlay/conversion.go | 1 - core/state/database.go | 15 ++++++++++++++- core/vm/contracts.go | 2 +- core/vm/evm.go | 5 +++-- core/vm/instructions.go | 8 +++++--- core/vm/interpreter.go | 8 ++++---- 7 files changed, 28 insertions(+), 14 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 8ec1482c2415..041b642fed4c 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -320,10 +320,9 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis // but that's left out to a later PR since there's not really a need // right now. bc.stateCache.InitTransitionStatus(true, true) - bc.stateCache.EndVerkleTransition() } - if !bc.HasState(head.Root) { + if !bc.stateCache.Transitioned() && !bc.HasState(head.Root) { // Head state is missing, before the state recovery, find out the // disk layer point of snapshot(if it's enabled). Make sure the // rewound point is lower than disk layer. diff --git a/core/overlay/conversion.go b/core/overlay/conversion.go index 89012c95dce1..ec694b49640b 100644 --- a/core/overlay/conversion.go +++ b/core/overlay/conversion.go @@ -225,7 +225,6 @@ func OverlayVerkleTransition(statedb *state.StateDB, root common.Hash, maxMovedC // verkle transition: if the conversion process is in progress, move // N values from the MPT into the verkle tree. if migrdb.InTransition() { - fmt.Printf("Processing verkle conversion starting at %x %x, building on top of %x\n", migrdb.GetCurrentAccountHash(), migrdb.GetCurrentSlotHash(), root) var ( now = time.Now() tt = statedb.GetTrie().(*trie.TransitionTrie) diff --git a/core/state/database.go b/core/state/database.go index e722226f98be..dfd8789aac4a 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -17,6 +17,7 @@ package state import ( + "bytes" "errors" "fmt" @@ -42,6 +43,8 @@ const ( codeCacheSize = 64 * 1024 * 1024 ) +var keyVerkleTransitionEnded = []byte("verkle-transition-ended") + // Database wraps access to tries and contract code. type Database interface { // OpenTrie opens the main account trie. @@ -227,6 +230,7 @@ func (db *cachingDB) StartVerkleTransition(originalRoot, translatedRoot common.H |__|`) db.CurrentTransitionState = &TransitionState{ started: true, + ended: false, // initialize so that the first storage-less accounts are processed StorageProcessed: true, } @@ -237,6 +241,9 @@ func (db *cachingDB) StartVerkleTransition(originalRoot, translatedRoot common.H if pragueTime != nil { chainConfig.PragueTime = pragueTime } + if err := db.disk.Put(keyVerkleTransitionEnded, []byte{0}); err != nil { + panic(err) // This is fine since this branch is only used for replay + } } func (db *cachingDB) ReorgThroughVerkleTransition() { @@ -264,6 +271,10 @@ func (db *cachingDB) EndVerkleTransition() { |____| |___| /\___ \___ |____/\___ | __/|___| (____ |___| |__| |___| (____ /_____/ |____(____ |___| \____ |\___ \____ | |__|`) db.CurrentTransitionState.ended = true + + if err := db.disk.Put(keyVerkleTransitionEnded, []byte{1}); err != nil { + panic(err) // This is fine since this branch is only used for replay + } } type TransitionState struct { @@ -566,8 +577,10 @@ func (db *cachingDB) LoadTransitionState(root common.Hash) { // as a verkle database. ts, ok := db.TransitionStatePerRoot[root] if !ok || ts == nil { + transitionEnded, _ := db.disk.Get(keyVerkleTransitionEnded) + ended := db.triedb.IsVerkle() || bytes.Equal(transitionEnded, []byte{0x1}) // Start with a fresh state - ts = &TransitionState{ended: db.triedb.IsVerkle()} + ts = &TransitionState{ended: ended} } // Copy so that the CurrentAddress pointer in the map diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 036d18a078fe..3b65b813e492 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -151,7 +151,7 @@ func init() { func ActivePrecompiles(rules params.Rules) []common.Address { switch { case rules.IsPrague: - return PrecompiledAddressesBerlin + return PrecompiledAddressesByzantium case rules.IsCancun: return PrecompiledAddressesCancun case rules.IsBerlin: diff --git a/core/vm/evm.go b/core/vm/evm.go index d066ed6dbe00..754ab421504d 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -41,14 +41,15 @@ type ( func (evm *EVM) precompile(addr common.Address) (PrecompiledContract, bool) { var precompiles map[common.Address]PrecompiledContract switch { - case evm.chainRules.IsPrague: - precompiles = PrecompiledContractsBerlin case evm.chainRules.IsCancun: precompiles = PrecompiledContractsCancun case evm.chainRules.IsBerlin: precompiles = PrecompiledContractsBerlin case evm.chainRules.IsIstanbul: precompiles = PrecompiledContractsIstanbul + case evm.chainRules.IsPrague: + precompiles = PrecompiledContractsByzantium + case evm.chainRules.IsByzantium: precompiles = PrecompiledContractsByzantium default: diff --git a/core/vm/instructions.go b/core/vm/instructions.go index a7860f0fde82..f755ae296ad5 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -399,11 +399,13 @@ func touchCodeChunksRangeOnReadAndChargeGas(contractAddr []byte, startPC, size u return 0 } - // endPC is the last PC that must be touched. - endPC := startPC + size - 1 - if startPC+size > codeLen { + endPC := startPC + size + if endPC > codeLen { endPC = codeLen } + if endPC > 0 { + endPC -= 1 // endPC is the last bytecode that will be touched. + } var statelessGasCharged uint64 for chunkNumber := startPC / 31; chunkNumber <= endPC/31; chunkNumber++ { diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index 02829a662edf..b0cd67ff5093 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -56,10 +56,6 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter { // If jump table was not initialised we set the default one. var table *JumpTable switch { - case evm.chainRules.IsPrague: - // TODO replace with prooper instruction set when fork is specified - // table = &shanghaiInstructionSet - table = &byzantiumInstructionSet case evm.chainRules.IsCancun: table = &cancunInstructionSet case evm.chainRules.IsShanghai: @@ -74,6 +70,10 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter { table = &istanbulInstructionSet case evm.chainRules.IsConstantinople: table = &constantinopleInstructionSet + case evm.chainRules.IsPrague: + // TODO replace with prooper instruction set when fork is specified + // table = &shanghaiInstructionSet + table = &byzantiumInstructionSet case evm.chainRules.IsByzantium: table = &byzantiumInstructionSet case evm.chainRules.IsEIP158: