Skip to content

Commit

Permalink
Merge pull request #3 from HAL-xyz/batching
Browse files Browse the repository at this point in the history
Batching Getting Blocks and Logs
  • Loading branch information
chakra-guy authored Feb 14, 2024
2 parents 3ea0bcd + 75a1537 commit 135f6a5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
54 changes: 54 additions & 0 deletions ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,60 @@ func (rpc *EthRPC) BatchGetLogsByRange(from, to int) ([][]Log, error) {
return batchedLogs, nil
}

// BatchGetBlocksAndLogs returns blocks and logs for a range in order, fetched in a single batched call
func (rpc *EthRPC) BatchGetBlocksAndLogs(numbers ...int) ([]*Block, [][]Log, error) {
var blockRequests []ethRequest
var logRequests []ethRequest
for _, number := range numbers {
blockRequests = append(blockRequests, ethRequest{
ID: number,
JSONRPC: "2.0",
Method: "eth_getBlockByNumber",
Params: []interface{}{IntToHex(number), true},
})
logRequests = append(logRequests, ethRequest{
ID: number,
JSONRPC: "2.0",
Method: "eth_getLogs",
Params: []interface{}{FilterParams{
FromBlock: IntToHex(number),
ToBlock: IntToHex(number),
}},
})
}

blockResponses, err := rpc.batchCall(blockRequests)
if err != nil {
return nil, nil, err
}

logResponses, err := rpc.batchCall(logRequests)
if err != nil {
return nil, nil, err
}

var blocks []*Block
for _, res := range blockResponses {
var proxy proxyBlockWithTransactions
if err := json.Unmarshal(res, &proxy); err != nil {
return nil, nil, err
}
block := proxy.toBlock()
blocks = append(blocks, &block)
}

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

return blocks, batchedLogs, nil
}

func (rpc *EthRPC) batchCall(req []ethRequest) ([]json.RawMessage, error) {
body, err := json.Marshal(req)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions ethrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"net/http"
"strconv"
"strings"
"testing"

"github.com/jarcoal/httpmock"
Expand Down Expand Up @@ -1177,6 +1178,32 @@ func (s *EthRPCTestSuite) TestBatchGetLogsByRange() {
s.Require().Nil(err)
}

func (s *EthRPCTestSuite) TestBatchGetBlocksAndLogs() {
numbers := []int{3274863, 3274864}

httpmock.Reset()

// Mock responder for both block and log requests
httpmock.RegisterResponder("POST", s.rpc.url, func(request *http.Request) (*http.Response, error) {
requestBody := string(s.getBody(request))
if strings.Contains(requestBody, `"method":"eth_getBlockByNumber"`) {
blockResponseJSON := `[{"jsonrpc":"2.0","id":3274863,"result":{}},{"jsonrpc":"2.0","id":3274864,"result":{}}]`
return httpmock.NewStringResponse(200, blockResponseJSON), nil
} else if strings.Contains(requestBody, `"method":"eth_getLogs"`) {
logResponseJSON := `[{"jsonrpc":"2.0","id":3274863,"result":[]},{"jsonrpc":"2.0","id":3274864,"result":[]}]`
return httpmock.NewStringResponse(200, logResponseJSON), nil
}
return nil, fmt.Errorf("unexpected request")
})

blocks, logs, err := s.rpc.BatchGetBlocksAndLogs(numbers...)

// Assertions
s.Require().Nil(err)
s.Require().NotNil(blocks)
s.Require().NotNil(logs)
}

func TestEthRPCTestSuite(t *testing.T) {
suite.Run(t, new(EthRPCTestSuite))
}
Expand Down

0 comments on commit 135f6a5

Please sign in to comment.