Skip to content

Commit

Permalink
feat: add TestGetLogsRange (#990)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonastheis authored Aug 20, 2024
1 parent f883ca8 commit 68713c4
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions eth/filters/filter_system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ import (
"github.com/scroll-tech/go-ethereum/core/rawdb"
"github.com/scroll-tech/go-ethereum/core/state"
"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/ethdb"
"github.com/scroll-tech/go-ethereum/event"
"github.com/scroll-tech/go-ethereum/internal/ethapi"
"github.com/scroll-tech/go-ethereum/params"
"github.com/scroll-tech/go-ethereum/rpc"
"github.com/scroll-tech/go-ethereum/trie"
)

type testBackend struct {
Expand Down Expand Up @@ -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.
func TestLogFilter(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 68713c4

Please sign in to comment.