|
| 1 | +// Copyright 2025 The go-ethereum Authors |
| 2 | +// This file is part of go-ethereum. |
| 3 | +// |
| 4 | +// go-ethereum is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU 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 | +// go-ethereum 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 General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "math/big" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/ethereum/go-ethereum" |
| 27 | + "github.com/ethereum/go-ethereum/common" |
| 28 | + "github.com/ethereum/go-ethereum/core/types" |
| 29 | + "github.com/ethereum/go-ethereum/crypto" |
| 30 | + "github.com/ethereum/go-ethereum/internal/utesting" |
| 31 | + "github.com/ethereum/go-ethereum/rlp" |
| 32 | + "github.com/ethereum/go-ethereum/rpc" |
| 33 | +) |
| 34 | + |
| 35 | +type filterTestSuite struct { |
| 36 | + cfg testConfig |
| 37 | + queries [][]*filterQuery |
| 38 | +} |
| 39 | + |
| 40 | +func newFilterTestSuite(cfg testConfig) *filterTestSuite { |
| 41 | + s := &filterTestSuite{cfg: cfg} |
| 42 | + if err := s.loadQueries(); err != nil { |
| 43 | + exit(err) |
| 44 | + } |
| 45 | + return s |
| 46 | +} |
| 47 | + |
| 48 | +func (s *filterTestSuite) allTests() []utesting.Test { |
| 49 | + return []utesting.Test{ |
| 50 | + {Name: "Filter/ShortRange", Fn: s.filterShortRange}, |
| 51 | + {Name: "Filter/LongRange", Fn: s.filterLongRange, Slow: true}, |
| 52 | + {Name: "Filter/FullRange", Fn: s.filterFullRange, Slow: true}, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (s *filterTestSuite) filterRange(t *utesting.T, test func(query *filterQuery) bool, do func(t *utesting.T, query *filterQuery)) { |
| 57 | + var count, total int |
| 58 | + for _, bucket := range s.queries { |
| 59 | + for _, query := range bucket { |
| 60 | + if test(query) { |
| 61 | + total++ |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + if total == 0 { |
| 66 | + t.Fatalf("No suitable queries available") |
| 67 | + } |
| 68 | + start := time.Now() |
| 69 | + last := start |
| 70 | + for _, bucket := range s.queries { |
| 71 | + for _, query := range bucket { |
| 72 | + if test(query) { |
| 73 | + do(t, query) |
| 74 | + count++ |
| 75 | + if time.Since(last) > time.Second*5 { |
| 76 | + t.Logf("Making filter query %d/%d (elapsed: %v)", count, total, time.Since(start)) |
| 77 | + last = time.Now() |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + t.Logf("Made %d filter queries (elapsed: %v)", count, time.Since(start)) |
| 83 | +} |
| 84 | + |
| 85 | +const filterRangeThreshold = 10000 |
| 86 | + |
| 87 | +// filterShortRange runs all short-range filter tests. |
| 88 | +func (s *filterTestSuite) filterShortRange(t *utesting.T) { |
| 89 | + s.filterRange(t, func(query *filterQuery) bool { |
| 90 | + return query.ToBlock+1-query.FromBlock <= filterRangeThreshold |
| 91 | + }, s.queryAndCheck) |
| 92 | +} |
| 93 | + |
| 94 | +// filterShortRange runs all long-range filter tests. |
| 95 | +func (s *filterTestSuite) filterLongRange(t *utesting.T) { |
| 96 | + s.filterRange(t, func(query *filterQuery) bool { |
| 97 | + return query.ToBlock+1-query.FromBlock > filterRangeThreshold |
| 98 | + }, s.queryAndCheck) |
| 99 | +} |
| 100 | + |
| 101 | +// filterFullRange runs all filter tests, extending their range from genesis up |
| 102 | +// to the latest block. Note that results are only partially verified in this mode. |
| 103 | +func (s *filterTestSuite) filterFullRange(t *utesting.T) { |
| 104 | + finalized := mustGetFinalizedBlock(s.cfg.client) |
| 105 | + s.filterRange(t, func(query *filterQuery) bool { |
| 106 | + return query.ToBlock+1-query.FromBlock > finalized/2 |
| 107 | + }, s.fullRangeQueryAndCheck) |
| 108 | +} |
| 109 | + |
| 110 | +func (s *filterTestSuite) queryAndCheck(t *utesting.T, query *filterQuery) { |
| 111 | + query.run(s.cfg.client) |
| 112 | + if query.Err != nil { |
| 113 | + t.Errorf("Filter query failed (fromBlock: %d toBlock: %d addresses: %v topics: %v error: %v)", query.FromBlock, query.ToBlock, query.Address, query.Topics, query.Err) |
| 114 | + return |
| 115 | + } |
| 116 | + if *query.ResultHash != query.calculateHash() { |
| 117 | + t.Fatalf("Filter query result mismatch (fromBlock: %d toBlock: %d addresses: %v topics: %v)", query.FromBlock, query.ToBlock, query.Address, query.Topics) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func (s *filterTestSuite) fullRangeQueryAndCheck(t *utesting.T, query *filterQuery) { |
| 122 | + frQuery := &filterQuery{ // create full range query |
| 123 | + FromBlock: 0, |
| 124 | + ToBlock: int64(rpc.LatestBlockNumber), |
| 125 | + Address: query.Address, |
| 126 | + Topics: query.Topics, |
| 127 | + } |
| 128 | + frQuery.run(s.cfg.client) |
| 129 | + if frQuery.Err != nil { |
| 130 | + t.Errorf("Full range filter query failed (addresses: %v topics: %v error: %v)", frQuery.Address, frQuery.Topics, frQuery.Err) |
| 131 | + return |
| 132 | + } |
| 133 | + // filter out results outside the original query range |
| 134 | + j := 0 |
| 135 | + for _, log := range frQuery.results { |
| 136 | + if int64(log.BlockNumber) >= query.FromBlock && int64(log.BlockNumber) <= query.ToBlock { |
| 137 | + frQuery.results[j] = log |
| 138 | + j++ |
| 139 | + } |
| 140 | + } |
| 141 | + frQuery.results = frQuery.results[:j] |
| 142 | + if *query.ResultHash != frQuery.calculateHash() { |
| 143 | + t.Fatalf("Full range filter query result mismatch (fromBlock: %d toBlock: %d addresses: %v topics: %v)", query.FromBlock, query.ToBlock, query.Address, query.Topics) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func (s *filterTestSuite) loadQueries() error { |
| 148 | + file, err := s.cfg.fsys.Open(s.cfg.filterQueryFile) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("can't open filterQueryFile: %v", err) |
| 151 | + } |
| 152 | + defer file.Close() |
| 153 | + |
| 154 | + var queries [][]*filterQuery |
| 155 | + if err := json.NewDecoder(file).Decode(&queries); err != nil { |
| 156 | + return fmt.Errorf("invalid JSON in %s: %v", s.cfg.filterQueryFile, err) |
| 157 | + } |
| 158 | + var count int |
| 159 | + for _, bucket := range queries { |
| 160 | + count += len(bucket) |
| 161 | + } |
| 162 | + if count == 0 { |
| 163 | + return fmt.Errorf("filterQueryFile %s is empty", s.cfg.filterQueryFile) |
| 164 | + } |
| 165 | + s.queries = queries |
| 166 | + return nil |
| 167 | +} |
| 168 | + |
| 169 | +// filterQuery is a single query for testing. |
| 170 | +type filterQuery struct { |
| 171 | + FromBlock int64 `json:"fromBlock"` |
| 172 | + ToBlock int64 `json:"toBlock"` |
| 173 | + Address []common.Address `json:"address"` |
| 174 | + Topics [][]common.Hash `json:"topics"` |
| 175 | + ResultHash *common.Hash `json:"resultHash,omitempty"` |
| 176 | + results []types.Log |
| 177 | + Err error `json:"error,omitempty"` |
| 178 | +} |
| 179 | + |
| 180 | +func (fq *filterQuery) isWildcard() bool { |
| 181 | + if len(fq.Address) != 0 { |
| 182 | + return false |
| 183 | + } |
| 184 | + for _, topics := range fq.Topics { |
| 185 | + if len(topics) != 0 { |
| 186 | + return false |
| 187 | + } |
| 188 | + } |
| 189 | + return true |
| 190 | +} |
| 191 | + |
| 192 | +func (fq *filterQuery) calculateHash() common.Hash { |
| 193 | + enc, err := rlp.EncodeToBytes(&fq.results) |
| 194 | + if err != nil { |
| 195 | + exit(fmt.Errorf("Error encoding logs: %v", err)) |
| 196 | + } |
| 197 | + return crypto.Keccak256Hash(enc) |
| 198 | +} |
| 199 | + |
| 200 | +func (fq *filterQuery) run(client *client) { |
| 201 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) |
| 202 | + defer cancel() |
| 203 | + logs, err := client.Eth.FilterLogs(ctx, ethereum.FilterQuery{ |
| 204 | + FromBlock: big.NewInt(fq.FromBlock), |
| 205 | + ToBlock: big.NewInt(fq.ToBlock), |
| 206 | + Addresses: fq.Address, |
| 207 | + Topics: fq.Topics, |
| 208 | + }) |
| 209 | + if err != nil { |
| 210 | + fq.Err = err |
| 211 | + fmt.Printf("Filter query failed: fromBlock: %d toBlock: %d addresses: %v topics: %v error: %v\n", |
| 212 | + fq.FromBlock, fq.ToBlock, fq.Address, fq.Topics, err) |
| 213 | + return |
| 214 | + } |
| 215 | + fq.results = logs |
| 216 | +} |
0 commit comments