diff --git a/ethrpc/ethrpc.go b/ethrpc/ethrpc.go index 91045aa..cbc9ff2 100644 --- a/ethrpc/ethrpc.go +++ b/ethrpc/ethrpc.go @@ -266,6 +266,26 @@ func (p *Provider) BlockByNumber(ctx context.Context, blockNum *big.Int) (*types return ret, err } +func (p *Provider) BlocksByNumbers(ctx context.Context, blockNumbers []*big.Int) ([]*types.Block, error) { + var headers = make([]*types.Block, len(blockNumbers)) + + var calls []Call + for index, blockNum := range blockNumbers { + calls = append(calls, BlockByNumber(blockNum).Strict(p.strictness).Into(&headers[index])) + } + + _, err := p.Do(ctx, calls...) + return headers, err +} + +func (p *Provider) BlocksByNumberRange(ctx context.Context, fromBlockNumber, toBlockNumber *big.Int) ([]*types.Block, error) { + var blockNumbers []*big.Int + for i := big.NewInt(0).Set(fromBlockNumber); i.Cmp(toBlockNumber) < 0; i.Add(i, big.NewInt(1)) { + blockNumbers = append(blockNumbers, big.NewInt(0).Set(i)) + } + return p.BlocksByNumbers(ctx, blockNumbers) +} + func (p *Provider) PeerCount(ctx context.Context) (uint64, error) { var ret uint64 _, err := p.Do(ctx, PeerCount().Strict(p.strictness).Into(&ret)) diff --git a/ethrpc/ethrpc_test.go b/ethrpc/ethrpc_test.go index ea4dc4b..2c3b884 100644 --- a/ethrpc/ethrpc_test.go +++ b/ethrpc/ethrpc_test.go @@ -100,6 +100,43 @@ func TestBlockByNumber(t *testing.T) { } } +func TestBlocskByNumbers(t *testing.T) { + p, err := ethrpc.NewProvider("https://nodes.sequence.app/polygon") + require.NoError(t, err) + + { + blocks, err := p.BlocksByNumbers(context.Background(), []*big.Int{ + big.NewInt(1_000_000), + big.NewInt(1_000_001), + }) + require.NoError(t, err) + require.NotNil(t, blocks) + require.Len(t, blocks, 2) + require.Equal(t, uint64(1_000_000), blocks[0].NumberU64()) + require.Equal(t, uint64(1_000_001), blocks[1].NumberU64()) + } +} + +func TestBlocksByNumberRange(t *testing.T) { + p, err := ethrpc.NewProvider("https://nodes.sequence.app/polygon") + require.NoError(t, err) + + { + blocks, err := p.BlocksByNumberRange(context.Background(), + big.NewInt(1_000_000), + big.NewInt(1_000_005), + ) + require.NoError(t, err) + require.NotNil(t, blocks) + require.Len(t, blocks, 5) + require.Equal(t, uint64(1_000_000), blocks[0].NumberU64()) + require.Equal(t, uint64(1_000_001), blocks[1].NumberU64()) + require.Equal(t, uint64(1_000_002), blocks[2].NumberU64()) + require.Equal(t, uint64(1_000_003), blocks[3].NumberU64()) + require.Equal(t, uint64(1_000_004), blocks[4].NumberU64()) + } +} + func TestHeadsByNumbers(t *testing.T) { p, err := ethrpc.NewProvider("https://nodes.sequence.app/polygon") require.NoError(t, err) diff --git a/ethrpc/interface.go b/ethrpc/interface.go index 10395a6..e0cee4a 100644 --- a/ethrpc/interface.go +++ b/ethrpc/interface.go @@ -80,6 +80,12 @@ type Interface interface { // BlockNumber = eth_blockNumber BlockNumber(ctx context.Context) (uint64, error) + // BlocksByNumbers = batch of eth_getBlockByNumber + BlocksByNumbers(ctx context.Context, blockNumbers []*big.Int) ([]*types.Block, error) + + // BlocksByNumberRange = batch of eth_getBlockByNumber + BlocksByNumberRange(ctx context.Context, fromBlockNumber, toBlockNumber *big.Int) ([]*types.Block, error) + // PeerCount = net_peerCount PeerCount(ctx context.Context) (uint64, error)