github.com/sexdefi/go-ethereum@v0.0.0-20230807164010-b4cd42fe399f/trie/nodeset.go (about)

     1  // Copyright 2022 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 trie
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/sexdefi/go-ethereum/common"
    23  )
    24  
    25  // memoryNode is all the information we know about a single cached trie node
    26  // in the memory.
    27  type memoryNode struct {
    28  	hash common.Hash // Node hash, computed by hashing rlp value
    29  	size uint16      // Byte size of the useful cached data
    30  	node node        // Cached collapsed trie node, or raw rlp data
    31  }
    32  
    33  // NodeSet contains all dirty nodes collected during the commit operation.
    34  // Each node is keyed by path. It's not thread-safe to use.
    35  type NodeSet struct {
    36  	owner  common.Hash            // the identifier of the trie
    37  	paths  []string               // the path of dirty nodes, sort by insertion order
    38  	nodes  map[string]*memoryNode // the map of dirty nodes, keyed by node path
    39  	leaves []*leaf                // the list of dirty leaves
    40  }
    41  
    42  // NewNodeSet initializes an empty node set to be used for tracking dirty nodes
    43  // from a specific account or storage trie. The owner is zero for the account
    44  // trie and the owning account address hash for storage tries.
    45  func NewNodeSet(owner common.Hash) *NodeSet {
    46  	return &NodeSet{
    47  		owner: owner,
    48  		nodes: make(map[string]*memoryNode),
    49  	}
    50  }
    51  
    52  // add caches node with provided path and node object.
    53  func (set *NodeSet) add(path string, node *memoryNode) {
    54  	set.paths = append(set.paths, path)
    55  	set.nodes[path] = node
    56  }
    57  
    58  // addLeaf caches the provided leaf node.
    59  func (set *NodeSet) addLeaf(node *leaf) {
    60  	set.leaves = append(set.leaves, node)
    61  }
    62  
    63  // Len returns the number of dirty nodes contained in the set.
    64  func (set *NodeSet) Len() int {
    65  	return len(set.nodes)
    66  }
    67  
    68  // MergedNodeSet represents a merged dirty node set for a group of tries.
    69  type MergedNodeSet struct {
    70  	sets map[common.Hash]*NodeSet
    71  }
    72  
    73  // NewMergedNodeSet initializes an empty merged set.
    74  func NewMergedNodeSet() *MergedNodeSet {
    75  	return &MergedNodeSet{sets: make(map[common.Hash]*NodeSet)}
    76  }
    77  
    78  // NewWithNodeSet constructs a merged nodeset with the provided single set.
    79  func NewWithNodeSet(set *NodeSet) *MergedNodeSet {
    80  	merged := NewMergedNodeSet()
    81  	merged.Merge(set)
    82  	return merged
    83  }
    84  
    85  // Merge merges the provided dirty nodes of a trie into the set. The assumption
    86  // is held that no duplicated set belonging to the same trie will be merged twice.
    87  func (set *MergedNodeSet) Merge(other *NodeSet) error {
    88  	_, present := set.sets[other.owner]
    89  	if present {
    90  		return fmt.Errorf("duplicate trie for owner %#x", other.owner)
    91  	}
    92  	set.sets[other.owner] = other
    93  	return nil
    94  }