github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/state/iterator_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package state 13 14 import ( 15 "bytes" 16 "testing" 17 18 "github.com/Sberex/go-sberex/common" 19 "github.com/Sberex/go-sberex/ethdb" 20 ) 21 22 // Tests that the node iterator indeed walks over the entire database contents. 23 func TestNodeIteratorCoverage(t *testing.T) { 24 // Create some arbitrary test state to iterate 25 db, root, _ := makeTestState() 26 27 state, err := New(root, db) 28 if err != nil { 29 t.Fatalf("failed to create state trie at %x: %v", root, err) 30 } 31 // Gather all the node hashes found by the iterator 32 hashes := make(map[common.Hash]struct{}) 33 for it := NewNodeIterator(state); it.Next(); { 34 if it.Hash != (common.Hash{}) { 35 hashes[it.Hash] = struct{}{} 36 } 37 } 38 // Cross check the iterated hashes and the database/nodepool content 39 for hash := range hashes { 40 if _, err := db.TrieDB().Node(hash); err != nil { 41 t.Errorf("failed to retrieve reported node %x", hash) 42 } 43 } 44 for _, hash := range db.TrieDB().Nodes() { 45 if _, ok := hashes[hash]; !ok { 46 t.Errorf("state entry not reported %x", hash) 47 } 48 } 49 for _, key := range db.TrieDB().DiskDB().(*ethdb.MemDatabase).Keys() { 50 if bytes.HasPrefix(key, []byte("secure-key-")) { 51 continue 52 } 53 if _, ok := hashes[common.BytesToHash(key)]; !ok { 54 t.Errorf("state entry not reported %x", key) 55 } 56 } 57 }