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