github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/core/state/iterator_test.go (about)

     1  package state
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/quickchainproject/quickchain/common"
     8  	"github.com/quickchainproject/quickchain/qctdb"
     9  )
    10  
    11  // Tests that the node iterator indeed walks over the entire database contents.
    12  func TestNodeIteratorCoverage(t *testing.T) {
    13  	// Create some arbitrary test state to iterate
    14  	db, root, _ := makeTestState()
    15  
    16  	state, err := New(root, db)
    17  	if err != nil {
    18  		t.Fatalf("failed to create state trie at %x: %v", root, err)
    19  	}
    20  	// Gather all the node hashes found by the iterator
    21  	hashes := make(map[common.Hash]struct{})
    22  	for it := NewNodeIterator(state); it.Next(); {
    23  		if it.Hash != (common.Hash{}) {
    24  			hashes[it.Hash] = struct{}{}
    25  		}
    26  	}
    27  	// Cross check the iterated hashes and the database/nodepool content
    28  	for hash := range hashes {
    29  		if _, err := db.TrieDB().Node(hash); err != nil {
    30  			t.Errorf("failed to retrieve reported node %x", hash)
    31  		}
    32  	}
    33  	for _, hash := range db.TrieDB().Nodes() {
    34  		if _, ok := hashes[hash]; !ok {
    35  			t.Errorf("state entry not reported %x", hash)
    36  		}
    37  	}
    38  	for _, key := range db.TrieDB().DiskDB().(*qctdb.MemDatabase).Keys() {
    39  		if bytes.HasPrefix(key, []byte("secure-key-")) {
    40  			continue
    41  		}
    42  		if _, ok := hashes[common.BytesToHash(key)]; !ok {
    43  			t.Errorf("state entry not reported %x", key)
    44  		}
    45  	}
    46  }