github.com/theQRL/go-zond@v0.1.1/zond/api_debug_test.go (about) 1 // Copyright 2017 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 zond 18 19 import ( 20 "bytes" 21 "fmt" 22 "math/big" 23 "reflect" 24 "testing" 25 26 "github.com/davecgh/go-spew/spew" 27 "github.com/theQRL/go-zond/common" 28 "github.com/theQRL/go-zond/core/rawdb" 29 "github.com/theQRL/go-zond/core/state" 30 "github.com/theQRL/go-zond/core/types" 31 "github.com/theQRL/go-zond/crypto" 32 "github.com/theQRL/go-zond/trie" 33 "golang.org/x/exp/slices" 34 ) 35 36 var dumper = spew.ConfigState{Indent: " "} 37 38 func accountRangeTest(t *testing.T, trie *state.Trie, statedb *state.StateDB, start common.Hash, requestedNum int, expectedNum int) state.IteratorDump { 39 result := statedb.IteratorDump(&state.DumpConfig{ 40 SkipCode: true, 41 SkipStorage: true, 42 OnlyWithAddresses: false, 43 Start: start.Bytes(), 44 Max: uint64(requestedNum), 45 }) 46 47 if len(result.Accounts) != expectedNum { 48 t.Fatalf("expected %d results, got %d", expectedNum, len(result.Accounts)) 49 } 50 for address := range result.Accounts { 51 if address == (common.Address{}) { 52 t.Fatalf("empty address returned") 53 } 54 if !statedb.Exist(address) { 55 t.Fatalf("account not found in state %s", address.Hex()) 56 } 57 } 58 return result 59 } 60 61 func TestAccountRange(t *testing.T) { 62 t.Parallel() 63 64 var ( 65 statedb = state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), &trie.Config{Preimages: true}) 66 sdb, _ = state.New(types.EmptyRootHash, statedb, nil) 67 addrs = [AccountRangeMaxResults * 2]common.Address{} 68 m = map[common.Address]bool{} 69 ) 70 71 for i := range addrs { 72 hash := common.HexToHash(fmt.Sprintf("%x", i)) 73 addr := common.BytesToAddress(crypto.Keccak256Hash(hash.Bytes()).Bytes()) 74 addrs[i] = addr 75 sdb.SetBalance(addrs[i], big.NewInt(1)) 76 if _, ok := m[addr]; ok { 77 t.Fatalf("bad") 78 } else { 79 m[addr] = true 80 } 81 } 82 root, _ := sdb.Commit(0, true) 83 sdb, _ = state.New(root, statedb, nil) 84 85 trie, err := statedb.OpenTrie(root) 86 if err != nil { 87 t.Fatal(err) 88 } 89 accountRangeTest(t, &trie, sdb, common.Hash{}, AccountRangeMaxResults/2, AccountRangeMaxResults/2) 90 // test pagination 91 firstResult := accountRangeTest(t, &trie, sdb, common.Hash{}, AccountRangeMaxResults, AccountRangeMaxResults) 92 secondResult := accountRangeTest(t, &trie, sdb, common.BytesToHash(firstResult.Next), AccountRangeMaxResults, AccountRangeMaxResults) 93 94 hList := make([]common.Hash, 0) 95 for addr1 := range firstResult.Accounts { 96 // If address is empty, then it makes no sense to compare 97 // them as they might be two different accounts. 98 if addr1 == (common.Address{}) { 99 continue 100 } 101 if _, duplicate := secondResult.Accounts[addr1]; duplicate { 102 t.Fatalf("pagination test failed: results should not overlap") 103 } 104 hList = append(hList, crypto.Keccak256Hash(addr1.Bytes())) 105 } 106 // Test to see if it's possible to recover from the middle of the previous 107 // set and get an even split between the first and second sets. 108 slices.SortFunc(hList, common.Hash.Cmp) 109 middleH := hList[AccountRangeMaxResults/2] 110 middleResult := accountRangeTest(t, &trie, sdb, middleH, AccountRangeMaxResults, AccountRangeMaxResults) 111 missing, infirst, insecond := 0, 0, 0 112 for h := range middleResult.Accounts { 113 if _, ok := firstResult.Accounts[h]; ok { 114 infirst++ 115 } else if _, ok := secondResult.Accounts[h]; ok { 116 insecond++ 117 } else { 118 missing++ 119 } 120 } 121 if missing != 0 { 122 t.Fatalf("%d hashes in the 'middle' set were neither in the first not the second set", missing) 123 } 124 if infirst != AccountRangeMaxResults/2 { 125 t.Fatalf("Imbalance in the number of first-test results: %d != %d", infirst, AccountRangeMaxResults/2) 126 } 127 if insecond != AccountRangeMaxResults/2 { 128 t.Fatalf("Imbalance in the number of second-test results: %d != %d", insecond, AccountRangeMaxResults/2) 129 } 130 } 131 132 func TestEmptyAccountRange(t *testing.T) { 133 t.Parallel() 134 135 var ( 136 statedb = state.NewDatabase(rawdb.NewMemoryDatabase()) 137 st, _ = state.New(types.EmptyRootHash, statedb, nil) 138 ) 139 // Commit(although nothing to flush) and re-init the statedb 140 st.Commit(0, true) 141 st, _ = state.New(types.EmptyRootHash, statedb, nil) 142 143 results := st.IteratorDump(&state.DumpConfig{ 144 SkipCode: true, 145 SkipStorage: true, 146 OnlyWithAddresses: true, 147 Max: uint64(AccountRangeMaxResults), 148 }) 149 if bytes.Equal(results.Next, (common.Hash{}).Bytes()) { 150 t.Fatalf("Empty results should not return a second page") 151 } 152 if len(results.Accounts) != 0 { 153 t.Fatalf("Empty state should not return addresses: %v", results.Accounts) 154 } 155 } 156 157 func TestStorageRangeAt(t *testing.T) { 158 t.Parallel() 159 160 // Create a state where account 0x010000... has a few storage entries. 161 var ( 162 db = state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), &trie.Config{Preimages: true}) 163 sdb, _ = state.New(types.EmptyRootHash, db, nil) 164 addr = common.Address{0x01} 165 keys = []common.Hash{ // hashes of Keys of storage 166 common.HexToHash("340dd630ad21bf010b4e676dbfa9ba9a02175262d1fa356232cfde6cb5b47ef2"), 167 common.HexToHash("426fcb404ab2d5d8e61a3d918108006bbb0a9be65e92235bb10eefbdb6dcd053"), 168 common.HexToHash("48078cfed56339ea54962e72c37c7f588fc4f8e5bc173827ba75cb10a63a96a5"), 169 common.HexToHash("5723d2c3a83af9b735e3b7f21531e5623d183a9095a56604ead41f3582fdfb75"), 170 } 171 storage = storageMap{ 172 keys[0]: {Key: &common.Hash{0x02}, Value: common.Hash{0x01}}, 173 keys[1]: {Key: &common.Hash{0x04}, Value: common.Hash{0x02}}, 174 keys[2]: {Key: &common.Hash{0x01}, Value: common.Hash{0x03}}, 175 keys[3]: {Key: &common.Hash{0x03}, Value: common.Hash{0x04}}, 176 } 177 ) 178 for _, entry := range storage { 179 sdb.SetState(addr, *entry.Key, entry.Value) 180 } 181 root, _ := sdb.Commit(0, false) 182 sdb, _ = state.New(root, db, nil) 183 184 // Check a few combinations of limit and start/end. 185 tests := []struct { 186 start []byte 187 limit int 188 want StorageRangeResult 189 }{ 190 { 191 start: []byte{}, limit: 0, 192 want: StorageRangeResult{storageMap{}, &keys[0]}, 193 }, 194 { 195 start: []byte{}, limit: 100, 196 want: StorageRangeResult{storage, nil}, 197 }, 198 { 199 start: []byte{}, limit: 2, 200 want: StorageRangeResult{storageMap{keys[0]: storage[keys[0]], keys[1]: storage[keys[1]]}, &keys[2]}, 201 }, 202 { 203 start: []byte{0x00}, limit: 4, 204 want: StorageRangeResult{storage, nil}, 205 }, 206 { 207 start: []byte{0x40}, limit: 2, 208 want: StorageRangeResult{storageMap{keys[1]: storage[keys[1]], keys[2]: storage[keys[2]]}, &keys[3]}, 209 }, 210 } 211 for _, test := range tests { 212 result, err := storageRangeAt(sdb, root, addr, test.start, test.limit) 213 if err != nil { 214 t.Error(err) 215 } 216 if !reflect.DeepEqual(result, test.want) { 217 t.Fatalf("wrong result for range %#x.., limit %d:\ngot %s\nwant %s", 218 test.start, test.limit, dumper.Sdump(result), dumper.Sdump(&test.want)) 219 } 220 } 221 }