Skip to content

Commit

Permalink
Add batch get logs
Browse files Browse the repository at this point in the history
  • Loading branch information
oilnam committed Jan 9, 2024
1 parent 326863e commit d89b980
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
36 changes: 34 additions & 2 deletions ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,8 @@ func Eth1() *big.Int {
return big.NewInt(1000000000000000000)
}

// GetBlocksByRange returns blocks by range
func (rpc *EthRPC) GetBlocksByRange(from, to int, withTransactions bool) ([]*Block, error) {
// BatchGetBlocksByRange returns blocks by range
func (rpc *EthRPC) BatchGetBlocksByRange(from, to int, withTransactions bool) ([]*Block, error) {
var requests []ethRequest
for i := from; i <= to; i++ {
requests = append(requests, ethRequest{
Expand Down Expand Up @@ -551,6 +551,38 @@ func (rpc *EthRPC) GetBlocksByRange(from, to int, withTransactions bool) ([]*Blo
return blocks, nil
}

// BatchGetLogsByRange returns all logs for a range, fetched in a single batched call
func (rpc *EthRPC) BatchGetLogsByRange(from, to int) ([][]Log, error) {
var requests []ethRequest
for i := from; i <= to; i++ {
requests = append(requests, ethRequest{
ID: i,
JSONRPC: "2.0",
Method: "eth_getLogs",
Params: []interface{}{FilterParams{
FromBlock: IntToHex(i),
ToBlock: IntToHex(i),
}},
})
}

responses, err := rpc.batchCall(requests)
if err != nil {
return nil, err
}

var batchedLogs [][]Log
for _, res := range responses {
var logs []Log
if err := json.Unmarshal(res, &logs); err != nil {
return nil, err
}
batchedLogs = append(batchedLogs, logs)
}

return batchedLogs, nil
}

func (rpc *EthRPC) batchCall(req []ethRequest) ([]json.RawMessage, error) {
body, err := json.Marshal(req)
if err != nil {
Expand Down
19 changes: 17 additions & 2 deletions ethrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ func (s *EthRPCTestSuite) TestEthUninstallFilter() {
s.Require().Equal(boolRes, uninstall)
}

func (s *EthRPCTestSuite) TestGetBlocksByRange() {
func (s *EthRPCTestSuite) TestBatchGetBlocksByRange() {
// Test with transactions
from := 3274863
to := from + 1
Expand All @@ -1159,7 +1159,21 @@ func (s *EthRPCTestSuite) TestGetBlocksByRange() {
return httpmock.NewStringResponse(200, `[]`), nil
})

_, err := s.rpc.GetBlocksByRange(from, to, true)
_, err := s.rpc.BatchGetBlocksByRange(from, to, true)
s.Require().Nil(err)
}

func (s *EthRPCTestSuite) TestBatchGetLogsByRange() {
from := 3274863
to := from + 1

httpmock.Reset()
httpmock.RegisterResponder("POST", s.rpc.url, func(request *http.Request) (*http.Response, error) {
s.Equal(`[{"id":3274863,"jsonrpc":"2.0","method":"eth_getLogs","params":[{"fromBlock":"0x31f86f","toBlock":"0x31f86f"}]},{"id":3274864,"jsonrpc":"2.0","method":"eth_getLogs","params":[{"fromBlock":"0x31f870","toBlock":"0x31f870"}]}]`, string(s.getBody(request)))
return httpmock.NewStringResponse(200, `[]`), nil
})

_, err := s.rpc.BatchGetLogsByRange(from, to)
s.Require().Nil(err)
}

Expand All @@ -1174,6 +1188,7 @@ func TestEthError(t *testing.T) {

err = EthError{32847, "Kuku"}
require.Equal(t, "Error 32847 (Kuku)", err.Error())

}

func TestEth1(t *testing.T) {
Expand Down

0 comments on commit d89b980

Please sign in to comment.