@@ -19,6 +19,7 @@ package filters
1919import (
2020 "context"
2121 "math/big"
22+ "reflect"
2223 "testing"
2324
2425 "github.com/ethereum/go-ethereum/common"
@@ -170,58 +171,66 @@ func TestFilters(t *testing.T) {
170171 rawdb .WriteReceipts (db , block .Hash (), block .NumberU64 (), receipts [i ])
171172 }
172173
173- filter := sys .NewRangeFilter (0 , - 1 , []common.Address {addr }, [][]common.Hash {{hash1 , hash2 , hash3 , hash4 }})
174+ // Set block 998 as Finalized (-3)
175+ rawdb .WriteFinalizedBlockHash (db , chain [998 ].Hash ())
174176
177+ filter := sys .NewRangeFilter (0 , - 1 , []common.Address {addr }, [][]common.Hash {{hash1 , hash2 , hash3 , hash4 }})
175178 logs , _ := filter .Logs (context .Background ())
176179 if len (logs ) != 4 {
177180 t .Error ("expected 4 log, got" , len (logs ))
178181 }
179182
180- filter = sys .NewRangeFilter (900 , 999 , []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 = sys .NewRangeFilter (990 , - 1 , []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 = sys .NewRangeFilter (1 , 10 , nil , [][]common.Hash {{hash1 , hash2 }})
199-
200- logs , _ = filter .Logs (context .Background ())
201- if len (logs ) != 2 {
202- t .Error ("expected 2 log, got" , len (logs ))
203- }
204-
205- failHash := common .BytesToHash ([]byte ("fail" ))
206- filter = sys .NewRangeFilter (0 , - 1 , nil , [][]common.Hash {{failHash }})
207-
208- logs , _ = filter .Logs (context .Background ())
209- if len (logs ) != 0 {
210- t .Error ("expected 0 log, got" , len (logs ))
211- }
212-
213- failAddr := common .BytesToAddress ([]byte ("failmenow" ))
214- filter = sys .NewRangeFilter (0 , - 1 , []common.Address {failAddr }, nil )
215-
216- logs , _ = filter .Logs (context .Background ())
217- if len (logs ) != 0 {
218- t .Error ("expected 0 log, got" , len (logs ))
219- }
220-
221- filter = sys .NewRangeFilter (0 , - 1 , nil , [][]common.Hash {{failHash }, {hash1 }})
222-
223- logs , _ = filter .Logs (context .Background ())
224- if len (logs ) != 0 {
225- t .Error ("expected 0 log, got" , len (logs ))
183+ for i , tc := range []struct {
184+ f * Filter
185+ wantHashes []common.Hash
186+ }{
187+ {
188+ sys .NewRangeFilter (900 , 999 , []common.Address {addr }, [][]common.Hash {{hash3 }}),
189+ []common.Hash {hash3 },
190+ }, {
191+ sys .NewRangeFilter (990 , - 1 , []common.Address {addr }, [][]common.Hash {{hash3 }}),
192+ []common.Hash {hash3 },
193+ }, {
194+ sys .NewRangeFilter (1 , 10 , nil , [][]common.Hash {{hash1 , hash2 }}),
195+ []common.Hash {hash1 , hash2 },
196+ }, {
197+ sys .NewRangeFilter (0 , - 1 , nil , [][]common.Hash {{common .BytesToHash ([]byte ("fail" ))}}),
198+ nil ,
199+ }, {
200+ sys .NewRangeFilter (0 , - 1 , []common.Address {common .BytesToAddress ([]byte ("failmenow" ))}, nil ),
201+ nil ,
202+ }, {
203+ sys .NewRangeFilter (0 , - 1 , nil , [][]common.Hash {{common .BytesToHash ([]byte ("fail" ))}, {hash1 }}),
204+ nil ,
205+ }, {
206+ sys .NewRangeFilter (- 1 , - 1 , nil , nil ), []common.Hash {hash4 },
207+ }, {
208+ sys .NewRangeFilter (- 3 , - 1 , nil , nil ), []common.Hash {hash3 , hash4 },
209+ }, {
210+ sys .NewRangeFilter (- 3 , - 3 , nil , nil ), []common.Hash {hash3 },
211+ }, {
212+ sys .NewRangeFilter (- 1 , - 3 , nil , nil ), nil ,
213+ }, {
214+ sys .NewRangeFilter (- 4 , - 1 , nil , nil ), nil ,
215+ }, {
216+ sys .NewRangeFilter (- 4 , - 4 , nil , nil ), nil ,
217+ }, {
218+ sys .NewRangeFilter (- 1 , - 4 , nil , nil ), nil ,
219+ },
220+ } {
221+ logs , _ := tc .f .Logs (context .Background ())
222+ var haveHashes []common.Hash
223+ for _ , l := range logs {
224+ haveHashes = append (haveHashes , l .Topics [0 ])
225+ }
226+ if have , want := len (haveHashes ), len (tc .wantHashes ); have != want {
227+ t .Fatalf ("test %d, have %d logs, want %d" , i , have , want )
228+ }
229+ if len (haveHashes ) == 0 {
230+ continue
231+ }
232+ if ! reflect .DeepEqual (tc .wantHashes , haveHashes ) {
233+ t .Fatalf ("test %d, have %v want %v" , i , haveHashes , tc .wantHashes )
234+ }
226235 }
227236}
0 commit comments