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