github.com/MetalBlockchain/subnet-evm@v0.4.9/trie/util_test.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  import (
    30  	"testing"
    31  
    32  	"github.com/MetalBlockchain/subnet-evm/core/rawdb"
    33  	"github.com/ethereum/go-ethereum/common"
    34  )
    35  
    36  // Tests if the trie diffs are tracked correctly.
    37  func TestTrieTracer(t *testing.T) {
    38  	db := NewDatabase(rawdb.NewMemoryDatabase())
    39  	trie := NewEmpty(db)
    40  	trie.tracer = newTracer()
    41  
    42  	// Insert a batch of entries, all the nodes should be marked as inserted
    43  	vals := []struct{ k, v string }{
    44  		{"do", "verb"},
    45  		{"ether", "wookiedoo"},
    46  		{"horse", "stallion"},
    47  		{"shaman", "horse"},
    48  		{"doge", "coin"},
    49  		{"dog", "puppy"},
    50  		{"somethingveryoddindeedthis is", "myothernodedata"},
    51  	}
    52  	for _, val := range vals {
    53  		trie.Update([]byte(val.k), []byte(val.v))
    54  	}
    55  	trie.Hash()
    56  
    57  	seen := make(map[string]struct{})
    58  	it := trie.NodeIterator(nil)
    59  	for it.Next(true) {
    60  		if it.Leaf() {
    61  			continue
    62  		}
    63  		seen[string(it.Path())] = struct{}{}
    64  	}
    65  	inserted := trie.tracer.insertList()
    66  	if len(inserted) != len(seen) {
    67  		t.Fatalf("Unexpected inserted node tracked want %d got %d", len(seen), len(inserted))
    68  	}
    69  	for _, k := range inserted {
    70  		_, ok := seen[string(k)]
    71  		if !ok {
    72  			t.Fatalf("Unexpected inserted node")
    73  		}
    74  	}
    75  	deleted := trie.tracer.deleteList()
    76  	if len(deleted) != 0 {
    77  		t.Fatalf("Unexpected deleted node tracked %d", len(deleted))
    78  	}
    79  
    80  	// Commit the changes and re-create with new root
    81  	root, nodes, _ := trie.Commit(false)
    82  	db.Update(NewWithNodeSet(nodes))
    83  	trie, _ = New(common.Hash{}, root, db)
    84  	trie.tracer = newTracer()
    85  
    86  	// Delete all the elements, check deletion set
    87  	for _, val := range vals {
    88  		trie.Delete([]byte(val.k))
    89  	}
    90  	trie.Hash()
    91  
    92  	inserted = trie.tracer.insertList()
    93  	if len(inserted) != 0 {
    94  		t.Fatalf("Unexpected inserted node tracked %d", len(inserted))
    95  	}
    96  	deleted = trie.tracer.deleteList()
    97  	if len(deleted) != len(seen) {
    98  		t.Fatalf("Unexpected deleted node tracked want %d got %d", len(seen), len(deleted))
    99  	}
   100  	for _, k := range deleted {
   101  		_, ok := seen[string(k)]
   102  		if !ok {
   103  			t.Fatalf("Unexpected inserted node")
   104  		}
   105  	}
   106  }
   107  
   108  func TestTrieTracerNoop(t *testing.T) {
   109  	trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase()))
   110  	trie.tracer = newTracer()
   111  
   112  	// Insert a batch of entries, all the nodes should be marked as inserted
   113  	vals := []struct{ k, v string }{
   114  		{"do", "verb"},
   115  		{"ether", "wookiedoo"},
   116  		{"horse", "stallion"},
   117  		{"shaman", "horse"},
   118  		{"doge", "coin"},
   119  		{"dog", "puppy"},
   120  		{"somethingveryoddindeedthis is", "myothernodedata"},
   121  	}
   122  	for _, val := range vals {
   123  		trie.Update([]byte(val.k), []byte(val.v))
   124  	}
   125  	for _, val := range vals {
   126  		trie.Delete([]byte(val.k))
   127  	}
   128  	if len(trie.tracer.insertList()) != 0 {
   129  		t.Fatalf("Unexpected inserted node tracked %d", len(trie.tracer.insertList()))
   130  	}
   131  	if len(trie.tracer.deleteList()) != 0 {
   132  		t.Fatalf("Unexpected deleted node tracked %d", len(trie.tracer.deleteList()))
   133  	}
   134  }