diff --git a/rpc/block.go b/rpc/block.go index fcbebab10d..7a6c606e01 100644 --- a/rpc/block.go +++ b/rpc/block.go @@ -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 @@ -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 @@ -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 } @@ -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) { @@ -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 } @@ -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 @@ -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 } @@ -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 { @@ -406,7 +378,6 @@ func adaptBlockHeader(header *core.Header, rpcVersion version) BlockHeader { L1DataGasPrice: &l1DataGasPrice, L1DAMode: &l1DAMode, StarknetVersion: header.ProtocolVersion, - rpcVersion: rpcVersion, } } diff --git a/rpc/subscriptions.go b/rpc/subscriptions.go index dda9b1ce83..39f79cdf2d 100644 --- a/rpc/subscriptions.go +++ b/rpc/subscriptions.go @@ -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 } @@ -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 } @@ -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 {