mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 13:16:42 +00:00
eth/filters: extended test to test topics and no error scenarios
This commit is contained in:
parent
f91cf22312
commit
efe91448d0
2 changed files with 68 additions and 32 deletions
|
|
@ -53,9 +53,6 @@ func (cfg Config) withDefaults() Config {
|
||||||
if cfg.LogCacheSize == 0 {
|
if cfg.LogCacheSize == 0 {
|
||||||
cfg.LogCacheSize = 32
|
cfg.LogCacheSize = 32
|
||||||
}
|
}
|
||||||
if cfg.LogQueryLimit == 0 {
|
|
||||||
cfg.LogQueryLimit = 1000
|
|
||||||
}
|
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,7 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/internal/ethapi"
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
||||||
"github.com/ethereum/go-ethereum/params"
|
"github.com/ethereum/go-ethereum/params"
|
||||||
"github.com/ethereum/go-ethereum/rpc"
|
"github.com/ethereum/go-ethereum/rpc"
|
||||||
|
"github.com/ethereum/go-ethereum/triedb"
|
||||||
)
|
)
|
||||||
|
|
||||||
type testBackend struct {
|
type testBackend struct {
|
||||||
|
|
@ -424,7 +425,7 @@ func TestInvalidLogFilterCreation(t *testing.T) {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
db = rawdb.NewMemoryDatabase()
|
db = rawdb.NewMemoryDatabase()
|
||||||
_, sys = newTestFilterSystem(db, Config{})
|
_, sys = newTestFilterSystem(db, Config{LogQueryLimit: 1000})
|
||||||
api = NewFilterAPI(sys)
|
api = NewFilterAPI(sys)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -528,51 +529,89 @@ func TestInvalidGetRangeLogsRequest(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestInvalidAddressLengthRequest tests getLogs with too many addresses
|
// TestExceedLogQueryLimit tests getLogs with too many addresses or topics
|
||||||
func TestInvalidAddressLengthRequest(t *testing.T) {
|
func TestExceedLogQueryLimit(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// Test with custom config (LogQueryLimit = 5 for easier testing)
|
// Test with custom config (LogQueryLimit = 5 for easier testing)
|
||||||
var (
|
var (
|
||||||
db = rawdb.NewMemoryDatabase()
|
db = rawdb.NewMemoryDatabase()
|
||||||
_, sys = newTestFilterSystem(db, Config{LogQueryLimit: 5})
|
backend, sys = newTestFilterSystem(db, Config{LogQueryLimit: 5})
|
||||||
api = NewFilterAPI(sys)
|
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
|
_, err := gspec.Commit(db, triedb.NewDatabase(db, nil))
|
||||||
invalidAddresses := make([]common.Address, 6)
|
if err != nil {
|
||||||
for i := range invalidAddresses {
|
t.Fatal(err)
|
||||||
invalidAddresses[i] = common.HexToAddress("0x1234567890123456789012345678901234567890")
|
}
|
||||||
|
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
|
// Add FromBlock and ToBlock to make it similar to other invalid tests
|
||||||
if _, err := api.GetLogs(context.Background(), FilterCriteria{
|
if _, err := api.GetLogs(context.Background(), FilterCriteria{
|
||||||
FromBlock: big.NewInt(0),
|
FromBlock: big.NewInt(0),
|
||||||
ToBlock: big.NewInt(100),
|
ToBlock: big.NewInt(100),
|
||||||
Addresses: invalidAddresses,
|
Addresses: addresses[:5],
|
||||||
}); err != errExceedLogQueryLimit {
|
}); err != nil {
|
||||||
t.Errorf("Expected GetLogs with 6 addresses to return errExceedLogQueryLimit, but got: %v", err)
|
t.Errorf("Expected GetLogs with 5 addresses to return with no error, got: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test with default config should reject 1001 addresses
|
// Test that 6 addresses fails with correct error
|
||||||
var (
|
if _, err := api.GetLogs(context.Background(), FilterCriteria{
|
||||||
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{
|
|
||||||
FromBlock: big.NewInt(0),
|
FromBlock: big.NewInt(0),
|
||||||
ToBlock: big.NewInt(100),
|
ToBlock: big.NewInt(100),
|
||||||
Addresses: tooManyAddresses,
|
Addresses: addresses,
|
||||||
}); err != errExceedLogQueryLimit {
|
}); 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue