diff --git a/rpc/block.go b/rpc/block.go index 921b8d45..aa4b7de6 100644 --- a/rpc/block.go +++ b/rpc/block.go @@ -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 } @@ -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 diff --git a/rpc/block_test.go b/rpc/block_test.go index 58beccd2..c9c2d390 100644 --- a/rpc/block_test.go +++ b/rpc/block_test.go @@ -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, }, }, { @@ -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) @@ -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") diff --git a/rpc/mock_test.go b/rpc/mock_test.go index e84b3246..97e4982f 100644 --- a/rpc/mock_test.go +++ b/rpc/mock_test.go @@ -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 { diff --git a/rpc/types_block.go b/rpc/types_block.go index 143ee34a..ec2b007c 100644 --- a/rpc/types_block.go +++ b/rpc/types_block.go @@ -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 { @@ -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"` } @@ -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"` }