2023-04-26 01:01:54 -05:00
|
|
|
// Copyright 2023 The go-ethereum Authors
|
|
|
|
// 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 trienode
|
|
|
|
|
2023-05-09 02:11:04 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
2023-08-11 17:04:12 -05:00
|
|
|
"sort"
|
2023-05-09 02:11:04 -05:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
2023-04-26 01:01:54 -05:00
|
|
|
|
|
|
|
// Node is a wrapper which contains the encoded blob of the trie node and its
|
2023-08-01 07:17:32 -05:00
|
|
|
// node hash. It is general enough that can be used to represent trie node
|
|
|
|
// corresponding to different trie implementations.
|
2023-04-26 01:01:54 -05:00
|
|
|
type Node struct {
|
|
|
|
Hash common.Hash // Node hash, empty for deleted node
|
|
|
|
Blob []byte // Encoded node blob, nil for the deleted node
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the total memory size used by this node.
|
|
|
|
func (n *Node) Size() int {
|
|
|
|
return len(n.Blob) + common.HashLength
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsDeleted returns the indicator if the node is marked as deleted.
|
|
|
|
func (n *Node) IsDeleted() bool {
|
2023-11-14 06:09:40 -06:00
|
|
|
return len(n.Blob) == 0
|
2023-04-26 01:01:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// New constructs a node with provided node information.
|
|
|
|
func New(hash common.Hash, blob []byte) *Node {
|
|
|
|
return &Node{Hash: hash, Blob: blob}
|
|
|
|
}
|
|
|
|
|
2023-08-01 07:17:32 -05:00
|
|
|
// NewDeleted constructs a node which is deleted.
|
|
|
|
func NewDeleted() *Node { return New(common.Hash{}, nil) }
|
2023-05-09 02:11:04 -05:00
|
|
|
|
|
|
|
// leaf represents a trie leaf node
|
|
|
|
type leaf struct {
|
|
|
|
Blob []byte // raw blob of leaf
|
|
|
|
Parent common.Hash // the hash of parent node
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeSet contains a set of nodes collected during the commit operation.
|
|
|
|
// Each node is keyed by path. It's not thread-safe to use.
|
|
|
|
type NodeSet struct {
|
|
|
|
Owner common.Hash
|
|
|
|
Leaves []*leaf
|
2023-08-01 07:17:32 -05:00
|
|
|
Nodes map[string]*Node
|
2023-05-09 02:11:04 -05:00
|
|
|
updates int // the count of updated and inserted nodes
|
|
|
|
deletes int // the count of deleted nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeSet initializes a node set. The owner is zero for the account trie and
|
|
|
|
// the owning account address hash for storage tries.
|
|
|
|
func NewNodeSet(owner common.Hash) *NodeSet {
|
|
|
|
return &NodeSet{
|
|
|
|
Owner: owner,
|
2023-08-01 07:17:32 -05:00
|
|
|
Nodes: make(map[string]*Node),
|
2023-05-09 02:11:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForEachWithOrder iterates the nodes with the order from bottom to top,
|
|
|
|
// right to left, nodes with the longest path will be iterated first.
|
|
|
|
func (set *NodeSet) ForEachWithOrder(callback func(path string, n *Node)) {
|
2023-06-19 04:41:31 -05:00
|
|
|
var paths []string
|
2023-05-09 02:11:04 -05:00
|
|
|
for path := range set.Nodes {
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
2023-08-01 07:17:32 -05:00
|
|
|
// Bottom-up, the longest path first
|
2023-08-11 17:04:12 -05:00
|
|
|
sort.Sort(sort.Reverse(sort.StringSlice(paths)))
|
2023-05-09 02:11:04 -05:00
|
|
|
for _, path := range paths {
|
2023-08-01 07:17:32 -05:00
|
|
|
callback(path, set.Nodes[path])
|
2023-05-09 02:11:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddNode adds the provided node into set.
|
2023-08-01 07:17:32 -05:00
|
|
|
func (set *NodeSet) AddNode(path []byte, n *Node) {
|
2023-05-09 02:11:04 -05:00
|
|
|
if n.IsDeleted() {
|
|
|
|
set.deletes += 1
|
|
|
|
} else {
|
|
|
|
set.updates += 1
|
|
|
|
}
|
|
|
|
set.Nodes[string(path)] = n
|
|
|
|
}
|
|
|
|
|
2023-07-11 08:43:23 -05:00
|
|
|
// Merge adds a set of nodes into the set.
|
2023-08-01 07:17:32 -05:00
|
|
|
func (set *NodeSet) Merge(owner common.Hash, nodes map[string]*Node) error {
|
2023-07-11 08:43:23 -05:00
|
|
|
if set.Owner != owner {
|
|
|
|
return fmt.Errorf("nodesets belong to different owner are not mergeable %x-%x", set.Owner, owner)
|
|
|
|
}
|
|
|
|
for path, node := range nodes {
|
|
|
|
prev, ok := set.Nodes[path]
|
|
|
|
if ok {
|
|
|
|
// overwrite happens, revoke the counter
|
|
|
|
if prev.IsDeleted() {
|
|
|
|
set.deletes -= 1
|
|
|
|
} else {
|
|
|
|
set.updates -= 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set.AddNode([]byte(path), node)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-09 02:11:04 -05:00
|
|
|
// AddLeaf adds the provided leaf node into set. TODO(rjl493456442) how can
|
|
|
|
// we get rid of it?
|
|
|
|
func (set *NodeSet) AddLeaf(parent common.Hash, blob []byte) {
|
|
|
|
set.Leaves = append(set.Leaves, &leaf{Blob: blob, Parent: parent})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the number of dirty nodes in set.
|
|
|
|
func (set *NodeSet) Size() (int, int) {
|
|
|
|
return set.updates, set.deletes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hashes returns the hashes of all updated nodes. TODO(rjl493456442) how can
|
|
|
|
// we get rid of it?
|
|
|
|
func (set *NodeSet) Hashes() []common.Hash {
|
|
|
|
var ret []common.Hash
|
|
|
|
for _, node := range set.Nodes {
|
|
|
|
ret = append(ret, node.Hash)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Summary returns a string-representation of the NodeSet.
|
|
|
|
func (set *NodeSet) Summary() string {
|
|
|
|
var out = new(strings.Builder)
|
|
|
|
fmt.Fprintf(out, "nodeset owner: %v\n", set.Owner)
|
|
|
|
if set.Nodes != nil {
|
|
|
|
for path, n := range set.Nodes {
|
|
|
|
// Deletion
|
|
|
|
if n.IsDeleted() {
|
2023-08-01 07:17:32 -05:00
|
|
|
fmt.Fprintf(out, " [-]: %x\n", path)
|
2023-05-09 02:11:04 -05:00
|
|
|
continue
|
|
|
|
}
|
2023-08-01 07:17:32 -05:00
|
|
|
// Insertion or update
|
|
|
|
fmt.Fprintf(out, " [+/*]: %x -> %v \n", path, n.Hash)
|
2023-05-09 02:11:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, n := range set.Leaves {
|
|
|
|
fmt.Fprintf(out, "[leaf]: %v\n", n)
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergedNodeSet represents a merged node set for a group of tries.
|
|
|
|
type MergedNodeSet struct {
|
|
|
|
Sets map[common.Hash]*NodeSet
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMergedNodeSet initializes an empty merged set.
|
|
|
|
func NewMergedNodeSet() *MergedNodeSet {
|
|
|
|
return &MergedNodeSet{Sets: make(map[common.Hash]*NodeSet)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWithNodeSet constructs a merged nodeset with the provided single set.
|
|
|
|
func NewWithNodeSet(set *NodeSet) *MergedNodeSet {
|
|
|
|
merged := NewMergedNodeSet()
|
|
|
|
merged.Merge(set)
|
|
|
|
return merged
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge merges the provided dirty nodes of a trie into the set. The assumption
|
|
|
|
// is held that no duplicated set belonging to the same trie will be merged twice.
|
|
|
|
func (set *MergedNodeSet) Merge(other *NodeSet) error {
|
2023-07-11 08:43:23 -05:00
|
|
|
subset, present := set.Sets[other.Owner]
|
2023-05-09 02:11:04 -05:00
|
|
|
if present {
|
2023-07-11 08:43:23 -05:00
|
|
|
return subset.Merge(other.Owner, other.Nodes)
|
2023-05-09 02:11:04 -05:00
|
|
|
}
|
|
|
|
set.Sets[other.Owner] = other
|
|
|
|
return nil
|
|
|
|
}
|
2023-08-01 07:17:32 -05:00
|
|
|
|
|
|
|
// Flatten returns a two-dimensional map for internal nodes.
|
|
|
|
func (set *MergedNodeSet) Flatten() map[common.Hash]map[string]*Node {
|
|
|
|
nodes := make(map[common.Hash]map[string]*Node)
|
|
|
|
for owner, set := range set.Sets {
|
|
|
|
nodes[owner] = set.Nodes
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|