github.com/rayrapetyan/go-ethereum@v1.8.21/trie/iterator_test.go (about)

     1  // Copyright 2014 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  	"bytes"
    21  	"fmt"
    22  	"math/rand"
    23  	"testing"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/ethdb"
    27  )
    28  
    29  func TestIterator(t *testing.T) {
    30  	trie := newEmpty()
    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  	all := make(map[string]string)
    41  	for _, val := range vals {
    42  		all[val.k] = val.v
    43  		trie.Update([]byte(val.k), []byte(val.v))
    44  	}
    45  	trie.Commit(nil)
    46  
    47  	found := make(map[string]string)
    48  	it := NewIterator(trie.NodeIterator(nil))
    49  	for it.Next() {
    50  		found[string(it.Key)] = string(it.Value)
    51  	}
    52  
    53  	for k, v := range all {
    54  		if found[k] != v {
    55  			t.Errorf("iterator value mismatch for %s: got %q want %q", k, found[k], v)
    56  		}
    57  	}
    58  }
    59  
    60  type kv struct {
    61  	k, v []byte
    62  	t    bool
    63  }
    64  
    65  func TestIteratorLargeData(t *testing.T) {
    66  	trie := newEmpty()
    67  	vals := make(map[string]*kv)
    68  
    69  	for i := byte(0); i < 255; i++ {
    70  		value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
    71  		value2 := &kv{common.LeftPadBytes([]byte{10, i}, 32), []byte{i}, false}
    72  		trie.Update(value.k, value.v)
    73  		trie.Update(value2.k, value2.v)
    74  		vals[string(value.k)] = value
    75  		vals[string(value2.k)] = value2
    76  	}
    77  
    78  	it := NewIterator(trie.NodeIterator(nil))
    79  	for it.Next() {
    80  		vals[string(it.Key)].t = true
    81  	}
    82  
    83  	var untouched []*kv
    84  	for _, value := range vals {
    85  		if !value.t {
    86  			untouched = append(untouched, value)
    87  		}
    88  	}
    89  
    90  	if len(untouched) > 0 {
    91  		t.Errorf("Missed %d nodes", len(untouched))
    92  		for _, value := range untouched {
    93  			t.Error(value)
    94  		}
    95  	}
    96  }
    97  
    98  // Tests that the node iterator indeed walks over the entire database contents.
    99  func TestNodeIteratorCoverage(t *testing.T) {
   100  	// Create some arbitrary test trie to iterate
   101  	db, trie, _ := makeTestTrie()
   102  
   103  	// Gather all the node hashes found by the iterator
   104  	hashes := make(map[common.Hash]struct{})
   105  	for it := trie.NodeIterator(nil); it.Next(true); {
   106  		if it.Hash() != (common.Hash{}) {
   107  			hashes[it.Hash()] = struct{}{}
   108  		}
   109  	}
   110  	// Cross check the hashes and the database itself
   111  	for hash := range hashes {
   112  		if _, err := db.Node(hash); err != nil {
   113  			t.Errorf("failed to retrieve reported node %x: %v", hash, err)
   114  		}
   115  	}
   116  	for hash, obj := range db.dirties {
   117  		if obj != nil && hash != (common.Hash{}) {
   118  			if _, ok := hashes[hash]; !ok {
   119  				t.Errorf("state entry not reported %x", hash)
   120  			}
   121  		}
   122  	}
   123  	for _, key := range db.diskdb.(*ethdb.MemDatabase).Keys() {
   124  		if _, ok := hashes[common.BytesToHash(key)]; !ok {
   125  			t.Errorf("state entry not reported %x", key)
   126  		}
   127  	}
   128  }
   129  
   130  type kvs struct{ k, v string }
   131  
   132  var testdata1 = []kvs{
   133  	{"barb", "ba"},
   134  	{"bard", "bc"},
   135  	{"bars", "bb"},
   136  	{"bar", "b"},
   137  	{"fab", "z"},
   138  	{"food", "ab"},
   139  	{"foos", "aa"},
   140  	{"foo", "a"},
   141  }
   142  
   143  var testdata2 = []kvs{
   144  	{"aardvark", "c"},
   145  	{"bar", "b"},
   146  	{"barb", "bd"},
   147  	{"bars", "be"},
   148  	{"fab", "z"},
   149  	{"foo", "a"},
   150  	{"foos", "aa"},
   151  	{"food", "ab"},
   152  	{"jars", "d"},
   153  }
   154  
   155  func TestIteratorSeek(t *testing.T) {
   156  	trie := newEmpty()
   157  	for _, val := range testdata1 {
   158  		trie.Update([]byte(val.k), []byte(val.v))
   159  	}
   160  
   161  	// Seek to the middle.
   162  	it := NewIterator(trie.NodeIterator([]byte("fab")))
   163  	if err := checkIteratorOrder(testdata1[4:], it); err != nil {
   164  		t.Fatal(err)
   165  	}
   166  
   167  	// Seek to a non-existent key.
   168  	it = NewIterator(trie.NodeIterator([]byte("barc")))
   169  	if err := checkIteratorOrder(testdata1[1:], it); err != nil {
   170  		t.Fatal(err)
   171  	}
   172  
   173  	// Seek beyond the end.
   174  	it = NewIterator(trie.NodeIterator([]byte("z")))
   175  	if err := checkIteratorOrder(nil, it); err != nil {
   176  		t.Fatal(err)
   177  	}
   178  }
   179  
   180  func checkIteratorOrder(want []kvs, it *Iterator) error {
   181  	for it.Next() {
   182  		if len(want) == 0 {
   183  			return fmt.Errorf("didn't expect any more values, got key %q", it.Key)
   184  		}
   185  		if !bytes.Equal(it.Key, []byte(want[0].k)) {
   186  			return fmt.Errorf("wrong key: got %q, want %q", it.Key, want[0].k)
   187  		}
   188  		want = want[1:]
   189  	}
   190  	if len(want) > 0 {
   191  		return fmt.Errorf("iterator ended early, want key %q", want[0])
   192  	}
   193  	return nil
   194  }
   195  
   196  func TestDifferenceIterator(t *testing.T) {
   197  	triea := newEmpty()
   198  	for _, val := range testdata1 {
   199  		triea.Update([]byte(val.k), []byte(val.v))
   200  	}
   201  	triea.Commit(nil)
   202  
   203  	trieb := newEmpty()
   204  	for _, val := range testdata2 {
   205  		trieb.Update([]byte(val.k), []byte(val.v))
   206  	}
   207  	trieb.Commit(nil)
   208  
   209  	found := make(map[string]string)
   210  	di, _ := NewDifferenceIterator(triea.NodeIterator(nil), trieb.NodeIterator(nil))
   211  	it := NewIterator(di)
   212  	for it.Next() {
   213  		found[string(it.Key)] = string(it.Value)
   214  	}
   215  
   216  	all := []struct{ k, v string }{
   217  		{"aardvark", "c"},
   218  		{"barb", "bd"},
   219  		{"bars", "be"},
   220  		{"jars", "d"},
   221  	}
   222  	for _, item := range all {
   223  		if found[item.k] != item.v {
   224  			t.Errorf("iterator value mismatch for %s: got %v want %v", item.k, found[item.k], item.v)
   225  		}
   226  	}
   227  	if len(found) != len(all) {
   228  		t.Errorf("iterator count mismatch: got %d values, want %d", len(found), len(all))
   229  	}
   230  }
   231  
   232  func TestUnionIterator(t *testing.T) {
   233  	triea := newEmpty()
   234  	for _, val := range testdata1 {
   235  		triea.Update([]byte(val.k), []byte(val.v))
   236  	}
   237  	triea.Commit(nil)
   238  
   239  	trieb := newEmpty()
   240  	for _, val := range testdata2 {
   241  		trieb.Update([]byte(val.k), []byte(val.v))
   242  	}
   243  	trieb.Commit(nil)
   244  
   245  	di, _ := NewUnionIterator([]NodeIterator{triea.NodeIterator(nil), trieb.NodeIterator(nil)})
   246  	it := NewIterator(di)
   247  
   248  	all := []struct{ k, v string }{
   249  		{"aardvark", "c"},
   250  		{"barb", "ba"},
   251  		{"barb", "bd"},
   252  		{"bard", "bc"},
   253  		{"bars", "bb"},
   254  		{"bars", "be"},
   255  		{"bar", "b"},
   256  		{"fab", "z"},
   257  		{"food", "ab"},
   258  		{"foos", "aa"},
   259  		{"foo", "a"},
   260  		{"jars", "d"},
   261  	}
   262  
   263  	for i, kv := range all {
   264  		if !it.Next() {
   265  			t.Errorf("Iterator ends prematurely at element %d", i)
   266  		}
   267  		if kv.k != string(it.Key) {
   268  			t.Errorf("iterator value mismatch for element %d: got key %s want %s", i, it.Key, kv.k)
   269  		}
   270  		if kv.v != string(it.Value) {
   271  			t.Errorf("iterator value mismatch for element %d: got value %s want %s", i, it.Value, kv.v)
   272  		}
   273  	}
   274  	if it.Next() {
   275  		t.Errorf("Iterator returned extra values.")
   276  	}
   277  }
   278  
   279  func TestIteratorNoDups(t *testing.T) {
   280  	var tr Trie
   281  	for _, val := range testdata1 {
   282  		tr.Update([]byte(val.k), []byte(val.v))
   283  	}
   284  	checkIteratorNoDups(t, tr.NodeIterator(nil), nil)
   285  }
   286  
   287  // This test checks that nodeIterator.Next can be retried after inserting missing trie nodes.
   288  func TestIteratorContinueAfterErrorDisk(t *testing.T)    { testIteratorContinueAfterError(t, false) }
   289  func TestIteratorContinueAfterErrorMemonly(t *testing.T) { testIteratorContinueAfterError(t, true) }
   290  
   291  func testIteratorContinueAfterError(t *testing.T, memonly bool) {
   292  	diskdb := ethdb.NewMemDatabase()
   293  	triedb := NewDatabase(diskdb)
   294  
   295  	tr, _ := New(common.Hash{}, triedb)
   296  	for _, val := range testdata1 {
   297  		tr.Update([]byte(val.k), []byte(val.v))
   298  	}
   299  	tr.Commit(nil)
   300  	if !memonly {
   301  		triedb.Commit(tr.Hash(), true)
   302  	}
   303  	wantNodeCount := checkIteratorNoDups(t, tr.NodeIterator(nil), nil)
   304  
   305  	var (
   306  		diskKeys [][]byte
   307  		memKeys  []common.Hash
   308  	)
   309  	if memonly {
   310  		memKeys = triedb.Nodes()
   311  	} else {
   312  		diskKeys = diskdb.Keys()
   313  	}
   314  	for i := 0; i < 20; i++ {
   315  		// Create trie that will load all nodes from DB.
   316  		tr, _ := New(tr.Hash(), triedb)
   317  
   318  		// Remove a random node from the database. It can't be the root node
   319  		// because that one is already loaded.
   320  		var (
   321  			rkey common.Hash
   322  			rval []byte
   323  			robj *cachedNode
   324  		)
   325  		for {
   326  			if memonly {
   327  				rkey = memKeys[rand.Intn(len(memKeys))]
   328  			} else {
   329  				copy(rkey[:], diskKeys[rand.Intn(len(diskKeys))])
   330  			}
   331  			if rkey != tr.Hash() {
   332  				break
   333  			}
   334  		}
   335  		if memonly {
   336  			robj = triedb.dirties[rkey]
   337  			delete(triedb.dirties, rkey)
   338  		} else {
   339  			rval, _ = diskdb.Get(rkey[:])
   340  			diskdb.Delete(rkey[:])
   341  		}
   342  		// Iterate until the error is hit.
   343  		seen := make(map[string]bool)
   344  		it := tr.NodeIterator(nil)
   345  		checkIteratorNoDups(t, it, seen)
   346  		missing, ok := it.Error().(*MissingNodeError)
   347  		if !ok || missing.NodeHash != rkey {
   348  			t.Fatal("didn't hit missing node, got", it.Error())
   349  		}
   350  
   351  		// Add the node back and continue iteration.
   352  		if memonly {
   353  			triedb.dirties[rkey] = robj
   354  		} else {
   355  			diskdb.Put(rkey[:], rval)
   356  		}
   357  		checkIteratorNoDups(t, it, seen)
   358  		if it.Error() != nil {
   359  			t.Fatal("unexpected error", it.Error())
   360  		}
   361  		if len(seen) != wantNodeCount {
   362  			t.Fatal("wrong node iteration count, got", len(seen), "want", wantNodeCount)
   363  		}
   364  	}
   365  }
   366  
   367  // Similar to the test above, this one checks that failure to create nodeIterator at a
   368  // certain key prefix behaves correctly when Next is called. The expectation is that Next
   369  // should retry seeking before returning true for the first time.
   370  func TestIteratorContinueAfterSeekErrorDisk(t *testing.T) {
   371  	testIteratorContinueAfterSeekError(t, false)
   372  }
   373  func TestIteratorContinueAfterSeekErrorMemonly(t *testing.T) {
   374  	testIteratorContinueAfterSeekError(t, true)
   375  }
   376  
   377  func testIteratorContinueAfterSeekError(t *testing.T, memonly bool) {
   378  	// Commit test trie to db, then remove the node containing "bars".
   379  	diskdb := ethdb.NewMemDatabase()
   380  	triedb := NewDatabase(diskdb)
   381  
   382  	ctr, _ := New(common.Hash{}, triedb)
   383  	for _, val := range testdata1 {
   384  		ctr.Update([]byte(val.k), []byte(val.v))
   385  	}
   386  	root, _ := ctr.Commit(nil)
   387  	if !memonly {
   388  		triedb.Commit(root, true)
   389  	}
   390  	barNodeHash := common.HexToHash("05041990364eb72fcb1127652ce40d8bab765f2bfe53225b1170d276cc101c2e")
   391  	var (
   392  		barNodeBlob []byte
   393  		barNodeObj  *cachedNode
   394  	)
   395  	if memonly {
   396  		barNodeObj = triedb.dirties[barNodeHash]
   397  		delete(triedb.dirties, barNodeHash)
   398  	} else {
   399  		barNodeBlob, _ = diskdb.Get(barNodeHash[:])
   400  		diskdb.Delete(barNodeHash[:])
   401  	}
   402  	// Create a new iterator that seeks to "bars". Seeking can't proceed because
   403  	// the node is missing.
   404  	tr, _ := New(root, triedb)
   405  	it := tr.NodeIterator([]byte("bars"))
   406  	missing, ok := it.Error().(*MissingNodeError)
   407  	if !ok {
   408  		t.Fatal("want MissingNodeError, got", it.Error())
   409  	} else if missing.NodeHash != barNodeHash {
   410  		t.Fatal("wrong node missing")
   411  	}
   412  	// Reinsert the missing node.
   413  	if memonly {
   414  		triedb.dirties[barNodeHash] = barNodeObj
   415  	} else {
   416  		diskdb.Put(barNodeHash[:], barNodeBlob)
   417  	}
   418  	// Check that iteration produces the right set of values.
   419  	if err := checkIteratorOrder(testdata1[2:], NewIterator(it)); err != nil {
   420  		t.Fatal(err)
   421  	}
   422  }
   423  
   424  func checkIteratorNoDups(t *testing.T, it NodeIterator, seen map[string]bool) int {
   425  	if seen == nil {
   426  		seen = make(map[string]bool)
   427  	}
   428  	for it.Next(true) {
   429  		if seen[string(it.Path())] {
   430  			t.Fatalf("iterator visited node path %x twice", it.Path())
   431  		}
   432  		seen[string(it.Path())] = true
   433  	}
   434  	return len(seen)
   435  }