github.com/MetalBlockchain/subnet-evm@v0.6.3/trie/trienode/node.go (about)

     1  // Copyright 2023 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>
    16  
    17  package trienode
    18  
    19  import (
    20  	"fmt"
    21  	"sort"
    22  	"strings"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  )
    26  
    27  // Node is a wrapper which contains the encoded blob of the trie node and its
    28  // node hash. It is general enough that can be used to represent trie node
    29  // corresponding to different trie implementations.
    30  type Node struct {
    31  	Hash common.Hash // Node hash, empty for deleted node
    32  	Blob []byte      // Encoded node blob, nil for the deleted node
    33  }
    34  
    35  // Size returns the total memory size used by this node.
    36  func (n *Node) Size() int {
    37  	return len(n.Blob) + common.HashLength
    38  }
    39  
    40  // IsDeleted returns the indicator if the node is marked as deleted.
    41  func (n *Node) IsDeleted() bool {
    42  	return n.Hash == (common.Hash{})
    43  }
    44  
    45  // New constructs a node with provided node information.
    46  func New(hash common.Hash, blob []byte) *Node {
    47  	return &Node{Hash: hash, Blob: blob}
    48  }
    49  
    50  // NewDeleted constructs a node which is deleted.
    51  func NewDeleted() *Node { return New(common.Hash{}, nil) }
    52  
    53  // leaf represents a trie leaf node
    54  type leaf struct {
    55  	Blob   []byte      // raw blob of leaf
    56  	Parent common.Hash // the hash of parent node
    57  }
    58  
    59  // NodeSet contains a set of nodes collected during the commit operation.
    60  // Each node is keyed by path. It's not thread-safe to use.
    61  type NodeSet struct {
    62  	Owner   common.Hash
    63  	Leaves  []*leaf
    64  	Nodes   map[string]*Node
    65  	updates int // the count of updated and inserted nodes
    66  	deletes int // the count of deleted nodes
    67  }
    68  
    69  // NewNodeSet initializes a node set. The owner is zero for the account trie and
    70  // the owning account address hash for storage tries.
    71  func NewNodeSet(owner common.Hash) *NodeSet {
    72  	return &NodeSet{
    73  		Owner: owner,
    74  		Nodes: make(map[string]*Node),
    75  	}
    76  }
    77  
    78  // ForEachWithOrder iterates the nodes with the order from bottom to top,
    79  // right to left, nodes with the longest path will be iterated first.
    80  func (set *NodeSet) ForEachWithOrder(callback func(path string, n *Node)) {
    81  	var paths []string
    82  	for path := range set.Nodes {
    83  		paths = append(paths, path)
    84  	}
    85  	// Bottom-up, the longest path first
    86  	sort.Sort(sort.Reverse(sort.StringSlice(paths)))
    87  	for _, path := range paths {
    88  		callback(path, set.Nodes[path])
    89  	}
    90  }
    91  
    92  // AddNode adds the provided node into set.
    93  func (set *NodeSet) AddNode(path []byte, n *Node) {
    94  	if n.IsDeleted() {
    95  		set.deletes += 1
    96  	} else {
    97  		set.updates += 1
    98  	}
    99  	set.Nodes[string(path)] = n
   100  }
   101  
   102  // Merge adds a set of nodes into the set.
   103  func (set *NodeSet) Merge(owner common.Hash, nodes map[string]*Node) error {
   104  	if set.Owner != owner {
   105  		return fmt.Errorf("nodesets belong to different owner are not mergeable %x-%x", set.Owner, owner)
   106  	}
   107  	for path, node := range nodes {
   108  		prev, ok := set.Nodes[path]
   109  		if ok {
   110  			// overwrite happens, revoke the counter
   111  			if prev.IsDeleted() {
   112  				set.deletes -= 1
   113  			} else {
   114  				set.updates -= 1
   115  			}
   116  		}
   117  		set.AddNode([]byte(path), node)
   118  	}
   119  	return nil
   120  }
   121  
   122  // AddLeaf adds the provided leaf node into set. TODO(rjl493456442) how can
   123  // we get rid of it?
   124  func (set *NodeSet) AddLeaf(parent common.Hash, blob []byte) {
   125  	set.Leaves = append(set.Leaves, &leaf{Blob: blob, Parent: parent})
   126  }
   127  
   128  // Size returns the number of dirty nodes in set.
   129  func (set *NodeSet) Size() (int, int) {
   130  	return set.updates, set.deletes
   131  }
   132  
   133  // Hashes returns the hashes of all updated nodes. TODO(rjl493456442) how can
   134  // we get rid of it?
   135  func (set *NodeSet) Hashes() []common.Hash {
   136  	var ret []common.Hash
   137  	for _, node := range set.Nodes {
   138  		ret = append(ret, node.Hash)
   139  	}
   140  	return ret
   141  }
   142  
   143  // Summary returns a string-representation of the NodeSet.
   144  func (set *NodeSet) Summary() string {
   145  	var out = new(strings.Builder)
   146  	fmt.Fprintf(out, "nodeset owner: %v\n", set.Owner)
   147  	if set.Nodes != nil {
   148  		for path, n := range set.Nodes {
   149  			// Deletion
   150  			if n.IsDeleted() {
   151  				fmt.Fprintf(out, "  [-]: %x\n", path)
   152  				continue
   153  			}
   154  			// Insertion or update
   155  			fmt.Fprintf(out, "  [+/*]: %x -> %v \n", path, n.Hash)
   156  		}
   157  	}
   158  	for _, n := range set.Leaves {
   159  		fmt.Fprintf(out, "[leaf]: %v\n", n)
   160  	}
   161  	return out.String()
   162  }
   163  
   164  // MergedNodeSet represents a merged node set for a group of tries.
   165  type MergedNodeSet struct {
   166  	Sets map[common.Hash]*NodeSet
   167  }
   168  
   169  // NewMergedNodeSet initializes an empty merged set.
   170  func NewMergedNodeSet() *MergedNodeSet {
   171  	return &MergedNodeSet{Sets: make(map[common.Hash]*NodeSet)}
   172  }
   173  
   174  // NewWithNodeSet constructs a merged nodeset with the provided single set.
   175  func NewWithNodeSet(set *NodeSet) *MergedNodeSet {
   176  	merged := NewMergedNodeSet()
   177  	merged.Merge(set)
   178  	return merged
   179  }
   180  
   181  // Merge merges the provided dirty nodes of a trie into the set. The assumption
   182  // is held that no duplicated set belonging to the same trie will be merged twice.
   183  func (set *MergedNodeSet) Merge(other *NodeSet) error {
   184  	subset, present := set.Sets[other.Owner]
   185  	if present {
   186  		return subset.Merge(other.Owner, other.Nodes)
   187  	}
   188  	set.Sets[other.Owner] = other
   189  	return nil
   190  }
   191  
   192  // Flatten returns a two-dimensional map for internal nodes.
   193  func (set *MergedNodeSet) Flatten() map[common.Hash]map[string]*Node {
   194  	nodes := make(map[common.Hash]map[string]*Node)
   195  	for owner, set := range set.Sets {
   196  		nodes[owner] = set.Nodes
   197  	}
   198  	return nodes
   199  }