Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ethrpc: add BlocksByNumbers and BlocksByNumberRange #145

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading