diff --git a/core/bintrie_transition_test.go b/core/bintrie_transition_test.go new file mode 100644 index 0000000000..4c3aff90f8 --- /dev/null +++ b/core/bintrie_transition_test.go @@ -0,0 +1,107 @@ +// Copyright 2026 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package core + +import ( + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/consensus/beacon" + "github.com/ethereum/go-ethereum/consensus/ethash" + "github.com/ethereum/go-ethereum/params" +) + +// TestBinaryTransitionRegistryBootstrap exercises the registry deployment on +// the first UBT block: pre-fork blocks must leave the registry uninitialised, +// the first UBT block must mark started=true and the base root must be +// captured for every subsequent UBT block. +func TestBinaryTransitionRegistryBootstrap(t *testing.T) { + var ( + ubtTime uint64 = 30 + coinbase = common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7") + gspec = &Genesis{ + Config: ¶ms.ChainConfig{ + ChainID: big.NewInt(1), + HomesteadBlock: big.NewInt(0), + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: big.NewInt(0), + Ethash: new(params.EthashConfig), + ShanghaiTime: u64(0), + CancunTime: u64(0), + PragueTime: u64(0), + UBTTime: &ubtTime, + TerminalTotalDifficulty: common.Big0, + EnableUBTAtGenesis: false, + BlobScheduleConfig: ¶ms.BlobScheduleConfig{ + Cancun: params.DefaultCancunBlobConfig, + Prague: params.DefaultPragueBlobConfig, + UBT: params.DefaultPragueBlobConfig, + }, + }, + Alloc: GenesisAlloc{ + coinbase: { + Balance: big.NewInt(1000000000000000000), + }, + params.BeaconRootsAddress: {Nonce: 1, Code: params.BeaconRootsCode, Balance: common.Big0}, + params.HistoryStorageAddress: {Nonce: 1, Code: params.HistoryStorageCode, Balance: common.Big0}, + params.WithdrawalQueueAddress: {Nonce: 1, Code: params.WithdrawalQueueCode, Balance: common.Big0}, + params.ConsolidationQueueAddress: {Nonce: 1, Code: params.ConsolidationQueueCode, Balance: common.Big0}, + }, + } + ) + config := gspec.Config + engine := beacon.New(ethash.NewFaker()) + + registryAddr := params.BinaryTransitionRegistryAddress + slotStarted := common.Hash{} + slotBaseRoot := common.BytesToHash([]byte{5}) + + GenerateChainWithGenesis(gspec, engine, 6, func(i int, gen *BlockGen) { + gen.SetPoS() + + blockNum := gen.Number() + blockTime := gen.Timestamp() + isUBT := config.IsUBT(blockNum, blockTime) + + started := gen.GetState(registryAddr, slotStarted) + baseRoot := gen.GetState(registryAddr, slotBaseRoot) + t.Logf("block %d: num=%d time=%d isUBT=%v started=%x baseRoot=%x", + i, blockNum.Uint64(), blockTime, isUBT, started, baseRoot) + + if !isUBT { + if started != (common.Hash{}) { + t.Errorf("block %d: pre-transition block should not have registry initialized", i) + } + return + } + if started == (common.Hash{}) { + t.Errorf("block %d: UBT block should have registry slot 0 (started) set", i) + } + if baseRoot == (common.Hash{}) { + t.Errorf("block %d: UBT block should have registry slot 5 (base root) set", i) + } + }) +} diff --git a/core/blockchain.go b/core/blockchain.go index 296ef6bc16..ce3efe9426 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -38,6 +38,7 @@ import ( "github.com/ethereum/go-ethereum/consensus" "github.com/ethereum/go-ethereum/consensus/misc/eip4844" "github.com/ethereum/go-ethereum/core/history" + "github.com/ethereum/go-ethereum/core/overlay" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state/snapshot" @@ -257,6 +258,13 @@ func (cfg BlockChainConfig) WithNoAsyncFlush(on bool) *BlockChainConfig { return &cfg } +// newBinaryTrieDB opens the sibling triedb that holds binary trie nodes +// during the MPT-to-binary transition. It uses the same scheme and limits as +// the main triedb but is forced into UBT mode. +func newBinaryTrieDB(db ethdb.Database, cfg *BlockChainConfig) *triedb.Database { + return triedb.NewDatabase(db, cfg.triedbConfig(true)) +} + // triedbConfig derives the configures for trie database. func (cfg *BlockChainConfig) triedbConfig(isUBT bool) *triedb.Config { config := &triedb.Config{ @@ -323,6 +331,7 @@ type BlockChain struct { lastWrite uint64 // Last block when the state was flushed flushInterval atomic.Int64 // Time interval (processing time) after which to flush a state triedb *triedb.Database // The database handler for maintaining trie nodes. + bintriedb *triedb.Database // Sibling triedb for binary trie nodes during the MPT-to-binary transition; nil if no UBT fork is configured or the chain is binary at genesis. codedb *state.CodeDB // The database handler for maintaining contract codes. txIndexer *txIndexer // Transaction indexer, might be nil if not enabled @@ -382,16 +391,26 @@ func NewBlockChain(db ethdb.Database, genesis *Genesis, engine consensus.Engine, if err != nil { return nil, err } - triedb := triedb.NewDatabase(db, cfg.triedbConfig(enableVerkle)) + mainTriedb := triedb.NewDatabase(db, cfg.triedbConfig(enableVerkle)) + + // Open a sibling binary trie database when the chain is configured for an + // MPT-to-binary transition (i.e. UBTTime is set and the chain did not start + // binary at genesis). The two databases run in parallel during the + // transition window: writes target the binary trie, reads fall through to + // the frozen MPT base resolved from the transition registry. + var bintriedb *triedb.Database // Write the supplied genesis to the database if it has not been initialized // yet. The corresponding chain config will be returned, either from the // provided genesis or from the locally stored configuration if the genesis // has already been initialized. - chainConfig, genesisHash, compatErr, err := SetupGenesisBlockWithOverride(db, triedb, genesis, cfg.Overrides, cfg.VmConfig.Tracer) + chainConfig, genesisHash, compatErr, err := SetupGenesisBlockWithOverride(db, mainTriedb, genesis, cfg.Overrides, cfg.VmConfig.Tracer) if err != nil { return nil, err } + if !enableVerkle && chainConfig.UBTTime != nil { + bintriedb = newBinaryTrieDB(db, cfg) + } log.Info("") log.Info(strings.Repeat("-", 153)) for _, line := range strings.Split(chainConfig.Description(), "\n") { @@ -404,7 +423,8 @@ func NewBlockChain(db ethdb.Database, genesis *Genesis, engine consensus.Engine, chainConfig: chainConfig, cfg: cfg, db: db, - triedb: triedb, + triedb: mainTriedb, + bintriedb: bintriedb, codedb: state.NewCodeDB(db), triegc: prque.New[int64, common.Hash](nil), chainmu: syncx.NewClosableMutex(), @@ -1349,6 +1369,11 @@ func (bc *BlockChain) Stop() { if err := bc.triedb.Journal(bc.CurrentBlock().Root); err != nil { log.Info("Failed to journal in-memory trie nodes", "err", err) } + if bc.bintriedb != nil { + if err := bc.bintriedb.Journal(bc.CurrentBlock().Root); err != nil { + log.Info("Failed to journal in-memory binary trie nodes", "err", err) + } + } } else { // Ensure the state of a recent block is also stored to disk before exiting. // We're writing three different states to catch different restart scenarios: @@ -1390,6 +1415,11 @@ func (bc *BlockChain) Stop() { if err := bc.triedb.Close(); err != nil { log.Error("Failed to close trie database", "err", err) } + if bc.bintriedb != nil { + if err := bc.bintriedb.Close(); err != nil { + log.Error("Failed to close binary trie database", "err", err) + } + } log.Info("Blockchain stopped") } @@ -2108,6 +2138,69 @@ type ExecuteConfig struct { EnableWitnessStats bool } +// stateDatabase returns the appropriate state.Database for executing a block +// with the given header against the supplied parent root. The routing is: +// +// - pre-UBT-fork blocks → MPTDatabase backed by bc.triedb. +// - first UBT block (parent is pre-UBT) → transition UBTDatabase, with the +// binary trie as primary store and the parent's MPT root as the frozen +// base. The MPT triedb is provided for read-through during the transition. +// - subsequent UBT blocks → if the registry still reports an active +// transition, a transition UBTDatabase is built; otherwise (transition +// ended, or chainConfig.UBTTransitionEndTime crossed) a plain UBTDatabase +// is returned with the transition wrap disabled. +func (bc *BlockChain) stateDatabase(parentRoot common.Hash, header *types.Header) state.Database { + if !bc.chainConfig.IsUBT(header.Number, header.Time) { + return state.NewMPTDatabase(bc.triedb, bc.codedb).WithSnapshot(bc.snaps) + } + // Past the configured transition end: pure binary, no wrap, no MPT base. + if !bc.chainConfig.UBTTransitionActive(header.Number, header.Time) { + return state.NewUBTDatabase(bc.bintriedbOrMain(), bc.codedb).WithTransitionTreeWrap(false) + } + // First UBT block: parent is pre-UBT; seed the transition with parent root. + parent := bc.GetHeaderByHash(header.ParentHash) + if parent != nil && !bc.chainConfig.IsUBT(parent.Number, parent.Time) { + return state.NewTransitionUBTDatabase(bc.bintriedbOrMain(), bc.triedb, bc.codedb, parentRoot) + } + // Subsequent UBT block while the transition is active: probe the registry + // to see whether a base root is still recorded. + return bc.probeTransitionDatabase(parentRoot) +} + +// bintriedbOrMain returns bc.bintriedb when present, otherwise the main triedb. +// Binary-at-genesis chains have no sibling binary triedb because the main +// triedb is already in UBT mode. +func (bc *BlockChain) bintriedbOrMain() *triedb.Database { + if bc.bintriedb != nil { + return bc.bintriedb + } + return bc.triedb +} + +// probeTransitionDatabase reads the transition registry from the binary trie +// at the given root and chooses between a transition-mode UBTDatabase (if +// the registry still records a base root) and a plain UBTDatabase otherwise. +func (bc *BlockChain) probeTransitionDatabase(root common.Hash) state.Database { + bindb := bc.bintriedbOrMain() + plain := state.NewUBTDatabase(bindb, bc.codedb) + reader, err := plain.StateReader(root) + if err != nil { + return plain + } + ts := overlay.LoadTransitionState(storageReaderFunc(reader.Storage), root) + if ts == nil || ts.Transitioned() || ts.BaseRoot == (common.Hash{}) { + return plain + } + return state.NewTransitionUBTDatabase(bindb, bc.triedb, bc.codedb, ts.BaseRoot) +} + +// storageReaderFunc adapts a state-reader Storage method to overlay.StorageReader. +type storageReaderFunc func(addr common.Address, slot common.Hash) (common.Hash, error) + +func (f storageReaderFunc) Storage(addr common.Address, slot common.Hash) (common.Hash, error) { + return f(addr, slot) +} + // ProcessBlock executes and validates the given block. If there was no error // it writes the block and associated state to database. func (bc *BlockChain) ProcessBlock(ctx context.Context, parentRoot common.Hash, block *types.Block, config ExecuteConfig) (result *blockProcessingResult, blockEndErr error) { @@ -2116,15 +2209,10 @@ func (bc *BlockChain) ProcessBlock(ctx context.Context, parentRoot common.Hash, startTime = time.Now() statedb *state.StateDB interrupt atomic.Bool - sdb state.Database ) defer interrupt.Store(true) // terminate the prefetch at the end - if bc.chainConfig.IsUBT(block.Number(), block.Time()) { - sdb = state.NewUBTDatabase(bc.triedb, bc.codedb) - } else { - sdb = state.NewMPTDatabase(bc.triedb, bc.codedb).WithSnapshot(bc.snaps) - } + sdb := bc.stateDatabase(parentRoot, block.Header()) // If prefetching is enabled, run that against the current state to pre-cache // transactions and probabilistically some of the account/storage trie nodes. // diff --git a/core/blockchain_reader.go b/core/blockchain_reader.go index 18afa9ce9d..4dd04b24c4 100644 --- a/core/blockchain_reader.go +++ b/core/blockchain_reader.go @@ -421,27 +421,13 @@ func (bc *BlockChain) State() (*state.StateDB, error) { // StateAt returns a new mutable state based on a particular point in time. func (bc *BlockChain) StateAt(header *types.Header) (*state.StateDB, error) { - if bc.chainConfig.IsUBT(header.Number, header.Time) { - return state.New(header.Root, state.NewUBTDatabase(bc.triedb, bc.codedb)) - } - return state.New(header.Root, state.NewMPTDatabase(bc.triedb, bc.codedb).WithSnapshot(bc.snaps)) + return state.New(header.Root, bc.stateDatabase(header.Root, header)) } // StateAtForkBoundary returns a new mutable state based on the parent state // and the given header, handling the transition across the UBT fork. func (bc *BlockChain) StateAtForkBoundary(parent *types.Header, header *types.Header) (*state.StateDB, error) { - // The parent is already in the UBT fork. - if bc.chainConfig.IsUBT(parent.Number, parent.Time) { - return state.New(parent.Root, state.NewUBTDatabase(bc.triedb, bc.codedb)) - } - // The current block is the first block in the UBT fork - // (i.e., the parent is the last MPT block). - if bc.chainConfig.IsUBT(header.Number, header.Time) { - // TODO(gballet): register chain context if needed - return state.New(parent.Root, state.NewUBTDatabase(bc.triedb, bc.codedb)) - } - // Both the parent and current block are in the MPT fork. - return state.New(parent.Root, state.NewMPTDatabase(bc.triedb, bc.codedb).WithSnapshot(bc.snaps)) + return state.New(parent.Root, bc.stateDatabase(parent.Root, header)) } // HistoricState returns a historic state specified by the given header. diff --git a/core/chain_makers.go b/core/chain_makers.go index b45f3f03af..aefae433ca 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -172,6 +172,14 @@ func (b *BlockGen) GetBalance(addr common.Address) *uint256.Int { return b.statedb.GetBalance(addr) } +// GetState returns the storage slot value for the given address and key as +// observed by the in-progress block generator. Useful in tests that need to +// inspect system-contract slots written during block production (e.g. the +// binary transition registry). +func (b *BlockGen) GetState(addr common.Address, key common.Hash) common.Hash { + return b.statedb.GetState(addr, key) +} + // AddUncheckedTx forcefully adds a transaction to the block without any validation. // // AddUncheckedTx will cause consensus failures when used during real diff --git a/core/state/database_ubt.go b/core/state/database_ubt.go index 66653f6112..437e2fdaf9 100644 --- a/core/state/database_ubt.go +++ b/core/state/database_ubt.go @@ -17,34 +17,78 @@ package state import ( + "errors" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/trie" "github.com/ethereum/go-ethereum/trie/bintrie" + "github.com/ethereum/go-ethereum/trie/transitiontrie" "github.com/ethereum/go-ethereum/triedb" ) // UBTDatabase is an implementation of Database interface for Unified Binary Trie. -// It provides the same functionality as MPTDatabase but uses unified binary -// trie for state hashing instead of Merkle Patricia Tries. +// +// In its plain form it uses a single binary trie database. During the +// MPT-to-binary transition, an optional MPT trie database (mpttriedb) and a +// frozen base root provide read-only access to pre-transition state. The +// wrapInTransitionTrie flag controls whether reads at this database are +// served via a TransitionTrie that overlays the binary trie on the MPT +// base; it is normally driven by chainConfig.UBTTransitionActive. type UBTDatabase struct { - triedb *triedb.Database - codedb *CodeDB + triedb *triedb.Database + mpttriedb *triedb.Database + codedb *CodeDB + baseRoot common.Hash + wrapInTransitionTrie bool } // Type returns Binary, indicating this database is backed by a Universal Binary Trie. func (db *UBTDatabase) Type() DatabaseType { return TypeUBT } // NewUBTDatabase creates a state database with the Unified binary trie manner. +// State access is wrapped in a TransitionTrie by default (which degenerates +// to a passthrough when there is no MPT base) so callers that don't care +// about the override get sensible defaults. func NewUBTDatabase(triedb *triedb.Database, codedb *CodeDB) *UBTDatabase { if codedb == nil { codedb = NewCodeDB(triedb.Disk()) } return &UBTDatabase{ - triedb: triedb, - codedb: codedb, + triedb: triedb, + codedb: codedb, + wrapInTransitionTrie: true, } } +// NewTransitionUBTDatabase creates a UBTDatabase for the active MPT-to-binary +// transition window. The binary trie is the primary store; reads fall through +// to the frozen MPT at baseRoot via the supplied mpttriedb, and writes go +// only to the binary trie. +func NewTransitionUBTDatabase(bintriedb, mpttriedb *triedb.Database, codedb *CodeDB, baseRoot common.Hash) *UBTDatabase { + if codedb == nil { + codedb = NewCodeDB(bintriedb.Disk()) + } + return &UBTDatabase{ + triedb: bintriedb, + mpttriedb: mpttriedb, + codedb: codedb, + baseRoot: baseRoot, + wrapInTransitionTrie: true, + } +} + +// WithTransitionTreeWrap toggles whether reads at this database are wrapped +// in a TransitionTrie. Setting it to false disables the wrap regardless of +// the registry state and is intended for callers that have already crossed +// the configured UBTTransitionEndTime. +func (db *UBTDatabase) WithTransitionTreeWrap(wrap bool) *UBTDatabase { + db.wrapInTransitionTrie = wrap + return db +} + // StateReader returns a state reader associated with the specified state root. func (db *UBTDatabase) StateReader(stateRoot common.Hash) (StateReader, error) { var readers []StateReader @@ -60,10 +104,8 @@ func (db *UBTDatabase) StateReader(stateRoot common.Hash) (StateReader, error) { } } // Configure the trie reader, which is expected to be available as the - // gatekeeper unless the state is corrupted. The transition tree wrap is - // kept on by default so the registry's BaseRoot (when populated) is - // honoured; the MPT triedb is plumbed through in a later commit. - tr, err := newUBTTrieReader(stateRoot, db.triedb, nil, true) + // gatekeeper unless the state is corrupted. + tr, err := newUBTTrieReader(stateRoot, db.triedb, db.mpttriedb, db.wrapInTransitionTrie) if err != nil { return nil, err } @@ -96,15 +138,36 @@ func (db *UBTDatabase) ReadersWithCacheStats(stateRoot common.Hash) (Reader, Rea return ra, rb, nil } -// OpenTrie opens the main account trie at a specific root hash. +// OpenTrie opens the main account trie at a specific root hash. During an +// active transition, the binary trie is wrapped in a TransitionTrie so writes +// land on the binary trie while reads fall through to the frozen MPT base. func (db *UBTDatabase) OpenTrie(root common.Hash) (Trie, error) { - return bintrie.NewBinaryTrie(root, db.triedb) + bt, err := bintrie.NewBinaryTrie(root, db.triedb) + if err != nil { + return nil, err + } + if db.mpttriedb == nil || db.baseRoot == (common.Hash{}) { + return transitiontrie.NewTransitionTrie(nil, bt, false), nil + } + base, err := trie.NewStateTrie(trie.StateTrieID(db.baseRoot), db.mpttriedb) + if err != nil { + return nil, err + } + return transitiontrie.NewTransitionTrie(base, bt, false), nil } -// OpenStorageTrie opens the storage trie of an account. In binary trie mode, -// all state objects share one unified trie, so the main trie is returned. +// OpenStorageTrie opens the storage trie of an account. In binary trie mode +// the unified trie carries all state, so the main trie is reused. During the +// transition, an MPT storage trie is opened for accounts that have not yet +// been migrated. func (db *UBTDatabase) OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash, self Trie) (Trie, error) { - return self, nil + if self != nil && self.IsUBT() { + return self, nil + } + if db.mpttriedb == nil { + return nil, errors.New("no MPT trie database available for storage trie outside the transition window") + } + return trie.NewStateTrie(trie.StorageTrieID(stateRoot, crypto.Keccak256Hash(address.Bytes()), root), db.mpttriedb) } // TrieDB retrieves any intermediate trie-node caching layer. @@ -130,10 +193,17 @@ func (db *UBTDatabase) Commit(update *StateUpdate) error { return err } } + // On the first transition block, the originRoot is the MPT base root, + // but the binary trie's parent state is empty. Substitute the empty + // binary hash so triedb.Update doesn't reject the mismatch. + originRoot := update.OriginRoot + if db.mpttriedb != nil && originRoot == db.baseRoot { + originRoot = types.EmptyBinaryHash + } // Encode the state mutations in the UBT format accounts, accountOrigin, storages, storageOrigin := update.EncodeUBTState() - return db.triedb.Update(update.Root, update.OriginRoot, update.BlockNumber, update.Nodes, &triedb.StateSet{ + return db.triedb.Update(update.Root, originRoot, update.BlockNumber, update.Nodes, &triedb.StateSet{ Accounts: accounts, AccountsOrigin: accountOrigin, Storages: storages,