sc_subspace_block_relay/consensus/
types.rs1use crate::protocol::compact_block::{
13 CompactBlockHandshake, CompactBlockInitialRequest, CompactBlockInitialResponse,
14};
15use crate::types::RelayError;
16use crate::utils::{RelayCounter, RelayCounterVec};
17use derive_more::From;
18use parity_scale_codec::{Decode, Encode};
19use sc_network_common::sync::message::{BlockAttributes, BlockData, BlockRequest};
20use sp_runtime::generic::BlockId;
21use sp_runtime::traits::{Block as BlockT, NumberFor};
22use sp_runtime::Justifications;
23use subspace_runtime_primitives::{BlockHashFor, ExtrinsicFor, HeaderFor};
24use substrate_prometheus_endpoint::{PrometheusError, Registry};
25
26const STATUS_LABEL: &str = "status";
27const STATUS_SUCCESS: &str = "success";
28
29const DOWNLOAD_LABEL: &str = "client_download";
30const DOWNLOAD_BLOCKS: &str = "blocks";
31const DOWNLOAD_BYTES: &str = "bytes";
32
33#[derive(From, Encode, Decode)]
35pub(crate) enum ConsensusRequest<Block: BlockT, TxHash> {
36 #[codec(index = 0)]
40 BlockDownloadV0(InitialRequest<Block>),
41
42 #[codec(index = 1)]
46 ProtocolMessageV0(ProtocolMessage<Block, TxHash>),
47
48 #[codec(index = 2)]
53 FullBlockDownloadV0(FullDownloadRequest<Block>),
54 }
57
58#[derive(Encode, Decode)]
60pub(crate) struct InitialRequest<Block: BlockT> {
61 pub(crate) from_block: BlockId<Block>,
63
64 pub(crate) block_attributes: BlockAttributes,
66
67 pub(crate) protocol_request: ProtocolInitialRequest,
69}
70
71#[derive(Encode, Decode)]
73pub(crate) struct InitialResponse<Block: BlockT, TxHash> {
74 pub(crate) block_hash: BlockHashFor<Block>,
76
77 pub(crate) partial_block: PartialBlock<Block>,
79
80 pub(crate) protocol_response: Option<ProtocolInitialResponse<Block, TxHash>>,
85}
86
87#[derive(From, Encode, Decode)]
89pub(crate) enum ProtocolInitialRequest {
90 #[codec(index = 0)]
91 CompactBlock(CompactBlockInitialRequest),
92 }
95
96#[derive(From, Encode, Decode)]
98pub(crate) enum ProtocolInitialResponse<Block: BlockT, TxHash> {
99 #[codec(index = 0)]
100 CompactBlock(CompactBlockInitialResponse<BlockHashFor<Block>, TxHash, ExtrinsicFor<Block>>),
101 }
104
105#[derive(From, Encode, Decode)]
107pub(crate) enum ProtocolMessage<Block: BlockT, TxHash> {
108 #[codec(index = 0)]
109 CompactBlock(CompactBlockHandshake<BlockHashFor<Block>, TxHash>),
110 }
113
114impl<Block: BlockT, TxHash> From<CompactBlockHandshake<BlockHashFor<Block>, TxHash>>
115 for ConsensusRequest<Block, TxHash>
116{
117 fn from(
118 inner: CompactBlockHandshake<BlockHashFor<Block>, TxHash>,
119 ) -> ConsensusRequest<Block, TxHash> {
120 ConsensusRequest::ProtocolMessageV0(ProtocolMessage::CompactBlock(inner))
121 }
122}
123
124#[derive(Encode, Decode)]
131pub(crate) struct FullDownloadRequest<Block: BlockT>(pub(crate) BlockRequest<Block>);
132
133#[derive(Encode, Decode)]
135pub(crate) struct FullDownloadResponse<Block: BlockT>(pub(crate) Vec<BlockData<Block>>);
136
137#[derive(Encode, Decode)]
141pub(crate) struct PartialBlock<Block: BlockT> {
142 pub(crate) parent_hash: BlockHashFor<Block>,
143 pub(crate) block_number: NumberFor<Block>,
144 pub(crate) block_header_hash: BlockHashFor<Block>,
145 pub(crate) header: Option<HeaderFor<Block>>,
146 pub(crate) indexed_body: Option<Vec<Vec<u8>>>,
147 pub(crate) justifications: Option<Justifications>,
148}
149
150impl<Block: BlockT> PartialBlock<Block> {
151 pub(crate) fn block_data(self, body: Option<Vec<ExtrinsicFor<Block>>>) -> BlockData<Block> {
153 BlockData::<Block> {
154 hash: self.block_header_hash,
155 header: self.header,
156 body,
157 indexed_body: self.indexed_body,
158 receipt: None,
159 message_queue: None,
160 justification: None,
161 justifications: self.justifications,
162 }
163 }
164}
165
166pub(crate) struct ConsensusClientMetrics {
168 pub(crate) requests: RelayCounterVec,
169 pub(crate) downloads: RelayCounterVec,
170 pub(crate) tx_pool_miss: RelayCounter,
171}
172
173impl ConsensusClientMetrics {
174 pub(crate) fn new(registry: Option<&Registry>) -> Result<Self, PrometheusError> {
175 Ok(Self {
176 requests: RelayCounterVec::new(
177 "relay_client_requests",
178 "Relay client request metrics(by completion status)",
179 &[STATUS_LABEL],
180 registry,
181 )?,
182 downloads: RelayCounterVec::new(
183 "relay_client_downloads",
184 "Relay client download metrics",
185 &[DOWNLOAD_LABEL],
186 registry,
187 )?,
188 tx_pool_miss: RelayCounter::new(
189 "relay_client_tx_pool_miss",
190 "Number of extrinsics not found in the tx pool",
191 registry,
192 )?,
193 })
194 }
195
196 pub(crate) fn on_download<Block: BlockT>(&self, blocks: &[BlockData<Block>]) {
198 self.requests.inc(STATUS_LABEL, STATUS_SUCCESS);
199 if let Ok(blocks) = u64::try_from(blocks.len()) {
200 self.downloads
201 .inc_by(DOWNLOAD_LABEL, DOWNLOAD_BLOCKS, blocks);
202 }
203 if let Ok(bytes) = u64::try_from(blocks.encoded_size()) {
204 self.downloads.inc_by(DOWNLOAD_LABEL, DOWNLOAD_BYTES, bytes);
205 }
206 }
207
208 pub(crate) fn on_download_fail(&self, err: &RelayError) {
210 self.requests.inc(STATUS_LABEL, err.as_ref());
211 }
212}
213
214pub(crate) struct ConsensusServerMetrics {
216 pub(crate) requests: RelayCounterVec,
217}
218
219impl ConsensusServerMetrics {
220 pub(crate) fn new(registry: Option<&Registry>) -> Result<Self, PrometheusError> {
221 Ok(Self {
222 requests: RelayCounterVec::new(
223 "relay_server_status",
224 "Relay server request metrics(by status)",
225 &[STATUS_LABEL],
226 registry,
227 )?,
228 })
229 }
230
231 pub(crate) fn on_request(&self) {
233 self.requests.inc(STATUS_LABEL, STATUS_SUCCESS);
234 }
235
236 pub(crate) fn on_failed_request(&self, err: &RelayError) {
238 self.requests.inc(STATUS_LABEL, err.as_ref());
239 }
240}