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"
|
2024-10-14 06:32:15 -05:00
|
|
|
"sync"
|
2020-02-03 09:28:30 -06:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2023-04-26 01:01:54 -05:00
|
|
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
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 {
|
2023-05-09 02:11:04 -05:00
|
|
|
nodes *trienode.NodeSet
|
2023-04-26 01:01:54 -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.
|
2023-05-09 02:11:04 -05:00
|
|
|
func newCommitter(nodeset *trienode.NodeSet, tracer *tracer, collectLeaf bool) *committer {
|
2022-08-04 03:03:20 -05:00
|
|
|
return &committer{
|
2023-03-14 03:50:53 -05:00
|
|
|
nodes: nodeset,
|
2023-04-26 01:01:54 -05:00
|
|
|
tracer: tracer,
|
2022-08-04 03:03:20 -05:00
|
|
|
collectLeaf: collectLeaf,
|
|
|
|
}
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
|
2023-03-14 03:50:53 -05:00
|
|
|
// Commit collapses a node down into a hash node.
|
2024-10-14 06:32:15 -05:00
|
|
|
func (c *committer) Commit(n node, parallel bool) hashNode {
|
|
|
|
return c.commit(nil, n, parallel).(hashNode)
|
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.
|
2024-10-14 06:32:15 -05:00
|
|
|
func (c *committer) commit(path []byte, n node, parallel bool) node {
|
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 {
|
2023-02-09 07:56:59 -06:00
|
|
|
return hash
|
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 {
|
2024-10-14 06:32:15 -05:00
|
|
|
collapsed.Val = c.commit(append(path, cn.Key...), cn.Val, false)
|
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 {
|
2023-02-09 07:56:59 -06:00
|
|
|
return hn
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
2023-02-09 07:56:59 -06:00
|
|
|
return collapsed
|
2020-02-03 09:28:30 -06:00
|
|
|
case *fullNode:
|
2024-10-14 06:32:15 -05:00
|
|
|
hashedKids := c.commitChildren(path, cn, parallel)
|
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 {
|
2023-02-09 07:56:59 -06:00
|
|
|
return hn
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
2023-02-09 07:56:59 -06:00
|
|
|
return collapsed
|
2020-02-03 09:28:30 -06:00
|
|
|
case hashNode:
|
2023-02-09 07:56:59 -06:00
|
|
|
return cn
|
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
|
2024-10-14 06:32:15 -05:00
|
|
|
func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) [17]node {
|
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
nodesMu sync.Mutex
|
|
|
|
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.
|
2024-10-14 06:32:15 -05:00
|
|
|
if !parallel {
|
|
|
|
children[i] = c.commit(append(path, byte(i)), child, false)
|
|
|
|
} else {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(index int) {
|
|
|
|
p := append(path, byte(index))
|
|
|
|
childSet := trienode.NewNodeSet(c.nodes.Owner)
|
|
|
|
childCommitter := newCommitter(childSet, c.tracer, c.collectLeaf)
|
|
|
|
children[index] = childCommitter.commit(p, child, false)
|
|
|
|
nodesMu.Lock()
|
|
|
|
c.nodes.MergeSet(childSet)
|
|
|
|
nodesMu.Unlock()
|
|
|
|
wg.Done()
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if parallel {
|
|
|
|
wg.Wait()
|
2020-09-30 06:45:56 -05: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 09:28:30 -06:00
|
|
|
}
|
2023-02-09 07:56:59 -06:00
|
|
|
return children
|
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 {
|
2023-03-14 03:50:53 -05: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 07:17:32 -05:00
|
|
|
_, ok := c.tracer.accessList[string(path)]
|
2023-04-26 01:01:54 -05:00
|
|
|
if ok {
|
2023-08-01 07:17:32 -05:00
|
|
|
c.nodes.AddNode(path, trienode.NewDeleted())
|
2023-03-14 03:50:53 -05:00
|
|
|
}
|
2020-09-30 06:45:56 -05:00
|
|
|
return n
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
2023-04-26 01:01:54 -05:00
|
|
|
// Collect the dirty node to nodeset for return.
|
2023-08-01 07:17:32 -05:00
|
|
|
nhash := common.BytesToHash(hash)
|
|
|
|
c.nodes.AddNode(path, trienode.New(nhash, nodeToBytes(n)))
|
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 {
|
2023-05-09 02:11:04 -05:00
|
|
|
c.nodes.AddLeaf(nhash, val)
|
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
|
|
|
}
|
|
|
|
|
2024-07-16 05:52:19 -05:00
|
|
|
// ForGatherChildren decodes the provided node and traverses the children inside.
|
|
|
|
func ForGatherChildren(node []byte, onChild func(common.Hash)) {
|
2023-04-24 02:38:52 -05: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 09:28:30 -06:00
|
|
|
switch n := n.(type) {
|
|
|
|
case *shortNode:
|
2023-04-24 02:38:52 -05:00
|
|
|
forGatherChildren(n.Val, onChild)
|
2020-02-03 09:28:30 -06:00
|
|
|
case *fullNode:
|
|
|
|
for i := 0; i < 16; i++ {
|
2023-04-24 02:38:52 -05:00
|
|
|
forGatherChildren(n.Children[i], onChild)
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
case hashNode:
|
2023-04-24 02:38:52 -05:00
|
|
|
onChild(common.BytesToHash(n))
|
|
|
|
case valueNode, nil:
|
2020-02-03 09:28:30 -06:00
|
|
|
default:
|
2023-04-24 02:38:52 -05:00
|
|
|
panic(fmt.Sprintf("unknown node type: %T", n))
|
2020-02-03 09:28:30 -06:00
|
|
|
}
|
|
|
|
}
|