Skip to content

Commit

Permalink
batch rpc calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ucwong committed Jul 20, 2024
1 parent d52425b commit a96c73b
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package robot

import (
//"context"
"errors"
"fmt"
"strconv"
"time"

Expand Down Expand Up @@ -77,13 +79,40 @@ func (m *Monitor) rpcBlockByNumber(blockNumber uint64) (*types.Block, error) {
}

func (m *Monitor) rpcBatchBlockByNumber(from, to uint64) (result []*types.Block, err error) {
batch := to - from
result = make([]*types.Block, batch)
reqs := make([]rpc.BatchElem, batch)
for i := range reqs {
reqs[i] = rpc.BatchElem{
Method: "ctxc_getBlockByNumber",
Args: []any{hexutil.EncodeUint64(from + uint64(i)), true},
Result: &result[i],
}
}

err = m.cl.BatchCall(reqs)

for i := range reqs {
if reqs[i].Error != nil {
return nil, reqs[i].Error
}
if result[i] == nil {
return nil, fmt.Errorf("got null block %d", i)
}
}

return
}

func (m *Monitor) rpcBatchBlockByNumberLegacy(from, to uint64) (result []*types.Block, err error) {
batch := to - from
result = make([]*types.Block, batch)
var e error = nil
for i := 0; i < int(batch); i++ {
m.rpcWg.Add(1)
go func(index int) {
defer m.rpcWg.Done()
//log.Info("bach rpc", "from", from, "to", to, "i", index)
result[index], e = m.rpcBlockByNumber(from + uint64(index))
if e != nil {
err = e
Expand Down

0 comments on commit a96c73b

Please sign in to comment.