Skip to content

Commit

Permalink
ethrpc: add BlocksByNumbers and BlocksByNumberRange (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
marino39 authored Nov 18, 2024
1 parent 443db39 commit 5ef9f9a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ethrpc/ethrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
37 changes: 37 additions & 0 deletions ethrpc/ethrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions ethrpc/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 5ef9f9a

Please sign in to comment.