diff --git a/core/chain_makers.go b/core/chain_makers.go index 674a9d8196..af55716cca 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -581,7 +581,6 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine func GenerateVerkleChainWithGenesis(genesis *Genesis, engine consensus.Engine, n int, gen func(int, *BlockGen)) (common.Hash, ethdb.Database, []*types.Block, []types.Receipts, []*verkle.VerkleProof, []verkle.StateDiff) { db := rawdb.NewMemoryDatabase() - saveVerkleTransitionStatusAtVerlkeGenesis(db) cacheConfig := DefaultConfig().WithStateScheme(rawdb.PathScheme) cacheConfig.SnapshotLimit = 0 triedb := triedb.NewDatabase(db, cacheConfig.triedbConfig(true)) diff --git a/core/genesis.go b/core/genesis.go index 2ef982afc8..5bf2555944 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -18,7 +18,6 @@ package core import ( "bytes" - "encoding/gob" "encoding/json" "errors" "fmt" @@ -146,9 +145,6 @@ func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) { emptyRoot = types.EmptyVerkleHash } db := rawdb.NewMemoryDatabase() - if isVerkle { - saveVerkleTransitionStatusAtVerlkeGenesis(db) - } statedb, err := state.New(emptyRoot, state.NewDatabase(triedb.NewDatabase(db, config), nil)) if err != nil { return common.Hash{}, err @@ -280,24 +276,6 @@ func (o *ChainOverrides) apply(cfg *params.ChainConfig) error { return cfg.CheckConfigForkOrder() } -// saveVerkleTransitionStatusAtVerlkeGenesis saves a conversion marker -// representing a converted state, which is used in devnets that activate -// verkle at genesis. -func saveVerkleTransitionStatusAtVerlkeGenesis(db ethdb.Database) { - saveVerkleTransitionStatus(db, common.Hash{}, &state.TransitionState{Ended: true}) -} - -func saveVerkleTransitionStatus(db ethdb.Database, root common.Hash, ts *state.TransitionState) { - var buf bytes.Buffer - enc := gob.NewEncoder(&buf) - err := enc.Encode(ts) - if err != nil { - log.Error("failed to encode transition state", "err", err) - return - } - rawdb.WriteVerkleTransitionState(db, root, buf.Bytes()) -} - // SetupGenesisBlock writes or updates the genesis block in db. // The block that will be used is: // @@ -321,11 +299,6 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *triedb.Database, g if genesis != nil && genesis.Config == nil { return nil, common.Hash{}, nil, errGenesisNoConfig } - // In case of verkle-at-genesis, we need to ensure that the conversion - // markers are indicating that the conversion has completed. - if genesis != nil && genesis.Config.VerkleTime != nil && *genesis.Config.VerkleTime == genesis.Timestamp { - saveVerkleTransitionStatusAtVerlkeGenesis(db) - } // Commit the genesis if the database is empty ghash := rawdb.ReadCanonicalHash(db, 0) if (ghash == common.Hash{}) { @@ -473,7 +446,7 @@ func (g *Genesis) chainConfigOrDefault(ghash common.Hash, stored *params.ChainCo // IsVerkle indicates whether the state is already stored in a verkle // tree at genesis time. func (g *Genesis) IsVerkle() bool { - return g != nil && g.Config != nil && g.Config.VerkleTime != nil && *g.Config.VerkleTime == g.Timestamp + return g.Config.IsVerkleGenesis() } // ToBlock returns the genesis block according to genesis specification. @@ -577,9 +550,6 @@ func (g *Genesis) Commit(db ethdb.Database, triedb *triedb.Database) (*types.Blo if err != nil { return nil, err } - if g.IsVerkle() { - saveVerkleTransitionStatus(db, block.Root(), &state.TransitionState{Ended: true}) - } batch := db.NewBatch() rawdb.WriteGenesisStateSpec(batch, block.Hash(), blob) rawdb.WriteBlock(batch, block) diff --git a/core/genesis_test.go b/core/genesis_test.go index 0077863193..702cd3445f 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -287,6 +287,7 @@ func TestVerkleGenesisCommit(t *testing.T) { OsakaTime: &verkleTime, VerkleTime: &verkleTime, TerminalTotalDifficulty: big.NewInt(0), + EnableVerkleAtGenesis: true, Ethash: nil, Clique: nil, BlobScheduleConfig: ¶ms.BlobScheduleConfig{ @@ -314,7 +315,6 @@ func TestVerkleGenesisCommit(t *testing.T) { } db := rawdb.NewMemoryDatabase() - saveVerkleTransitionStatusAtVerlkeGenesis(db) config := *pathdb.Defaults config.NoAsyncFlush = true diff --git a/core/state/database.go b/core/state/database.go index 3cb9a78d55..ea24b4f5cc 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -276,12 +276,14 @@ func (db *CachingDB) ReadersWithCacheStats(stateRoot common.Hash) (ReaderWithSta // OpenTrie opens the main account trie at a specific root hash. func (db *CachingDB) OpenTrie(root common.Hash) (Trie, error) { - ts := db.LoadTransitionState(root) - if ts.InTransition() { - panic("transition isn't supported yet") - } - if ts.Transitioned() { - return trie.NewVerkleTrie(root, db.triedb, db.pointCache) + if db.triedb.IsVerkle() { + ts := db.LoadTransitionState(root) + if ts.InTransition() { + panic("transition isn't supported yet") + } + if ts.Transitioned() { + return trie.NewVerkleTrie(root, db.triedb, db.pointCache) + } } tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb) if err != nil { @@ -292,11 +294,7 @@ func (db *CachingDB) OpenTrie(root common.Hash) (Trie, error) { // OpenStorageTrie opens the storage trie of an account. func (db *CachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash, self Trie) (Trie, error) { - ts := db.LoadTransitionState(stateRoot) - // In the verkle case, there is only one tree. But the two-tree structure - // is hardcoded in the codebase. So we need to return the same trie in this - // case. - if ts.InTransition() || ts.Transitioned() { + if db.triedb.IsVerkle() { return self, nil } tr, err := trie.NewStateTrie(trie.StorageTrieID(stateRoot, crypto.Keccak256Hash(address.Bytes()), root), db.triedb) diff --git a/core/verkle_witness_test.go b/core/verkle_witness_test.go index 47e3c885d9..e200bf7f50 100644 --- a/core/verkle_witness_test.go +++ b/core/verkle_witness_test.go @@ -59,6 +59,7 @@ var ( ShanghaiTime: u64(0), VerkleTime: u64(0), TerminalTotalDifficulty: common.Big0, + EnableVerkleAtGenesis: true, BlobScheduleConfig: ¶ms.BlobScheduleConfig{ Verkle: params.DefaultPragueBlobConfig, }, @@ -82,6 +83,7 @@ var ( ShanghaiTime: u64(0), VerkleTime: u64(0), TerminalTotalDifficulty: common.Big0, + EnableVerkleAtGenesis: true, BlobScheduleConfig: ¶ms.BlobScheduleConfig{ Verkle: params.DefaultPragueBlobConfig, }, @@ -209,7 +211,7 @@ func TestProcessVerkle(t *testing.T) { t.Fatalf("block %d imported with error: %v", endnum, err) } - for i := 0; i < 2; i++ { + for i := range 2 { b := blockchain.GetBlockByNumber(uint64(i) + 1) if b == nil { t.Fatalf("expected block %d to be present in chain", i+1) diff --git a/params/config.go b/params/config.go index 4e74bf2082..85619bbe22 100644 --- a/params/config.go +++ b/params/config.go @@ -423,6 +423,19 @@ type ChainConfig struct { DepositContractAddress common.Address `json:"depositContractAddress,omitempty"` + // EnableVerkleAtGenesis is a flag that specifies whether the network uses + // the Verkle tree starting from the genesis block. If set to true, the + // genesis state will be committed using the Verkle tree, eliminating the + // need for any Verkle transition later. + // + // This is a temporary flag only for verkle devnet testing, where verkle is + // activated at genesis, and the configured activation date has already passed. + // + // In production networks (mainnet and public testnets), verkle activation + // always occurs after the genesis block, making this flag irrelevant in + // those cases. + EnableVerkleAtGenesis bool `json:"enableVerkleAtGenesis,omitempty"` + // Various consensus engines Ethash *EthashConfig `json:"ethash,omitempty"` Clique *CliqueConfig `json:"clique,omitempty"` @@ -691,6 +704,20 @@ func (c *ChainConfig) IsBPO5(num *big.Int, time uint64) bool { return c.IsLondon(num) && isTimestampForked(c.BPO5Time, time) } +// IsVerkleGenesis checks whether the verkle fork is activated at the genesis block. +// +// Verkle mode is considered enabled if the verkle fork time is configured, +// regardless of whether the local time has surpassed the fork activation time. +// This is a temporary workaround for verkle devnet testing, where verkle is +// activated at genesis, and the configured activation date has already passed. +// +// In production networks (mainnet and public testnets), verkle activation +// always occurs after the genesis block, making this function irrelevant in +// those cases. +func (c *ChainConfig) IsVerkleGenesis() bool { + return c.EnableVerkleAtGenesis +} + // IsEIP4762 returns whether eip 4762 has been activated at given block. func (c *ChainConfig) IsEIP4762(num *big.Int, time uint64) bool { return c.IsVerkle(num, time)