github.com/MetalBlockchain/subnet-evm@v0.4.9/trie/utils.go (about)

     1  // (c) 2022, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2022 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package trie
    28  
    29  // tracer tracks the changes of trie nodes. During the trie operations,
    30  // some nodes can be deleted from the trie, while these deleted nodes
    31  // won't be captured by trie.Hasher or trie.Committer. Thus, these deleted
    32  // nodes won't be removed from the disk at all. Tracer is an auxiliary tool
    33  // used to track all insert and delete operations of trie and capture all
    34  // deleted nodes eventually.
    35  //
    36  // The changed nodes can be mainly divided into two categories: the leaf
    37  // node and intermediate node. The former is inserted/deleted by callers
    38  // while the latter is inserted/deleted in order to follow the rule of trie.
    39  // This tool can track all of them no matter the node is embedded in its
    40  // parent or not, but valueNode is never tracked.
    41  //
    42  // Besides, it's also used for recording the original value of the nodes
    43  // when they are resolved from the disk. The pre-value of the nodes will
    44  // be used to construct reverse-diffs in the future.
    45  //
    46  // Note tracer is not thread-safe, callers should be responsible for handling
    47  // the concurrency issues by themselves.
    48  type tracer struct {
    49  	insert map[string]struct{}
    50  	delete map[string]struct{}
    51  	origin map[string][]byte
    52  }
    53  
    54  // newTracer initializes the tracer for capturing trie changes.
    55  func newTracer() *tracer {
    56  	return &tracer{
    57  		insert: make(map[string]struct{}),
    58  		delete: make(map[string]struct{}),
    59  		origin: make(map[string][]byte),
    60  	}
    61  }
    62  
    63  /*
    64  // onRead tracks the newly loaded trie node and caches the rlp-encoded blob internally.
    65  // Don't change the value outside of function since it's not deep-copied.
    66  func (t *tracer) onRead(key []byte, val []byte) {
    67  	// Tracer isn't used right now, remove this check later.
    68  	if t == nil {
    69  		return
    70  	}
    71  	t.origin[string(key)] = val
    72  }
    73  */
    74  
    75  // onInsert tracks the newly inserted trie node. If it's already in the deletion set
    76  // (resurrected node), then just wipe it from the deletion set as the "untouched".
    77  func (t *tracer) onInsert(key []byte) {
    78  	// Tracer isn't used right now, remove this check later.
    79  	if t == nil {
    80  		return
    81  	}
    82  	if _, present := t.delete[string(key)]; present {
    83  		delete(t.delete, string(key))
    84  		return
    85  	}
    86  	t.insert[string(key)] = struct{}{}
    87  }
    88  
    89  // onDelete tracks the newly deleted trie node. If it's already
    90  // in the addition set, then just wipe it from the addition set
    91  // as it's untouched.
    92  func (t *tracer) onDelete(key []byte) {
    93  	// Tracer isn't used right now, remove this check later.
    94  	if t == nil {
    95  		return
    96  	}
    97  	if _, present := t.insert[string(key)]; present {
    98  		delete(t.insert, string(key))
    99  		return
   100  	}
   101  	t.delete[string(key)] = struct{}{}
   102  }
   103  
   104  // insertList returns the tracked inserted trie nodes in list format.
   105  func (t *tracer) insertList() [][]byte {
   106  	// Tracer isn't used right now, remove this check later.
   107  	if t == nil {
   108  		return nil
   109  	}
   110  	var ret [][]byte
   111  	for key := range t.insert {
   112  		ret = append(ret, []byte(key))
   113  	}
   114  	return ret
   115  }
   116  
   117  // deleteList returns the tracked deleted trie nodes in list format.
   118  func (t *tracer) deleteList() [][]byte {
   119  	// Tracer isn't used right now, remove this check later.
   120  	if t == nil {
   121  		return nil
   122  	}
   123  	var ret [][]byte
   124  	for key := range t.delete {
   125  		ret = append(ret, []byte(key))
   126  	}
   127  	return ret
   128  }
   129  
   130  /*
   131  // getPrev returns the cached original value of the specified node.
   132  func (t *tracer) getPrev(key []byte) []byte {
   133  	// Don't panic on uninitialized tracer, it's possible in testing.
   134  	if t == nil {
   135  		return nil
   136  	}
   137  	return t.origin[string(key)]
   138  }
   139  */
   140  
   141  // reset clears the content tracked by tracer.
   142  func (t *tracer) reset() {
   143  	// Tracer isn't used right now, remove this check later.
   144  	if t == nil {
   145  		return
   146  	}
   147  	t.insert = make(map[string]struct{})
   148  	t.delete = make(map[string]struct{})
   149  	t.origin = make(map[string][]byte)
   150  }
   151  
   152  // copy returns a deep copied tracer instance.
   153  func (t *tracer) copy() *tracer {
   154  	// Tracer isn't used right now, remove this check later.
   155  	if t == nil {
   156  		return nil
   157  	}
   158  	var (
   159  		insert = make(map[string]struct{})
   160  		delete = make(map[string]struct{})
   161  		origin = make(map[string][]byte)
   162  	)
   163  	for key := range t.insert {
   164  		insert[key] = struct{}{}
   165  	}
   166  	for key := range t.delete {
   167  		delete[key] = struct{}{}
   168  	}
   169  	for key, val := range t.origin {
   170  		origin[key] = val
   171  	}
   172  	return &tracer{
   173  		insert: insert,
   174  		delete: delete,
   175  		origin: origin,
   176  	}
   177  }