github.com/jimmyx0x/go-ethereum@v1.10.28/eth/filters/filter_test.go (about) 1 // Copyright 2015 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 filters 18 19 import ( 20 "context" 21 "math/big" 22 "reflect" 23 "testing" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/consensus/ethash" 27 "github.com/ethereum/go-ethereum/core" 28 "github.com/ethereum/go-ethereum/core/rawdb" 29 "github.com/ethereum/go-ethereum/core/types" 30 "github.com/ethereum/go-ethereum/crypto" 31 "github.com/ethereum/go-ethereum/params" 32 ) 33 34 func makeReceipt(addr common.Address) *types.Receipt { 35 receipt := types.NewReceipt(nil, false, 0) 36 receipt.Logs = []*types.Log{ 37 {Address: addr}, 38 } 39 receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) 40 return receipt 41 } 42 43 func BenchmarkFilters(b *testing.B) { 44 var ( 45 db, _ = rawdb.NewLevelDBDatabase(b.TempDir(), 0, 0, "", false) 46 _, sys = newTestFilterSystem(b, db, Config{}) 47 key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 48 addr1 = crypto.PubkeyToAddress(key1.PublicKey) 49 addr2 = common.BytesToAddress([]byte("jeff")) 50 addr3 = common.BytesToAddress([]byte("ethereum")) 51 addr4 = common.BytesToAddress([]byte("random addresses please")) 52 53 gspec = &core.Genesis{ 54 Alloc: core.GenesisAlloc{addr1: {Balance: big.NewInt(1000000)}}, 55 BaseFee: big.NewInt(params.InitialBaseFee), 56 Config: params.TestChainConfig, 57 } 58 ) 59 defer db.Close() 60 _, chain, receipts := core.GenerateChainWithGenesis(gspec, ethash.NewFaker(), 100010, func(i int, gen *core.BlockGen) { 61 switch i { 62 case 2403: 63 receipt := makeReceipt(addr1) 64 gen.AddUncheckedReceipt(receipt) 65 gen.AddUncheckedTx(types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, gen.BaseFee(), nil)) 66 case 1034: 67 receipt := makeReceipt(addr2) 68 gen.AddUncheckedReceipt(receipt) 69 gen.AddUncheckedTx(types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, gen.BaseFee(), nil)) 70 case 34: 71 receipt := makeReceipt(addr3) 72 gen.AddUncheckedReceipt(receipt) 73 gen.AddUncheckedTx(types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, gen.BaseFee(), nil)) 74 case 99999: 75 receipt := makeReceipt(addr4) 76 gen.AddUncheckedReceipt(receipt) 77 gen.AddUncheckedTx(types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, gen.BaseFee(), nil)) 78 } 79 }) 80 // The test txs are not properly signed, can't simply create a chain 81 // and then import blocks. TODO(rjl493456442) try to get rid of the 82 // manual database writes. 83 gspec.MustCommit(db) 84 85 for i, block := range chain { 86 rawdb.WriteBlock(db, block) 87 rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64()) 88 rawdb.WriteHeadBlockHash(db, block.Hash()) 89 rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i]) 90 } 91 b.ResetTimer() 92 93 filter := sys.NewRangeFilter(0, -1, []common.Address{addr1, addr2, addr3, addr4}, nil) 94 95 for i := 0; i < b.N; i++ { 96 logs, _ := filter.Logs(context.Background()) 97 if len(logs) != 4 { 98 b.Fatal("expected 4 logs, got", len(logs)) 99 } 100 } 101 } 102 103 func TestFilters(t *testing.T) { 104 var ( 105 db, _ = rawdb.NewLevelDBDatabase(t.TempDir(), 0, 0, "", false) 106 _, sys = newTestFilterSystem(t, db, Config{}) 107 key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 108 addr = crypto.PubkeyToAddress(key1.PublicKey) 109 110 hash1 = common.BytesToHash([]byte("topic1")) 111 hash2 = common.BytesToHash([]byte("topic2")) 112 hash3 = common.BytesToHash([]byte("topic3")) 113 hash4 = common.BytesToHash([]byte("topic4")) 114 115 gspec = &core.Genesis{ 116 Config: params.TestChainConfig, 117 Alloc: core.GenesisAlloc{addr: {Balance: big.NewInt(1000000)}}, 118 BaseFee: big.NewInt(params.InitialBaseFee), 119 } 120 ) 121 defer db.Close() 122 123 _, chain, receipts := core.GenerateChainWithGenesis(gspec, ethash.NewFaker(), 1000, func(i int, gen *core.BlockGen) { 124 switch i { 125 case 1: 126 receipt := types.NewReceipt(nil, false, 0) 127 receipt.Logs = []*types.Log{ 128 { 129 Address: addr, 130 Topics: []common.Hash{hash1}, 131 }, 132 } 133 gen.AddUncheckedReceipt(receipt) 134 gen.AddUncheckedTx(types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, gen.BaseFee(), nil)) 135 case 2: 136 receipt := types.NewReceipt(nil, false, 0) 137 receipt.Logs = []*types.Log{ 138 { 139 Address: addr, 140 Topics: []common.Hash{hash2}, 141 }, 142 } 143 gen.AddUncheckedReceipt(receipt) 144 gen.AddUncheckedTx(types.NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, gen.BaseFee(), nil)) 145 146 case 998: 147 receipt := types.NewReceipt(nil, false, 0) 148 receipt.Logs = []*types.Log{ 149 { 150 Address: addr, 151 Topics: []common.Hash{hash3}, 152 }, 153 } 154 gen.AddUncheckedReceipt(receipt) 155 gen.AddUncheckedTx(types.NewTransaction(998, common.HexToAddress("0x998"), big.NewInt(998), 998, gen.BaseFee(), nil)) 156 case 999: 157 receipt := types.NewReceipt(nil, false, 0) 158 receipt.Logs = []*types.Log{ 159 { 160 Address: addr, 161 Topics: []common.Hash{hash4}, 162 }, 163 } 164 gen.AddUncheckedReceipt(receipt) 165 gen.AddUncheckedTx(types.NewTransaction(999, common.HexToAddress("0x999"), big.NewInt(999), 999, gen.BaseFee(), nil)) 166 } 167 }) 168 // The test txs are not properly signed, can't simply create a chain 169 // and then import blocks. TODO(rjl493456442) try to get rid of the 170 // manual database writes. 171 gspec.MustCommit(db) 172 for i, block := range chain { 173 rawdb.WriteBlock(db, block) 174 rawdb.WriteCanonicalHash(db, block.Hash(), block.NumberU64()) 175 rawdb.WriteHeadBlockHash(db, block.Hash()) 176 rawdb.WriteReceipts(db, block.Hash(), block.NumberU64(), receipts[i]) 177 } 178 179 // Set block 998 as Finalized (-3) 180 rawdb.WriteFinalizedBlockHash(db, chain[998].Hash()) 181 182 filter := sys.NewRangeFilter(0, -1, []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}}) 183 logs, _ := filter.Logs(context.Background()) 184 if len(logs) != 4 { 185 t.Error("expected 4 log, got", len(logs)) 186 } 187 188 for i, tc := range []struct { 189 f *Filter 190 wantHashes []common.Hash 191 }{ 192 { 193 sys.NewRangeFilter(900, 999, []common.Address{addr}, [][]common.Hash{{hash3}}), 194 []common.Hash{hash3}, 195 }, { 196 sys.NewRangeFilter(990, -1, []common.Address{addr}, [][]common.Hash{{hash3}}), 197 []common.Hash{hash3}, 198 }, { 199 sys.NewRangeFilter(1, 10, nil, [][]common.Hash{{hash1, hash2}}), 200 []common.Hash{hash1, hash2}, 201 }, { 202 sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}}), 203 nil, 204 }, { 205 sys.NewRangeFilter(0, -1, []common.Address{common.BytesToAddress([]byte("failmenow"))}, nil), 206 nil, 207 }, { 208 sys.NewRangeFilter(0, -1, nil, [][]common.Hash{{common.BytesToHash([]byte("fail"))}, {hash1}}), 209 nil, 210 }, { 211 sys.NewRangeFilter(-1, -1, nil, nil), []common.Hash{hash4}, 212 }, { 213 sys.NewRangeFilter(-3, -1, nil, nil), []common.Hash{hash3, hash4}, 214 }, { 215 sys.NewRangeFilter(-3, -3, nil, nil), []common.Hash{hash3}, 216 }, { 217 sys.NewRangeFilter(-1, -3, nil, nil), nil, 218 }, { 219 sys.NewRangeFilter(-4, -1, nil, nil), nil, 220 }, { 221 sys.NewRangeFilter(-4, -4, nil, nil), nil, 222 }, { 223 sys.NewRangeFilter(-1, -4, nil, nil), nil, 224 }, 225 } { 226 logs, _ := tc.f.Logs(context.Background()) 227 var haveHashes []common.Hash 228 for _, l := range logs { 229 haveHashes = append(haveHashes, l.Topics[0]) 230 } 231 if have, want := len(haveHashes), len(tc.wantHashes); have != want { 232 t.Fatalf("test %d, have %d logs, want %d", i, have, want) 233 } 234 if len(haveHashes) == 0 { 235 continue 236 } 237 if !reflect.DeepEqual(tc.wantHashes, haveHashes) { 238 t.Fatalf("test %d, have %v want %v", i, haveHashes, tc.wantHashes) 239 } 240 } 241 }