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