Skip to content

Commit

Permalink
Merge pull request #1 from HAL-xyz/batch-get-blocks
Browse files Browse the repository at this point in the history
Batch Get Blocks
  • Loading branch information
chakra-guy authored Jan 5, 2024
2 parents 7445644 + b36e067 commit 326863e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
77 changes: 77 additions & 0 deletions ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math/big"
Expand Down Expand Up @@ -512,3 +513,79 @@ func (rpc *EthRPC) Eth1() *big.Int {
func Eth1() *big.Int {
return big.NewInt(1000000000000000000)
}

// GetBlocksByRange returns blocks by range
func (rpc *EthRPC) GetBlocksByRange(from, to int, withTransactions bool) ([]*Block, error) {
var requests []ethRequest
for i := from; i <= to; i++ {
requests = append(requests, ethRequest{
ID: i,
JSONRPC: "2.0",
Method: "eth_getBlockByNumber",
Params: []interface{}{IntToHex(i), withTransactions},
})
}

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

var blocks []*Block
for _, res := range responses {
var proxy proxyBlock
if withTransactions {
proxy = new(proxyBlockWithTransactions)
} else {
proxy = new(proxyBlockWithoutTransactions)
}

if err := json.Unmarshal(res, proxy); err != nil {
return nil, err
}

block := proxy.toBlock()
blocks = append(blocks, &block)
}

return blocks, nil
}

func (rpc *EthRPC) batchCall(req []ethRequest) ([]json.RawMessage, error) {
body, err := json.Marshal(req)
if err != nil {
return nil, err
}

response, err := rpc.client.Post(rpc.url, "application/json", bytes.NewBuffer(body))
if response != nil {
defer response.Body.Close()
}
if err != nil {
return nil, err
}

data, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}

if rpc.Debug {
rpc.log.Println(fmt.Sprintf("Batch Request: %s\nResponse: %s\n", body, data))
}

var responses []ethResponse
if err := json.Unmarshal(data, &responses); err != nil {
return nil, err
}

var results []json.RawMessage
for _, res := range responses {
if res.Error != nil {
return nil, *res.Error
}
results = append(results, res.Result)
}

return results, nil
}
15 changes: 15 additions & 0 deletions ethrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,21 @@ func (s *EthRPCTestSuite) TestEthUninstallFilter() {
s.Require().Equal(boolRes, uninstall)
}

func (s *EthRPCTestSuite) TestGetBlocksByRange() {
// Test with transactions
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_getBlockByNumber","params":["0x31f86f",true]},{"id":3274864,"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["0x31f870",true]}]`, string(s.getBody(request)))
return httpmock.NewStringResponse(200, `[]`), nil
})

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

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

0 comments on commit 326863e

Please sign in to comment.