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