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