Skip to content

Commit

Permalink
add blockByNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
augustbleeds committed Apr 26, 2024
1 parent 7f8b4db commit 0b1d9f9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
43 changes: 38 additions & 5 deletions relayer/pkg/starknet/chain_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type FinalizedBlock = starknetrpc.Block
// used to create batch requests
type StarknetBatchBuilder interface {
RequestBlockByHash(h *felt.Felt) StarknetBatchBuilder
// RequestBlockByNumber(id uint64) (StarknetBatchBuilder, error)
// RequestLatestPendingBlock() (StarknetBatchBuilder, error)
RequestBlockByNumber(id uint64) StarknetBatchBuilder
// RequestLatestPendingBlock() (StarknetBatchBuilder)
RequestLatestBlockHashAndNumber() StarknetBatchBuilder
// RequestEventsByFilter(f starknetrpc.EventFilter) (StarknetBatchBuilder, error)
// RequestTxReceiptByHash(h *felt.Felt) (StarknetBatchBuilder, error)
// RequestEventsByFilter(f starknetrpc.EventFilter) (StarknetBatchBuilder)
// RequestTxReceiptByHash(h *felt.Felt) (StarknetBatchBuilder)
Build() []gethrpc.BatchElem
}

Expand All @@ -46,6 +46,17 @@ func (b *batchBuilder) RequestBlockByHash(h *felt.Felt) StarknetBatchBuilder {
return b
}

func (b *batchBuilder) RequestBlockByNumber(id uint64) StarknetBatchBuilder {
b.args = append(b.args, gethrpc.BatchElem{
Method: "starknet_getBlockWithTxs",
Args: []interface{}{
starknetrpc.BlockID{Number: &id},
},
Result: &FinalizedBlock{},
})
return b
}

func (b *batchBuilder) RequestLatestBlockHashAndNumber() StarknetBatchBuilder {
b.args = append(b.args, gethrpc.BatchElem{
Method: "starknet_blockHashAndNumber",
Expand All @@ -63,7 +74,7 @@ type StarknetChainClient interface {
// only finalized blocks have a block hashes
BlockByHash(ctx context.Context, h *felt.Felt) (FinalizedBlock, error)
// only finalized blocks have numbers
// BlockByNumber(ctx context.Context, id uint64) (FinalizedBlock, error)
BlockByNumber(ctx context.Context, id uint64) (FinalizedBlock, error)
// only way to get the latest pending block (only 1 pending block exists at a time)
// LatestPendingBlock(ctx context.Context) (starknetrpc.PendingBlock, error)
// returns block number and block has of latest finalized block
Expand Down Expand Up @@ -98,6 +109,28 @@ func (c *Client) BlockByHash(ctx context.Context, h *felt.Felt) (FinalizedBlock,
return *finalizedBlock, nil
}

func (c *Client) BlockByNumber(ctx context.Context, id uint64) (FinalizedBlock, error) {
if c.defaultTimeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, c.defaultTimeout)
defer cancel()
}

block, err := c.Provider.BlockWithTxs(ctx, starknetrpc.BlockID{Number: &id})

if err != nil {
return FinalizedBlock{}, fmt.Errorf("error in BlockByNumber: %w", err)
}

finalizedBlock, ok := block.(*FinalizedBlock)

if !ok {
return FinalizedBlock{}, fmt.Errorf("expected type Finalized block but found: %T", block)
}

return *finalizedBlock, nil
}

func (c *Client) LatestBlockHashAndNumber(ctx context.Context) (starknetrpc.BlockHashAndNumberOutput, error) {
if c.defaultTimeout != 0 {
var cancel context.CancelFunc
Expand Down
40 changes: 32 additions & 8 deletions relayer/pkg/starknet/chain_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,20 @@ func TestChainClient(t *testing.T) {
"id": %d,
%s
},
{
"jsonrpc": "2.0",
"id": %d,
%s
},
{
"jsonrpc": "2.0",
"id": %d,
"result": %s
}
]`, batchCall[0].Id, blockResponse, batchCall[1].Id, blockHashAndNumberResponse)
]`, batchCall[0].Id, blockResponse,
batchCall[1].Id, blockResponse,
batchCall[2].Id, blockHashAndNumberResponse,
)

out = []byte(response)

Expand All @@ -139,6 +147,12 @@ func TestChainClient(t *testing.T) {
assert.Equal(t, blockHash, block.BlockHash)
})

t.Run("get BlockByNumber", func(t *testing.T) {
block, err := client.BlockByNumber(context.TODO(), uint64(blockNumber))
require.NoError(t, err)
assert.Equal(t, uint64(blockNumber), block.BlockNumber)
})

t.Run("get LatestBlockHashAndNumber", func(t *testing.T) {
output, err := client.LatestBlockHashAndNumber(context.TODO())
require.NoError(t, err)
Expand All @@ -148,14 +162,15 @@ func TestChainClient(t *testing.T) {

t.Run("get Batch", func(t *testing.T) {
builder := NewBatchBuilder()
builder.RequestBlockByHash(
blockHash,
).RequestLatestBlockHashAndNumber()
builder.
RequestBlockByHash(blockHash).
RequestBlockByNumber(uint64(blockNumber)).
RequestLatestBlockHashAndNumber()

results, err := client.Batch(context.TODO(), builder)
require.NoError(t, err)

assert.Equal(t, 2, len(results))
assert.Equal(t, 3, len(results))

t.Run("gets BlockByHash in Batch", func(t *testing.T) {
assert.Equal(t, "starknet_getBlockWithTxs", results[0].Method)
Expand All @@ -165,10 +180,19 @@ func TestChainClient(t *testing.T) {
assert.Equal(t, blockHash, block.BlockHash)
})

t.Run("gets LatestBlockHashAndNumber in Batch", func(t *testing.T) {
assert.Equal(t, "starknet_blockHashAndNumber", results[1].Method)
t.Run("gets BlockByNumber in Batch", func(t *testing.T) {
assert.Equal(t, "starknet_getBlockWithTxs", results[1].Method)
assert.Nil(t, results[1].Error)
info, ok := results[1].Result.(*starknetrpc.BlockHashAndNumberOutput)
block, ok := results[1].Result.(*FinalizedBlock)
assert.True(t, ok)
assert.Equal(t, uint64(blockNumber), block.BlockNumber)

})

t.Run("gets LatestBlockHashAndNumber in Batch", func(t *testing.T) {
assert.Equal(t, "starknet_blockHashAndNumber", results[2].Method)
assert.Nil(t, results[2].Error)
info, ok := results[2].Result.(*starknetrpc.BlockHashAndNumberOutput)
assert.True(t, ok)
assert.Equal(t, blockHash, info.BlockHash)
assert.Equal(t, uint64(blockNumber), info.BlockNumber)
Expand Down

0 comments on commit 0b1d9f9

Please sign in to comment.