core, eth, triedb: expose historic state to RPC
This commit is contained in:
parent
53691eec4e
commit
ee7810b239
|
@ -353,6 +353,13 @@ func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) {
|
|||
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.
|
||||
func (bc *BlockChain) Config() *params.ChainConfig { return bc.chainConfig }
|
||||
|
||||
|
|
|
@ -0,0 +1,151 @@
|
|||
// 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.
|
||||
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
|
||||
}
|
|
@ -123,10 +123,14 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
|
|||
start = time.Now()
|
||||
logged = time.Now()
|
||||
)
|
||||
log.Info("Trie dumping started", "root", s.trie.Hash())
|
||||
c.OnRoot(s.trie.Hash())
|
||||
log.Info("Trie dumping started", "root", s.originalRoot)
|
||||
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 {
|
||||
log.Error("Trie dumping error", "err", err)
|
||||
return nil
|
||||
|
@ -147,7 +151,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
|
|||
}
|
||||
address *common.Address
|
||||
addr common.Address
|
||||
addrBytes = s.trie.GetKey(it.Key)
|
||||
addrBytes = tr.GetKey(it.Key)
|
||||
)
|
||||
if addrBytes == nil {
|
||||
missingPreimages++
|
||||
|
@ -165,12 +169,13 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
|
|||
}
|
||||
if !conf.SkipStorage {
|
||||
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 {
|
||||
log.Error("Failed to load storage trie", "err", err)
|
||||
continue
|
||||
}
|
||||
trieIt, err := tr.NodeIterator(nil)
|
||||
trieIt, err := storageTr.NodeIterator(nil)
|
||||
if err != nil {
|
||||
log.Error("Failed to create trie iterator", "err", err)
|
||||
continue
|
||||
|
@ -182,7 +187,7 @@ func (s *StateDB) DumpToCollector(c DumpCollector, conf *DumpConfig) (nextKey []
|
|||
log.Error("Failed to decode the value returned by iterator", "error", err)
|
||||
continue
|
||||
}
|
||||
account.Storage[common.BytesToHash(s.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
|
||||
account.Storage[common.BytesToHash(storageTr.GetKey(storageIt.Key))] = common.Bytes2Hex(content)
|
||||
}
|
||||
}
|
||||
c.OnAccount(address, account)
|
||||
|
|
|
@ -32,6 +32,7 @@ import (
|
|||
// required in order to resolve the contract address.
|
||||
type nodeIterator struct {
|
||||
state *StateDB // State being iterated
|
||||
tr Trie // Primary account trie for traversal
|
||||
|
||||
stateIt trie.NodeIterator // Primary iterator for the global state trie
|
||||
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 {
|
||||
return nil
|
||||
}
|
||||
// Initialize the iterator if we've just started
|
||||
var err error
|
||||
if it.stateIt == nil {
|
||||
it.stateIt, err = it.state.trie.NodeIterator(nil)
|
||||
if it.tr == nil {
|
||||
tr, err := it.state.db.OpenTrie(it.state.originalRoot)
|
||||
if err != nil {
|
||||
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 it.dataIt != nil {
|
||||
|
@ -116,14 +124,14 @@ func (it *nodeIterator) step() error {
|
|||
return err
|
||||
}
|
||||
// Lookup the preimage of account hash
|
||||
preimage := it.state.trie.GetKey(it.stateIt.LeafKey())
|
||||
preimage := it.tr.GetKey(it.stateIt.LeafKey())
|
||||
if preimage == nil {
|
||||
return errors.New("account address is not available")
|
||||
}
|
||||
address := common.BytesToAddress(preimage)
|
||||
|
||||
// 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 {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -52,10 +52,6 @@ func TestDump(t *testing.T) {
|
|||
obj2.SetCode(crypto.Keccak256Hash([]byte{3, 3, 3, 3, 3, 3, 3}), []byte{3, 3, 3, 3, 3, 3, 3})
|
||||
obj3 := s.state.getOrNewStateObject(common.BytesToAddress([]byte{0x02}))
|
||||
obj3.SetBalance(uint256.NewInt(44))
|
||||
|
||||
// write some of them to the trie
|
||||
s.state.updateStateObject(obj1)
|
||||
s.state.updateStateObject(obj2)
|
||||
root, _ := s.state.Commit(0, false, false)
|
||||
|
||||
// check that DumpToCollector contains the state objects that are in trie
|
||||
|
@ -114,8 +110,6 @@ func TestIterativeDump(t *testing.T) {
|
|||
obj4.AddBalance(uint256.NewInt(1337))
|
||||
|
||||
// write some of them to the trie
|
||||
s.state.updateStateObject(obj1)
|
||||
s.state.updateStateObject(obj2)
|
||||
root, _ := s.state.Commit(0, false, false)
|
||||
s.state, _ = New(root, tdb)
|
||||
|
||||
|
|
|
@ -79,7 +79,7 @@ func (m *mutation) isDelete() bool {
|
|||
type StateDB struct {
|
||||
db Database
|
||||
prefetcher *triePrefetcher
|
||||
trie Trie
|
||||
trie Trie // trie is only resolved when it's accessed
|
||||
reader Reader
|
||||
|
||||
// originalRoot is the pre-state root, before any changes were made.
|
||||
|
@ -159,17 +159,12 @@ type StateDB struct {
|
|||
|
||||
// New creates a new state from a given trie.
|
||||
func New(root common.Hash, db Database) (*StateDB, error) {
|
||||
tr, err := db.OpenTrie(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reader, err := db.Reader(root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sdb := &StateDB{
|
||||
db: db,
|
||||
trie: tr,
|
||||
originalRoot: root,
|
||||
reader: reader,
|
||||
stateObjects: make(map[common.Address]*stateObject),
|
||||
|
@ -654,7 +649,6 @@ func (s *StateDB) Copy() *StateDB {
|
|||
reader, _ := s.db.Reader(s.originalRoot) // impossible to fail
|
||||
state := &StateDB{
|
||||
db: s.db,
|
||||
trie: mustCopyTrie(s.trie),
|
||||
reader: reader,
|
||||
originalRoot: s.originalRoot,
|
||||
stateObjects: make(map[common.Address]*stateObject, len(s.stateObjects)),
|
||||
|
@ -678,6 +672,9 @@ func (s *StateDB) Copy() *StateDB {
|
|||
transientStorage: s.transientStorage.Copy(),
|
||||
journal: s.journal.copy(),
|
||||
}
|
||||
if s.trie != nil {
|
||||
state.trie = mustCopyTrie(s.trie)
|
||||
}
|
||||
if s.witness != nil {
|
||||
state.witness = s.witness.Copy()
|
||||
}
|
||||
|
@ -773,6 +770,15 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
|
|||
// Finalise all the dirty storage states and write them into the tries
|
||||
s.Finalise(deleteEmptyObjects)
|
||||
|
||||
// Initialize the trie if it's not constructed yet
|
||||
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
|
||||
// individual storage tries can be updated as soon as the disk load finishes.
|
||||
if s.prefetcher != nil {
|
||||
|
|
|
@ -171,7 +171,6 @@ func TestCopy(t *testing.T) {
|
|||
for i := byte(0); i < 255; i++ {
|
||||
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
||||
obj.AddBalance(uint256.NewInt(uint64(i)))
|
||||
orig.updateStateObject(obj)
|
||||
}
|
||||
orig.Finalise(false)
|
||||
|
||||
|
@ -190,10 +189,6 @@ func TestCopy(t *testing.T) {
|
|||
origObj.AddBalance(uint256.NewInt(2 * uint64(i)))
|
||||
copyObj.AddBalance(uint256.NewInt(3 * uint64(i)))
|
||||
ccopyObj.AddBalance(uint256.NewInt(4 * uint64(i)))
|
||||
|
||||
orig.updateStateObject(origObj)
|
||||
copy.updateStateObject(copyObj)
|
||||
ccopy.updateStateObject(copyObj)
|
||||
}
|
||||
|
||||
// Finalise the changes on all concurrently
|
||||
|
@ -238,7 +233,6 @@ func TestCopyWithDirtyJournal(t *testing.T) {
|
|||
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
||||
obj.AddBalance(uint256.NewInt(uint64(i)))
|
||||
obj.data.Root = common.HexToHash("0xdeadbeef")
|
||||
orig.updateStateObject(obj)
|
||||
}
|
||||
root, _ := orig.Commit(0, true, false)
|
||||
orig, _ = New(root, db)
|
||||
|
@ -248,8 +242,6 @@ func TestCopyWithDirtyJournal(t *testing.T) {
|
|||
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
||||
amount := uint256.NewInt(uint64(i))
|
||||
obj.SetBalance(new(uint256.Int).Sub(obj.Balance(), amount))
|
||||
|
||||
orig.updateStateObject(obj)
|
||||
}
|
||||
cpy := orig.Copy()
|
||||
|
||||
|
@ -284,7 +276,6 @@ func TestCopyObjectState(t *testing.T) {
|
|||
obj := orig.getOrNewStateObject(common.BytesToAddress([]byte{i}))
|
||||
obj.AddBalance(uint256.NewInt(uint64(i)))
|
||||
obj.data.Root = common.HexToHash("0xdeadbeef")
|
||||
orig.updateStateObject(obj)
|
||||
}
|
||||
orig.Finalise(true)
|
||||
cpy := orig.Copy()
|
||||
|
|
|
@ -157,7 +157,7 @@ func ApplyTransactionWithEVM(msg *Message, gp *GasPool, statedb *state.StateDB,
|
|||
|
||||
// Merge the tx-local access event into the "block-local" one, in order to collect
|
||||
// all values, so that the witness can be built.
|
||||
if statedb.GetTrie().IsVerkle() {
|
||||
if statedb.Database().TrieDB().IsVerkle() {
|
||||
statedb.AccessEvents().Merge(evm.AccessEvents)
|
||||
}
|
||||
|
||||
|
|
|
@ -205,7 +205,10 @@ func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.B
|
|||
}
|
||||
stateDb, err := b.eth.BlockChain().StateAt(header.Root)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
stateDb, err = b.eth.BlockChain().HistoricState(header.Root)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
return stateDb, header, nil
|
||||
}
|
||||
|
@ -227,7 +230,10 @@ func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockN
|
|||
}
|
||||
stateDb, err := b.eth.BlockChain().StateAt(header.Root)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
stateDb, err = b.eth.BlockChain().HistoricState(header.Root)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
return stateDb, header, nil
|
||||
}
|
||||
|
|
|
@ -182,10 +182,11 @@ func (eth *Ethereum) pathState(block *types.Block) (*state.StateDB, func(), erro
|
|||
if err == nil {
|
||||
return statedb, noopReleaser, nil
|
||||
}
|
||||
// TODO historic state is not supported in path-based scheme.
|
||||
// Fully archive node in pbss will be implemented by relying
|
||||
// on state history, but needs more work on top.
|
||||
return nil, nil, errors.New("historical state not available in path scheme yet")
|
||||
statedb, err = eth.blockchain.HistoricState(block.Root())
|
||||
if err == nil {
|
||||
return statedb, noopReleaser, nil
|
||||
}
|
||||
return nil, nil, errors.New("historical state is not available")
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// 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
|
||||
// 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
|
||||
|
|
|
@ -422,7 +422,7 @@ func (i *indexIniter) index(done chan struct{}, interrupt *atomic.Int32, lastID
|
|||
)
|
||||
// Override the ETA if larger than the largest until now
|
||||
eta := time.Duration(left/speed) * time.Millisecond
|
||||
log.Info("Indexing state history", "counter", 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
|
||||
|
|
Loading…
Reference in New Issue