mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 06:06:44 +00:00
Merge branch 'ethereum:master' into master
This commit is contained in:
commit
7965ee73a1
17 changed files with 263 additions and 60 deletions
|
|
@ -1891,11 +1891,11 @@ func MakeBeaconLightConfig(ctx *cli.Context) bparams.ClientConfig {
|
||||||
if c, err := hexutil.Decode(ctx.String(BeaconGenesisRootFlag.Name)); err == nil && len(c) <= 32 {
|
if c, err := hexutil.Decode(ctx.String(BeaconGenesisRootFlag.Name)); err == nil && len(c) <= 32 {
|
||||||
copy(config.GenesisValidatorsRoot[:len(c)], c)
|
copy(config.GenesisValidatorsRoot[:len(c)], c)
|
||||||
} else {
|
} else {
|
||||||
Fatalf("Invalid hex string", "beacon.genesis.gvroot", ctx.String(BeaconGenesisRootFlag.Name), "error", err)
|
Fatalf("Could not parse --%s: %v", BeaconGenesisRootFlag.Name, err)
|
||||||
}
|
}
|
||||||
configFile := ctx.String(BeaconConfigFlag.Name)
|
configFile := ctx.String(BeaconConfigFlag.Name)
|
||||||
if err := config.ChainConfig.LoadForks(configFile); err != nil {
|
if err := config.ChainConfig.LoadForks(configFile); err != nil {
|
||||||
Fatalf("Could not load beacon chain config", "file", configFile, "error", err)
|
Fatalf("Could not load beacon chain config '%s': %v", configFile, err)
|
||||||
}
|
}
|
||||||
log.Info("Using custom beacon chain config", "file", configFile)
|
log.Info("Using custom beacon chain config", "file", configFile)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -1912,17 +1912,17 @@ func MakeBeaconLightConfig(ctx *cli.Context) bparams.ClientConfig {
|
||||||
// are saved to the specified file.
|
// are saved to the specified file.
|
||||||
if ctx.IsSet(BeaconCheckpointFileFlag.Name) {
|
if ctx.IsSet(BeaconCheckpointFileFlag.Name) {
|
||||||
if _, err := config.SetCheckpointFile(ctx.String(BeaconCheckpointFileFlag.Name)); err != nil {
|
if _, err := config.SetCheckpointFile(ctx.String(BeaconCheckpointFileFlag.Name)); err != nil {
|
||||||
Fatalf("Could not load beacon checkpoint file", "beacon.checkpoint.file", ctx.String(BeaconCheckpointFileFlag.Name), "error", err)
|
Fatalf("Could not load beacon checkpoint file '%s': %v", ctx.String(BeaconCheckpointFileFlag.Name), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ctx.IsSet(BeaconCheckpointFlag.Name) {
|
if ctx.IsSet(BeaconCheckpointFlag.Name) {
|
||||||
hex := ctx.String(BeaconCheckpointFlag.Name)
|
hex := ctx.String(BeaconCheckpointFlag.Name)
|
||||||
c, err := hexutil.Decode(hex)
|
c, err := hexutil.Decode(hex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Fatalf("Invalid hex string", "beacon.checkpoint", hex, "error", err)
|
Fatalf("Could not parse --%s: %v", BeaconCheckpointFlag.Name, err)
|
||||||
}
|
}
|
||||||
if len(c) != 32 {
|
if len(c) != 32 {
|
||||||
Fatalf("Invalid hex string length", "beacon.checkpoint", hex, "length", len(c))
|
Fatalf("Could not parse --%s: invalid length %d, want 32", BeaconCheckpointFlag.Name, len(c))
|
||||||
}
|
}
|
||||||
copy(config.Checkpoint[:len(c)], c)
|
copy(config.Checkpoint[:len(c)], c)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -391,8 +391,12 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error opening pre-state tree root: %w", err)
|
return nil, fmt.Errorf("error opening pre-state tree root: %w", err)
|
||||||
}
|
}
|
||||||
|
postTrie := state.GetTrie()
|
||||||
|
if postTrie == nil {
|
||||||
|
return nil, errors.New("post-state tree is not available")
|
||||||
|
}
|
||||||
vktPreTrie, okpre := preTrie.(*trie.VerkleTrie)
|
vktPreTrie, okpre := preTrie.(*trie.VerkleTrie)
|
||||||
vktPostTrie, okpost := state.GetTrie().(*trie.VerkleTrie)
|
vktPostTrie, okpost := postTrie.(*trie.VerkleTrie)
|
||||||
|
|
||||||
// The witness is only attached iff both parent and current block are
|
// The witness is only attached iff both parent and current block are
|
||||||
// using verkle tree.
|
// using verkle tree.
|
||||||
|
|
|
||||||
|
|
@ -370,6 +370,13 @@ func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
|
||||||
return state.New(root, bc.statedb)
|
return state.New(root, bc.statedb)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HistoricState returns a historic state specified by the given root.
|
||||||
|
// Live states are not available and won't be served, please use `State`
|
||||||
|
// or `StateAt` instead.
|
||||||
|
func (bc *BlockChain) HistoricState(root common.Hash) (*state.StateDB, error) {
|
||||||
|
return state.New(root, state.NewHistoricDatabase(bc.db, bc.triedb))
|
||||||
|
}
|
||||||
|
|
||||||
// Config retrieves the chain's fork configuration.
|
// Config retrieves the chain's fork configuration.
|
||||||
func (bc *BlockChain) Config() *params.ChainConfig { return bc.chainConfig }
|
func (bc *BlockChain) Config() *params.ChainConfig { return bc.chainConfig }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,7 @@ func (b *BlockGen) addTx(bc *BlockChain, vmConfig vm.Config, tx *types.Transacti
|
||||||
}
|
}
|
||||||
// Merge the tx-local access event into the "block-local" one, in order to collect
|
// Merge the tx-local access event into the "block-local" one, in order to collect
|
||||||
// all values, so that the witness can be built.
|
// all values, so that the witness can be built.
|
||||||
if b.statedb.GetTrie().IsVerkle() {
|
if b.statedb.Database().TrieDB().IsVerkle() {
|
||||||
b.statedb.AccessEvents().Merge(evm.AccessEvents)
|
b.statedb.AccessEvents().Merge(evm.AccessEvents)
|
||||||
}
|
}
|
||||||
b.txs = append(b.txs, tx)
|
b.txs = append(b.txs, tx)
|
||||||
|
|
|
||||||
155
core/state/database_history.go
Normal file
155
core/state/database_history.go
Normal file
|
|
@ -0,0 +1,155 @@
|
||||||
|
// Copyright 2025 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package state
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/common/lru"
|
||||||
|
"github.com/ethereum/go-ethereum/core/state/snapshot"
|
||||||
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/ethdb"
|
||||||
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
|
"github.com/ethereum/go-ethereum/trie/utils"
|
||||||
|
"github.com/ethereum/go-ethereum/triedb"
|
||||||
|
"github.com/ethereum/go-ethereum/triedb/pathdb"
|
||||||
|
)
|
||||||
|
|
||||||
|
// historicReader wraps a historical state reader defined in path database,
|
||||||
|
// providing historic state serving over the path scheme.
|
||||||
|
//
|
||||||
|
// TODO(rjl493456442): historicReader is not thread-safe and does not fully
|
||||||
|
// comply with the StateReader interface requirements, needs to be fixed.
|
||||||
|
// Currently, it is only used in a non-concurrent context, so it is safe for now.
|
||||||
|
type historicReader struct {
|
||||||
|
reader *pathdb.HistoricalStateReader
|
||||||
|
}
|
||||||
|
|
||||||
|
// newHistoricReader constructs a reader for historic state serving.
|
||||||
|
func newHistoricReader(r *pathdb.HistoricalStateReader) *historicReader {
|
||||||
|
return &historicReader{reader: r}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Account implements StateReader, retrieving the account specified by the address.
|
||||||
|
//
|
||||||
|
// An error will be returned if the associated snapshot is already stale or
|
||||||
|
// the requested account is not yet covered by the snapshot.
|
||||||
|
//
|
||||||
|
// The returned account might be nil if it's not existent.
|
||||||
|
func (r *historicReader) Account(addr common.Address) (*types.StateAccount, error) {
|
||||||
|
account, err := r.reader.Account(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if account == nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
acct := &types.StateAccount{
|
||||||
|
Nonce: account.Nonce,
|
||||||
|
Balance: account.Balance,
|
||||||
|
CodeHash: account.CodeHash,
|
||||||
|
Root: common.BytesToHash(account.Root),
|
||||||
|
}
|
||||||
|
if len(acct.CodeHash) == 0 {
|
||||||
|
acct.CodeHash = types.EmptyCodeHash.Bytes()
|
||||||
|
}
|
||||||
|
if acct.Root == (common.Hash{}) {
|
||||||
|
acct.Root = types.EmptyRootHash
|
||||||
|
}
|
||||||
|
return acct, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Storage implements StateReader, retrieving the storage slot specified by the
|
||||||
|
// address and slot key.
|
||||||
|
//
|
||||||
|
// An error will be returned if the associated snapshot is already stale or
|
||||||
|
// the requested storage slot is not yet covered by the snapshot.
|
||||||
|
//
|
||||||
|
// The returned storage slot might be empty if it's not existent.
|
||||||
|
func (r *historicReader) Storage(addr common.Address, key common.Hash) (common.Hash, error) {
|
||||||
|
blob, err := r.reader.Storage(addr, key)
|
||||||
|
if err != nil {
|
||||||
|
return common.Hash{}, err
|
||||||
|
}
|
||||||
|
if len(blob) == 0 {
|
||||||
|
return common.Hash{}, nil
|
||||||
|
}
|
||||||
|
_, content, _, err := rlp.Split(blob)
|
||||||
|
if err != nil {
|
||||||
|
return common.Hash{}, err
|
||||||
|
}
|
||||||
|
var slot common.Hash
|
||||||
|
slot.SetBytes(content)
|
||||||
|
return slot, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// HistoricDB is the implementation of Database interface, with the ability to
|
||||||
|
// access historical state.
|
||||||
|
type HistoricDB struct {
|
||||||
|
disk ethdb.KeyValueStore
|
||||||
|
triedb *triedb.Database
|
||||||
|
codeCache *lru.SizeConstrainedCache[common.Hash, []byte]
|
||||||
|
codeSizeCache *lru.Cache[common.Hash, int]
|
||||||
|
pointCache *utils.PointCache
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHistoricDatabase creates a historic state database.
|
||||||
|
func NewHistoricDatabase(disk ethdb.KeyValueStore, triedb *triedb.Database) *HistoricDB {
|
||||||
|
return &HistoricDB{
|
||||||
|
disk: disk,
|
||||||
|
triedb: triedb,
|
||||||
|
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
|
||||||
|
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
|
||||||
|
pointCache: utils.NewPointCache(pointCacheSize),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reader implements Database interface, returning a reader of the specific state.
|
||||||
|
func (db *HistoricDB) Reader(stateRoot common.Hash) (Reader, error) {
|
||||||
|
hr, err := db.triedb.HistoricReader(stateRoot)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return newReader(newCachingCodeReader(db.disk, db.codeCache, db.codeSizeCache), newHistoricReader(hr)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpenTrie opens the main account trie. It's not supported by historic database.
|
||||||
|
func (db *HistoricDB) OpenTrie(root common.Hash) (Trie, error) {
|
||||||
|
return nil, errors.New("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpenStorageTrie opens the storage trie of an account. It's not supported by
|
||||||
|
// historic database.
|
||||||
|
func (db *HistoricDB) OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash, trie Trie) (Trie, error) {
|
||||||
|
return nil, errors.New("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
// PointCache returns the cache holding points used in verkle tree key computation
|
||||||
|
func (db *HistoricDB) PointCache() *utils.PointCache {
|
||||||
|
return db.pointCache
|
||||||
|
}
|
||||||
|
|
||||||
|
// TrieDB returns the underlying trie database for managing trie nodes.
|
||||||
|
func (db *HistoricDB) TrieDB() *triedb.Database {
|
||||||
|
return db.triedb
|
||||||
|
}
|
||||||
|
|
||||||
|
// Snapshot returns the underlying state snapshot.
|
||||||
|
func (db *HistoricDB) Snapshot() *snapshot.Tree {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
@ -112,6 +112,9 @@ func (d iterativeDump) OnRoot(root common.Hash) {
|
||||||
|
|
||||||
// DumpToCollector iterates the state according to the given options and inserts
|
// DumpToCollector iterates the state according to the given options and inserts
|
||||||
// the items into a collector for aggregation or serialization.
|
// the items into a collector for aggregation or serialization.
|
||||||
|
//
|
||||||
|
// The state iterator is still trie-based and can be converted to snapshot-based
|
||||||
|
// once the state snapshot is fully integrated into database. TODO(rjl493456442).
|
||||||
func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []byte) {
|
func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []byte) {
|
||||||
// Sanitize the input to allow nil configs
|
// Sanitize the input to allow nil configs
|
||||||
if conf == nil {
|
if conf == nil {
|
||||||
|
|
@ -123,15 +126,20 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
|
||||||
start = time.Now()
|
start = time.Now()
|
||||||
logged = time.Now()
|
logged = time.Now()
|
||||||
)
|
)
|
||||||
log.Info("Trie dumping started", "root", s.trie.Hash())
|
log.Info("Trie dumping started", "root", s.originalRoot)
|
||||||
c.OnRoot(s.trie.Hash())
|
c.OnRoot(s.originalRoot)
|
||||||
|
|
||||||
trieIt, err := s.trie.NodeIterator(conf.Start)
|
tr, err := s.db.OpenTrie(s.originalRoot)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
trieIt, err := tr.NodeIterator(conf.Start)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Trie dumping error", "err", err)
|
log.Error("Trie dumping error", "err", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
it := trie.NewIterator(trieIt)
|
it := trie.NewIterator(trieIt)
|
||||||
|
|
||||||
for it.Next() {
|
for it.Next() {
|
||||||
var data types.StateAccount
|
var data types.StateAccount
|
||||||
if err := rlp.DecodeBytes(it.Value, &data); err != nil {
|
if err := rlp.DecodeBytes(it.Value, &data); err != nil {
|
||||||
|
|
@ -147,7 +155,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
|
||||||
}
|
}
|
||||||
address *common.Address
|
address *common.Address
|
||||||
addr common.Address
|
addr common.Address
|
||||||
addrBytes = s.trie.GetKey(it.Key)
|
addrBytes = tr.GetKey(it.Key)
|
||||||
)
|
)
|
||||||
if addrBytes == nil {
|
if addrBytes == nil {
|
||||||
missingPreimages++
|
missingPreimages++
|
||||||
|
|
@ -165,12 +173,13 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
|
||||||
}
|
}
|
||||||
if !conf.SkipStorage {
|
if !conf.SkipStorage {
|
||||||
account.Storage = make(map[common.Hash]string)
|
account.Storage = make(map[common.Hash]string)
|
||||||
tr, err := obj.getTrie()
|
|
||||||
|
storageTr, err := s.db.OpenStorageTrie(s.originalRoot, addr, obj.Root(), tr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to load storage trie", "err", err)
|
log.Error("Failed to load storage trie", "err", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
trieIt, err := tr.NodeIterator(nil)
|
trieIt, err := storageTr.NodeIterator(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to create trie iterator", "err", err)
|
log.Error("Failed to create trie iterator", "err", err)
|
||||||
continue
|
continue
|
||||||
|
|
@ -182,7 +191,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
|
||||||
log.Error("Failed to decode the value returned by iterator", "error", err)
|
log.Error("Failed to decode the value returned by iterator", "error", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
key := s.trie.GetKey(storageIt.Key)
|
key := storageTr.GetKey(storageIt.Key)
|
||||||
if key == nil {
|
if key == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ import (
|
||||||
// required in order to resolve the contract address.
|
// required in order to resolve the contract address.
|
||||||
type nodeIterator struct {
|
type nodeIterator struct {
|
||||||
state *StateDB // State being iterated
|
state *StateDB // State being iterated
|
||||||
|
tr Trie // Primary account trie for traversal
|
||||||
|
|
||||||
stateIt trie.NodeIterator // Primary iterator for the global state trie
|
stateIt trie.NodeIterator // Primary iterator for the global state trie
|
||||||
dataIt trie.NodeIterator // Secondary iterator for the data trie of a contract
|
dataIt trie.NodeIterator // Secondary iterator for the data trie of a contract
|
||||||
|
|
@ -75,13 +76,20 @@ func (it *nodeIterator) step() error {
|
||||||
if it.state == nil {
|
if it.state == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// Initialize the iterator if we've just started
|
if it.tr == nil {
|
||||||
var err error
|
tr, err := it.state.db.OpenTrie(it.state.originalRoot)
|
||||||
if it.stateIt == nil {
|
|
||||||
it.stateIt, err = it.state.trie.NodeIterator(nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
it.tr = tr
|
||||||
|
}
|
||||||
|
// Initialize the iterator if we've just started
|
||||||
|
if it.stateIt == nil {
|
||||||
|
stateIt, err := it.tr.NodeIterator(nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
it.stateIt = stateIt
|
||||||
}
|
}
|
||||||
// If we had data nodes previously, we surely have at least state nodes
|
// If we had data nodes previously, we surely have at least state nodes
|
||||||
if it.dataIt != nil {
|
if it.dataIt != nil {
|
||||||
|
|
@ -116,14 +124,14 @@ func (it *nodeIterator) step() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Lookup the preimage of account hash
|
// Lookup the preimage of account hash
|
||||||
preimage := it.state.trie.GetKey(it.stateIt.LeafKey())
|
preimage := it.tr.GetKey(it.stateIt.LeafKey())
|
||||||
if preimage == nil {
|
if preimage == nil {
|
||||||
return errors.New("account address is not available")
|
return errors.New("account address is not available")
|
||||||
}
|
}
|
||||||
address := common.BytesToAddress(preimage)
|
address := common.BytesToAddress(preimage)
|
||||||
|
|
||||||
// Traverse the storage slots belong to the account
|
// Traverse the storage slots belong to the account
|
||||||
dataTrie, err := it.state.db.OpenStorageTrie(it.state.originalRoot, address, account.Root, it.state.trie)
|
dataTrie, err := it.state.db.OpenStorageTrie(it.state.originalRoot, address, account.Root, it.tr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -124,6 +124,7 @@ func (s *stateObject) touch() {
|
||||||
// subsequent reads to expand the same trie instead of reloading from disk.
|
// subsequent reads to expand the same trie instead of reloading from disk.
|
||||||
func (s *stateObject) getTrie() (Trie, error) {
|
func (s *stateObject) getTrie() (Trie, error) {
|
||||||
if s.trie == nil {
|
if s.trie == nil {
|
||||||
|
// Assumes the primary account trie is already loaded
|
||||||
tr, err := s.db.db.OpenStorageTrie(s.db.originalRoot, s.address, s.data.Root, s.db.trie)
|
tr, err := s.db.db.OpenStorageTrie(s.db.originalRoot, s.address, s.data.Root, s.db.trie)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -54,8 +54,6 @@ func TestDump(t *testing.T) {
|
||||||
obj3.SetBalance(uint256.NewInt(44))
|
obj3.SetBalance(uint256.NewInt(44))
|
||||||
|
|
||||||
// write some of them to the trie
|
// write some of them to the trie
|
||||||
s.state.updateStateObject(obj1)
|
|
||||||
s.state.updateStateObject(obj2)
|
|
||||||
root, _ := s.state.Commit(0, false, false)
|
root, _ := s.state.Commit(0, false, false)
|
||||||
|
|
||||||
// check that DumpToCollector contains the state objects that are in trie
|
// check that DumpToCollector contains the state objects that are in trie
|
||||||
|
|
@ -114,8 +112,6 @@ func TestIterativeDump(t *testing.T) {
|
||||||
obj4.AddBalance(uint256.NewInt(1337))
|
obj4.AddBalance(uint256.NewInt(1337))
|
||||||
|
|
||||||
// write some of them to the trie
|
// write some of them to the trie
|
||||||
s.state.updateStateObject(obj1)
|
|
||||||
s.state.updateStateObject(obj2)
|
|
||||||
root, _ := s.state.Commit(0, false, false)
|
root, _ := s.state.Commit(0, false, false)
|
||||||
s.state, _ = New(root, tdb)
|
s.state, _ = New(root, tdb)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -79,8 +79,8 @@ func (m *mutation) isDelete() bool {
|
||||||
type StateDB struct {
|
type StateDB struct {
|
||||||
db Database
|
db Database
|
||||||
prefetcher *triePrefetcher
|
prefetcher *triePrefetcher
|
||||||
trie Trie
|
|
||||||
reader Reader
|
reader Reader
|
||||||
|
trie Trie // it's resolved on first access
|
||||||
|
|
||||||
// originalRoot is the pre-state root, before any changes were made.
|
// originalRoot is the pre-state root, before any changes were made.
|
||||||
// It will be updated when the Commit is called.
|
// It will be updated when the Commit is called.
|
||||||
|
|
@ -169,13 +169,8 @@ func New(root common.Hash, db Database) (*StateDB, error) {
|
||||||
// NewWithReader creates a new state for the specified state root. Unlike New,
|
// NewWithReader creates a new state for the specified state root. Unlike New,
|
||||||
// this function accepts an additional Reader which is bound to the given root.
|
// this function accepts an additional Reader which is bound to the given root.
|
||||||
func NewWithReader(root common.Hash, db Database, reader Reader) (*StateDB, error) {
|
func NewWithReader(root common.Hash, db Database, reader Reader) (*StateDB, error) {
|
||||||
tr, err := db.OpenTrie(root)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
sdb := &StateDB{
|
sdb := &StateDB{
|
||||||
db: db,
|
db: db,
|
||||||
trie: tr,
|
|
||||||
originalRoot: root,
|
originalRoot: root,
|
||||||
reader: reader,
|
reader: reader,
|
||||||
stateObjects: make(map[common.Address]*stateObject),
|
stateObjects: make(map[common.Address]*stateObject),
|
||||||
|
|
@ -664,7 +659,6 @@ func (s *StateDB) Copy() *StateDB {
|
||||||
// Copy all the basic fields, initialize the memory ones
|
// Copy all the basic fields, initialize the memory ones
|
||||||
state := &StateDB{
|
state := &StateDB{
|
||||||
db: s.db,
|
db: s.db,
|
||||||
trie: mustCopyTrie(s.trie),
|
|
||||||
reader: s.reader,
|
reader: s.reader,
|
||||||
originalRoot: s.originalRoot,
|
originalRoot: s.originalRoot,
|
||||||
stateObjects: make(map[common.Address]*stateObject, len(s.stateObjects)),
|
stateObjects: make(map[common.Address]*stateObject, len(s.stateObjects)),
|
||||||
|
|
@ -688,6 +682,9 @@ func (s *StateDB) Copy() *StateDB {
|
||||||
transientStorage: s.transientStorage.Copy(),
|
transientStorage: s.transientStorage.Copy(),
|
||||||
journal: s.journal.copy(),
|
journal: s.journal.copy(),
|
||||||
}
|
}
|
||||||
|
if s.trie != nil {
|
||||||
|
state.trie = mustCopyTrie(s.trie)
|
||||||
|
}
|
||||||
if s.witness != nil {
|
if s.witness != nil {
|
||||||
state.witness = s.witness.Copy()
|
state.witness = s.witness.Copy()
|
||||||
}
|
}
|
||||||
|
|
@ -783,6 +780,20 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
|
||||||
// Finalise all the dirty storage states and write them into the tries
|
// Finalise all the dirty storage states and write them into the tries
|
||||||
s.Finalise(deleteEmptyObjects)
|
s.Finalise(deleteEmptyObjects)
|
||||||
|
|
||||||
|
// Initialize the trie if it's not constructed yet. If the prefetch
|
||||||
|
// is enabled, the trie constructed below will be replaced by the
|
||||||
|
// prefetched one.
|
||||||
|
//
|
||||||
|
// This operation must be done before state object storage hashing,
|
||||||
|
// as it assumes the main trie is already loaded.
|
||||||
|
if s.trie == nil {
|
||||||
|
tr, err := s.db.OpenTrie(s.originalRoot)
|
||||||
|
if err != nil {
|
||||||
|
s.setError(err)
|
||||||
|
return common.Hash{}
|
||||||
|
}
|
||||||
|
s.trie = tr
|
||||||
|
}
|
||||||
// If there was a trie prefetcher operating, terminate it async so that the
|
// If there was a trie prefetcher operating, terminate it async so that the
|
||||||
// individual storage tries can be updated as soon as the disk load finishes.
|
// individual storage tries can be updated as soon as the disk load finishes.
|
||||||
if s.prefetcher != nil {
|
if s.prefetcher != nil {
|
||||||
|
|
|
||||||
|
|
@ -171,7 +171,6 @@ func TestCopy(t *testing.T) {
|
||||||
for i := byte(0); i < 255; i++ {
|
for i := byte(0); i < 255; i++ {
|
||||||
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
||||||
obj.AddBalance(uint256.NewInt(uint64(i)))
|
obj.AddBalance(uint256.NewInt(uint64(i)))
|
||||||
orig.updateStateObject(obj)
|
|
||||||
}
|
}
|
||||||
orig.Finalise(false)
|
orig.Finalise(false)
|
||||||
|
|
||||||
|
|
@ -190,10 +189,6 @@ func TestCopy(t *testing.T) {
|
||||||
origObj.AddBalance(uint256.NewInt(2 * uint64(i)))
|
origObj.AddBalance(uint256.NewInt(2 * uint64(i)))
|
||||||
copyObj.AddBalance(uint256.NewInt(3 * uint64(i)))
|
copyObj.AddBalance(uint256.NewInt(3 * uint64(i)))
|
||||||
ccopyObj.AddBalance(uint256.NewInt(4 * uint64(i)))
|
ccopyObj.AddBalance(uint256.NewInt(4 * uint64(i)))
|
||||||
|
|
||||||
orig.updateStateObject(origObj)
|
|
||||||
copy.updateStateObject(copyObj)
|
|
||||||
ccopy.updateStateObject(copyObj)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finalise the changes on all concurrently
|
// Finalise the changes on all concurrently
|
||||||
|
|
@ -238,7 +233,6 @@ func TestCopyWithDirtyJournal(t *testing.T) {
|
||||||
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
||||||
obj.AddBalance(uint256.NewInt(uint64(i)))
|
obj.AddBalance(uint256.NewInt(uint64(i)))
|
||||||
obj.data.Root = common.HexToHash("0xdeadbeef")
|
obj.data.Root = common.HexToHash("0xdeadbeef")
|
||||||
orig.updateStateObject(obj)
|
|
||||||
}
|
}
|
||||||
root, _ := orig.Commit(0, true, false)
|
root, _ := orig.Commit(0, true, false)
|
||||||
orig, _ = New(root, db)
|
orig, _ = New(root, db)
|
||||||
|
|
@ -248,8 +242,6 @@ func TestCopyWithDirtyJournal(t *testing.T) {
|
||||||
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
||||||
amount := uint256.NewInt(uint64(i))
|
amount := uint256.NewInt(uint64(i))
|
||||||
obj.SetBalance(new(uint256.Int).Sub(obj.Balance(), amount))
|
obj.SetBalance(new(uint256.Int).Sub(obj.Balance(), amount))
|
||||||
|
|
||||||
orig.updateStateObject(obj)
|
|
||||||
}
|
}
|
||||||
cpy := orig.Copy()
|
cpy := orig.Copy()
|
||||||
|
|
||||||
|
|
@ -284,7 +276,6 @@ func TestCopyObjectState(t *testing.T) {
|
||||||
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
||||||
obj.AddBalance(uint256.NewInt(uint64(i)))
|
obj.AddBalance(uint256.NewInt(uint64(i)))
|
||||||
obj.data.Root = common.HexToHash("0xdeadbeef")
|
obj.data.Root = common.HexToHash("0xdeadbeef")
|
||||||
orig.updateStateObject(obj)
|
|
||||||
}
|
}
|
||||||
orig.Finalise(true)
|
orig.Finalise(true)
|
||||||
cpy := orig.Copy()
|
cpy := orig.Copy()
|
||||||
|
|
@ -573,7 +564,7 @@ func forEachStorage(s *StateDB, addr common.Address, cb func(key, value common.H
|
||||||
)
|
)
|
||||||
|
|
||||||
for it.Next() {
|
for it.Next() {
|
||||||
key := common.BytesToHash(s.trie.GetKey(it.Key))
|
key := common.BytesToHash(tr.GetKey(it.Key))
|
||||||
visited[key] = true
|
visited[key] = true
|
||||||
if value, dirty := so.dirtyStorage[key]; dirty {
|
if value, dirty := so.dirtyStorage[key]; dirty {
|
||||||
if !cb(key, value) {
|
if !cb(key, value) {
|
||||||
|
|
|
||||||
|
|
@ -161,10 +161,9 @@ func ApplyTransactionWithEVM(msg *Message, gp *GasPool, statedb *state.StateDB,
|
||||||
|
|
||||||
// Merge the tx-local access event into the "block-local" one, in order to collect
|
// Merge the tx-local access event into the "block-local" one, in order to collect
|
||||||
// all values, so that the witness can be built.
|
// all values, so that the witness can be built.
|
||||||
if statedb.GetTrie().IsVerkle() {
|
if statedb.Database().TrieDB().IsVerkle() {
|
||||||
statedb.AccessEvents().Merge(evm.AccessEvents)
|
statedb.AccessEvents().Merge(evm.AccessEvents)
|
||||||
}
|
}
|
||||||
|
|
||||||
return MakeReceipt(evm, result, statedb, blockNumber, blockHash, blockTime, tx, *usedGas, root), nil
|
return MakeReceipt(evm, result, statedb, blockNumber, blockHash, blockTime, tx, *usedGas, root), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -237,9 +237,12 @@ func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.B
|
||||||
return nil, nil, errors.New("header not found")
|
return nil, nil, errors.New("header not found")
|
||||||
}
|
}
|
||||||
stateDb, err := b.eth.BlockChain().StateAt(header.Root)
|
stateDb, err := b.eth.BlockChain().StateAt(header.Root)
|
||||||
|
if err != nil {
|
||||||
|
stateDb, err = b.eth.BlockChain().HistoricState(header.Root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return stateDb, header, nil
|
return stateDb, header, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -259,9 +262,12 @@ func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockN
|
||||||
return nil, nil, errors.New("hash is not currently canonical")
|
return nil, nil, errors.New("hash is not currently canonical")
|
||||||
}
|
}
|
||||||
stateDb, err := b.eth.BlockChain().StateAt(header.Root)
|
stateDb, err := b.eth.BlockChain().StateAt(header.Root)
|
||||||
|
if err != nil {
|
||||||
|
stateDb, err = b.eth.BlockChain().HistoricState(header.Root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return stateDb, header, nil
|
return stateDb, header, nil
|
||||||
}
|
}
|
||||||
return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
|
return nil, nil, errors.New("invalid arguments; neither block nor hash specified")
|
||||||
|
|
|
||||||
|
|
@ -140,7 +140,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
|
||||||
log.Warn("Sanitizing invalid miner gas price", "provided", config.Miner.GasPrice, "updated", ethconfig.Defaults.Miner.GasPrice)
|
log.Warn("Sanitizing invalid miner gas price", "provided", config.Miner.GasPrice, "updated", ethconfig.Defaults.Miner.GasPrice)
|
||||||
config.Miner.GasPrice = new(big.Int).Set(ethconfig.Defaults.Miner.GasPrice)
|
config.Miner.GasPrice = new(big.Int).Set(ethconfig.Defaults.Miner.GasPrice)
|
||||||
}
|
}
|
||||||
if config.NoPruning && config.TrieDirtyCache > 0 {
|
if config.NoPruning && config.TrieDirtyCache > 0 && config.StateScheme == rawdb.HashScheme {
|
||||||
if config.SnapshotCache > 0 {
|
if config.SnapshotCache > 0 {
|
||||||
config.TrieCleanCache += config.TrieDirtyCache * 3 / 5
|
config.TrieCleanCache += config.TrieDirtyCache * 3 / 5
|
||||||
config.SnapshotCache += config.TrieDirtyCache * 2 / 5
|
config.SnapshotCache += config.TrieDirtyCache * 2 / 5
|
||||||
|
|
|
||||||
|
|
@ -182,10 +182,11 @@ func (eth *Ethereum) pathState(block *types.Block) (*state.StateDB, func(), erro
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return statedb, noopReleaser, nil
|
return statedb, noopReleaser, nil
|
||||||
}
|
}
|
||||||
// TODO historic state is not supported in path-based scheme.
|
statedb, err = eth.blockchain.HistoricState(block.Root())
|
||||||
// Fully archive node in pbss will be implemented by relying
|
if err == nil {
|
||||||
// on state history, but needs more work on top.
|
return statedb, noopReleaser, nil
|
||||||
return nil, nil, errors.New("historical state not available in path scheme yet")
|
}
|
||||||
|
return nil, nil, errors.New("historical state is not available")
|
||||||
}
|
}
|
||||||
|
|
||||||
// stateAtBlock retrieves the state database associated with a certain block.
|
// stateAtBlock retrieves the state database associated with a certain block.
|
||||||
|
|
|
||||||
|
|
@ -129,6 +129,15 @@ func (db *Database) StateReader(blockRoot common.Hash) (database.StateReader, er
|
||||||
return db.backend.StateReader(blockRoot)
|
return db.backend.StateReader(blockRoot)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HistoricReader constructs a reader for accessing the requested historic state.
|
||||||
|
func (db *Database) HistoricReader(root common.Hash) (*pathdb.HistoricalStateReader, error) {
|
||||||
|
pdb, ok := db.backend.(*pathdb.Database)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("not supported")
|
||||||
|
}
|
||||||
|
return pdb.HistoricReader(root)
|
||||||
|
}
|
||||||
|
|
||||||
// Update performs a state transition by committing dirty nodes contained in the
|
// Update performs a state transition by committing dirty nodes contained in the
|
||||||
// given set in order to update state from the specified parent to the specified
|
// given set in order to update state from the specified parent to the specified
|
||||||
// root. The held pre-images accumulated up to this point will be flushed in case
|
// root. The held pre-images accumulated up to this point will be flushed in case
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,8 @@ func (b *batchIndexer) finish(force bool) error {
|
||||||
var (
|
var (
|
||||||
batch = b.db.NewBatch()
|
batch = b.db.NewBatch()
|
||||||
batchMu sync.RWMutex
|
batchMu sync.RWMutex
|
||||||
|
storages int
|
||||||
|
start = time.Now()
|
||||||
eg errgroup.Group
|
eg errgroup.Group
|
||||||
)
|
)
|
||||||
eg.SetLimit(runtime.NumCPU())
|
eg.SetLimit(runtime.NumCPU())
|
||||||
|
|
@ -167,6 +169,7 @@ func (b *batchIndexer) finish(force bool) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
for addrHash, slots := range b.storages {
|
for addrHash, slots := range b.storages {
|
||||||
|
storages += len(slots)
|
||||||
for storageHash, idList := range slots {
|
for storageHash, idList := range slots {
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
if !b.delete {
|
if !b.delete {
|
||||||
|
|
@ -216,6 +219,7 @@ func (b *batchIndexer) finish(force bool) error {
|
||||||
if err := batch.Write(); err != nil {
|
if err := batch.Write(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
log.Debug("Committed batch indexer", "accounts", len(b.accounts), "storages", storages, "records", b.counter, "elapsed", common.PrettyDuration(time.Since(start)))
|
||||||
b.counter = 0
|
b.counter = 0
|
||||||
b.accounts = make(map[common.Hash][]uint64)
|
b.accounts = make(map[common.Hash][]uint64)
|
||||||
b.storages = make(map[common.Hash]map[common.Hash][]uint64)
|
b.storages = make(map[common.Hash]map[common.Hash][]uint64)
|
||||||
|
|
@ -224,9 +228,10 @@ func (b *batchIndexer) finish(force bool) error {
|
||||||
|
|
||||||
// indexSingle processes the state history with the specified ID for indexing.
|
// indexSingle processes the state history with the specified ID for indexing.
|
||||||
func indexSingle(historyID uint64, db ethdb.KeyValueStore, freezer ethdb.AncientReader) error {
|
func indexSingle(historyID uint64, db ethdb.KeyValueStore, freezer ethdb.AncientReader) error {
|
||||||
defer func(start time.Time) {
|
start := time.Now()
|
||||||
|
defer func() {
|
||||||
indexHistoryTimer.UpdateSince(start)
|
indexHistoryTimer.UpdateSince(start)
|
||||||
}(time.Now())
|
}()
|
||||||
|
|
||||||
metadata := loadIndexMetadata(db)
|
metadata := loadIndexMetadata(db)
|
||||||
if metadata == nil || metadata.Last+1 != historyID {
|
if metadata == nil || metadata.Last+1 != historyID {
|
||||||
|
|
@ -247,15 +252,16 @@ func indexSingle(historyID uint64, db ethdb.KeyValueStore, freezer ethdb.Ancient
|
||||||
if err := b.finish(true); err != nil {
|
if err := b.finish(true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Debug("Indexed state history", "id", historyID)
|
log.Debug("Indexed state history", "id", historyID, "elapsed", common.PrettyDuration(time.Since(start)))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// unindexSingle processes the state history with the specified ID for unindexing.
|
// unindexSingle processes the state history with the specified ID for unindexing.
|
||||||
func unindexSingle(historyID uint64, db ethdb.KeyValueStore, freezer ethdb.AncientReader) error {
|
func unindexSingle(historyID uint64, db ethdb.KeyValueStore, freezer ethdb.AncientReader) error {
|
||||||
defer func(start time.Time) {
|
start := time.Now()
|
||||||
|
defer func() {
|
||||||
unindexHistoryTimer.UpdateSince(start)
|
unindexHistoryTimer.UpdateSince(start)
|
||||||
}(time.Now())
|
}()
|
||||||
|
|
||||||
metadata := loadIndexMetadata(db)
|
metadata := loadIndexMetadata(db)
|
||||||
if metadata == nil || metadata.Last != historyID {
|
if metadata == nil || metadata.Last != historyID {
|
||||||
|
|
@ -276,7 +282,7 @@ func unindexSingle(historyID uint64, db ethdb.KeyValueStore, freezer ethdb.Ancie
|
||||||
if err := b.finish(true); err != nil {
|
if err := b.finish(true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Debug("Unindexed state history", "id", historyID)
|
log.Debug("Unindexed state history", "id", historyID, "elapsed", common.PrettyDuration(time.Since(start)))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -498,7 +504,7 @@ func (i *indexIniter) index(done chan struct{}, interrupt *atomic.Int32, lastID
|
||||||
)
|
)
|
||||||
// Override the ETA if larger than the largest until now
|
// Override the ETA if larger than the largest until now
|
||||||
eta := time.Duration(left/speed) * time.Millisecond
|
eta := time.Duration(left/speed) * time.Millisecond
|
||||||
log.Info("Indexing state history", "processed", done, "left", left, "eta", common.PrettyDuration(eta))
|
log.Info("Indexing state history", "processed", done, "left", left, "elapsed", common.PrettyDuration(time.Since(start)), "eta", common.PrettyDuration(eta))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Check interruption signal and abort process if it's fired
|
// Check interruption signal and abort process if it's fired
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue