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