github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/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/electroneum/electroneum-sc/common"
    23  	"github.com/electroneum/electroneum-sc/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, _ := New(common.Hash{}, 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
    71  	trie.Commit(nil)
    72  
    73  	// Delete all the elements, check deletion set
    74  	for _, val := range vals {
    75  		trie.Delete([]byte(val.k))
    76  	}
    77  	trie.Hash()
    78  
    79  	inserted = trie.tracer.insertList()
    80  	if len(inserted) != 0 {
    81  		t.Fatalf("Unexpected inserted node tracked %d", len(inserted))
    82  	}
    83  	deleted = trie.tracer.deleteList()
    84  	if len(deleted) != len(seen) {
    85  		t.Fatalf("Unexpected deleted node tracked want %d got %d", len(seen), len(deleted))
    86  	}
    87  	for _, k := range deleted {
    88  		_, ok := seen[string(k)]
    89  		if !ok {
    90  			t.Fatalf("Unexpected inserted node")
    91  		}
    92  	}
    93  }
    94  
    95  func TestTrieTracerNoop(t *testing.T) {
    96  	db := NewDatabase(rawdb.NewMemoryDatabase())
    97  	trie, _ := New(common.Hash{}, db)
    98  	trie.tracer = newTracer()
    99  
   100  	// Insert a batch of entries, all the nodes should be marked as inserted
   101  	vals := []struct{ k, v string }{
   102  		{"do", "verb"},
   103  		{"ether", "wookiedoo"},
   104  		{"horse", "stallion"},
   105  		{"shaman", "horse"},
   106  		{"doge", "coin"},
   107  		{"dog", "puppy"},
   108  		{"somethingveryoddindeedthis is", "myothernodedata"},
   109  	}
   110  	for _, val := range vals {
   111  		trie.Update([]byte(val.k), []byte(val.v))
   112  	}
   113  	for _, val := range vals {
   114  		trie.Delete([]byte(val.k))
   115  	}
   116  	if len(trie.tracer.insertList()) != 0 {
   117  		t.Fatalf("Unexpected inserted node tracked %d", len(trie.tracer.insertList()))
   118  	}
   119  	if len(trie.tracer.deleteList()) != 0 {
   120  		t.Fatalf("Unexpected deleted node tracked %d", len(trie.tracer.deleteList()))
   121  	}
   122  }