github.com/klaytn/klaytn@v1.10.2/blockchain/state/iterator_test.go (about)

     1  // Copyright 2016 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  // This file is derived from ethdb/iterator_test.go (2020/05/20).
    18  // Modified and improved for the klaytn development.
    19  
    20  package state
    21  
    22  import (
    23  	"bytes"
    24  	"testing"
    25  
    26  	"github.com/klaytn/klaytn/common"
    27  )
    28  
    29  // Tests that the node iterator indeed walks over the entire database contents.
    30  func TestNodeIteratorCoverage(t *testing.T) {
    31  	// Create some arbitrary test state to iterate
    32  	db, root, _ := makeTestState(t)
    33  	db.TrieDB().Commit(root, false, 0)
    34  
    35  	state, err := New(root, db, nil)
    36  	if err != nil {
    37  		t.Fatalf("failed to create state trie at %x: %v", root, err)
    38  	}
    39  	// Gather all the node hashes found by the iterator
    40  	hashes := make(map[common.Hash]struct{})
    41  	for it := NewNodeIterator(state); it.Next(); {
    42  		if it.Hash != (common.Hash{}) {
    43  			hashes[it.Hash] = struct{}{}
    44  		}
    45  	}
    46  	// Cross check the iterated hashes and the database/nodepool content
    47  	for hash := range hashes {
    48  		if _, err = db.TrieDB().Node(hash); err != nil {
    49  			_, err = db.ContractCode(hash)
    50  		}
    51  		if err != nil {
    52  			t.Errorf("failed to retrieve reported node %x", hash)
    53  		}
    54  	}
    55  	for _, hash := range db.TrieDB().Nodes() {
    56  		if _, ok := hashes[hash]; !ok && hash != emptyCode {
    57  			t.Errorf("state entry not reported %x", hash)
    58  		}
    59  	}
    60  	it := db.TrieDB().DiskDB().GetMemDB().NewIterator(nil, nil)
    61  	for it.Next() {
    62  		key := it.Key()
    63  		if bytes.HasPrefix(key, []byte("secure-key-")) {
    64  			continue
    65  		}
    66  		if bytes.Compare(emptyCode[:], common.BytesToHash(key).Bytes()) == 0 {
    67  			continue
    68  		}
    69  		if _, ok := hashes[common.BytesToHash(key)]; !ok {
    70  			t.Errorf("state entry not reported %x", key)
    71  		}
    72  	}
    73  	it.Release()
    74  }