2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00: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 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00: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 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-07-07 03:08:16 +00:00
|
|
|
// Package trie implements Merkle Patricia Tries.
|
2014-10-31 13:45:03 +00:00
|
|
|
package trie
|
2014-02-14 22:56:09 +00:00
|
|
|
|
|
|
|
import (
|
2014-07-02 15:47:18 +00:00
|
|
|
"bytes"
|
2020-12-14 09:27:15 +00:00
|
|
|
"errors"
|
2014-02-14 22:56:09 +00:00
|
|
|
"fmt"
|
2014-08-04 08:38:18 +00:00
|
|
|
|
2015-03-16 10:27:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-02-21 11:12:27 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-02-22 12:10:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2023-05-09 07:11:04 +00:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
2014-02-14 22:56:09 +00:00
|
|
|
)
|
|
|
|
|
2022-08-04 08:03:20 +00:00
|
|
|
// Trie is a Merkle Patricia Trie. Use New to create a trie that sits on
|
|
|
|
// top of a database. Whenever trie performs a commit operation, the generated
|
|
|
|
// nodes will be gathered and returned in a set. Once the trie is committed,
|
|
|
|
// it's not usable anymore. Callers have to re-create the trie with new root
|
|
|
|
// based on the updated trie database.
|
2015-07-05 23:19:48 +00:00
|
|
|
//
|
|
|
|
// Trie is not safe for concurrent use.
|
|
|
|
type Trie struct {
|
2022-06-06 15:14:55 +00:00
|
|
|
root node
|
|
|
|
owner common.Hash
|
2022-03-31 07:28:32 +00:00
|
|
|
|
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-20 19:31:45 +00:00
|
|
|
// Flag whether the commit operation is already performed. If so the
|
|
|
|
// trie is not usable(latest states is invisible).
|
|
|
|
committed bool
|
|
|
|
|
2022-03-31 07:28:32 +00:00
|
|
|
// Keep track of the number leaves which have been inserted since the last
|
2020-02-04 12:02:38 +00:00
|
|
|
// hashing operation. This number will not directly map to the number of
|
2022-08-04 08:03:20 +00:00
|
|
|
// actually unhashed nodes.
|
2020-02-04 12:02:38 +00:00
|
|
|
unhashed int
|
2022-03-31 07:28:32 +00:00
|
|
|
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
// reader is the handler trie can retrieve nodes from.
|
|
|
|
reader *trieReader
|
2023-02-20 14:54:52 +00:00
|
|
|
|
|
|
|
// tracer is the tool to track the trie changes.
|
|
|
|
// It will be reset after each commit operation.
|
|
|
|
tracer *tracer
|
2016-10-14 16:04:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newFlag returns the cache flag value for a newly created node.
|
|
|
|
func (t *Trie) newFlag() nodeFlag {
|
2019-03-14 13:25:12 +00:00
|
|
|
return nodeFlag{dirty: true}
|
2014-02-24 11:11:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 07:28:32 +00:00
|
|
|
// Copy returns a copy of Trie.
|
|
|
|
func (t *Trie) Copy() *Trie {
|
|
|
|
return &Trie{
|
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-20 19:31:45 +00:00
|
|
|
root: t.root,
|
|
|
|
owner: t.owner,
|
|
|
|
committed: t.committed,
|
|
|
|
unhashed: t.unhashed,
|
|
|
|
reader: t.reader,
|
|
|
|
tracer: t.tracer.copy(),
|
2022-03-31 07:28:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
// New creates the trie instance with provided trie id and the read-only
|
|
|
|
// database. The state specified by trie id must be available, otherwise
|
|
|
|
// an error will be returned. The trie root specified by trie id can be
|
|
|
|
// zero hash or the sha3 hash of an empty string, then trie is initially
|
|
|
|
// empty, otherwise, the root node must be present in database or returns
|
|
|
|
// a MissingNodeError if not.
|
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-20 19:31:45 +00:00
|
|
|
func New(id *ID, db *Database) (*Trie, error) {
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
reader, err := newTrieReader(id.StateRoot, id.Owner, db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-05 16:40:32 +00:00
|
|
|
trie := &Trie{
|
2023-02-20 14:54:52 +00:00
|
|
|
owner: id.Owner,
|
|
|
|
reader: reader,
|
2023-03-14 08:50:53 +00:00
|
|
|
tracer: newTracer(),
|
2018-02-05 16:40:32 +00:00
|
|
|
}
|
2023-02-21 11:12:27 +00:00
|
|
|
if id.Root != (common.Hash{}) && id.Root != types.EmptyRootHash {
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
rootnode, err := trie.resolveAndTrack(id.Root[:], nil)
|
2016-10-17 19:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
2016-10-17 19:31:27 +00:00
|
|
|
trie.root = rootnode
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
return trie, nil
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 08:03:20 +00:00
|
|
|
// NewEmpty is a shortcut to create empty tree. It's mostly used in tests.
|
|
|
|
func NewEmpty(db *Database) *Trie {
|
2023-05-09 07:11:04 +00:00
|
|
|
tr, _ := New(TrieID(types.EmptyRootHash), db)
|
2022-08-04 08:03:20 +00:00
|
|
|
return tr
|
|
|
|
}
|
|
|
|
|
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-20 19:31:45 +00:00
|
|
|
// MustNodeIterator is a wrapper of NodeIterator and will omit any encountered
|
|
|
|
// error but just print out an error message.
|
|
|
|
func (t *Trie) MustNodeIterator(start []byte) NodeIterator {
|
|
|
|
it, err := t.NodeIterator(start)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unhandled trie error in Trie.NodeIterator", "err", err)
|
|
|
|
}
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
|
2017-04-13 12:41:24 +00:00
|
|
|
// NodeIterator returns an iterator that returns nodes of the trie. Iteration starts at
|
|
|
|
// the key after the given start key.
|
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-20 19:31:45 +00:00
|
|
|
func (t *Trie) NodeIterator(start []byte) (NodeIterator, error) {
|
|
|
|
// Short circuit if the trie is already committed and not usable.
|
|
|
|
if t.committed {
|
|
|
|
return nil, ErrCommitted
|
|
|
|
}
|
|
|
|
return newNodeIterator(t, start), nil
|
2014-04-29 10:36:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 10:57:24 +00:00
|
|
|
// MustGet is a wrapper of Get and will omit any encountered error but just
|
|
|
|
// print out an error message.
|
|
|
|
func (t *Trie) MustGet(key []byte) []byte {
|
|
|
|
res, err := t.Get(key)
|
2017-02-22 12:10:07 +00:00
|
|
|
if err != nil {
|
2022-09-01 06:41:10 +00:00
|
|
|
log.Error("Unhandled trie error in Trie.Get", "err", err)
|
2015-11-25 17:28:21 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-04-20 10:57:24 +00:00
|
|
|
// Get returns the value for key stored in the trie.
|
2015-11-25 17:28:21 +00:00
|
|
|
// The value bytes must not be modified by the caller.
|
2023-04-20 10:57:24 +00:00
|
|
|
//
|
|
|
|
// If the requested node is not present in trie, no error will be returned.
|
|
|
|
// If the trie is corrupted, a MissingNodeError is returned.
|
|
|
|
func (t *Trie) Get(key []byte) ([]byte, error) {
|
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-20 19:31:45 +00:00
|
|
|
// Short circuit if the trie is already committed and not usable.
|
|
|
|
if t.committed {
|
|
|
|
return nil, ErrCommitted
|
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
value, newroot, didResolve, err := t.get(t.root, keybytesToHex(key), 0)
|
2016-09-25 18:49:02 +00:00
|
|
|
if err == nil && didResolve {
|
|
|
|
t.root = newroot
|
|
|
|
}
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
2023-04-20 10:57:24 +00:00
|
|
|
func (t *Trie) get(origNode node, key []byte, pos int) (value []byte, newnode node, didResolve bool, err error) {
|
2016-09-25 18:49:02 +00:00
|
|
|
switch n := (origNode).(type) {
|
|
|
|
case nil:
|
|
|
|
return nil, nil, false, nil
|
|
|
|
case valueNode:
|
|
|
|
return n, n, false, nil
|
2016-10-14 16:04:33 +00:00
|
|
|
case *shortNode:
|
2016-09-25 18:49:02 +00:00
|
|
|
if len(key)-pos < len(n.Key) || !bytes.Equal(n.Key, key[pos:pos+len(n.Key)]) {
|
|
|
|
// key not found in trie
|
|
|
|
return nil, n, false, nil
|
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
value, newnode, didResolve, err = t.get(n.Val, key, pos+len(n.Key))
|
2016-09-25 18:49:02 +00:00
|
|
|
if err == nil && didResolve {
|
2016-10-14 16:04:33 +00:00
|
|
|
n = n.copy()
|
2016-09-25 18:49:02 +00:00
|
|
|
n.Val = newnode
|
|
|
|
}
|
2016-10-14 16:04:33 +00:00
|
|
|
return value, n, didResolve, err
|
|
|
|
case *fullNode:
|
2023-04-20 10:57:24 +00:00
|
|
|
value, newnode, didResolve, err = t.get(n.Children[key[pos]], key, pos+1)
|
2016-09-25 18:49:02 +00:00
|
|
|
if err == nil && didResolve {
|
2016-10-14 16:04:33 +00:00
|
|
|
n = n.copy()
|
2016-09-25 18:49:02 +00:00
|
|
|
n.Children[key[pos]] = newnode
|
|
|
|
}
|
2016-10-14 16:04:33 +00:00
|
|
|
return value, n, didResolve, err
|
2016-09-25 18:49:02 +00:00
|
|
|
case hashNode:
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
child, err := t.resolveAndTrack(n, key[:pos])
|
2016-09-25 18:49:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, n, true, err
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
value, newnode, _, err := t.get(child, key, pos)
|
2016-09-25 18:49:02 +00:00
|
|
|
return value, newnode, true, err
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode))
|
2014-10-29 13:20:42 +00:00
|
|
|
}
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 10:57:24 +00:00
|
|
|
// MustGetNode is a wrapper of GetNode and will omit any encountered error but
|
|
|
|
// just print out an error message.
|
|
|
|
func (t *Trie) MustGetNode(path []byte) ([]byte, int) {
|
|
|
|
item, resolved, err := t.GetNode(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unhandled trie error in Trie.GetNode", "err", err)
|
|
|
|
}
|
|
|
|
return item, resolved
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNode retrieves a trie node by compact-encoded path. It is not possible
|
|
|
|
// to use keybyte-encoding as the path might contain odd nibbles.
|
|
|
|
//
|
|
|
|
// If the requested node is not present in trie, no error will be returned.
|
|
|
|
// If the trie is corrupted, a MissingNodeError is returned.
|
|
|
|
func (t *Trie) GetNode(path []byte) ([]byte, int, error) {
|
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-20 19:31:45 +00:00
|
|
|
// Short circuit if the trie is already committed and not usable.
|
|
|
|
if t.committed {
|
|
|
|
return nil, 0, ErrCommitted
|
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
item, newroot, resolved, err := t.getNode(t.root, compactToHex(path), 0)
|
2020-08-28 07:50:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, resolved, err
|
|
|
|
}
|
|
|
|
if resolved > 0 {
|
|
|
|
t.root = newroot
|
|
|
|
}
|
|
|
|
if item == nil {
|
|
|
|
return nil, resolved, nil
|
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
return item, resolved, nil
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 10:57:24 +00:00
|
|
|
func (t *Trie) getNode(origNode node, path []byte, pos int) (item []byte, newnode node, resolved int, err error) {
|
2021-09-29 13:19:40 +00:00
|
|
|
// If non-existent path requested, abort
|
|
|
|
if origNode == nil {
|
|
|
|
return nil, nil, 0, nil
|
|
|
|
}
|
2020-08-28 07:50:37 +00:00
|
|
|
// If we reached the requested path, return the current node
|
|
|
|
if pos >= len(path) {
|
2020-12-14 09:27:15 +00:00
|
|
|
// Although we most probably have the original node expanded, encoding
|
|
|
|
// that into consensus form can be nasty (needs to cascade down) and
|
|
|
|
// time consuming. Instead, just pull the hash up from disk directly.
|
|
|
|
var hash hashNode
|
|
|
|
if node, ok := origNode.(hashNode); ok {
|
|
|
|
hash = node
|
|
|
|
} else {
|
|
|
|
hash, _ = origNode.cache()
|
|
|
|
}
|
|
|
|
if hash == nil {
|
|
|
|
return nil, origNode, 0, errors.New("non-consensus node")
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
2023-04-24 07:38:52 +00:00
|
|
|
blob, err := t.reader.node(path, common.BytesToHash(hash))
|
2020-12-14 09:27:15 +00:00
|
|
|
return blob, origNode, 1, err
|
2020-08-28 07:50:37 +00:00
|
|
|
}
|
|
|
|
// Path still needs to be traversed, descend into children
|
|
|
|
switch n := (origNode).(type) {
|
|
|
|
case valueNode:
|
|
|
|
// Path prematurely ended, abort
|
|
|
|
return nil, nil, 0, nil
|
|
|
|
|
|
|
|
case *shortNode:
|
|
|
|
if len(path)-pos < len(n.Key) || !bytes.Equal(n.Key, path[pos:pos+len(n.Key)]) {
|
|
|
|
// Path branches off from short node
|
|
|
|
return nil, n, 0, nil
|
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
item, newnode, resolved, err = t.getNode(n.Val, path, pos+len(n.Key))
|
2020-08-28 07:50:37 +00:00
|
|
|
if err == nil && resolved > 0 {
|
|
|
|
n = n.copy()
|
|
|
|
n.Val = newnode
|
|
|
|
}
|
|
|
|
return item, n, resolved, err
|
|
|
|
|
|
|
|
case *fullNode:
|
2023-04-20 10:57:24 +00:00
|
|
|
item, newnode, resolved, err = t.getNode(n.Children[path[pos]], path, pos+1)
|
2020-08-28 07:50:37 +00:00
|
|
|
if err == nil && resolved > 0 {
|
|
|
|
n = n.copy()
|
|
|
|
n.Children[path[pos]] = newnode
|
|
|
|
}
|
|
|
|
return item, n, resolved, err
|
|
|
|
|
|
|
|
case hashNode:
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
child, err := t.resolveAndTrack(n, path[:pos])
|
2020-08-28 07:50:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, n, 1, err
|
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
item, newnode, resolved, err := t.getNode(child, path, pos)
|
2020-08-28 07:50:37 +00:00
|
|
|
return item, newnode, resolved + 1, err
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 10:57:24 +00:00
|
|
|
// MustUpdate is a wrapper of Update and will omit any encountered error but
|
|
|
|
// just print out an error message.
|
|
|
|
func (t *Trie) MustUpdate(key, value []byte) {
|
|
|
|
if err := t.Update(key, value); err != nil {
|
2022-09-01 06:41:10 +00:00
|
|
|
log.Error("Unhandled trie error in Trie.Update", "err", err)
|
2015-11-25 17:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 10:57:24 +00:00
|
|
|
// Update associates key with value in the trie. Subsequent calls to
|
2015-11-25 17:28:21 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2023-04-20 10:57:24 +00:00
|
|
|
// If the requested node is not present in trie, no error will be returned.
|
|
|
|
// If the trie is corrupted, a MissingNodeError is returned.
|
|
|
|
func (t *Trie) Update(key, value []byte) error {
|
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-20 19:31:45 +00:00
|
|
|
// Short circuit if the trie is already committed and not usable.
|
|
|
|
if t.committed {
|
|
|
|
return ErrCommitted
|
|
|
|
}
|
2023-04-20 10:57:24 +00:00
|
|
|
return t.update(key, value)
|
2022-08-04 14:13:18 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 10:57:24 +00:00
|
|
|
func (t *Trie) update(key, value []byte) error {
|
2020-02-04 12:02:38 +00:00
|
|
|
t.unhashed++
|
2017-04-18 11:25:07 +00:00
|
|
|
k := keybytesToHex(key)
|
2015-01-08 10:47:04 +00:00
|
|
|
if len(value) != 0 {
|
2016-05-19 10:24:14 +00:00
|
|
|
_, n, err := t.insert(t.root, nil, k, valueNode(value))
|
2015-11-25 17:28:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.root = n
|
2014-11-18 11:03:09 +00:00
|
|
|
} else {
|
2016-05-19 10:24:14 +00:00
|
|
|
_, n, err := t.delete(t.root, nil, k)
|
2015-11-25 17:28:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.root = n
|
2014-11-18 11:03:09 +00:00
|
|
|
}
|
2015-11-25 17:28:21 +00:00
|
|
|
return nil
|
2014-07-02 11:40:02 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:24:14 +00:00
|
|
|
func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) {
|
2015-01-08 10:47:04 +00:00
|
|
|
if len(key) == 0 {
|
2016-05-19 10:24:14 +00:00
|
|
|
if v, ok := n.(valueNode); ok {
|
|
|
|
return !bytes.Equal(v, value.(valueNode)), value, nil
|
|
|
|
}
|
|
|
|
return true, value, nil
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
switch n := n.(type) {
|
2016-10-14 16:04:33 +00:00
|
|
|
case *shortNode:
|
2015-07-05 23:19:48 +00:00
|
|
|
matchlen := prefixLen(key, n.Key)
|
|
|
|
// If the whole key matches, keep this short node as is
|
|
|
|
// and only update the value.
|
|
|
|
if matchlen == len(n.Key) {
|
2016-05-19 10:24:14 +00:00
|
|
|
dirty, nn, err := t.insert(n.Val, append(prefix, key[:matchlen]...), key[matchlen:], value)
|
2016-10-14 16:04:33 +00:00
|
|
|
if !dirty || err != nil {
|
|
|
|
return false, n, err
|
2016-05-19 10:24:14 +00:00
|
|
|
}
|
2016-10-14 16:04:33 +00:00
|
|
|
return true, &shortNode{n.Key, nn, t.newFlag()}, nil
|
2015-01-08 10:47:04 +00:00
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
// Otherwise branch out at the index where they differ.
|
2016-10-14 16:04:33 +00:00
|
|
|
branch := &fullNode{flags: t.newFlag()}
|
2015-11-25 17:28:21 +00:00
|
|
|
var err error
|
2016-05-19 10:24:14 +00:00
|
|
|
_, branch.Children[n.Key[matchlen]], err = t.insert(nil, append(prefix, n.Key[:matchlen+1]...), n.Key[matchlen+1:], n.Val)
|
2015-11-25 17:28:21 +00:00
|
|
|
if err != nil {
|
2016-05-19 10:24:14 +00:00
|
|
|
return false, nil, err
|
2015-11-25 17:28:21 +00:00
|
|
|
}
|
2016-05-19 10:24:14 +00:00
|
|
|
_, branch.Children[key[matchlen]], err = t.insert(nil, append(prefix, key[:matchlen+1]...), key[matchlen+1:], value)
|
2015-11-25 17:28:21 +00:00
|
|
|
if err != nil {
|
2016-05-19 10:24:14 +00:00
|
|
|
return false, nil, err
|
2015-11-25 17:28:21 +00:00
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
// Replace this shortNode with the branch if it occurs at index 0.
|
|
|
|
if matchlen == 0 {
|
2016-05-19 10:24:14 +00:00
|
|
|
return true, branch, nil
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
2023-02-20 14:54:52 +00:00
|
|
|
// New branch node is created as a child of the original short node.
|
|
|
|
// Track the newly inserted node in the tracer. The node identifier
|
|
|
|
// passed is the path from the root node.
|
|
|
|
t.tracer.onInsert(append(prefix, key[:matchlen]...))
|
|
|
|
|
2022-03-31 07:28:32 +00:00
|
|
|
// Replace it with a short node leading up to the branch.
|
2016-10-14 16:04:33 +00:00
|
|
|
return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil
|
2014-02-14 22:56:09 +00:00
|
|
|
|
2016-10-14 16:04:33 +00:00
|
|
|
case *fullNode:
|
2016-05-19 10:24:14 +00:00
|
|
|
dirty, nn, err := t.insert(n.Children[key[0]], append(prefix, key[0]), key[1:], value)
|
2016-10-14 16:04:33 +00:00
|
|
|
if !dirty || err != nil {
|
|
|
|
return false, n, err
|
2015-11-25 17:28:21 +00:00
|
|
|
}
|
2016-10-14 16:04:33 +00:00
|
|
|
n = n.copy()
|
2016-10-17 14:13:50 +00:00
|
|
|
n.flags = t.newFlag()
|
|
|
|
n.Children[key[0]] = nn
|
2016-05-19 10:24:14 +00:00
|
|
|
return true, n, nil
|
2014-02-14 22:56:09 +00:00
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
case nil:
|
2023-02-20 14:54:52 +00:00
|
|
|
// New short node is created and track it in the tracer. The node identifier
|
|
|
|
// passed is the path from the root node. Note the valueNode won't be tracked
|
|
|
|
// since it's always embedded in its parent.
|
|
|
|
t.tracer.onInsert(prefix)
|
|
|
|
|
2016-10-14 16:04:33 +00:00
|
|
|
return true, &shortNode{key, value, t.newFlag()}, nil
|
2014-02-14 22:56:09 +00:00
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
case hashNode:
|
|
|
|
// We've hit a part of the trie that isn't loaded yet. Load
|
|
|
|
// the node and insert into it. This leaves all child nodes on
|
|
|
|
// the path to the value in the trie.
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
rn, err := t.resolveAndTrack(n, prefix)
|
2015-11-25 17:28:21 +00:00
|
|
|
if err != nil {
|
2016-05-19 10:24:14 +00:00
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
dirty, nn, err := t.insert(rn, prefix, key, value)
|
2016-10-14 16:04:33 +00:00
|
|
|
if !dirty || err != nil {
|
|
|
|
return false, rn, err
|
2016-05-19 10:24:14 +00:00
|
|
|
}
|
|
|
|
return true, nn, nil
|
2014-02-14 22:56:09 +00:00
|
|
|
|
2015-01-08 10:47:04 +00:00
|
|
|
default:
|
2015-07-05 23:19:48 +00:00
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", n, n))
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 10:57:24 +00:00
|
|
|
// MustDelete is a wrapper of Delete and will omit any encountered error but
|
|
|
|
// just print out an error message.
|
|
|
|
func (t *Trie) MustDelete(key []byte) {
|
|
|
|
if err := t.Delete(key); err != nil {
|
2022-09-01 06:41:10 +00:00
|
|
|
log.Error("Unhandled trie error in Trie.Delete", "err", err)
|
2015-11-25 17:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-20 10:57:24 +00:00
|
|
|
// Delete removes any existing value for key from the trie.
|
|
|
|
//
|
|
|
|
// If the requested node is not present in trie, no error will be returned.
|
|
|
|
// If the trie is corrupted, a MissingNodeError is returned.
|
|
|
|
func (t *Trie) Delete(key []byte) error {
|
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-20 19:31:45 +00:00
|
|
|
// Short circuit if the trie is already committed and not usable.
|
|
|
|
if t.committed {
|
|
|
|
return ErrCommitted
|
|
|
|
}
|
2020-02-04 12:02:38 +00:00
|
|
|
t.unhashed++
|
2017-04-18 11:25:07 +00:00
|
|
|
k := keybytesToHex(key)
|
2016-05-19 10:24:14 +00:00
|
|
|
_, n, err := t.delete(t.root, nil, k)
|
2015-11-25 17:28:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.root = n
|
|
|
|
return nil
|
2014-02-14 22:56:09 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
// delete returns the new root of the trie with key deleted.
|
|
|
|
// It reduces the trie to minimal form by simplifying
|
|
|
|
// nodes on the way up after deleting recursively.
|
2016-05-19 10:24:14 +00:00
|
|
|
func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
|
2015-07-05 23:19:48 +00:00
|
|
|
switch n := n.(type) {
|
2016-10-14 16:04:33 +00:00
|
|
|
case *shortNode:
|
2015-07-05 23:19:48 +00:00
|
|
|
matchlen := prefixLen(key, n.Key)
|
|
|
|
if matchlen < len(n.Key) {
|
2016-05-19 10:24:14 +00:00
|
|
|
return false, n, nil // don't replace n on mismatch
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
if matchlen == len(key) {
|
2023-02-20 14:54:52 +00:00
|
|
|
// The matched short node is deleted entirely and track
|
|
|
|
// it in the deletion set. The same the valueNode doesn't
|
|
|
|
// need to be tracked at all since it's always embedded.
|
|
|
|
t.tracer.onDelete(prefix)
|
|
|
|
|
2016-05-19 10:24:14 +00:00
|
|
|
return true, nil, nil // remove n entirely for whole matches
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
// The key is longer than n.Key. Remove the remaining suffix
|
|
|
|
// from the subtrie. Child can never be nil here since the
|
|
|
|
// subtrie must contain at least two other values with keys
|
|
|
|
// longer than n.Key.
|
2016-05-19 10:24:14 +00:00
|
|
|
dirty, child, err := t.delete(n.Val, append(prefix, key[:len(n.Key)]...), key[len(n.Key):])
|
2016-10-14 16:04:33 +00:00
|
|
|
if !dirty || err != nil {
|
|
|
|
return false, n, err
|
2015-11-25 17:28:21 +00:00
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
switch child := child.(type) {
|
2016-10-14 16:04:33 +00:00
|
|
|
case *shortNode:
|
2023-02-20 14:54:52 +00:00
|
|
|
// The child shortNode is merged into its parent, track
|
|
|
|
// is deleted as well.
|
|
|
|
t.tracer.onDelete(append(prefix, n.Key...))
|
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
// Deleting from the subtrie reduced it to another
|
|
|
|
// short node. Merge the nodes to avoid creating a
|
|
|
|
// shortNode{..., shortNode{...}}. Use concat (which
|
|
|
|
// always creates a new slice) instead of append to
|
|
|
|
// avoid modifying n.Key since it might be shared with
|
|
|
|
// other nodes.
|
2016-10-14 16:04:33 +00:00
|
|
|
return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil
|
2015-07-05 23:19:48 +00:00
|
|
|
default:
|
2016-10-14 16:04:33 +00:00
|
|
|
return true, &shortNode{n.Key, child, t.newFlag()}, nil
|
2014-02-20 13:40:00 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 16:04:33 +00:00
|
|
|
case *fullNode:
|
2016-05-19 10:24:14 +00:00
|
|
|
dirty, nn, err := t.delete(n.Children[key[0]], append(prefix, key[0]), key[1:])
|
2016-10-14 16:04:33 +00:00
|
|
|
if !dirty || err != nil {
|
|
|
|
return false, n, err
|
2016-05-19 10:24:14 +00:00
|
|
|
}
|
2016-10-14 16:04:33 +00:00
|
|
|
n = n.copy()
|
2016-10-17 14:13:50 +00:00
|
|
|
n.flags = t.newFlag()
|
|
|
|
n.Children[key[0]] = nn
|
2016-05-19 10:24:14 +00:00
|
|
|
|
2021-06-20 13:59:00 +00:00
|
|
|
// Because n is a full node, it must've contained at least two children
|
|
|
|
// before the delete operation. If the new child value is non-nil, n still
|
|
|
|
// has at least two children after the deletion, and cannot be reduced to
|
|
|
|
// a short node.
|
|
|
|
if nn != nil {
|
|
|
|
return true, n, nil
|
|
|
|
}
|
|
|
|
// Reduction:
|
2015-07-05 23:19:48 +00:00
|
|
|
// Check how many non-nil entries are left after deleting and
|
|
|
|
// reduce the full node to a short node if only one entry is
|
|
|
|
// left. Since n must've contained at least two children
|
|
|
|
// before deletion (otherwise it would not be a full node) n
|
|
|
|
// can never be reduced to nil.
|
|
|
|
//
|
|
|
|
// When the loop is done, pos contains the index of the single
|
|
|
|
// value that is left in n or -2 if n contains at least two
|
|
|
|
// values.
|
2015-01-08 10:47:04 +00:00
|
|
|
pos := -1
|
2018-08-07 10:56:40 +00:00
|
|
|
for i, cld := range &n.Children {
|
2015-07-05 23:19:48 +00:00
|
|
|
if cld != nil {
|
2015-01-08 10:47:04 +00:00
|
|
|
if pos == -1 {
|
|
|
|
pos = i
|
2014-02-20 13:40:00 +00:00
|
|
|
} else {
|
2015-01-08 10:47:04 +00:00
|
|
|
pos = -2
|
2015-07-05 23:19:48 +00:00
|
|
|
break
|
2014-02-20 13:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
if pos >= 0 {
|
|
|
|
if pos != 16 {
|
|
|
|
// If the remaining entry is a short node, it replaces
|
|
|
|
// n and its key gets the missing nibble tacked to the
|
|
|
|
// front. This avoids creating an invalid
|
|
|
|
// shortNode{..., shortNode{...}}. Since the entry
|
|
|
|
// might not be loaded yet, resolve it just for this
|
|
|
|
// check.
|
2022-08-04 08:03:20 +00:00
|
|
|
cnode, err := t.resolve(n.Children[pos], append(prefix, byte(pos)))
|
2015-11-25 17:28:21 +00:00
|
|
|
if err != nil {
|
2016-05-19 10:24:14 +00:00
|
|
|
return false, nil, err
|
2015-11-25 17:28:21 +00:00
|
|
|
}
|
2016-10-14 16:04:33 +00:00
|
|
|
if cnode, ok := cnode.(*shortNode); ok {
|
2023-02-20 14:54:52 +00:00
|
|
|
// Replace the entire full node with the short node.
|
|
|
|
// Mark the original short node as deleted since the
|
|
|
|
// value is embedded into the parent now.
|
|
|
|
t.tracer.onDelete(append(prefix, byte(pos)))
|
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
k := append([]byte{byte(pos)}, cnode.Key...)
|
2016-10-14 16:04:33 +00:00
|
|
|
return true, &shortNode{k, cnode.Val, t.newFlag()}, nil
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
2015-01-08 10:47:04 +00:00
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
// Otherwise, n is replaced by a one-nibble short node
|
|
|
|
// containing the child.
|
2016-10-14 16:04:33 +00:00
|
|
|
return true, &shortNode{[]byte{byte(pos)}, n.Children[pos], t.newFlag()}, nil
|
2014-02-20 13:40:00 +00:00
|
|
|
}
|
2015-07-05 23:19:48 +00:00
|
|
|
// n still contains at least two values and cannot be reduced.
|
2016-05-19 10:24:14 +00:00
|
|
|
return true, n, nil
|
2014-02-20 13:40:00 +00:00
|
|
|
|
2016-09-29 14:51:32 +00:00
|
|
|
case valueNode:
|
|
|
|
return true, nil, nil
|
|
|
|
|
2015-01-08 10:47:04 +00:00
|
|
|
case nil:
|
2016-05-19 10:24:14 +00:00
|
|
|
return false, nil, nil
|
2015-07-05 23:19:48 +00:00
|
|
|
|
|
|
|
case hashNode:
|
|
|
|
// We've hit a part of the trie that isn't loaded yet. Load
|
|
|
|
// the node and delete from it. This leaves all child nodes on
|
|
|
|
// the path to the value in the trie.
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
rn, err := t.resolveAndTrack(n, prefix)
|
2015-11-25 17:28:21 +00:00
|
|
|
if err != nil {
|
2016-05-19 10:24:14 +00:00
|
|
|
return false, nil, err
|
2015-11-25 17:28:21 +00:00
|
|
|
}
|
2016-05-19 10:24:14 +00:00
|
|
|
dirty, nn, err := t.delete(rn, prefix, key)
|
2016-10-14 16:04:33 +00:00
|
|
|
if !dirty || err != nil {
|
|
|
|
return false, rn, err
|
2016-05-19 10:24:14 +00:00
|
|
|
}
|
|
|
|
return true, nn, nil
|
2015-07-05 23:19:48 +00:00
|
|
|
|
2015-01-08 10:47:04 +00:00
|
|
|
default:
|
2015-07-05 23:19:48 +00:00
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v (%v)", n, n, key))
|
2014-02-20 13:40:00 +00:00
|
|
|
}
|
2014-02-24 11:11:00 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 23:19:48 +00:00
|
|
|
func concat(s1 []byte, s2 ...byte) []byte {
|
|
|
|
r := make([]byte, len(s1)+len(s2))
|
|
|
|
copy(r, s1)
|
|
|
|
copy(r[len(s1):], s2)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-06-20 16:26:09 +00:00
|
|
|
func (t *Trie) resolve(n node, prefix []byte) (node, error) {
|
2015-07-05 23:19:48 +00:00
|
|
|
if n, ok := n.(hashNode); ok {
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
return t.resolveAndTrack(n, prefix)
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
2015-11-25 17:28:21 +00:00
|
|
|
return n, nil
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
|
|
|
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
// resolveAndTrack loads node from the underlying store with the given node hash
|
|
|
|
// and path prefix and also tracks the loaded node blob in tracer treated as the
|
|
|
|
// node's original value. The rlp-encoded blob is preferred to be loaded from
|
|
|
|
// database because it's easy to decode node while complex to encode node to blob.
|
|
|
|
func (t *Trie) resolveAndTrack(n hashNode, prefix []byte) (node, error) {
|
2023-04-24 07:38:52 +00:00
|
|
|
blob, err := t.reader.node(prefix, common.BytesToHash(n))
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-15 08:07:27 +00:00
|
|
|
}
|
2023-02-20 14:54:52 +00:00
|
|
|
t.tracer.onRead(prefix, blob)
|
cmd, core, eth, les, light: track deleted nodes (#25757)
* cmd, core, eth, les, light: track deleted nodes
* trie: add docs
* trie: address comments
* cmd, core, eth, les, light, trie: trie id
* trie: add tests
* trie, core: updates
* trie: fix imports
* trie: add utility print-method for nodeset
* trie: import err
* trie: fix go vet warnings
Co-authored-by: Martin Holst Swende <martin@swende.se>
2022-09-27 08:01:02 +00:00
|
|
|
return mustDecodeNode(n, blob), nil
|
2022-02-15 08:07:27 +00:00
|
|
|
}
|
|
|
|
|
2015-07-05 23:19:48 +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.
|
|
|
|
func (t *Trie) Hash() common.Hash {
|
2023-03-14 08:50:53 +00:00
|
|
|
hash, cached := t.hashRoot()
|
2016-05-19 10:24:14 +00:00
|
|
|
t.root = cached
|
|
|
|
return common.BytesToHash(hash.(hashNode))
|
2014-02-24 11:11:00 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 10:36:07 +00:00
|
|
|
// Commit collects all dirty nodes in the trie and replaces them with the
|
|
|
|
// corresponding node hash. All collected nodes (including dirty leaves if
|
2022-08-04 08:03:20 +00:00
|
|
|
// collectLeaf is true) will be encapsulated into a nodeset for return.
|
2022-09-08 10:36:07 +00:00
|
|
|
// The returned nodeset can be nil if the trie is clean (nothing to commit).
|
2022-08-04 08:03:20 +00:00
|
|
|
// 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 12:36:38 +00:00
|
|
|
func (t *Trie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet, error) {
|
2023-02-20 14:54:52 +00:00
|
|
|
defer t.tracer.reset()
|
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-20 19:31:45 +00:00
|
|
|
defer func() {
|
|
|
|
t.committed = true
|
|
|
|
}()
|
2022-12-19 09:56:13 +00:00
|
|
|
// Trie is empty and can be classified into two types of situations:
|
2023-08-01 12:17:32 +00:00
|
|
|
// (a) The trie was empty and no update happens => return nil
|
|
|
|
// (b) The trie was non-empty and all nodes are dropped => return
|
|
|
|
// the node set includes all deleted nodes
|
2020-02-03 15:28:30 +00:00
|
|
|
if t.root == nil {
|
2023-08-01 12:17:32 +00:00
|
|
|
paths := t.tracer.deletedNodes()
|
|
|
|
if len(paths) == 0 {
|
|
|
|
return types.EmptyRootHash, nil, nil // case (a)
|
|
|
|
}
|
|
|
|
nodes := trienode.NewNodeSet(t.owner)
|
|
|
|
for _, path := range paths {
|
|
|
|
nodes.AddNode([]byte(path), trienode.NewDeleted())
|
|
|
|
}
|
|
|
|
return types.EmptyRootHash, nodes, nil // case (b)
|
2020-02-03 15:28:30 +00:00
|
|
|
}
|
2020-09-30 11:45:56 +00:00
|
|
|
// Derive the hash for all dirty nodes first. We hold the assumption
|
|
|
|
// in the following procedure that all nodes are hashed.
|
2020-02-03 15:28:30 +00:00
|
|
|
rootHash := t.Hash()
|
2020-09-30 11:45:56 +00:00
|
|
|
|
2022-08-04 08:03:20 +00:00
|
|
|
// Do a quick check if we really need to commit. This can happen e.g.
|
|
|
|
// if we load a trie for reading storage values, but don't write to it.
|
2022-06-06 15:14:55 +00:00
|
|
|
if hashedNode, dirty := t.root.cache(); !dirty {
|
|
|
|
// Replace the root node with the origin hash in order to
|
|
|
|
// ensure all resolved nodes are dropped after the commit.
|
|
|
|
t.root = hashedNode
|
2023-06-27 12:36:38 +00:00
|
|
|
return rootHash, nil, nil
|
2020-02-03 15:28:30 +00:00
|
|
|
}
|
2023-08-01 12:17:32 +00:00
|
|
|
nodes := trienode.NewNodeSet(t.owner)
|
|
|
|
for _, path := range t.tracer.deletedNodes() {
|
|
|
|
nodes.AddNode([]byte(path), trienode.NewDeleted())
|
|
|
|
}
|
2023-04-26 06:01:54 +00:00
|
|
|
t.root = newCommitter(nodes, t.tracer, collectLeaf).Commit(t.root)
|
2023-06-27 12:36:38 +00:00
|
|
|
return rootHash, nodes, nil
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
2014-05-26 23:08:51 +00:00
|
|
|
|
2020-02-03 15:28:30 +00:00
|
|
|
// hashRoot calculates the root hash of the given trie
|
2023-03-14 08:50:53 +00:00
|
|
|
func (t *Trie) hashRoot() (node, node) {
|
2015-07-05 23:19:48 +00:00
|
|
|
if t.root == nil {
|
2023-03-14 08:50:53 +00:00
|
|
|
return hashNode(types.EmptyRootHash.Bytes()), nil
|
2015-07-05 23:19:48 +00:00
|
|
|
}
|
2020-02-04 12:02:38 +00:00
|
|
|
// If the number of changes is below 100, we let one thread handle it
|
|
|
|
h := newHasher(t.unhashed >= 100)
|
2023-03-14 08:50:53 +00:00
|
|
|
defer func() {
|
|
|
|
returnHasherToPool(h)
|
|
|
|
t.unhashed = 0
|
|
|
|
}()
|
2020-02-03 15:28:30 +00:00
|
|
|
hashed, cached := h.hash(t.root, true)
|
2023-03-14 08:50:53 +00:00
|
|
|
return hashed, cached
|
2014-05-26 23:08:51 +00:00
|
|
|
}
|
2020-08-21 12:10:40 +00:00
|
|
|
|
|
|
|
// Reset drops the referenced root node and cleans all internal state.
|
|
|
|
func (t *Trie) Reset() {
|
|
|
|
t.root = nil
|
2022-06-06 15:14:55 +00:00
|
|
|
t.owner = common.Hash{}
|
2020-08-21 12:10:40 +00:00
|
|
|
t.unhashed = 0
|
2023-02-20 14:54:52 +00:00
|
|
|
t.tracer.reset()
|
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-20 19:31:45 +00:00
|
|
|
t.committed = false
|
2020-08-21 12:10:40 +00:00
|
|
|
}
|