eth/filters: extended test to test topics and no error scenarios

This commit is contained in:
zsfelfoldi 2025-09-16 13:56:26 +02:00
parent f91cf22312
commit efe91448d0
2 changed files with 68 additions and 32 deletions

View file

@ -53,9 +53,6 @@ func (cfg Config) withDefaults() Config {
if cfg.LogCacheSize == 0 {
cfg.LogCacheSize = 32
}
if cfg.LogQueryLimit == 0 {
cfg.LogQueryLimit = 1000
}
return cfg
}

View file

@ -36,6 +36,7 @@ import (
"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/triedb"
)
type testBackend struct {
@ -424,7 +425,7 @@ func TestInvalidLogFilterCreation(t *testing.T) {
var (
db = rawdb.NewMemoryDatabase()
_, sys = newTestFilterSystem(db, Config{})
_, sys = newTestFilterSystem(db, Config{LogQueryLimit: 1000})
api = NewFilterAPI(sys)
)
@ -528,51 +529,89 @@ func TestInvalidGetRangeLogsRequest(t *testing.T) {
}
}
// TestInvalidAddressLengthRequest tests getLogs with too many addresses
func TestInvalidAddressLengthRequest(t *testing.T) {
// TestExceedLogQueryLimit tests getLogs with too many addresses or topics
func TestExceedLogQueryLimit(t *testing.T) {
t.Parallel()
// Test with custom config (LogQueryLimit = 5 for easier testing)
var (
db = rawdb.NewMemoryDatabase()
_, sys = newTestFilterSystem(db, Config{LogQueryLimit: 5})
backend, sys = newTestFilterSystem(db, Config{LogQueryLimit: 5})
api = NewFilterAPI(sys)
gspec = &core.Genesis{
Config: params.TestChainConfig,
Alloc: types.GenesisAlloc{},
BaseFee: big.NewInt(params.InitialBaseFee),
}
)
// Test that 6 addresses fails with correct error
invalidAddresses := make([]common.Address, 6)
for i := range invalidAddresses {
invalidAddresses[i] = common.HexToAddress("0x1234567890123456789012345678901234567890")
_, err := gspec.Commit(db, triedb.NewDatabase(db, nil))
if err != nil {
t.Fatal(err)
}
chain, _ := core.GenerateChain(gspec.Config, gspec.ToBlock(), ethash.NewFaker(), db, 1000, func(i int, gen *core.BlockGen) {})
options := core.DefaultConfig().WithStateScheme(rawdb.HashScheme)
options.TxLookupLimit = 0 // index all txs
bc, err := core.NewBlockChain(db, gspec, ethash.NewFaker(), options)
if err != nil {
t.Fatal(err)
}
_, err = bc.InsertChain(chain[:600])
if err != nil {
t.Fatal(err)
}
backend.startFilterMaps(200, false, filtermaps.RangeTestParams)
defer backend.stopFilterMaps()
addresses := make([]common.Address, 6)
for i := range addresses {
addresses[i] = common.HexToAddress("0x1234567890123456789012345678901234567890")
}
topics := make([]common.Hash, 6)
for i := range topics {
topics[i] = common.HexToHash("0x123456789012345678901234567890123456789001234567890012345678901234")
}
// Test that 5 addresses do not result in error
// Add FromBlock and ToBlock to make it similar to other invalid tests
if _, err := api.GetLogs(context.Background(), FilterCriteria{
FromBlock: big.NewInt(0),
ToBlock: big.NewInt(100),
Addresses: invalidAddresses,
}); err != errExceedLogQueryLimit {
t.Errorf("Expected GetLogs with 6 addresses to return errExceedLogQueryLimit, but got: %v", err)
Addresses: addresses[:5],
}); err != nil {
t.Errorf("Expected GetLogs with 5 addresses to return with no error, got: %v", err)
}
// Test with default config should reject 1001 addresses
var (
db2 = rawdb.NewMemoryDatabase()
_, sys2 = newTestFilterSystem(db2, Config{}) // Uses default LogQueryLimit = 1000
api2 = NewFilterAPI(sys2)
)
// Test that 1001 addresses fails with correct error
tooManyAddresses := make([]common.Address, 1001)
for i := range tooManyAddresses {
tooManyAddresses[i] = common.HexToAddress("0x1234567890123456789012345678901234567890")
}
if _, err := api2.GetLogs(context.Background(), FilterCriteria{
// Test that 6 addresses fails with correct error
if _, err := api.GetLogs(context.Background(), FilterCriteria{
FromBlock: big.NewInt(0),
ToBlock: big.NewInt(100),
Addresses: tooManyAddresses,
Addresses: addresses,
}); err != errExceedLogQueryLimit {
t.Errorf("Expected GetLogs with 1001 addresses to return errExceedLogQueryLimit, but got: %v", err)
t.Errorf("Expected GetLogs with 6 addresses to return errExceedLogQueryLimit, got: %v", err)
}
// Test that 5 topics at one position do not result in error
if _, err := api.GetLogs(context.Background(), FilterCriteria{
FromBlock: big.NewInt(0),
ToBlock: big.NewInt(100),
Addresses: addresses[:1],
Topics: [][]common.Hash{topics[:5]},
}); err != nil {
t.Errorf("Expected GetLogs with 5 topics at one position to return with no error, got: %v", err)
}
// Test that 6 topics at one position fails with correct error
if _, err := api.GetLogs(context.Background(), FilterCriteria{
FromBlock: big.NewInt(0),
ToBlock: big.NewInt(100),
Addresses: addresses[:1],
Topics: [][]common.Hash{topics},
}); err != errExceedLogQueryLimit {
t.Errorf("Expected GetLogs with 6 topics at one position to return errExceedLogQueryLimit, got: %v", err)
}
}