2023-04-25 09:14:04 +00:00
|
|
|
// Copyright 2017 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/>.
|
|
|
|
|
2023-03-28 13:04:05 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-04-25 09:14:04 +00:00
|
|
|
"fmt"
|
2023-03-28 13:04:05 +00:00
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
"github.com/crate-crypto/go-ipa/banderwagon"
|
2023-03-28 13:04:05 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-04-25 09:14:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/lru"
|
2023-03-28 13:04:05 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
|
2023-04-20 15:53:37 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-statedb/internal"
|
2023-04-09 17:25:11 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/trie"
|
2024-05-29 10:00:12 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/trie/trienode"
|
|
|
|
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/trie/utils"
|
|
|
|
"github.com/cerc-io/ipld-eth-statedb/trie_by_cid/triedb"
|
2023-03-28 13:04:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Number of codehash->size associations to keep.
|
|
|
|
codeSizeCacheSize = 100000
|
|
|
|
|
|
|
|
// Cache size granted for caching clean code.
|
|
|
|
codeCacheSize = 64 * 1024 * 1024
|
2024-05-29 10:00:12 +00:00
|
|
|
|
|
|
|
// commitmentSize is the size of commitment stored in cache.
|
|
|
|
commitmentSize = banderwagon.UncompressedSize
|
|
|
|
|
|
|
|
// Cache item granted for caching commitment results.
|
|
|
|
commitmentCacheItems = 64 * 1024 * 1024 / (commitmentSize + common.AddressLength)
|
2023-03-28 13:04:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Database wraps access to tries and contract code.
|
|
|
|
type Database interface {
|
|
|
|
// OpenTrie opens the main account trie.
|
|
|
|
OpenTrie(root common.Hash) (Trie, error)
|
|
|
|
|
|
|
|
// OpenStorageTrie opens the storage trie of an account.
|
2024-05-29 10:00:12 +00:00
|
|
|
OpenStorageTrie(stateRoot, addrHash common.Hash, root common.Hash, trie Trie) (Trie, error)
|
2023-04-25 09:14:04 +00:00
|
|
|
|
|
|
|
// CopyTrie returns an independent copy of the given trie.
|
|
|
|
CopyTrie(Trie) Trie
|
2023-03-28 13:04:05 +00:00
|
|
|
|
|
|
|
// ContractCode retrieves a particular contract's code.
|
2024-05-29 10:00:12 +00:00
|
|
|
ContractCode(addr common.Address, codeHash common.Hash) ([]byte, error)
|
2023-03-28 13:04:05 +00:00
|
|
|
|
|
|
|
// ContractCodeSize retrieves a particular contracts code's size.
|
2024-05-29 10:00:12 +00:00
|
|
|
ContractCodeSize(addr common.Address, codeHash common.Hash) (int, error)
|
2023-03-28 13:04:05 +00:00
|
|
|
|
2023-04-25 09:14:04 +00:00
|
|
|
// DiskDB returns the underlying key-value disk database.
|
|
|
|
DiskDB() ethdb.KeyValueStore
|
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
// TrieDB returns the underlying trie database for managing trie nodes.
|
|
|
|
TrieDB() *triedb.Database
|
2023-03-28 13:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trie is a Ethereum Merkle Patricia trie.
|
|
|
|
type Trie interface {
|
2023-04-25 09:14:04 +00:00
|
|
|
// GetKey returns the sha3 preimage of a hashed key that was previously used
|
|
|
|
// to store a value.
|
|
|
|
//
|
|
|
|
// TODO(fjl): remove this when StateTrie is removed
|
|
|
|
GetKey([]byte) []byte
|
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
// GetAccount abstracts an account read from the trie. It retrieves the
|
2023-04-25 09:14:04 +00:00
|
|
|
// account blob from the trie with provided account address and decodes it
|
|
|
|
// with associated decoding algorithm. If the specified account is not in
|
|
|
|
// the trie, nil will be returned. If the trie is corrupted(e.g. some nodes
|
|
|
|
// are missing or the account blob is incorrect for decoding), an error will
|
|
|
|
// be returned.
|
2024-05-29 10:00:12 +00:00
|
|
|
GetAccount(address common.Address) (*types.StateAccount, error)
|
2023-04-25 09:14:04 +00:00
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
// GetStorage returns the value for key stored in the trie. The value bytes
|
|
|
|
// must not be modified by the caller. If a node was not found in the database,
|
|
|
|
// a trie.MissingNodeError is returned.
|
|
|
|
GetStorage(addr common.Address, key []byte) ([]byte, error)
|
2023-04-25 09:14:04 +00:00
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
// UpdateAccount abstracts an account write to the trie. It encodes the
|
2023-04-25 09:14:04 +00:00
|
|
|
// provided account object with associated algorithm and then updates it
|
|
|
|
// in the trie with provided address.
|
2024-05-29 10:00:12 +00:00
|
|
|
UpdateAccount(address common.Address, account *types.StateAccount) error
|
|
|
|
|
|
|
|
// UpdateStorage associates key with value in the trie. If value has length zero,
|
|
|
|
// any existing value is deleted from the trie. The value bytes must not be modified
|
|
|
|
// by the caller while they are stored in the trie. If a node was not found in the
|
|
|
|
// database, a trie.MissingNodeError is returned.
|
|
|
|
UpdateStorage(addr common.Address, key, value []byte) error
|
2023-04-25 09:14:04 +00:00
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
// DeleteAccount abstracts an account deletion from the trie.
|
|
|
|
DeleteAccount(address common.Address) error
|
2023-04-25 09:14:04 +00:00
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
// DeleteStorage removes any existing value for key from the trie. If a node
|
|
|
|
// was not found in the database, a trie.MissingNodeError is returned.
|
|
|
|
DeleteStorage(addr common.Address, key []byte) error
|
|
|
|
|
|
|
|
// UpdateContractCode abstracts code write to the trie. It is expected
|
|
|
|
// to be moved to the stateWriter interface when the latter is ready.
|
|
|
|
UpdateContractCode(address common.Address, codeHash common.Hash, code []byte) error
|
2023-04-25 09:14:04 +00:00
|
|
|
|
|
|
|
// Hash returns the root hash of the trie. It does not write to the database and
|
|
|
|
// can be used even if the trie doesn't have one.
|
2023-03-28 13:04:05 +00:00
|
|
|
Hash() common.Hash
|
2023-04-25 09:14:04 +00:00
|
|
|
|
|
|
|
// Commit collects all dirty nodes in the trie and replace them with the
|
|
|
|
// corresponding node hash. All collected nodes(including dirty leaves if
|
|
|
|
// collectLeaf is true) will be encapsulated into a nodeset for return.
|
|
|
|
// The returned nodeset can be nil if the trie is clean(nothing to commit).
|
|
|
|
// Once the trie is committed, it's not usable anymore. A new trie must
|
|
|
|
// be created with new root and updated trie database for following usage
|
2024-05-29 10:00:12 +00:00
|
|
|
Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error)
|
2023-04-25 09:14:04 +00:00
|
|
|
|
|
|
|
// NodeIterator returns an iterator that returns nodes of the trie. Iteration
|
2024-05-29 10:00:12 +00:00
|
|
|
// starts at the key after the given start key. And error will be returned
|
|
|
|
// if fails to create node iterator.
|
|
|
|
NodeIterator(startKey []byte) (trie.NodeIterator, error)
|
2023-04-25 09:14:04 +00:00
|
|
|
|
|
|
|
// Prove constructs a Merkle proof for key. The result contains all encoded nodes
|
|
|
|
// on the path to the value at key. The value itself is also included in the last
|
|
|
|
// node and can be retrieved by verifying the proof.
|
|
|
|
//
|
|
|
|
// If the trie does not contain a value for key, the returned proof contains all
|
|
|
|
// nodes of the longest existing prefix of the key (at least the root), ending
|
|
|
|
// with the node that proves the absence of the key.
|
2024-05-29 10:00:12 +00:00
|
|
|
Prove(key []byte, proofDb ethdb.KeyValueWriter) error
|
2023-03-28 13:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDatabase creates a backing store for state. The returned database is safe for
|
|
|
|
// concurrent use, but does not retain any recent trie nodes in memory. To keep some
|
|
|
|
// historical state in memory, use the NewDatabaseWithConfig constructor.
|
|
|
|
func NewDatabase(db ethdb.Database) Database {
|
|
|
|
return NewDatabaseWithConfig(db, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDatabaseWithConfig creates a backing store for state. The returned database
|
|
|
|
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
|
|
|
|
// large memory cache.
|
2024-05-29 10:00:12 +00:00
|
|
|
func NewDatabaseWithConfig(db ethdb.Database, config *triedb.Config) Database {
|
2023-03-28 13:04:05 +00:00
|
|
|
return &cachingDB{
|
2023-04-25 09:14:04 +00:00
|
|
|
disk: db,
|
|
|
|
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
|
|
|
|
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
|
2024-05-29 10:00:12 +00:00
|
|
|
triedb: triedb.NewDatabase(db, config),
|
2023-04-25 09:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDatabaseWithNodeDB creates a state database with an already initialized node database.
|
2024-05-29 10:00:12 +00:00
|
|
|
func NewDatabaseWithNodeDB(db ethdb.Database, triedb *triedb.Database) Database {
|
2023-04-25 09:14:04 +00:00
|
|
|
return &cachingDB{
|
|
|
|
disk: db,
|
|
|
|
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
|
|
|
|
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
|
|
|
|
triedb: triedb,
|
2023-03-28 13:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type cachingDB struct {
|
2023-04-25 09:14:04 +00:00
|
|
|
disk ethdb.KeyValueStore
|
|
|
|
codeSizeCache *lru.Cache[common.Hash, int]
|
|
|
|
codeCache *lru.SizeConstrainedCache[common.Hash, []byte]
|
2024-05-29 10:00:12 +00:00
|
|
|
triedb *triedb.Database
|
2023-03-28 13:04:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenTrie opens the main account trie at a specific root hash.
|
|
|
|
func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
|
2024-05-29 10:00:12 +00:00
|
|
|
if db.triedb.IsVerkle() {
|
|
|
|
return trie.NewVerkleTrie(root, db.triedb, utils.NewPointCache(commitmentCacheItems))
|
|
|
|
}
|
|
|
|
tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb)
|
2023-03-28 13:04:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenStorageTrie opens the storage trie of an account.
|
2024-05-29 10:00:12 +00:00
|
|
|
func (db *cachingDB) OpenStorageTrie(stateRoot, addrHash common.Hash, root common.Hash, self Trie) (Trie, error) {
|
|
|
|
// 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 db.triedb.IsVerkle() {
|
|
|
|
return self, nil
|
|
|
|
}
|
|
|
|
tr, err := trie.NewStateTrie(trie.StorageTrieID(stateRoot, addrHash, root), db.triedb)
|
2023-03-28 13:04:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tr, nil
|
|
|
|
}
|
|
|
|
|
2023-04-25 09:14:04 +00:00
|
|
|
// CopyTrie returns an independent copy of the given trie.
|
|
|
|
func (db *cachingDB) CopyTrie(t Trie) Trie {
|
|
|
|
switch t := t.(type) {
|
|
|
|
case *trie.StateTrie:
|
|
|
|
return t.Copy()
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unknown trie type %T", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-28 13:04:05 +00:00
|
|
|
// ContractCode retrieves a particular contract's code.
|
2024-05-29 10:00:12 +00:00
|
|
|
func (db *cachingDB) ContractCode(address common.Address, codeHash common.Hash) ([]byte, error) {
|
2023-04-25 09:14:04 +00:00
|
|
|
code, _ := db.codeCache.Get(codeHash)
|
|
|
|
if len(code) > 0 {
|
2023-03-28 13:04:05 +00:00
|
|
|
return code, nil
|
|
|
|
}
|
2024-05-29 10:00:12 +00:00
|
|
|
code, err := internal.ReadCode(db.disk, codeHash)
|
2023-03-28 13:04:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(code) > 0 {
|
2023-04-25 09:14:04 +00:00
|
|
|
db.codeCache.Add(codeHash, code)
|
2023-03-28 13:04:05 +00:00
|
|
|
db.codeSizeCache.Add(codeHash, len(code))
|
|
|
|
return code, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("not found")
|
|
|
|
}
|
|
|
|
|
2024-05-29 10:00:12 +00:00
|
|
|
// ContractCodeWithPrefix retrieves a particular contract's code. If the
|
|
|
|
// code can't be found in the cache, then check the existence with **new**
|
|
|
|
// db scheme.
|
|
|
|
func (db *cachingDB) ContractCodeWithPrefix(address common.Address, codeHash common.Hash) ([]byte, error) {
|
|
|
|
return db.ContractCode(address, codeHash)
|
|
|
|
}
|
|
|
|
|
2023-03-28 13:04:05 +00:00
|
|
|
// ContractCodeSize retrieves a particular contracts code's size.
|
2024-05-29 10:00:12 +00:00
|
|
|
func (db *cachingDB) ContractCodeSize(addr common.Address, codeHash common.Hash) (int, error) {
|
2023-03-28 13:04:05 +00:00
|
|
|
if cached, ok := db.codeSizeCache.Get(codeHash); ok {
|
2023-04-25 09:14:04 +00:00
|
|
|
return cached, nil
|
2023-03-28 13:04:05 +00:00
|
|
|
}
|
2024-05-29 10:00:12 +00:00
|
|
|
code, err := db.ContractCode(addr, codeHash)
|
2023-03-28 13:04:05 +00:00
|
|
|
return len(code), err
|
|
|
|
}
|
|
|
|
|
2023-04-25 09:14:04 +00:00
|
|
|
// DiskDB returns the underlying key-value disk database.
|
|
|
|
func (db *cachingDB) DiskDB() ethdb.KeyValueStore {
|
|
|
|
return db.disk
|
|
|
|
}
|
|
|
|
|
2023-03-28 13:04:05 +00:00
|
|
|
// TrieDB retrieves any intermediate trie-node caching layer.
|
2024-05-29 10:00:12 +00:00
|
|
|
func (db *cachingDB) TrieDB() *triedb.Database {
|
2023-04-25 09:14:04 +00:00
|
|
|
return db.triedb
|
2023-03-28 13:04:05 +00:00
|
|
|
}
|