github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/intprotocol/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 "io/ioutil" 22 "math/big" 23 "os" 24 "testing" 25 26 "github.com/intfoundation/intchain/common" 27 "github.com/intfoundation/intchain/core" 28 "github.com/intfoundation/intchain/core/types" 29 "github.com/intfoundation/intchain/crypto" 30 "github.com/intfoundation/intchain/event" 31 "github.com/intfoundation/intchain/intdb" 32 "github.com/intfoundation/intchain/params" 33 ) 34 35 func makeReceipt(addr common.Address) *types.Receipt { 36 receipt := types.NewReceipt(nil, false, 0) 37 receipt.Logs = []*types.Log{ 38 {Address: addr}, 39 } 40 receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) 41 return receipt 42 } 43 44 func BenchmarkFilters(b *testing.B) { 45 dir, err := ioutil.TempDir("", "filtertest") 46 if err != nil { 47 b.Fatal(err) 48 } 49 defer os.RemoveAll(dir) 50 51 var ( 52 //db, _ = intdb.Database(dir, 0, 0) 53 mux = new(event.TypeMux) 54 txFeed = new(event.Feed) 55 rmLogsFeed = new(event.Feed) 56 logsFeed = new(event.Feed) 57 chainFeed = new(event.Feed) 58 backend = &testBackend{mux, nil, 0, txFeed, rmLogsFeed, logsFeed, chainFeed} 59 key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 60 addr1 = crypto.PubkeyToAddress(key1.PublicKey) 61 addr2 = common.BytesToAddress([]byte("jeff")) 62 addr3 = common.BytesToAddress([]byte("ethereum")) 63 addr4 = common.BytesToAddress([]byte("random addresses please")) 64 ) 65 //defer db.Close() 66 67 genesis := core.GenesisBlockForTesting(nil, addr1, big.NewInt(1000000)) 68 chain, receipts := core.GenerateChain(params.TestChainConfig, genesis, nil, nil, 100010, func(i int, gen *core.BlockGen) { 69 switch i { 70 case 2403: 71 receipt := makeReceipt(addr1) 72 gen.AddUncheckedReceipt(receipt) 73 case 1034: 74 receipt := makeReceipt(addr2) 75 gen.AddUncheckedReceipt(receipt) 76 case 34: 77 receipt := makeReceipt(addr3) 78 gen.AddUncheckedReceipt(receipt) 79 case 99999: 80 receipt := makeReceipt(addr4) 81 gen.AddUncheckedReceipt(receipt) 82 83 } 84 }) 85 for i, block := range chain { 86 //core.WriteBlock(nil, block) 87 if err := core.WriteCanonicalHash(db, block.Hash(), block.NumberU64()); err != nil { 88 b.Fatalf("failed to insert block number: %v", err) 89 } 90 if err := core.WriteHeadBlockHash(db, block.Hash()); err != nil { 91 b.Fatalf("failed to insert block number: %v", err) 92 } 93 if err := core.WriteBlockReceipts(db, block.Hash(), block.NumberU64(), receipts[i]); err != nil { 94 b.Fatal("error writing block receipts:", err) 95 } 96 } 97 b.ResetTimer() 98 99 filter := New(backend, 0, -1, []common.Address{addr1, addr2, addr3, addr4}, nil) 100 101 for i := 0; i < b.N; i++ { 102 logs, _ := filter.Logs(context.Background()) 103 if len(logs) != 4 { 104 b.Fatal("expected 4 logs, got", len(logs)) 105 } 106 } 107 } 108 109 func TestFilters(t *testing.T) { 110 dir, err := ioutil.TempDir("", "filtertest") 111 if err != nil { 112 t.Fatal(err) 113 } 114 defer os.RemoveAll(dir) 115 116 var ( 117 db, _ = intdb.NewLDBDatabase(dir, 0, 0) 118 mux = new(event.TypeMux) 119 txFeed = new(event.Feed) 120 rmLogsFeed = new(event.Feed) 121 logsFeed = new(event.Feed) 122 chainFeed = new(event.Feed) 123 backend = &testBackend{mux, db, 0, txFeed, rmLogsFeed, logsFeed, chainFeed} 124 key1, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 125 addr = crypto.PubkeyToAddress(key1.PublicKey) 126 127 hash1 = common.BytesToHash([]byte("topic1")) 128 hash2 = common.BytesToHash([]byte("topic2")) 129 hash3 = common.BytesToHash([]byte("topic3")) 130 hash4 = common.BytesToHash([]byte("topic4")) 131 ) 132 defer db.Close() 133 134 genesis := core.GenesisBlockForTesting(db, addr, big.NewInt(1000000)) 135 chain, receipts := core.GenerateChain(params.TestChainConfig, genesis, nil, db, 1000, func(i int, gen *core.BlockGen) { 136 switch i { 137 case 1: 138 receipt := types.NewReceipt(nil, false, 0) 139 receipt.Logs = []*types.Log{ 140 { 141 Address: addr, 142 Topics: []common.Hash{hash1}, 143 }, 144 } 145 gen.AddUncheckedReceipt(receipt) 146 case 2: 147 receipt := types.NewReceipt(nil, false, 0) 148 receipt.Logs = []*types.Log{ 149 { 150 Address: addr, 151 Topics: []common.Hash{hash2}, 152 }, 153 } 154 gen.AddUncheckedReceipt(receipt) 155 case 998: 156 receipt := types.NewReceipt(nil, false, 0) 157 receipt.Logs = []*types.Log{ 158 { 159 Address: addr, 160 Topics: []common.Hash{hash3}, 161 }, 162 } 163 gen.AddUncheckedReceipt(receipt) 164 case 999: 165 receipt := types.NewReceipt(nil, false, 0) 166 receipt.Logs = []*types.Log{ 167 { 168 Address: addr, 169 Topics: []common.Hash{hash4}, 170 }, 171 } 172 gen.AddUncheckedReceipt(receipt) 173 } 174 }) 175 for i, block := range chain { 176 core.WriteBlock(db, block) 177 if err := core.WriteCanonicalHash(db, block.Hash(), block.NumberU64()); err != nil { 178 t.Fatalf("failed to insert block number: %v", err) 179 } 180 if err := core.WriteHeadBlockHash(db, block.Hash()); err != nil { 181 t.Fatalf("failed to insert block number: %v", err) 182 } 183 if err := core.WriteBlockReceipts(db, block.Hash(), block.NumberU64(), receipts[i]); err != nil { 184 t.Fatal("error writing block receipts:", err) 185 } 186 } 187 188 filter := New(backend, 0, -1, []common.Address{addr}, [][]common.Hash{{hash1, hash2, hash3, hash4}}) 189 190 logs, _ := filter.Logs(context.Background()) 191 if len(logs) != 4 { 192 t.Error("expected 4 log, got", len(logs)) 193 } 194 195 filter = New(backend, 900, 999, []common.Address{addr}, [][]common.Hash{{hash3}}) 196 logs, _ = filter.Logs(context.Background()) 197 if len(logs) != 1 { 198 t.Error("expected 1 log, got", len(logs)) 199 } 200 if len(logs) > 0 && logs[0].Topics[0] != hash3 { 201 t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0]) 202 } 203 204 filter = New(backend, 990, -1, []common.Address{addr}, [][]common.Hash{{hash3}}) 205 logs, _ = filter.Logs(context.Background()) 206 if len(logs) != 1 { 207 t.Error("expected 1 log, got", len(logs)) 208 } 209 if len(logs) > 0 && logs[0].Topics[0] != hash3 { 210 t.Errorf("expected log[0].Topics[0] to be %x, got %x", hash3, logs[0].Topics[0]) 211 } 212 213 filter = New(backend, 1, 10, nil, [][]common.Hash{{hash1, hash2}}) 214 215 logs, _ = filter.Logs(context.Background()) 216 if len(logs) != 2 { 217 t.Error("expected 2 log, got", len(logs)) 218 } 219 220 failHash := common.BytesToHash([]byte("fail")) 221 filter = New(backend, 0, -1, nil, [][]common.Hash{{failHash}}) 222 223 logs, _ = filter.Logs(context.Background()) 224 if len(logs) != 0 { 225 t.Error("expected 0 log, got", len(logs)) 226 } 227 228 failAddr := common.BytesToAddress([]byte("failmenow")) 229 filter = New(backend, 0, -1, []common.Address{failAddr}, nil) 230 231 logs, _ = filter.Logs(context.Background()) 232 if len(logs) != 0 { 233 t.Error("expected 0 log, got", len(logs)) 234 } 235 236 filter = New(backend, 0, -1, nil, [][]common.Hash{{failHash}, {hash1}}) 237 238 logs, _ = filter.Logs(context.Background()) 239 if len(logs) != 0 { 240 t.Error("expected 0 log, got", len(logs)) 241 } 242 }