Skip to content

Commit efe9144

Browse files
committed
eth/filters: extended test to test topics and no error scenarios
1 parent f91cf22 commit efe9144

File tree

2 files changed

+65
-29
lines changed

2 files changed

+65
-29
lines changed

eth/filters/filter_system.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ func (cfg Config) withDefaults() Config {
5353
if cfg.LogCacheSize == 0 {
5454
cfg.LogCacheSize = 32
5555
}
56-
if cfg.LogQueryLimit == 0 {
57-
cfg.LogQueryLimit = 1000
58-
}
5956
return cfg
6057
}
6158

eth/filters/filter_system_test.go

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/ethereum/go-ethereum/internal/ethapi"
3737
"github.com/ethereum/go-ethereum/params"
3838
"github.com/ethereum/go-ethereum/rpc"
39+
"github.com/ethereum/go-ethereum/triedb"
3940
)
4041

4142
type testBackend struct {
@@ -424,7 +425,7 @@ func TestInvalidLogFilterCreation(t *testing.T) {
424425

425426
var (
426427
db = rawdb.NewMemoryDatabase()
427-
_, sys = newTestFilterSystem(db, Config{})
428+
_, sys = newTestFilterSystem(db, Config{LogQueryLimit: 1000})
428429
api = NewFilterAPI(sys)
429430
)
430431

@@ -528,51 +529,89 @@ func TestInvalidGetRangeLogsRequest(t *testing.T) {
528529
}
529530
}
530531

531-
// TestInvalidAddressLengthRequest tests getLogs with too many addresses
532-
func TestInvalidAddressLengthRequest(t *testing.T) {
532+
// TestExceedLogQueryLimit tests getLogs with too many addresses or topics
533+
func TestExceedLogQueryLimit(t *testing.T) {
533534
t.Parallel()
534535

535536
// Test with custom config (LogQueryLimit = 5 for easier testing)
536537
var (
537-
db = rawdb.NewMemoryDatabase()
538-
_, sys = newTestFilterSystem(db, Config{LogQueryLimit: 5})
539-
api = NewFilterAPI(sys)
538+
db = rawdb.NewMemoryDatabase()
539+
backend, sys = newTestFilterSystem(db, Config{LogQueryLimit: 5})
540+
api = NewFilterAPI(sys)
541+
gspec = &core.Genesis{
542+
Config: params.TestChainConfig,
543+
Alloc: types.GenesisAlloc{},
544+
BaseFee: big.NewInt(params.InitialBaseFee),
545+
}
540546
)
541547

542-
// Test that 6 addresses fails with correct error
543-
invalidAddresses := make([]common.Address, 6)
544-
for i := range invalidAddresses {
545-
invalidAddresses[i] = common.HexToAddress("0x1234567890123456789012345678901234567890")
548+
_, err := gspec.Commit(db, triedb.NewDatabase(db, nil))
549+
if err != nil {
550+
t.Fatal(err)
551+
}
552+
chain, _ := core.GenerateChain(gspec.Config, gspec.ToBlock(), ethash.NewFaker(), db, 1000, func(i int, gen *core.BlockGen) {})
553+
554+
options := core.DefaultConfig().WithStateScheme(rawdb.HashScheme)
555+
options.TxLookupLimit = 0 // index all txs
556+
bc, err := core.NewBlockChain(db, gspec, ethash.NewFaker(), options)
557+
if err != nil {
558+
t.Fatal(err)
559+
}
560+
_, err = bc.InsertChain(chain[:600])
561+
if err != nil {
562+
t.Fatal(err)
546563
}
547564

565+
backend.startFilterMaps(200, false, filtermaps.RangeTestParams)
566+
defer backend.stopFilterMaps()
567+
568+
addresses := make([]common.Address, 6)
569+
for i := range addresses {
570+
addresses[i] = common.HexToAddress("0x1234567890123456789012345678901234567890")
571+
}
572+
573+
topics := make([]common.Hash, 6)
574+
for i := range topics {
575+
topics[i] = common.HexToHash("0x123456789012345678901234567890123456789001234567890012345678901234")
576+
}
577+
578+
// Test that 5 addresses do not result in error
548579
// Add FromBlock and ToBlock to make it similar to other invalid tests
549580
if _, err := api.GetLogs(context.Background(), FilterCriteria{
550581
FromBlock: big.NewInt(0),
551582
ToBlock: big.NewInt(100),
552-
Addresses: invalidAddresses,
553-
}); err != errExceedLogQueryLimit {
554-
t.Errorf("Expected GetLogs with 6 addresses to return errExceedLogQueryLimit, but got: %v", err)
583+
Addresses: addresses[:5],
584+
}); err != nil {
585+
t.Errorf("Expected GetLogs with 5 addresses to return with no error, got: %v", err)
555586
}
556587

557-
// Test with default config should reject 1001 addresses
558-
var (
559-
db2 = rawdb.NewMemoryDatabase()
560-
_, sys2 = newTestFilterSystem(db2, Config{}) // Uses default LogQueryLimit = 1000
561-
api2 = NewFilterAPI(sys2)
562-
)
588+
// Test that 6 addresses fails with correct error
589+
if _, err := api.GetLogs(context.Background(), FilterCriteria{
590+
FromBlock: big.NewInt(0),
591+
ToBlock: big.NewInt(100),
592+
Addresses: addresses,
593+
}); err != errExceedLogQueryLimit {
594+
t.Errorf("Expected GetLogs with 6 addresses to return errExceedLogQueryLimit, got: %v", err)
595+
}
563596

564-
// Test that 1001 addresses fails with correct error
565-
tooManyAddresses := make([]common.Address, 1001)
566-
for i := range tooManyAddresses {
567-
tooManyAddresses[i] = common.HexToAddress("0x1234567890123456789012345678901234567890")
597+
// Test that 5 topics at one position do not result in error
598+
if _, err := api.GetLogs(context.Background(), FilterCriteria{
599+
FromBlock: big.NewInt(0),
600+
ToBlock: big.NewInt(100),
601+
Addresses: addresses[:1],
602+
Topics: [][]common.Hash{topics[:5]},
603+
}); err != nil {
604+
t.Errorf("Expected GetLogs with 5 topics at one position to return with no error, got: %v", err)
568605
}
569606

570-
if _, err := api2.GetLogs(context.Background(), FilterCriteria{
607+
// Test that 6 topics at one position fails with correct error
608+
if _, err := api.GetLogs(context.Background(), FilterCriteria{
571609
FromBlock: big.NewInt(0),
572610
ToBlock: big.NewInt(100),
573-
Addresses: tooManyAddresses,
611+
Addresses: addresses[:1],
612+
Topics: [][]common.Hash{topics},
574613
}); err != errExceedLogQueryLimit {
575-
t.Errorf("Expected GetLogs with 1001 addresses to return errExceedLogQueryLimit, but got: %v", err)
614+
t.Errorf("Expected GetLogs with 6 topics at one position to return errExceedLogQueryLimit, got: %v", err)
576615
}
577616
}
578617

0 commit comments

Comments
 (0)