Skip to content

Commit

Permalink
Fix e2e
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkushinDaniil committed Jan 29, 2025
1 parent fbe6f5c commit 21e7587
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 58 deletions.
79 changes: 25 additions & 54 deletions rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,45 +107,10 @@ type BlockHeader struct {
Timestamp uint64 `json:"timestamp"`
SequencerAddress *felt.Felt `json:"sequencer_address,omitempty"`
L1GasPrice *ResourcePrice `json:"l1_gas_price"`
L2GasPrice *ResourcePrice `json:"l2_gas_price"`
L2GasPrice *ResourcePrice `json:"l2_gas_price,omitempty"`
L1DataGasPrice *ResourcePrice `json:"l1_data_gas_price,omitempty"`
L1DAMode *L1DAMode `json:"l1_da_mode,omitempty"`
StarknetVersion string `json:"starknet_version"`
rpcVersion version
}

func (h BlockHeader) MarshalJSON() ([]byte, error) { //nolint:gocritic
switch h.rpcVersion {
case V0_7:
return json.Marshal(struct {
Hash *felt.Felt `json:"block_hash,omitempty"`
ParentHash *felt.Felt `json:"parent_hash"`
Number *uint64 `json:"block_number,omitempty"`
NewRoot *felt.Felt `json:"new_root,omitempty"`
Timestamp uint64 `json:"timestamp"`
SequencerAddress *felt.Felt `json:"sequencer_address,omitempty"`
L1GasPrice *ResourcePrice `json:"l1_gas_price"`
L1DataGasPrice *ResourcePrice `json:"l1_data_gas_price,omitempty"`
L1DAMode *L1DAMode `json:"l1_da_mode,omitempty"`
StarknetVersion string `json:"starknet_version"`
}{
Hash: h.Hash,
ParentHash: h.ParentHash,
Number: h.Number,
NewRoot: h.NewRoot,
Timestamp: h.Timestamp,
SequencerAddress: h.SequencerAddress,
L1GasPrice: h.L1GasPrice,
L1DataGasPrice: h.L1DataGasPrice,
L1DAMode: h.L1DAMode,
StarknetVersion: h.StarknetVersion,
})
case V0_8:
type alias BlockHeader // avoid infinite recursion
return json.Marshal(alias(h))
default:
return nil, errors.New("unknown BlockHeader version")
}
}

