feat: add TestGetLogsRange (#990)

This commit is contained in:
Jonas Theis 2024-08-21 07:06:37 +08:00 committed by GitHub
parent f883ca89cb
commit 68713c4264
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -35,12 +35,14 @@ import (
"github.com/scroll-tech/go-ethereum/core/rawdb" "github.com/scroll-tech/go-ethereum/core/rawdb"
"github.com/scroll-tech/go-ethereum/core/state" "github.com/scroll-tech/go-ethereum/core/state"
"github.com/scroll-tech/go-ethereum/core/types" "github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/core/vm"
"github.com/scroll-tech/go-ethereum/crypto" "github.com/scroll-tech/go-ethereum/crypto"
"github.com/scroll-tech/go-ethereum/ethdb" "github.com/scroll-tech/go-ethereum/ethdb"
"github.com/scroll-tech/go-ethereum/event" "github.com/scroll-tech/go-ethereum/event"
"github.com/scroll-tech/go-ethereum/internal/ethapi" "github.com/scroll-tech/go-ethereum/internal/ethapi"
"github.com/scroll-tech/go-ethereum/params" "github.com/scroll-tech/go-ethereum/params"
"github.com/scroll-tech/go-ethereum/rpc" "github.com/scroll-tech/go-ethereum/rpc"
"github.com/scroll-tech/go-ethereum/trie"
) )
type testBackend struct { type testBackend struct {
@ -456,6 +458,62 @@ func TestInvalidGetLogsRequest(t *testing.T) {
} }
} }
func TestGetLogsRange(t *testing.T) {
var (
db = rawdb.NewMemoryDatabase()
_, sys = newTestFilterSystem(t, db, Config{})
api = NewFilterAPI(sys, false, 2)
)
gspec := &core.Genesis{
Config: params.TestChainConfig,
}
_, err := gspec.Commit(db, trie.NewDatabase(db, &trie.Config{IsUsingZktrie: true}))
if err != nil {
t.Fatal(err)
}
var l uint64
chain, err := core.NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil, &l)
if err != nil {
t.Fatal(err)
}
bs, _ := core.GenerateChain(params.TestChainConfig, chain.Genesis(), ethash.NewFaker(), db, 10, nil)
if _, err := chain.InsertChain(bs); err != nil {
panic(err)
}
// those test cases should fail because block range is greater then limit
failTestCases := []FilterCriteria{
// from 0 to 2 block
0: {FromBlock: big.NewInt(0), ToBlock: big.NewInt(2)},
// from 8 to latest block (10)
1: {FromBlock: big.NewInt(8)},
// from 0 to latest block (10)
2: {FromBlock: big.NewInt(0)},
}
for i, test := range failTestCases {
if _, err := api.GetLogs(context.Background(), test); err == nil {
t.Errorf("Expected Logs for failing case #%d to fail", i)
}
}
okTestCases := []FilterCriteria{
// from latest to latest block
0: {},
// from 9 to last block (10)
1: {FromBlock: big.NewInt(9)},
// from 3 to 4 block
2: {FromBlock: big.NewInt(3), ToBlock: big.NewInt(4)},
}
for i, test := range okTestCases {
if _, err := api.GetLogs(context.Background(), test); err != nil {
t.Errorf("Expected Logs for ok case #%d not to fail", i)
}
}
}
// TestLogFilter tests whether log filters match the correct logs that are posted to the event feed. // TestLogFilter tests whether log filters match the correct logs that are posted to the event feed.
func TestLogFilter(t *testing.T) { func TestLogFilter(t *testing.T) {
t.Parallel() t.Parallel()