2022-05-24 21:39:40 +03:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2020-02-03 18:28:30 +03: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"
|
2023-04-26 09:01:54 +03:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
2020-02-03 18:28:30 +03:00
|
|
|
)
|
|
|
|
|
2022-08-04 11:03:20 +03: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 18:28:30 +03:00
|
|
|
type committer struct {
|
2023-05-09 10:11:04 +03:00
|
|
|
nodes *trienode.NodeSet
|
2023-04-26 09:01:54 +03:00
|
|
|
tracer *tracer
|
2022-08-04 11:03:20 +03:00
|
|
|
collectLeaf bool
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newCommitter creates a new committer or picks one from the pool.
|
2023-05-09 10:11:04 +03:00
|
|
|
func newCommitter(nodeset *trienode.NodeSet, tracer *tracer, collectLeaf bool) *committer {
|
2022-08-04 11:03:20 +03:00
|
|
|
return &committer{
|
2023-03-14 11:50:53 +03:00
|
|
|
nodes: nodeset,
|
2023-04-26 09:01:54 +03:00
|
|
|
tracer: tracer,
|
2022-08-04 11:03:20 +03:00
|
|
|
collectLeaf: collectLeaf,
|
|
|
|
}
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
|
|
|
|
2023-03-14 11:50:53 +03:00
|
|
|
// Commit collapses a node down into a hash node.
|
|
|
|
func (c *committer) Commit(n node) hashNode {
|
|
|
|
return c.commit(nil, n).(hashNode)
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
|
|
|
|
2022-09-08 13:36:07 +03:00
|
|
|
// commit collapses a node down into a hash node and returns it.
|
2023-02-09 16:56:59 +03:00
|
|
|
func (c *committer) commit(path []byte, n node) node {
|
2020-02-03 18:28:30 +03:00
|
|
|
// if this path is clean, use available cached data
|
|
|
|
hash, dirty := n.cache()
|
|
|
|
if hash != nil && !dirty {
|
2023-02-09 16:56:59 +03:00
|
|
|
return hash
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
2022-03-31 10:28:32 +03:00
|
|
|
// Commit children, then parent, and remove the dirty flag.
|
2020-02-03 18:28:30 +03:00
|
|
|
switch cn := n.(type) {
|
|
|
|
case *shortNode:
|
|
|
|
// Commit child
|
|
|
|
collapsed := cn.copy()
|
2020-09-30 14:45:56 +03:00
|
|
|
|
2021-08-24 22:00:42 +03:00
|
|
|
// If the child is fullNode, recursively commit,
|
|
|
|
// otherwise it can only be hashNode or valueNode.
|
2020-09-30 14:45:56 +03:00
|
|
|
if _, ok := cn.Val.(*fullNode); ok {
|
2023-03-14 11:50:53 +03:00
|
|
|
collapsed.Val = c.commit(append(path, cn.Key...), cn.Val)
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
2022-09-08 13:36:07 +03:00
|
|
|
// The key needs to be copied, since we're adding it to the
|
|
|
|
// modified nodeset.
|
2020-02-03 18:28:30 +03:00
|
|
|
collapsed.Key = hexToCompact(cn.Key)
|
2022-08-04 11:03:20 +03:00
|
|
|
hashedNode := c.store(path, collapsed)
|
2020-02-03 18:28:30 +03:00
|
|
|
if hn, ok := hashedNode.(hashNode); ok {
|
2023-02-09 16:56:59 +03:00
|
|
|
return hn
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
2023-02-09 16:56:59 +03:00
|
|
|
return collapsed
|
2020-02-03 18:28:30 +03:00
|
|
|
case *fullNode:
|
2023-02-09 16:56:59 +03:00
|
|
|
hashedKids := c.commitChildren(path, cn)
|
2020-02-03 18:28:30 +03:00
|
|
|
collapsed := cn.copy()
|
|
|
|
collapsed.Children = hashedKids
|
|
|
|
|
2022-08-04 11:03:20 +03:00
|
|
|
hashedNode := c.store(path, collapsed)
|
2020-02-03 18:28:30 +03:00
|
|
|
if hn, ok := hashedNode.(hashNode); ok {
|
2023-02-09 16:56:59 +03:00
|
|
|
return hn
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
2023-02-09 16:56:59 +03:00
|
|
|
return collapsed
|
2020-02-03 18:28:30 +03:00
|
|
|
case hashNode:
|
2023-02-09 16:56:59 +03:00
|
|
|
return cn
|
2020-09-30 14:45:56 +03:00
|
|
|
default:
|
|
|
|
// nil, valuenode shouldn't be committed
|
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", n, n))
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// commitChildren commits the children of the given fullnode
|
2023-02-09 16:56:59 +03:00
|
|
|
func (c *committer) commitChildren(path []byte, n *fullNode) [17]node {
|
2022-08-04 11:03:20 +03:00
|
|
|
var children [17]node
|
2020-09-30 14:45:56 +03:00
|
|
|
for i := 0; i < 16; i++ {
|
|
|
|
child := n.Children[i]
|
2020-02-03 18:28:30 +03:00
|
|
|
if child == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-30 14:45:56 +03: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 22:00:42 +03:00
|
|
|
// is a valueNode.
|
2020-09-30 14:45:56 +03:00
|
|
|
if hn, ok := child.(hashNode); ok {
|
|
|
|
children[i] = hn
|
|
|
|
continue
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
2020-09-30 14:45:56 +03: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 22:00:42 +03:00
|
|
|
// possible the type is not hashNode.
|
2023-03-14 11:50:53 +03:00
|
|
|
children[i] = c.commit(append(path, byte(i)), child)
|
2020-09-30 14:45:56 +03:00
|
|
|
}
|
|
|
|
// For the 17th child, it's possible the type is valuenode.
|
|
|
|
if n.Children[16] != nil {
|
|
|
|
children[16] = n.Children[16]
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
2023-02-09 16:56:59 +03:00
|
|
|
return children
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
|
|
|
|
2022-09-08 13:36:07 +03: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 11:03:20 +03:00
|
|
|
func (c *committer) store(path []byte, n node) node {
|
2020-02-03 18:28:30 +03:00
|
|
|
// Larger nodes are replaced by their hash and stored in the database.
|
2022-08-04 11:03:20 +03: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 13:36:07 +03:00
|
|
|
// usually is leaf node). But small value (less than 32bytes) is not
|
|
|
|
// our target (leaves in account trie only).
|
2020-02-03 18:28:30 +03:00
|
|
|
if hash == nil {
|
2023-03-14 11:50:53 +03:00
|
|
|
// The node is embedded in its parent, in other words, this node
|
|
|
|
// will not be stored in the database independently, mark it as
|
|
|
|
// deleted only if the node was existent in database before.
|
2023-08-01 15:17:32 +03:00
|
|
|
_, ok := c.tracer.accessList[string(path)]
|
2023-04-26 09:01:54 +03:00
|
|
|
if ok {
|
2023-08-01 15:17:32 +03:00
|
|
|
c.nodes.AddNode(path, trienode.NewDeleted())
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
2020-09-30 14:45:56 +03:00
|
|
|
return n
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
2023-04-26 09:01:54 +03:00
|
|
|
// Collect the dirty node to nodeset for return.
|
2023-08-01 15:17:32 +03:00
|
|
|
nhash := common.BytesToHash(hash)
|
|
|
|
c.nodes.AddNode(path, trienode.New(nhash, nodeToBytes(n)))
|
2022-08-04 11:03:20 +03: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 {
|
2023-05-09 10:11:04 +03:00
|
|
|
c.nodes.AddLeaf(nhash, val)
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 11:03:20 +03:00
|
|
|
return hash
|
2020-02-03 18:28:30 +03: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
|
|
|
|
// with 1000 leafs, the only errors above 1% are on small shortnodes, where this
|
|
|
|
// 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 11:00:04 +03:00
|
|
|
s++
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
case valueNode:
|
|
|
|
return 1 + len(n)
|
|
|
|
case hashNode:
|
|
|
|
return 1 + len(n)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("node type %T", n))
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 12:46:08 +03:00
|
|
|
|
2024-02-13 16:49:53 +03:00
|
|
|
// MerkleResolver the children resolver in merkle-patricia-tree.
|
|
|
|
type MerkleResolver struct{}
|
2023-04-24 10:38:52 +03:00
|
|
|
|
|
|
|
// ForEach implements childResolver, decodes the provided node and
|
|
|
|
// traverses the children inside.
|
2024-02-13 16:49:53 +03:00
|
|
|
func (resolver MerkleResolver) ForEach(node []byte, onChild func(common.Hash)) {
|
2023-04-24 10:38:52 +03:00
|
|
|
forGatherChildren(mustDecodeNodeUnsafe(nil, node), onChild)
|
|
|
|
}
|
|
|
|
|
|
|
|
// forGatherChildren traverses the node hierarchy and invokes the callback
|
|
|
|
// for all the hashnode children.
|
|
|
|
func forGatherChildren(n node, onChild func(hash common.Hash)) {
|
2020-02-03 18:28:30 +03:00
|
|
|
switch n := n.(type) {
|
|
|
|
case *shortNode:
|
2023-04-24 10:38:52 +03:00
|
|
|
forGatherChildren(n.Val, onChild)
|
2020-02-03 18:28:30 +03:00
|
|
|
case *fullNode:
|
|
|
|
for i := 0; i < 16; i++ {
|
2023-04-24 10:38:52 +03:00
|
|
|
forGatherChildren(n.Children[i], onChild)
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
|
|
|
case hashNode:
|
2023-04-24 10:38:52 +03:00
|
|
|
onChild(common.BytesToHash(n))
|
|
|
|
case valueNode, nil:
|
2020-02-03 18:28:30 +03:00
|
|
|
default:
|
2023-04-24 10:38:52 +03:00
|
|
|
panic(fmt.Sprintf("unknown node type: %T", n))
|
2020-02-03 18:28:30 +03:00
|
|
|
}
|
|
|
|
}
|