2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2015-02-28 21:52:10 +02:00
|
|
|
package trie
|
|
|
|
|
2015-07-06 02:19:48 +03:00
|
|
|
import (
|
2017-02-22 15:10:07 +03:00
|
|
|
"fmt"
|
|
|
|
|
2015-07-06 02:19:48 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-09-28 11:48:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-02-22 15:10:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2021-09-28 11:48:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2015-07-06 02:19:48 +03:00
|
|
|
)
|
2015-03-24 16:14:03 +02:00
|
|
|
|
2022-08-04 17:13:18 +03:00
|
|
|
// SecureTrie is the old name of StateTrie.
|
|
|
|
// Deprecated: use StateTrie.
|
|
|
|
type SecureTrie = StateTrie
|
|
|
|
|
|
|
|
// NewSecure creates a new StateTrie.
|
|
|
|
// Deprecated: use NewStateTrie.
|
|
|
|
func NewSecure(owner common.Hash, root common.Hash, db *Database) (*SecureTrie, error) {
|
|
|
|
return NewStateTrie(owner, root, db)
|
|
|
|
}
|
|
|
|
|
|
|
|
// StateTrie wraps a trie with key hashing. In a secure trie, all
|
2015-07-06 02:19:48 +03:00
|
|
|
// access operations hash the key using keccak256. This prevents
|
|
|
|
// calling code from creating long chains of nodes that
|
|
|
|
// increase the access time.
|
|
|
|
//
|
2022-08-04 17:13:18 +03:00
|
|
|
// Contrary to a regular trie, a StateTrie can only be created with
|
2015-07-06 02:19:48 +03:00
|
|
|
// New and must have an attached database. The database also stores
|
|
|
|
// the preimage of each key.
|
|
|
|
//
|
2022-08-04 17:13:18 +03:00
|
|
|
// StateTrie is not safe for concurrent use.
|
|
|
|
type StateTrie struct {
|
2016-09-27 13:13:13 +03:00
|
|
|
trie Trie
|
2022-07-27 21:37:04 +03:00
|
|
|
preimages *preimageStore
|
2018-02-05 19:40:32 +03:00
|
|
|
hashKeyBuf [common.HashLength]byte
|
2016-09-27 13:13:13 +03:00
|
|
|
secKeyCache map[string][]byte
|
2022-08-04 17:13:18 +03:00
|
|
|
secKeyCacheOwner *StateTrie // Pointer to self, replace the key cache on mismatch
|
2015-02-28 21:52:10 +02:00
|
|
|
}
|
|
|
|
|
2022-08-04 17:13:18 +03:00
|
|
|
// NewStateTrie creates a trie with an existing root node from a backing database
|
2018-02-05 19:40:32 +03:00
|
|
|
// and optional intermediate in-memory node pool.
|
2015-07-06 02:19:48 +03:00
|
|
|
//
|
|
|
|
// If root is the zero hash or the sha3 hash of an empty string, the
|
2015-11-25 19:28:21 +02:00
|
|
|
// trie is initially empty. Otherwise, New will panic if db is nil
|
|
|
|
// and returns MissingNodeError if the root node cannot be found.
|
2016-10-14 19:04:33 +03:00
|
|
|
//
|
2018-02-05 19:40:32 +03:00
|
|
|
// Accessing the trie loads nodes from the database or node pool on demand.
|
2016-10-14 19:04:33 +03:00
|
|
|
// Loaded nodes are kept around until their 'cache generation' expires.
|
|
|
|
// A new cache generation is created by each call to Commit.
|
|
|
|
// cachelimit sets the number of past cache generations to keep.
|
2022-08-04 17:13:18 +03:00
|
|
|
func NewStateTrie(owner common.Hash, root common.Hash, db *Database) (*StateTrie, error) {
|
2015-07-06 02:19:48 +03:00
|
|
|
if db == nil {
|
2018-02-05 19:40:32 +03:00
|
|
|
panic("trie.NewSecure called without a database")
|
2015-07-06 02:19:48 +03:00
|
|
|
}
|
2022-06-06 18:14:55 +03:00
|
|
|
trie, err := New(owner, root, db)
|
2015-07-06 02:19:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-04 17:13:18 +03:00
|
|
|
return &StateTrie{trie: *trie, preimages: db.preimages}, nil
|
2015-02-28 21:52:10 +02:00
|
|
|
}
|
|
|
|
|
2015-07-06 02:19:48 +03:00
|
|
|
// Get returns the value for key stored in the trie.
|
|
|
|
// The value bytes must not be modified by the caller.
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) Get(key []byte) []byte {
|
2015-11-25 19:28:21 +02:00
|
|
|
res, err := t.TryGet(key)
|
2017-02-22 15:10:07 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
|
2015-11-25 19:28:21 +02:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryGet 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 MissingNodeError is returned.
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) TryGet(key []byte) ([]byte, error) {
|
2016-09-25 21:49:02 +03:00
|
|
|
return t.trie.TryGet(t.hashKey(key))
|
2015-02-28 21:52:10 +02:00
|
|
|
}
|
2015-07-06 02:19:48 +03:00
|
|
|
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) TryGetAccount(key []byte) (*types.StateAccount, error) {
|
|
|
|
var ret types.StateAccount
|
|
|
|
res, err := t.trie.TryGet(t.hashKey(key))
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
|
|
|
|
return &ret, err
|
|
|
|
}
|
|
|
|
if res == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
err = rlp.DecodeBytes(res, &ret)
|
|
|
|
return &ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryGetAccountWithPreHashedKey does the same thing as TryGetAccount, however
|
|
|
|
// it expects a key that is already hashed. This constitutes an abstraction leak,
|
|
|
|
// since the client code needs to know the key format.
|
|
|
|
func (t *StateTrie) TryGetAccountWithPreHashedKey(key []byte) (*types.StateAccount, error) {
|
|
|
|
var ret types.StateAccount
|
|
|
|
res, err := t.trie.TryGet(key)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
|
|
|
|
return &ret, err
|
|
|
|
}
|
|
|
|
if res == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
err = rlp.DecodeBytes(res, &ret)
|
|
|
|
return &ret, err
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:50:37 +03:00
|
|
|
// TryGetNode attempts to retrieve a trie node by compact-encoded path. It is not
|
|
|
|
// possible to use keybyte-encoding as the path might contain odd nibbles.
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) TryGetNode(path []byte) ([]byte, int, error) {
|
2020-08-28 10:50:37 +03:00
|
|
|
return t.trie.TryGetNode(path)
|
|
|
|
}
|
|
|
|
|
2022-03-31 10:28:32 +03:00
|
|
|
// TryUpdateAccount account will abstract the write of an account to the
|
2021-09-28 11:48:07 +03:00
|
|
|
// secure trie.
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) TryUpdateAccount(key []byte, acc *types.StateAccount) error {
|
2021-09-28 11:48:07 +03:00
|
|
|
hk := t.hashKey(key)
|
|
|
|
data, err := rlp.EncodeToBytes(acc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := t.trie.TryUpdate(hk, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.getSecKeyCache()[string(hk)] = common.CopyBytes(key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-06 02:19:48 +03:00
|
|
|
// Update associates key with value in the trie. Subsequent calls to
|
|
|
|
// Get will return value. If value has length zero, any existing value
|
|
|
|
// is deleted from the trie and calls to Get will return nil.
|
|
|
|
//
|
|
|
|
// The value bytes must not be modified by the caller while they are
|
|
|
|
// stored in the trie.
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) Update(key, value []byte) {
|
2017-02-22 15:10:07 +03:00
|
|
|
if err := t.TryUpdate(key, value); err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
|
2015-11-25 19:28:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryUpdate associates key with value in the trie. Subsequent calls to
|
|
|
|
// Get will return value. If value has length zero, any existing value
|
|
|
|
// is deleted from the trie and calls to Get will return nil.
|
|
|
|
//
|
|
|
|
// 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 MissingNodeError is returned.
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) TryUpdate(key, value []byte) error {
|
2015-07-06 02:19:48 +03:00
|
|
|
hk := t.hashKey(key)
|
2016-09-25 21:49:02 +03:00
|
|
|
err := t.trie.TryUpdate(hk, value)
|
2015-11-25 19:28:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-27 13:13:13 +03:00
|
|
|
t.getSecKeyCache()[string(hk)] = common.CopyBytes(key)
|
2015-11-25 19:28:21 +02:00
|
|
|
return nil
|
2015-02-28 21:52:10 +02:00
|
|
|
}
|
|
|
|
|
2015-07-06 02:19:48 +03:00
|
|
|
// Delete removes any existing value for key from the trie.
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) Delete(key []byte) {
|
2017-02-22 15:10:07 +03:00
|
|
|
if err := t.TryDelete(key); err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
|
2015-11-25 19:28:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryDelete removes any existing value for key from the trie.
|
|
|
|
// If a node was not found in the database, a MissingNodeError is returned.
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) TryDelete(key []byte) error {
|
2016-01-20 16:06:28 +02:00
|
|
|
hk := t.hashKey(key)
|
2016-09-27 13:13:13 +03:00
|
|
|
delete(t.getSecKeyCache(), string(hk))
|
2016-09-25 21:49:02 +03:00
|
|
|
return t.trie.TryDelete(hk)
|
2015-02-28 21:52:10 +02:00
|
|
|
}
|
2015-07-06 02:19:48 +03:00
|
|
|
|
2022-08-17 14:14:49 +03:00
|
|
|
// TryDeleteACcount abstracts an account deletion from the trie.
|
|
|
|
func (t *StateTrie) TryDeleteAccount(key []byte) error {
|
|
|
|
hk := t.hashKey(key)
|
|
|
|
delete(t.getSecKeyCache(), string(hk))
|
|
|
|
return t.trie.TryDelete(hk)
|
|
|
|
}
|
|
|
|
|
2015-07-06 02:19:48 +03:00
|
|
|
// GetKey returns the sha3 preimage of a hashed key that was
|
|
|
|
// previously used to store a value.
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) GetKey(shaKey []byte) []byte {
|
2016-09-27 13:13:13 +03:00
|
|
|
if key, ok := t.getSecKeyCache()[string(shaKey)]; ok {
|
2016-01-20 12:09:24 +02:00
|
|
|
return key
|
|
|
|
}
|
2022-07-27 21:37:04 +03:00
|
|
|
if t.preimages == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return t.preimages.preimage(common.BytesToHash(shaKey))
|
2015-02-28 21:52:10 +02:00
|
|
|
}
|
2015-03-03 13:25:44 +02:00
|
|
|
|
2022-08-04 11:03:20 +03: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).
|
|
|
|
// All cached preimages will be also flushed if preimages recording is enabled.
|
|
|
|
// 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
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *NodeSet, error) {
|
2018-02-05 19:40:32 +03:00
|
|
|
// Write all the pre-images to the actual disk database
|
|
|
|
if len(t.getSecKeyCache()) > 0 {
|
2022-07-27 21:37:04 +03:00
|
|
|
if t.preimages != nil {
|
|
|
|
preimages := make(map[common.Hash][]byte)
|
2020-11-18 12:51:33 +03:00
|
|
|
for hk, key := range t.secKeyCache {
|
2022-07-27 21:37:04 +03:00
|
|
|
preimages[common.BytesToHash([]byte(hk))] = key
|
2020-11-18 12:51:33 +03:00
|
|
|
}
|
2022-07-27 21:37:04 +03:00
|
|
|
t.preimages.insertPreimage(preimages)
|
2018-02-05 19:40:32 +03:00
|
|
|
}
|
|
|
|
t.secKeyCache = make(map[string][]byte)
|
|
|
|
}
|
|
|
|
// Commit the trie to its intermediate node database
|
2022-08-04 11:03:20 +03:00
|
|
|
return t.trie.Commit(collectLeaf)
|
2016-09-25 21:49:02 +03:00
|
|
|
}
|
|
|
|
|
2022-08-04 17:13:18 +03:00
|
|
|
// Hash returns the root hash of StateTrie. It does not write to the
|
2018-05-21 23:41:31 +03:00
|
|
|
// database and can be used even if the trie doesn't have one.
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) Hash() common.Hash {
|
2016-09-25 21:49:02 +03:00
|
|
|
return t.trie.Hash()
|
|
|
|
}
|
|
|
|
|
2022-08-04 17:13:18 +03:00
|
|
|
// Copy returns a copy of StateTrie.
|
|
|
|
func (t *StateTrie) Copy() *StateTrie {
|
|
|
|
return &StateTrie{
|
2022-03-31 10:28:32 +03:00
|
|
|
trie: *t.trie.Copy(),
|
2022-07-27 21:37:04 +03:00
|
|
|
preimages: t.preimages,
|
2022-03-31 10:28:32 +03:00
|
|
|
secKeyCache: t.secKeyCache,
|
|
|
|
}
|
2017-06-27 16:57:06 +03:00
|
|
|
}
|
|
|
|
|
2017-04-13 15:41:24 +03:00
|
|
|
// NodeIterator returns an iterator that returns nodes of the underlying trie. Iteration
|
|
|
|
// starts at the key after the given start key.
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) NodeIterator(start []byte) NodeIterator {
|
2017-04-13 15:41:24 +03:00
|
|
|
return t.trie.NodeIterator(start)
|
2016-01-20 12:09:24 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 21:49:02 +03:00
|
|
|
// hashKey returns the hash of key as an ephemeral buffer.
|
|
|
|
// The caller must not hold onto the return value because it will become
|
|
|
|
// invalid on the next call to hashKey or secKey.
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) hashKey(key []byte) []byte {
|
2020-02-04 15:02:38 +03:00
|
|
|
h := newHasher(false)
|
2018-01-15 16:32:14 +03:00
|
|
|
h.sha.Reset()
|
|
|
|
h.sha.Write(key)
|
2020-07-16 15:06:19 +03:00
|
|
|
h.sha.Read(t.hashKeyBuf[:])
|
2018-01-15 16:32:14 +03:00
|
|
|
returnHasherToPool(h)
|
2020-07-16 15:06:19 +03:00
|
|
|
return t.hashKeyBuf[:]
|
2015-03-24 16:14:03 +02:00
|
|
|
}
|
2016-09-27 13:13:13 +03:00
|
|
|
|
|
|
|
// getSecKeyCache returns the current secure key cache, creating a new one if
|
|
|
|
// ownership changed (i.e. the current secure trie is a copy of another owning
|
|
|
|
// the actual cache).
|
2022-08-04 17:13:18 +03:00
|
|
|
func (t *StateTrie) getSecKeyCache() map[string][]byte {
|
2016-09-27 13:13:13 +03:00
|
|
|
if t != t.secKeyCacheOwner {
|
|
|
|
t.secKeyCacheOwner = t
|
|
|
|
t.secKeyCache = make(map[string][]byte)
|
|
|
|
}
|
|
|
|
return t.secKeyCache
|
|
|
|
}
|