2022-05-24 13:39:40 -05:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2020-02-03 09:28:30 -06:00
|
|
|
// 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 trie
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2022-08-04 03:03:20 -05:00
|
|
|
// leaf represents a trie leaf node
|
2020-02-03 09:28:30 -06:00
|
|
|
type leaf struct {
|
2022-08-04 03:03:20 -05:00
|
|
|
blob []byte // raw blob of leaf
|
|
|
|
parent common.Hash // the hash of parent node
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
|
2022-08-04 03:03:20 -05:00
|
|
|
// committer is the tool used for the trie Commit operation. The committer will
|
|
|
|
// capture all dirty nodes during the commit process and keep them cached in
|
|
|
|
// insertion order.
|
2020-02-03 09:28:30 -06:00
|
|
|
type committer struct {
|
2022-08-04 03:03:20 -05:00
|
|
|
nodes *NodeSet
|
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 03:01:02 -05:00
|
|
|
tracer *tracer
|
2022-08-04 03:03:20 -05:00
|
|
|
collectLeaf bool
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// newCommitter creates a new committer or picks one from the pool.
|
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 03:01:02 -05:00
|
|
|
func newCommitter(owner common.Hash, tracer *tracer, collectLeaf bool) *committer {
|
2022-08-04 03:03:20 -05:00
|
|
|
return &committer{
|
|
|
|
nodes: NewNodeSet(owner),
|
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 03:01:02 -05:00
|
|
|
tracer: tracer,
|
2022-08-04 03:03:20 -05:00
|
|
|
collectLeaf: collectLeaf,
|
|
|
|
}
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
|
2022-09-08 05:36:07 -05:00
|
|
|
// Commit collapses a node down into a hash node and returns it along with
|
|
|
|
// the modified nodeset.
|
2022-08-04 03:03:20 -05:00
|
|
|
func (c *committer) Commit(n node) (hashNode, *NodeSet, error) {
|
|
|
|
h, err := c.commit(nil, n)
|
2020-02-03 09:28:30 -06:00
|
|
|
if err != nil {
|
2022-08-04 03:03:20 -05:00
|
|
|
return nil, nil, err
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
2022-12-19 03:56:13 -06:00
|
|
|
// Some nodes can be deleted from trie which can't be captured
|
|
|
|
// by committer itself. Iterate all deleted nodes tracked by
|
|
|
|
// tracer and marked them as deleted only if they are present
|
|
|
|
// in database previously.
|
|
|
|
c.tracer.markDeletions(c.nodes)
|
2022-08-04 03:03:20 -05:00
|
|
|
return h.(hashNode), c.nodes, nil
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
|
2022-09-08 05:36:07 -05:00
|
|
|
// commit collapses a node down into a hash node and returns it.
|
2022-08-04 03:03:20 -05:00
|
|
|
func (c *committer) commit(path []byte, n node) (node, error) {
|
2020-02-03 09:28:30 -06:00
|
|
|
// if this path is clean, use available cached data
|
|
|
|
hash, dirty := n.cache()
|
|
|
|
if hash != nil && !dirty {
|
2022-08-04 03:03:20 -05:00
|
|
|
return hash, nil
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
2022-03-31 02:28:32 -05:00
|
|
|
// Commit children, then parent, and remove the dirty flag.
|
2020-02-03 09:28:30 -06:00
|
|
|
switch cn := n.(type) {
|
|
|
|
case *shortNode:
|
|
|
|
// Commit child
|
|
|
|
collapsed := cn.copy()
|
2020-09-30 06:45:56 -05:00
|
|
|
|
2021-08-24 14:00:42 -05:00
|
|
|
// If the child is fullNode, recursively commit,
|
|
|
|
// otherwise it can only be hashNode or valueNode.
|
2020-09-30 06:45:56 -05:00
|
|
|
if _, ok := cn.Val.(*fullNode); ok {
|
2022-08-04 03:03:20 -05:00
|
|
|
childV, err := c.commit(append(path, cn.Key...), cn.Val)
|
2020-07-15 03:00:04 -05:00
|
|
|
if err != nil {
|
2022-08-04 03:03:20 -05:00
|
|
|
return nil, err
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
2022-08-04 03:03:20 -05:00
|
|
|
collapsed.Val = childV
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
2022-09-08 05:36:07 -05:00
|
|
|
// The key needs to be copied, since we're adding it to the
|
|
|
|
// modified nodeset.
|
2020-02-03 09:28:30 -06:00
|
|
|
collapsed.Key = hexToCompact(cn.Key)
|
2022-08-04 03:03:20 -05:00
|
|
|
hashedNode := c.store(path, collapsed)
|
2020-02-03 09:28:30 -06:00
|
|
|
if hn, ok := hashedNode.(hashNode); ok {
|
2022-08-04 03:03:20 -05:00
|
|
|
return hn, nil
|
2020-02-03 09:28:30 -06: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 03:01:02 -05:00
|
|
|
// The short node now is embedded in its parent. Mark the node as
|
|
|
|
// deleted if it's present in database previously. It's equivalent
|
|
|
|
// as deletion from database's perspective.
|
|
|
|
if prev := c.tracer.getPrev(path); len(prev) != 0 {
|
|
|
|
c.nodes.markDeleted(path, prev)
|
|
|
|
}
|
2022-08-04 03:03:20 -05:00
|
|
|
return collapsed, nil
|
2020-02-03 09:28:30 -06:00
|
|
|
case *fullNode:
|
2022-08-04 03:03:20 -05:00
|
|
|
hashedKids, err := c.commitChildren(path, cn)
|
2020-02-03 09:28:30 -06:00
|
|
|
if err != nil {
|
2022-08-04 03:03:20 -05:00
|
|
|
return nil, err
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
collapsed := cn.copy()
|
|
|
|
collapsed.Children = hashedKids
|
|
|
|
|
2022-08-04 03:03:20 -05:00
|
|
|
hashedNode := c.store(path, collapsed)
|
2020-02-03 09:28:30 -06:00
|
|
|
if hn, ok := hashedNode.(hashNode); ok {
|
2022-08-04 03:03:20 -05:00
|
|
|
return hn, nil
|
2020-02-03 09:28:30 -06: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 03:01:02 -05:00
|
|
|
// The full node now is embedded in its parent. Mark the node as
|
|
|
|
// deleted if it's present in database previously. It's equivalent
|
|
|
|
// as deletion from database's perspective.
|
|
|
|
if prev := c.tracer.getPrev(path); len(prev) != 0 {
|
|
|
|
c.nodes.markDeleted(path, prev)
|
|
|
|
}
|
2022-08-04 03:03:20 -05:00
|
|
|
return collapsed, nil
|
2020-02-03 09:28:30 -06:00
|
|
|
case hashNode:
|
2022-08-04 03:03:20 -05:00
|
|
|
return cn, nil
|
2020-09-30 06:45:56 -05:00
|
|
|
default:
|
|
|
|
// nil, valuenode shouldn't be committed
|
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", n, n))
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// commitChildren commits the children of the given fullnode
|
2022-08-04 03:03:20 -05:00
|
|
|
func (c *committer) commitChildren(path []byte, n *fullNode) ([17]node, error) {
|
|
|
|
var children [17]node
|
2020-09-30 06:45:56 -05:00
|
|
|
for i := 0; i < 16; i++ {
|
|
|
|
child := n.Children[i]
|
2020-02-03 09:28:30 -06:00
|
|
|
if child == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-30 06:45:56 -05:00
|
|
|
// If it's the hashed child, save the hash value directly.
|
|
|
|
// Note: it's impossible that the child in range [0, 15]
|
2021-08-24 14:00:42 -05:00
|
|
|
// is a valueNode.
|
2020-09-30 06:45:56 -05:00
|
|
|
if hn, ok := child.(hashNode); ok {
|
|
|
|
children[i] = hn
|
|
|
|
continue
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
2020-09-30 06:45:56 -05:00
|
|
|
// Commit the child recursively and store the "hashed" value.
|
|
|
|
// Note the returned node can be some embedded nodes, so it's
|
2021-08-24 14:00:42 -05:00
|
|
|
// possible the type is not hashNode.
|
2022-08-04 03:03:20 -05:00
|
|
|
hashed, err := c.commit(append(path, byte(i)), child)
|
2020-09-30 06:45:56 -05:00
|
|
|
if err != nil {
|
2022-08-04 03:03:20 -05:00
|
|
|
return children, err
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
2020-09-30 06:45:56 -05:00
|
|
|
children[i] = hashed
|
|
|
|
}
|
|
|
|
// For the 17th child, it's possible the type is valuenode.
|
|
|
|
if n.Children[16] != nil {
|
|
|
|
children[16] = n.Children[16]
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
2022-08-04 03:03:20 -05:00
|
|
|
return children, nil
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
|
2022-09-08 05:36:07 -05:00
|
|
|
// store hashes the node n and adds it to the modified nodeset. If leaf collection
|
|
|
|
// is enabled, leaf nodes will be tracked in the modified nodeset as well.
|
2022-08-04 03:03:20 -05:00
|
|
|
func (c *committer) store(path []byte, n node) node {
|
2020-02-03 09:28:30 -06:00
|
|
|
// Larger nodes are replaced by their hash and stored in the database.
|
2022-08-04 03:03:20 -05:00
|
|
|
var hash, _ = n.cache()
|
|
|
|
|
|
|
|
// This was not generated - must be a small node stored in the parent.
|
|
|
|
// In theory, we should check if the node is leaf here (embedded node
|
2022-09-08 05:36:07 -05:00
|
|
|
// usually is leaf node). But small value (less than 32bytes) is not
|
|
|
|
// our target (leaves in account trie only).
|
2020-02-03 09:28:30 -06:00
|
|
|
if hash == nil {
|
2020-09-30 06:45:56 -05:00
|
|
|
return n
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
2022-08-04 03:03:20 -05:00
|
|
|
// We have the hash already, estimate the RLP encoding-size of the node.
|
|
|
|
// The size is used for mem tracking, does not need to be exact
|
|
|
|
var (
|
|
|
|
size = estimateSize(n)
|
|
|
|
nhash = common.BytesToHash(hash)
|
|
|
|
mnode = &memoryNode{
|
|
|
|
hash: nhash,
|
|
|
|
node: simplifyNode(n),
|
|
|
|
size: uint16(size),
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
2022-08-04 03:03:20 -05:00
|
|
|
)
|
|
|
|
// Collect the dirty node to nodeset for return.
|
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 03:01:02 -05:00
|
|
|
c.nodes.markUpdated(path, mnode, c.tracer.getPrev(path))
|
2022-08-04 03:03:20 -05:00
|
|
|
|
|
|
|
// Collect the corresponding leaf node if it's required. We don't check
|
|
|
|
// full node since it's impossible to store value in fullNode. The key
|
|
|
|
// length of leaves should be exactly same.
|
|
|
|
if c.collectLeaf {
|
|
|
|
if sn, ok := n.(*shortNode); ok {
|
|
|
|
if val, ok := sn.Val.(valueNode); ok {
|
|
|
|
c.nodes.addLeaf(&leaf{blob: val, parent: nhash})
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 03:03:20 -05:00
|
|
|
return hash
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// estimateSize estimates the size of an rlp-encoded node, without actually
|
|
|
|
// rlp-encoding it (zero allocs). This method has been experimentally tried, and with a trie
|
2022-08-04 03:03:20 -05:00
|
|
|
// with 1000 leaves, the only errors above 1% are on small shortnodes, where this
|
2020-02-03 09:28:30 -06:00
|
|
|
// method overestimates by 2 or 3 bytes (e.g. 37 instead of 35)
|
|
|
|
func estimateSize(n node) int {
|
|
|
|
switch n := n.(type) {
|
|
|
|
case *shortNode:
|
|
|
|
// A short node contains a compacted key, and a value.
|
|
|
|
return 3 + len(n.Key) + estimateSize(n.Val)
|
|
|
|
case *fullNode:
|
|
|
|
// A full node contains up to 16 hashes (some nils), and a key
|
|
|
|
s := 3
|
|
|
|
for i := 0; i < 16; i++ {
|
|
|
|
if child := n.Children[i]; child != nil {
|
|
|
|
s += estimateSize(child)
|
|
|
|
} else {
|
2020-07-15 03:00:04 -05:00
|
|
|
s++
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
case valueNode:
|
|
|
|
return 1 + len(n)
|
|
|
|
case hashNode:
|
|
|
|
return 1 + len(n)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("node type %T", n))
|
|
|
|
}
|
|
|
|
}
|