From 68713c4264148cde28f6bca8e5fe88a597600fd5 Mon Sep 17 00:00:00 2001 From: Jonas Theis <4181434+jonastheis@users.noreply.github.com> Date: Wed, 21 Aug 2024 07:06:37 +0800 Subject: [PATCH] feat: add TestGetLogsRange (#990) --- eth/filters/filter_system_test.go | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/eth/filters/filter_system_test.go b/eth/filters/filter_system_test.go index 81f56f17894b..32326acb9afa 100644 --- a/eth/filters/filter_system_test.go +++ b/eth/filters/filter_system_test.go @@ -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 { @@ -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()