Skip to content

Commit

Permalink
rpcv05 Update Block header and pending block header (#435)
Browse files Browse the repository at this point in the history
* rpcv05 Update Block header and pending block header

* wip

* update type

* remove unnecessary prints

* fix PriceInWei property type

---------

Co-authored-by: Carmen Cabrera <[email protected]>
  • Loading branch information
rianhughes and cicr99 authored Oct 25, 2023
1 parent 04f9adc commit d623521
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 42 deletions.
22 changes: 12 additions & 10 deletions rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,18 @@ func WithBlockTag(tag string) BlockID {
// BlockWithTxHashes gets block information given the block id.
func (provider *Provider) BlockWithTxHashes(ctx context.Context, blockID BlockID) (interface{}, error) {
var result BlockTxHashes
if err := do(ctx, provider.c, "starknet_getBlockWithTxHashes", &result, blockID); err != nil {
return nil, tryUnwrapToRPCErr(err,ErrBlockNotFound )
if err := do(ctx, provider.c, "starknet_getBlockWithTxHashes", &result, blockID); err != nil {
return nil, tryUnwrapToRPCErr(err, ErrBlockNotFound)
}

// if header.Hash == nil it's a pending block
if result.BlockHeader.BlockHash == nil {
return &PendingBlockTxHashes{
ParentHash: result.ParentHash,
Timestamp: result.Timestamp,
SequencerAddress: result.SequencerAddress,
Transactions: result.Transactions,
PendingBlockHeader{
ParentHash: result.ParentHash,
Timestamp: result.Timestamp,
SequencerAddress: result.SequencerAddress},
result.Transactions,
}, nil
}

Expand Down Expand Up @@ -96,10 +97,11 @@ func (provider *Provider) BlockWithTxs(ctx context.Context, blockID BlockID) (in
// if header.Hash == nil it's a pending block
if result.BlockHeader.BlockHash == nil {
return &PendingBlock{
ParentHash: result.ParentHash,
Timestamp: result.Timestamp,
SequencerAddress: result.SequencerAddress,
Transactions: result.Transactions,
PendingBlockHeader{
ParentHash: result.ParentHash,
Timestamp: result.Timestamp,
SequencerAddress: result.SequencerAddress},
result.Transactions,
}, nil
}
return &result, nil
Expand Down
20 changes: 10 additions & 10 deletions rpc/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,14 @@ func TestBlockWithTxHashes(t *testing.T) {
testSet := map[string][]testSetType{
"mock": {
{
BlockID: BlockID{Tag: "latest"},
BlockID: BlockID{Tag: "latest"},
ExpectedError: nil,
ExpectedPendingBlockWithTxHashes: &PendingBlockTxHashes{
ParentHash: &felt.Zero,
Timestamp: 123,
SequencerAddress: &felt.Zero,
Transactions: txHashes,
PendingBlockHeader{
ParentHash: &felt.Zero,
Timestamp: 123,
SequencerAddress: &felt.Zero},
txHashes,
},
},
{
Expand Down Expand Up @@ -166,14 +168,12 @@ func TestBlockWithTxHashes(t *testing.T) {
},
"mainnet": {},
}[testEnv]

for _, test := range testSet {
spy := NewSpy(testConfig.provider.c)
testConfig.provider.c = spy
result, err := testConfig.provider.BlockWithTxHashes(context.Background(), test.BlockID)
if err != test.ExpectedError {
t.Fatal("BlockWithTxHashes match the expected error:", err)
}
require.Equal(t, test.ExpectedError, err, "Error in BlockWithTxHashes")
switch resultType := result.(type) {
case *BlockTxHashes:
block, ok := result.(*BlockTxHashes)
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestBlockWithTxHashes(t *testing.T) {
if !ok {
t.Fatalf("should return *PendingBlockTxHashes, instead: %T\n", result)
}

require.Equal(t, pBlock.ParentHash, test.ExpectedPendingBlockWithTxHashes.ParentHash, "Error in PendingBlockTxHashes ParentHash")
require.Equal(t, pBlock.SequencerAddress, test.ExpectedPendingBlockWithTxHashes.SequencerAddress, "Error in PendingBlockTxHashes SequencerAddress")
require.Equal(t, pBlock.Timestamp, test.ExpectedPendingBlockWithTxHashes.Timestamp, "Error in PendingBlockTxHashes Timestamp")
Expand Down
19 changes: 12 additions & 7 deletions rpc/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,16 +634,21 @@ func mock_starknet_getBlockWithTxHashes(result interface{}, method string, args
}

if blockId.Tag == "latest" {
pBlock, err := json.Marshal(PendingBlockTxHashes{
ParentHash: &felt.Zero,
Timestamp: 123,
SequencerAddress: &felt.Zero,
Transactions: txHashes,
})
pBlock, err := json.Marshal(
PendingBlockTxHashes{
PendingBlockHeader{
ParentHash: &felt.Zero,
Timestamp: 123,
SequencerAddress: &felt.Zero},
txHashes,
})
if err != nil {
return err
}
err = json.Unmarshal(pBlock, &r)
if err != nil {
return err
}
json.Unmarshal(pBlock, &r)
} else {
blockHash, err := utils.HexToFelt("0xbeef")
if err != nil {
Expand Down
42 changes: 27 additions & 15 deletions rpc/types_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,8 @@ type Block struct {
}

type PendingBlock struct {
// ParentHash The hash of this block's parent
ParentHash *felt.Felt `json:"parent_hash"`
// Timestamp the time in which the block was created, encoded in Unix time
Timestamp uint64 `json:"timestamp"`
// SequencerAddress the StarkNet identity of the sequencer submitting this block
SequencerAddress *felt.Felt `json:"sequencer_address"`
// Transactions The transactions in this block
Transactions BlockTransactions `json:"transactions"`
PendingBlockHeader
BlockTransactions
}

type BlockTxHashes struct {
Expand All @@ -107,13 +101,7 @@ type BlockTxHashes struct {
}

type PendingBlockTxHashes struct {
// ParentHash The hash of this block's parent
ParentHash *felt.Felt `json:"parent_hash"`
// Timestamp the time in which the block was created, encoded in Unix time
Timestamp uint64 `json:"timestamp"`
// SequencerAddress the StarkNet identity of the sequencer submitting this block
SequencerAddress *felt.Felt `json:"sequencer_address"`
// Transactions The hashes of the transactions included in this block
PendingBlockHeader
Transactions []*felt.Felt `json:"transactions"`
}

Expand All @@ -130,4 +118,28 @@ type BlockHeader struct {
Timestamp uint64 `json:"timestamp"`
// SequencerAddress the StarkNet identity of the sequencer submitting this block
SequencerAddress *felt.Felt `json:"sequencer_address"`
// The price of l1 gas in the block
L1GasPrice ResourcePrice `json:"l1_gas_price"`
// Semver of the current Starknet protocol
StarknetVersion string `json:"starknet_version"`
}

type PendingBlockHeader struct {
// ParentHash The hash of this block's parent
ParentHash *felt.Felt `json:"parent_hash"`
// Timestamp the time in which the block was created, encoded in Unix time
Timestamp uint64 `json:"timestamp"`
// SequencerAddress the StarkNet identity of the sequencer submitting this block
SequencerAddress *felt.Felt `json:"sequencer_address"`
// The price of l1 gas in the block
L1GasPrice ResourcePrice `json:"l1_gas_price"`
// Semver of the current Starknet protocol
StarknetVersion string `json:"starknet_version"`
}

type ResourcePrice struct {
// The price of one unit of the given resource, denominated in strk
PriceInStrk NumAsHex `json:"price_in_strk,omitempty"`
// The price of one unit of the given resource, denominated in wei
PriceInWei NumAsHex `json:"price_in_wei"`
}

0 comments on commit d623521

Please sign in to comment.