// https://github.com/starkware-libs/starknet-specs/blob/a789ccc3432c57777beceaa53a34a7ae2f25fda0/api/starknet_api_openrpc.json#L1131
Expand Down Expand Up @@ -206,15 +171,16 @@ func (h *Handler) BlockHashAndNumber() (*BlockHashAndNumber, *jsonrpc.Error) {
//
// It follows the specification defined here:
// https://github.com/starkware-libs/starknet-specs/blob/a789ccc3432c57777beceaa53a34a7ae2f25fda0/api/starknet_api_openrpc.json#L11
func (h *Handler) BlockWithTxHashes(id BlockID) (*BlockWithTxHashes, *jsonrpc.Error) {
return h.blockWithTxHashes(id, V0_8)
}

func (h *Handler) BlockWithTxHashesV0_7(id BlockID) (*BlockWithTxHashes, *jsonrpc.Error) {
return h.blockWithTxHashes(id, V0_7)
blockWithTxHashes, rpcErr := h.BlockWithTxHashes(id)
if rpcErr != nil {
return nil, rpcErr
}
blockWithTxHashes.L2GasPrice = nil
return blockWithTxHashes, nil
}

func (h *Handler) blockWithTxHashes(id BlockID, rpcVersion version) (*BlockWithTxHashes, *jsonrpc.Error) {
func (h *Handler) BlockWithTxHashes(id BlockID) (*BlockWithTxHashes, *jsonrpc.Error) {
block, rpcErr := h.blockByID(&id)
if rpcErr != nil {
return nil, rpcErr
Expand All @@ -232,7 +198,7 @@ func (h *Handler) blockWithTxHashes(id BlockID, rpcVersion version) (*BlockWithT

return &BlockWithTxHashes{
Status: status,
BlockHeader: adaptBlockHeader(block.Header, rpcVersion),
BlockHeader: adaptBlockHeader(block.Header),
TxnHashes: txnHashes,
}, nil
}
Expand All @@ -251,7 +217,12 @@ func (h *Handler) BlockTransactionCount(id BlockID) (uint64, *jsonrpc.Error) {
}

func (h *Handler) BlockWithReceiptsV0_7(id BlockID) (*BlockWithReceipts, *jsonrpc.Error) {
return h.blockWithReceipts(id, V0_7)
blockWithReceipts, err := h.blockWithReceipts(id, V0_7)
if err != nil {
return nil, err
}
blockWithReceipts.L2GasPrice = nil
return blockWithReceipts, nil
}

func (h *Handler) BlockWithReceipts(id BlockID) (*BlockWithReceipts, *jsonrpc.Error) {
Expand Down Expand Up @@ -289,7 +260,7 @@ func (h *Handler) blockWithReceipts(id BlockID, rpcVersion version) (*BlockWithR

return &BlockWithReceipts{
Status: blockStatus,
BlockHeader: adaptBlockHeader(block.Header, rpcVersion),
BlockHeader: adaptBlockHeader(block.Header),
Transactions: txsWithReceipts,
}, nil
}
Expand All @@ -298,15 +269,16 @@ func (h *Handler) blockWithReceipts(id BlockID, rpcVersion version) (*BlockWithR
//
// It follows the specification defined here:
// https://github.com/starkware-libs/starknet-specs/blob/a789ccc3432c57777beceaa53a34a7ae2f25fda0/api/starknet_api_openrpc.json#L44
func (h *Handler) BlockWithTxs(id BlockID) (*BlockWithTxs, *jsonrpc.Error) {
return h.blockWithTxs(id, V0_8)
}

func (h *Handler) BlockWithTxsV0_7(id BlockID) (*BlockWithTxs, *jsonrpc.Error) {
return h.blockWithTxs(id, V0_7)
blockWithTxs, rpcErr := h.BlockWithTxs(id)
if rpcErr != nil {
return nil, rpcErr
}
blockWithTxs.L2GasPrice = nil
return blockWithTxs, nil
}

func (h *Handler) blockWithTxs(id BlockID, rpcVersion version) (*BlockWithTxs, *jsonrpc.Error) {
func (h *Handler) BlockWithTxs(id BlockID) (*BlockWithTxs, *jsonrpc.Error) {
block, rpcErr := h.blockByID(&id)
if rpcErr != nil {
return nil, rpcErr
Expand All @@ -324,7 +296,7 @@ func (h *Handler) blockWithTxs(id BlockID, rpcVersion version) (*BlockWithTxs, *

return &BlockWithTxs{
Status: status,
BlockHeader: adaptBlockHeader(block.Header, rpcVersion),
BlockHeader: adaptBlockHeader(block.Header),
Transactions: txs,
}, nil
}
Expand All @@ -345,7 +317,7 @@ func (h *Handler) blockStatus(id BlockID, block *core.Block) (BlockStatus, *json
return status, nil
}

func adaptBlockHeader(header *core.Header, rpcVersion version) BlockHeader {
func adaptBlockHeader(header *core.Header) BlockHeader {
var blockNumber *uint64
// if header.Hash == nil it's a pending block
if header.Hash != nil {
Expand Down Expand Up @@ -406,7 +378,6 @@ func adaptBlockHeader(header *core.Header, rpcVersion version) BlockHeader {
L1DataGasPrice: &l1DataGasPrice,
L1DAMode: &l1DAMode,
StarknetVersion: header.ProtocolVersion,
rpcVersion: rpcVersion,
}
}

Expand Down
8 changes: 4 additions & 4 deletions rpc/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ func (h *Handler) sendHistoricalHeaders(
case <-ctx.Done():
return ctx.Err()
default:
if err := h.sendHeader(w, curHeader, id, rpcVersion); err != nil {
if err := h.sendHeader(w, curHeader, id); err != nil {
return err
}

Expand All @@ -596,7 +596,7 @@ func (h *Handler) processNewHeaders(ctx context.Context, headerSub *feed.Subscri
case <-ctx.Done():
return
case header := <-headerSub.Recv():
if err := h.sendHeader(w, header, id, rpcVersion); err != nil {
if err := h.sendHeader(w, header, id); err != nil {
h.log.Warnw("Error sending header", "err", err)
return
}
Expand All @@ -605,13 +605,13 @@ func (h *Handler) processNewHeaders(ctx context.Context, headerSub *feed.Subscri
}

// sendHeader creates a request and sends it to the client
func (h *Handler) sendHeader(w jsonrpc.Conn, header *core.Header, id uint64, rpcVersion version) error {
func (h *Handler) sendHeader(w jsonrpc.Conn, header *core.Header, id uint64) error {
resp, err := json.Marshal(SubscriptionResponse{
Version: "2.0",
Method: "starknet_subscriptionNewHeads",
Params: map[string]any{
"subscription_id": id,
"result": adaptBlockHeader(header, rpcVersion),
"result": adaptBlockHeader(header),
},
})
if err != nil {
Expand Down

0 comments on commit 21e7587

Please sign in to comment.