2017-06-27 15:57:06 +02: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/>.
|
|
|
|
|
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2020-08-21 20:10:40 +08:00
|
|
|
"errors"
|
2017-06-27 15:57:06 +02:00
|
|
|
"fmt"
|
|
|
|
|
2024-08-09 17:29:47 +08:00
|
|
|
versa "github.com/bnb-chain/versioned-state-database"
|
2023-11-14 13:09:40 +01:00
|
|
|
"github.com/crate-crypto/go-ipa/banderwagon"
|
2017-06-27 15:57:06 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-11-14 15:41:56 +01:00
|
|
|
"github.com/ethereum/go-ethereum/common/lru"
|
2020-08-21 20:10:40 +08:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2021-09-28 10:48:07 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 11:11:02 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-06-27 15:57:06 +02:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2023-05-09 15:11:04 +08:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
2023-11-14 13:09:40 +01:00
|
|
|
"github.com/ethereum/go-ethereum/trie/utils"
|
2024-02-13 21:49:53 +08:00
|
|
|
"github.com/ethereum/go-ethereum/triedb"
|
2017-06-27 15:57:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Number of codehash->size associations to keep.
|
|
|
|
codeSizeCacheSize = 100000
|
2020-08-21 20:10:40 +08:00
|
|
|
|
|
|
|
// Cache size granted for caching clean code.
|
|
|
|
codeCacheSize = 64 * 1024 * 1024
|
2023-11-14 13:09:40 +01: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)
|
2017-06-27 15:57:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Database wraps access to tries and contract code.
|
|
|
|
type Database interface {
|
|
|
|
// OpenTrie opens the main account trie.
|
|
|
|
OpenTrie(root common.Hash) (Trie, error)
|
2018-02-05 18:40:32 +02:00
|
|
|
|
|
|
|
// OpenStorageTrie opens the storage trie of an account.
|
2023-11-14 13:09:40 +01:00
|
|
|
OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash, trie Trie) (Trie, error)
|
2018-02-05 18:40:32 +02:00
|
|
|
|
2017-06-27 15:57:06 +02:00
|
|
|
// CopyTrie returns an independent copy of the given trie.
|
|
|
|
CopyTrie(Trie) Trie
|
2018-02-05 18:40:32 +02:00
|
|
|
|
|
|
|
// ContractCode retrieves a particular contract's code.
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 11:11:02 +02:00
|
|
|
ContractCode(addr common.Address, codeHash common.Hash) ([]byte, error)
|
2018-02-05 18:40:32 +02:00
|
|
|
|
|
|
|
// ContractCodeSize retrieves a particular contracts code's size.
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 11:11:02 +02:00
|
|
|
ContractCodeSize(addr common.Address, codeHash common.Hash) (int, error)
|
2018-02-05 18:40:32 +02:00
|
|
|
|
2022-09-07 15:08:56 +08:00
|
|
|
// DiskDB returns the underlying key-value disk database.
|
|
|
|
DiskDB() ethdb.KeyValueStore
|
2018-02-05 18:40:32 +02:00
|
|
|
|
all: activate pbss as experimental feature from eth (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-11 03:21:36 +08:00
|
|
|
// TrieDB returns the underlying trie database for managing trie nodes.
|
2024-02-13 21:49:53 +08:00
|
|
|
TrieDB() *triedb.Database
|
2022-07-05 11:14:21 +08:00
|
|
|
|
2024-08-09 17:29:47 +08:00
|
|
|
// Scheme returns triedb scheme, used to distinguish version triedb.
|
|
|
|
Scheme() string
|
|
|
|
|
|
|
|
// Flush used for version caching versa db to commit block state data.
|
|
|
|
Flush() error
|
|
|
|
|
|
|
|
// Release used for caching versa db to release resource.
|
|
|
|
Release() error
|
|
|
|
|
|
|
|
// Reset used for caching versa db to clean up meta data.
|
|
|
|
Reset()
|
|
|
|
|
|
|
|
// Copy used for caching versa db to copy db, main to transfer triedb with rw mode.
|
|
|
|
Copy() Database
|
|
|
|
|
|
|
|
// HasState returns the state data whether in the triedb.
|
|
|
|
HasState(root common.Hash) bool
|
|
|
|
|
|
|
|
// HasTreeExpired used for caching versa db, whether the state where the opened tree resides has been closed
|
|
|
|
HasTreeExpired(tr Trie) bool
|
|
|
|
|
2022-07-05 11:14:21 +08:00
|
|
|
// NoTries returns whether the database has tries storage.
|
|
|
|
NoTries() bool
|
2024-08-09 17:29:47 +08:00
|
|
|
|
|
|
|
SetVersion(version int64)
|
|
|
|
|
|
|
|
GetVersion() int64
|
2017-06-27 15:57:06 +02:00
|
|
|
}
|
|
|
|
|
2019-03-14 15:25:12 +02:00
|
|
|
// Trie is a Ethereum Merkle Patricia trie.
|
2017-06-27 15:57:06 +02:00
|
|
|
type Trie interface {
|
2019-03-14 15:25:12 +02:00
|
|
|
// GetKey returns the sha3 preimage of a hashed key that was previously used
|
|
|
|
// to store a value.
|
|
|
|
//
|
2022-08-04 16:13:18 +02:00
|
|
|
// TODO(fjl): remove this when StateTrie is removed
|
2019-03-14 15:25:12 +02:00
|
|
|
GetKey([]byte) []byte
|
|
|
|
|
2023-03-27 10:48:46 +02:00
|
|
|
// GetAccount abstracts an account read from the trie. It retrieves the
|
2023-01-03 21:41:40 +08: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.
|
2023-03-27 10:48:46 +02:00
|
|
|
GetAccount(address common.Address) (*types.StateAccount, error)
|
2021-09-28 10:48:07 +02:00
|
|
|
|
2023-11-14 13:09:40 +01: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)
|
2019-03-14 15:25:12 +02:00
|
|
|
|
2023-03-27 10:48:46 +02:00
|
|
|
// UpdateAccount abstracts an account write to the trie. It encodes the
|
2023-01-03 21:41:40 +08:00
|
|
|
// provided account object with associated algorithm and then updates it
|
|
|
|
// in the trie with provided address.
|
2023-03-27 10:48:46 +02:00
|
|
|
UpdateAccount(address common.Address, account *types.StateAccount) error
|
2022-08-04 16:13:18 +02:00
|
|
|
|
2023-11-14 13:09:40 +01:00
|
|
|
// 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
|
|
|
|
|
|
|
|
// DeleteAccount abstracts an account deletion from the trie.
|
|
|
|
DeleteAccount(address common.Address) error
|
2023-06-22 14:52:52 +02:00
|
|
|
|
2023-03-27 10:48:46 +02:00
|
|
|
// DeleteStorage removes any existing value for key from the trie. If a node
|
2023-03-23 11:52:22 +01:00
|
|
|
// was not found in the database, a trie.MissingNodeError is returned.
|
2023-03-27 10:48:46 +02:00
|
|
|
DeleteStorage(addr common.Address, key []byte) error
|
2019-03-14 15:25:12 +02:00
|
|
|
|
2023-11-14 13:09:40 +01:00
|
|
|
// 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
|
2019-03-14 15:25:12 +02: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.
|
2017-06-27 15:57:06 +02:00
|
|
|
Hash() common.Hash
|
2019-03-14 15:25:12 +02:00
|
|
|
|
2022-08-04 16:03:20 +08: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
|
2023-06-27 14:36:38 +02:00
|
|
|
Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error)
|
2019-03-14 15:25:12 +02:00
|
|
|
|
|
|
|
// NodeIterator returns an iterator that returns nodes of the trie. Iteration
|
cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
2023-06-21 03:31:45 +08: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)
|
2019-03-14 15:25:12 +02: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.
|
2023-06-19 22:28:40 +08:00
|
|
|
Prove(key []byte, proofDb ethdb.KeyValueWriter) error
|
2017-06-27 15:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDatabase creates a backing store for state. The returned database is safe for
|
2019-03-14 15:25:12 +02:00
|
|
|
// concurrent use, but does not retain any recent trie nodes in memory. To keep some
|
2020-11-18 17:51:33 +08:00
|
|
|
// historical state in memory, use the NewDatabaseWithConfig constructor.
|
2017-06-27 15:57:06 +02:00
|
|
|
func NewDatabase(db ethdb.Database) Database {
|
2024-08-09 17:29:47 +08:00
|
|
|
return NewDatabaseWithConfig(db, nil, false)
|
2018-11-12 18:47:34 +02:00
|
|
|
}
|
|
|
|
|
2020-11-18 17:51:33 +08:00
|
|
|
// NewDatabaseWithConfig creates a backing store for state. The returned database
|
2019-03-14 15:25:12 +02:00
|
|
|
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
|
|
|
|
// large memory cache.
|
2024-08-09 17:29:47 +08:00
|
|
|
func NewDatabaseWithConfig(db ethdb.Database, config *triedb.Config, needCommit bool) Database {
|
2022-07-05 11:14:21 +08:00
|
|
|
noTries := config != nil && config.NoTries
|
2023-08-23 17:46:08 +08:00
|
|
|
|
2024-08-09 17:29:47 +08:00
|
|
|
triedb := triedb.NewDatabase(db, config)
|
|
|
|
if triedb.Scheme() == rawdb.VersionScheme {
|
|
|
|
if needCommit {
|
|
|
|
return NewVersaDatabase(db, triedb, versa.S_COMMIT)
|
|
|
|
}
|
|
|
|
return NewVersaDatabase(db, triedb, versa.S_RW)
|
|
|
|
}
|
|
|
|
|
2018-02-05 18:40:32 +02:00
|
|
|
return &cachingDB{
|
2022-09-07 15:08:56 +08:00
|
|
|
disk: db,
|
2022-11-14 15:41:56 +01:00
|
|
|
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
|
|
|
|
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
|
2024-08-09 17:29:47 +08:00
|
|
|
triedb: triedb,
|
2023-08-23 17:46:08 +08:00
|
|
|
noTries: noTries,
|
2022-11-28 21:31:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDatabaseWithNodeDB creates a state database with an already initialized node database.
|
2024-08-09 17:29:47 +08:00
|
|
|
func NewDatabaseWithNodeDB(db ethdb.Database, triedb *triedb.Database, needCommit bool) Database {
|
2023-09-07 16:39:29 +08:00
|
|
|
noTries := triedb != nil && triedb.Config() != nil && triedb.Config().NoTries
|
2023-08-23 17:46:08 +08:00
|
|
|
|
2024-08-09 17:29:47 +08:00
|
|
|
if triedb.Scheme() == rawdb.VersionScheme {
|
|
|
|
if needCommit {
|
|
|
|
return NewVersaDatabase(db, triedb, versa.S_COMMIT)
|
|
|
|
}
|
|
|
|
return NewVersaDatabase(db, triedb, versa.S_RW)
|
|
|
|
}
|
|
|
|
|
2022-11-28 21:31:28 +08:00
|
|
|
return &cachingDB{
|
|
|
|
disk: db,
|
|
|
|
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
|
|
|
|
codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
|
|
|
|
triedb: triedb,
|
2022-07-05 11:14:21 +08:00
|
|
|
noTries: noTries,
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
2017-06-27 15:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type cachingDB struct {
|
2023-10-30 10:29:54 +08:00
|
|
|
disk ethdb.KeyValueStore
|
|
|
|
codeSizeCache *lru.Cache[common.Hash, int]
|
|
|
|
codeCache *lru.SizeConstrainedCache[common.Hash, []byte]
|
2024-02-13 21:49:53 +08:00
|
|
|
triedb *triedb.Database
|
2023-10-30 10:29:54 +08:00
|
|
|
noTries bool
|
2024-08-09 17:29:47 +08:00
|
|
|
|
|
|
|
debug *DebugHashState
|
2017-06-27 15:57:06 +02:00
|
|
|
}
|
|
|
|
|
2019-03-14 15:25:12 +02:00
|
|
|
// OpenTrie opens the main account trie at a specific root hash.
|
2017-06-27 15:57:06 +02:00
|
|
|
func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
|
2022-07-05 11:14:21 +08:00
|
|
|
if db.noTries {
|
|
|
|
return trie.NewEmptyTrie(), nil
|
|
|
|
}
|
2023-11-14 13:09:40 +01:00
|
|
|
if db.triedb.IsVerkle() {
|
|
|
|
return trie.NewVerkleTrie(root, db.triedb, utils.NewPointCache(commitmentCacheItems))
|
|
|
|
}
|
2022-11-28 21:31:28 +08:00
|
|
|
tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb)
|
2020-02-05 13:12:09 +01:00
|
|
|
if err != nil {
|
2024-08-09 17:29:47 +08:00
|
|
|
if db.debug != nil {
|
|
|
|
db.debug.OnError(fmt.Errorf("failed to open tree, root: %s, error: %s", root.String(), err.Error()))
|
|
|
|
}
|
2020-02-05 13:12:09 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-09 17:29:47 +08:00
|
|
|
ht := &HashTrie{
|
|
|
|
trie: tr,
|
|
|
|
root: root,
|
|
|
|
address: common.Address{},
|
|
|
|
owner: common.Hash{},
|
|
|
|
debug: db.debug,
|
|
|
|
}
|
|
|
|
if db.debug != nil {
|
|
|
|
db.debug.OnOpenTree(root, common.Hash{}, common.Address{})
|
|
|
|
}
|
|
|
|
return ht, nil
|
2017-06-27 15:57:06 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 18:40:32 +02:00
|
|
|
// OpenStorageTrie opens the storage trie of an account.
|
2023-11-14 13:09:40 +01:00
|
|
|
func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash, self Trie) (Trie, error) {
|
2022-07-05 11:14:21 +08:00
|
|
|
if db.noTries {
|
|
|
|
return trie.NewEmptyTrie(), nil
|
|
|
|
}
|
|
|
|
|
2023-11-14 13:09:40 +01:00
|
|
|
// 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
|
|
|
|
}
|
2024-08-09 17:29:47 +08:00
|
|
|
owner := crypto.Keccak256Hash(address.Bytes())
|
|
|
|
tr, err := trie.NewStateTrie(trie.StorageTrieID(stateRoot, owner, root), db.triedb)
|
2020-02-05 13:12:09 +01:00
|
|
|
if err != nil {
|
2024-08-09 17:29:47 +08:00
|
|
|
if db.debug != nil {
|
|
|
|
db.debug.OnError(fmt.Errorf("failed to storage open tree, stateRoot: %s, address: %s, root: %s, error: %s",
|
|
|
|
stateRoot.String(), address.String(), root.String(), err.Error()))
|
|
|
|
}
|
2020-02-05 13:12:09 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-08-09 17:29:47 +08:00
|
|
|
ht := &HashTrie{
|
|
|
|
trie: tr,
|
|
|
|
root: stateRoot,
|
|
|
|
statRoot: root,
|
|
|
|
address: address,
|
|
|
|
owner: owner,
|
|
|
|
debug: db.debug,
|
|
|
|
}
|
|
|
|
if db.debug != nil {
|
|
|
|
db.debug.OnOpenTree(root, owner, address)
|
|
|
|
}
|
|
|
|
return ht, nil
|
2017-06-27 15:57:06 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 11:14:21 +08:00
|
|
|
func (db *cachingDB) NoTries() bool {
|
|
|
|
return db.noTries
|
|
|
|
}
|
|
|
|
|
2018-02-05 18:40:32 +02:00
|
|
|
// CopyTrie returns an independent copy of the given trie.
|
2017-06-27 15:57:06 +02:00
|
|
|
func (db *cachingDB) CopyTrie(t Trie) Trie {
|
2022-07-05 11:14:21 +08:00
|
|
|
if t == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-27 15:57:06 +02:00
|
|
|
switch t := t.(type) {
|
2022-08-04 16:13:18 +02:00
|
|
|
case *trie.StateTrie:
|
2017-06-27 15:57:06 +02:00
|
|
|
return t.Copy()
|
2022-07-05 11:14:21 +08:00
|
|
|
case *trie.EmptyTrie:
|
|
|
|
return t.Copy()
|
2024-08-09 17:29:47 +08:00
|
|
|
case *HashTrie:
|
|
|
|
return db.CopyTrie(t.trie)
|
2017-06-27 15:57:06 +02:00
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unknown trie type %T", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 18:40:32 +02:00
|
|
|
// ContractCode retrieves a particular contract's code.
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 11:11:02 +02:00
|
|
|
func (db *cachingDB) ContractCode(address common.Address, codeHash common.Hash) ([]byte, error) {
|
2024-08-09 17:29:47 +08:00
|
|
|
if db.debug != nil {
|
|
|
|
db.debug.OnGetCode(address, codeHash)
|
|
|
|
}
|
2022-11-14 15:41:56 +01:00
|
|
|
code, _ := db.codeCache.Get(codeHash)
|
|
|
|
if len(code) > 0 {
|
2020-08-21 20:10:40 +08:00
|
|
|
return code, nil
|
|
|
|
}
|
2022-11-14 15:41:56 +01:00
|
|
|
code = rawdb.ReadCode(db.disk, codeHash)
|
2020-08-21 20:10:40 +08:00
|
|
|
if len(code) > 0 {
|
2022-07-05 11:14:21 +08:00
|
|
|
db.codeCache.Add(codeHash, code)
|
2020-08-21 20:10:40 +08:00
|
|
|
db.codeSizeCache.Add(codeHash, len(code))
|
|
|
|
return code, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 11:11:02 +02:00
|
|
|
func (db *cachingDB) ContractCodeWithPrefix(address common.Address, codeHash common.Hash) ([]byte, error) {
|
2022-11-14 15:41:56 +01:00
|
|
|
code, _ := db.codeCache.Get(codeHash)
|
|
|
|
if len(code) > 0 {
|
2020-08-21 20:10:40 +08:00
|
|
|
return code, nil
|
|
|
|
}
|
2022-11-14 15:41:56 +01:00
|
|
|
code = rawdb.ReadCodeWithPrefix(db.disk, codeHash)
|
2020-08-21 20:10:40 +08:00
|
|
|
if len(code) > 0 {
|
2022-07-05 11:14:21 +08:00
|
|
|
db.codeCache.Add(codeHash, code)
|
2017-06-27 15:57:06 +02:00
|
|
|
db.codeSizeCache.Add(codeHash, len(code))
|
2020-08-21 20:10:40 +08:00
|
|
|
return code, nil
|
2017-06-27 15:57:06 +02:00
|
|
|
}
|
2020-08-21 20:10:40 +08:00
|
|
|
return nil, errors.New("not found")
|
2017-06-27 15:57:06 +02:00
|
|
|
}
|
|
|
|
|
2018-02-05 18:40:32 +02:00
|
|
|
// ContractCodeSize retrieves a particular contracts code's size.
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 11:11:02 +02:00
|
|
|
func (db *cachingDB) ContractCodeSize(addr common.Address, codeHash common.Hash) (int, error) {
|
2017-06-27 15:57:06 +02:00
|
|
|
if cached, ok := db.codeSizeCache.Get(codeHash); ok {
|
2022-11-14 15:41:56 +01:00
|
|
|
return cached, nil
|
2017-06-27 15:57:06 +02:00
|
|
|
}
|
core/state, light, les: make signature of ContractCode hash-independent (#27209)
* core/state, light, les: make signature of ContractCode hash-independent
* push current state for feedback
* les: fix unit test
* core, les, light: fix les unittests
* core/state, trie, les, light: fix state iterator
* core, les: address comments
* les: fix lint
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2023-06-28 11:11:02 +02:00
|
|
|
code, err := db.ContractCode(addr, codeHash)
|
2017-06-27 15:57:06 +02:00
|
|
|
return len(code), err
|
|
|
|
}
|
|
|
|
|
2022-09-07 15:08:56 +08:00
|
|
|
// DiskDB returns the underlying key-value disk database.
|
|
|
|
func (db *cachingDB) DiskDB() ethdb.KeyValueStore {
|
|
|
|
return db.disk
|
|
|
|
}
|
|
|
|
|
2018-02-05 18:40:32 +02:00
|
|
|
// TrieDB retrieves any intermediate trie-node caching layer.
|
2024-02-13 21:49:53 +08:00
|
|
|
func (db *cachingDB) TrieDB() *triedb.Database {
|
2022-11-28 21:31:28 +08:00
|
|
|
return db.triedb
|
2018-02-05 18:40:32 +02:00
|
|
|
}
|
2024-08-09 17:29:47 +08:00
|
|
|
|
|
|
|
func (db *cachingDB) Reset() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *cachingDB) Scheme() string {
|
|
|
|
return db.triedb.Scheme()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *cachingDB) Flush() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *cachingDB) Release() error {
|
|
|
|
db.debug.flush()
|
|
|
|
db.debug = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *cachingDB) SetVersion(version int64) {
|
|
|
|
db.debug = NewDebugHashState(db.disk)
|
|
|
|
db.debug.Version = version
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *cachingDB) GetVersion() int64 {
|
|
|
|
return db.debug.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *cachingDB) Copy() Database {
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *cachingDB) HasState(root common.Hash) bool {
|
|
|
|
_, err := db.OpenTrie(root)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *cachingDB) HasTreeExpired(_ Trie) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type HashTrie struct {
|
|
|
|
trie Trie
|
|
|
|
root common.Hash
|
|
|
|
statRoot common.Hash
|
|
|
|
address common.Address
|
|
|
|
owner common.Hash
|
|
|
|
|
|
|
|
debug *DebugHashState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *HashTrie) GetKey(key []byte) []byte {
|
|
|
|
return ht.trie.GetKey(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *HashTrie) GetAccount(address common.Address) (*types.StateAccount, error) {
|
|
|
|
acc, err := ht.trie.GetAccount(address)
|
|
|
|
if err != nil {
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnError(fmt.Errorf("failed to get account, address: %s, error: %s", address.String(), err.Error()))
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnGetAccount(address, acc)
|
|
|
|
}
|
|
|
|
return acc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *HashTrie) GetStorage(addr common.Address, key []byte) ([]byte, error) {
|
|
|
|
val, err := ht.trie.GetStorage(addr, key)
|
|
|
|
if err != nil {
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnError(fmt.Errorf("failed to get storage, address: %s, error: %s", addr.String(), err.Error()))
|
|
|
|
}
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnGetStorage(addr, key, val)
|
|
|
|
}
|
|
|
|
return val, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *HashTrie) UpdateAccount(address common.Address, account *types.StateAccount) error {
|
|
|
|
err := ht.trie.UpdateAccount(address, account)
|
|
|
|
if err != nil {
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnError(fmt.Errorf("failed to update account, address: %s, account: %s, error: %s",
|
|
|
|
address.String(), account.String(), err.Error()))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnUpdateAccount(address, account)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *HashTrie) UpdateStorage(addr common.Address, key, value []byte) error {
|
|
|
|
err := ht.trie.UpdateStorage(addr, key, value)
|
|
|
|
if err != nil {
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnError(fmt.Errorf("failed to update storage, address: %s, key: %s, val: %s, error: %s",
|
|
|
|
addr.String(), common.Bytes2Hex(key), common.Bytes2Hex(value), err.Error()))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnUpdateStorage(addr, key, value)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *HashTrie) DeleteAccount(address common.Address) error {
|
|
|
|
err := ht.trie.DeleteAccount(address)
|
|
|
|
if err != nil {
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnError(fmt.Errorf("failed to delete account, address: %s, error: %s", address.String(), err.Error()))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnDeleteAccount(address)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *HashTrie) DeleteStorage(addr common.Address, key []byte) error {
|
|
|
|
err := ht.trie.DeleteStorage(addr, key)
|
|
|
|
if err != nil {
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnError(fmt.Errorf("failed to update storage, address: %s, key: %s, error: %s",
|
|
|
|
addr.String(), common.Bytes2Hex(key), err.Error()))
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnDeleteStorage(addr, key)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *HashTrie) UpdateContractCode(address common.Address, codeHash common.Hash, code []byte) error {
|
|
|
|
return ht.trie.UpdateContractCode(address, codeHash, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *HashTrie) Hash() common.Hash {
|
|
|
|
root := ht.trie.Hash()
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnCalcHash(ht.address, root)
|
|
|
|
}
|
|
|
|
return root
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *HashTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error) {
|
|
|
|
hash, set, err := ht.trie.Commit(collectLeaf)
|
|
|
|
if err != nil {
|
|
|
|
ht.debug.OnError(fmt.Errorf("failed to commit tree, address: %s, error: %s",
|
|
|
|
ht.address.String(), err.Error()))
|
|
|
|
return hash, set, err
|
|
|
|
}
|
|
|
|
if ht.debug != nil {
|
|
|
|
ht.debug.OnCalcHash(ht.address, hash)
|
|
|
|
ht.debug.OnCommitTree(ht.address, hash)
|
|
|
|
}
|
|
|
|
return hash, set, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *HashTrie) NodeIterator(startKey []byte) (trie.NodeIterator, error) {
|
|
|
|
return ht.trie.NodeIterator(startKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *HashTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
|
|
|
|
return ht.trie.Prove(key, proofDb)
|
|
|
|
}
|