Skip to content

Commit

Permalink
add blockhashandnumber method
Browse files Browse the repository at this point in the history
  • Loading branch information
augustbleeds committed Apr 25, 2024
1 parent 7dd066d commit 7f8b4db
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 24 deletions.
46 changes: 36 additions & 10 deletions relayer/pkg/starknet/chain_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ type FinalizedBlock = starknetrpc.Block

// used to create batch requests
type StarknetBatchBuilder interface {
RequestBlockByHash(h *felt.Felt) (StarknetBatchBuilder, error)
RequestBlockByHash(h *felt.Felt) StarknetBatchBuilder
// RequestBlockByNumber(id uint64) (StarknetBatchBuilder, error)
// RequestLatestPendingBlock() (StarknetBatchBuilder, error)
// RequestLatestBlockHashAndNumber() (StarknetBatchBuilder, error)
RequestLatestBlockHashAndNumber() StarknetBatchBuilder
// RequestEventsByFilter(f starknetrpc.EventFilter) (StarknetBatchBuilder, error)
// RequestTxReceiptByHash(h *felt.Felt) (StarknetBatchBuilder, error)
Build() []gethrpc.BatchElem
Expand All @@ -31,19 +31,28 @@ type batchBuilder struct {

func NewBatchBuilder() StarknetBatchBuilder {
return &batchBuilder{
args: make([]gethrpc.BatchElem, 0),
args: nil,
}
}

func (b *batchBuilder) RequestBlockByHash(h *felt.Felt) (StarknetBatchBuilder, error) {
func (b *batchBuilder) RequestBlockByHash(h *felt.Felt) StarknetBatchBuilder {
b.args = append(b.args, gethrpc.BatchElem{
Method: "starknet_getBlockWithTxs",
Args: []interface{}{
starknetrpc.BlockID{Hash: h},
},
Result: &FinalizedBlock{},
})
return b, nil
return b
}

func (b *batchBuilder) RequestLatestBlockHashAndNumber() StarknetBatchBuilder {
b.args = append(b.args, gethrpc.BatchElem{
Method: "starknet_blockHashAndNumber",
Args: nil,
Result: &starknetrpc.BlockHashAndNumberOutput{},
})
return b
}

func (b *batchBuilder) Build() []gethrpc.BatchElem {
Expand All @@ -54,17 +63,19 @@ 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)
// LatestPendingBlock(ctx context.Context) (starknetrpc.PendingBlock, error)
// returns block number and block has of latest finalized block
LatestBlockHashAndNumber(ctx context.Context) (starknetrpc.BlockHashAndNumberOutput, error)
// get block logs, event logs, etc.
EventsByFilter(ctx context.Context, f starknetrpc.EventFilter) ([]starknetrpc.EmittedEvent, error)
TxReceiptByHash(ctx context.Context, h *felt.Felt) (starknetrpc.TransactionReceipt, error)
Batch(builder StarknetBatchBuilder) ([]gethrpc.BatchElem, error)
// EventsByFilter(ctx context.Context, f starknetrpc.EventFilter) ([]starknetrpc.EmittedEvent, error)
// TxReceiptByHash(ctx context.Context, h *felt.Felt) (starknetrpc.TransactionReceipt, error)
Batch(ctx context.Context, builder StarknetBatchBuilder) ([]gethrpc.BatchElem, error)
}

var _ StarknetChainClient = (*Client)(nil)

func (c *Client) BlockByHash(ctx context.Context, h *felt.Felt) (FinalizedBlock, error) {
if c.defaultTimeout != 0 {
var cancel context.CancelFunc
Expand All @@ -87,6 +98,21 @@ func (c *Client) BlockByHash(ctx context.Context, h *felt.Felt) (FinalizedBlock,
return *finalizedBlock, nil
}

func (c *Client) LatestBlockHashAndNumber(ctx context.Context) (starknetrpc.BlockHashAndNumberOutput, error) {
if c.defaultTimeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, c.defaultTimeout)
defer cancel()
}

info, err := c.Provider.BlockHashAndNumber(ctx)
if err != nil {
return starknetrpc.BlockHashAndNumberOutput{}, fmt.Errorf("error in LatestBlockHashAndNumber: %w", err)
}

return *info, nil
}

func (c *Client) Batch(ctx context.Context, builder StarknetBatchBuilder) ([]gethrpc.BatchElem, error) {
if c.defaultTimeout != 0 {
var cancel context.CancelFunc
Expand Down
65 changes: 51 additions & 14 deletions relayer/pkg/starknet/chain_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/NethermindEth/juno/core/felt"
starknetrpc "github.com/NethermindEth/starknet.go/rpc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand All @@ -20,6 +21,7 @@ import (
var (
myChainID = "SN_SEPOLIA"
myTimeout = 10 * time.Second
blockNumber = 48719
blockHash, _ = new(felt.Felt).SetString("0x725407fcc3bd43e50884f50f1e0ef32aa9f814af3da475411934a7dbd4b41a")
blockResponse = []byte(`
"result": {
Expand Down Expand Up @@ -65,6 +67,10 @@ var (
]
}]
}`)
blockHashAndNumberResponse = fmt.Sprintf(`{"block_hash": "%s", "block_number": %d}`,
"0x725407fcc3bd43e50884f50f1e0ef32aa9f814af3da475411934a7dbd4b41a",
48719,
)
)

func TestChainClient(t *testing.T) {
Expand All @@ -88,6 +94,8 @@ func TestChainClient(t *testing.T) {
out = []byte(fmt.Sprintf(`{ %s }`, blockResponse))
case "starknet_blockNumber":
out = []byte(`{"result": 1}`)
case "starknet_blockHashAndNumber":
out = []byte(fmt.Sprintf(`{"result": %s}`, blockHashAndNumberResponse))
default:
require.False(t, true, "unsupported RPC method %s", call.Method)
}
Expand All @@ -97,14 +105,21 @@ func TestChainClient(t *testing.T) {
errBatchMarshal := json.Unmarshal(req, &batchCall)
assert.NoError(t, errBatchMarshal)

out = []byte(fmt.Sprintf(`
[
{
"jsonrpc": "2.0",
"id": %d,
%s
}
]`, batchCall[0].Id, blockResponse))
response := fmt.Sprintf(`
[
{
"jsonrpc": "2.0",
"id": %d,
%s
},
{
"jsonrpc": "2.0",
"id": %d,
"result": %s
}
]`, batchCall[0].Id, blockResponse, batchCall[1].Id, blockHashAndNumberResponse)

out = []byte(response)

}

Expand All @@ -124,18 +139,40 @@ func TestChainClient(t *testing.T) {
assert.Equal(t, blockHash, block.BlockHash)
})

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

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

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

assert.Equal(t, "starknet_getBlockWithTxs", results[0].Method)
assert.Nil(t, results[0].Error)
assert.Equal(t, 2, len(results))

t.Run("gets BlockByHash in Batch", func(t *testing.T) {
assert.Equal(t, "starknet_getBlockWithTxs", results[0].Method)
assert.Nil(t, results[0].Error)
block, ok := results[0].Result.(*FinalizedBlock)
assert.True(t, ok)
assert.Equal(t, blockHash, block.BlockHash)
})

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

block, ok := results[0].Result.(*FinalizedBlock)
assert.True(t, ok)
assert.Equal(t, blockHash, block.BlockHash)
})
}

0 comments on commit 7f8b4db

Please sign in to comment.