1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
//! Consensus block relay implementation.

use crate::consensus::types::{
    BlockHash, ConsensusClientMetrics, ConsensusRequest, ConsensusServerMetrics, Extrinsic,
    FullDownloadRequest, FullDownloadResponse, InitialRequest, InitialResponse, PartialBlock,
    ProtocolInitialRequest, ProtocolInitialResponse, ProtocolMessage,
};
use crate::protocol::compact_block::{
    CompactBlockClient, CompactBlockHandshake, CompactBlockServer,
};
use crate::protocol::{ClientBackend, ProtocolUnitInfo, ServerBackend};
use crate::types::{RelayError, RequestResponseErr};
use crate::utils::{NetworkPeerHandle, NetworkWrapper};
use crate::LOG_TARGET;
use async_trait::async_trait;
use codec::{Compact, CompactLen, Decode, Encode};
use futures::channel::oneshot;
use futures::stream::StreamExt;
use sc_client_api::{BlockBackend, HeaderBackend};
use sc_network::request_responses::{IncomingRequest, OutgoingResponse, ProtocolConfig};
use sc_network::types::ProtocolName;
use sc_network::{NetworkWorker, OutboundFailure, PeerId, RequestFailure};
use sc_network_common::sync::message::{
    BlockAttributes, BlockData, BlockRequest, Direction, FromBlock,
};
use sc_network_sync::block_relay_protocol::{
    BlockDownloader, BlockRelayParams, BlockResponseError, BlockServer,
};
use sc_transaction_pool_api::{InPoolTransaction, TransactionPool, TxHash};
use sp_api::ProvideRuntimeApi;
use sp_consensus_subspace::{FarmerPublicKey, SubspaceApi};
use sp_runtime::generic::BlockId;
use sp_runtime::traits::{Block as BlockT, Header, One, Zero};
use std::num::{NonZeroU32, NonZeroUsize};
use std::sync::Arc;
use std::time::{Duration, Instant};
use substrate_prometheus_endpoint::{PrometheusError, Registry};
use tracing::{debug, info, trace, warn};

const SYNC_PROTOCOL: &str = "/subspace/consensus-block-relay/1";

// TODO: size these properly, or move to config
const NUM_PEER_HINT: NonZeroUsize = NonZeroUsize::new(100).expect("Not zero; qed");

/// These are the same limits used by substrate block handler.
/// Maximum response size (bytes).
const MAX_RESPONSE_SIZE: NonZeroUsize = NonZeroUsize::new(8 * 1024 * 1024).expect("Not zero; qed");
/// Maximum blocks in the response.
const MAX_RESPONSE_BLOCKS: NonZeroU32 = NonZeroU32::new(128).expect("Not zero; qed");

/// If the encoded size of the extrinsic is less than the threshold,
/// return the full extrinsic along with the tx hash.
const TX_SIZE_THRESHOLD: NonZeroUsize = NonZeroUsize::new(32).expect("Not zero; qed");

/// The client side of the consensus block relay
struct ConsensusRelayClient<Block, Pool>
where
    Block: BlockT,
    Pool: TransactionPool,
{
    network: Arc<NetworkWrapper>,
    protocol_name: ProtocolName,
    compact_block: CompactBlockClient<BlockHash<Block>, TxHash<Pool>, Extrinsic<Block>>,
    backend: Arc<ConsensusClientBackend<Pool>>,
    metrics: ConsensusClientMetrics,
    _phantom_data: std::marker::PhantomData<(Block, Pool)>,
}

impl<Block, Pool> ConsensusRelayClient<Block, Pool>
where
    Block: BlockT,
    Pool: TransactionPool<Block = Block> + 'static,
{
    /// Creates the consensus relay client.
    fn new(
        network: Arc<NetworkWrapper>,
        protocol_name: ProtocolName,
        compact_block: CompactBlockClient<BlockHash<Block>, TxHash<Pool>, Extrinsic<Block>>,
        backend: Arc<ConsensusClientBackend<Pool>>,
        metrics: ConsensusClientMetrics,
    ) -> Self {
        Self {
            network,
            protocol_name,
            compact_block,
            backend,
            metrics,
            _phantom_data: Default::default(),
        }
    }

    /// Downloads the requested block from the peer using the relay protocol.
    async fn download(
        &self,
        who: PeerId,
        request: BlockRequest<Block>,
    ) -> Result<Vec<BlockData<Block>>, RelayError> {
        let start_ts = Instant::now();
        let network_peer_handle = self
            .network
            .network_peer_handle(self.protocol_name.clone(), who)?;

        // Perform the initial request/response
        let initial_request = InitialRequest {
            from_block: match request.from {
                FromBlock::Hash(h) => BlockId::<Block>::Hash(h),
                FromBlock::Number(n) => BlockId::<Block>::Number(n),
            },
            block_attributes: request.fields,
            protocol_request: ProtocolInitialRequest::from(
                self.compact_block
                    .build_initial_request(self.backend.as_ref()),
            ),
        };
        let initial_response = network_peer_handle
            .request::<_, InitialResponse<Block, TxHash<Pool>>>(ConsensusRequest::<
                Block,
                TxHash<Pool>,
            >::from(initial_request))
            .await?;

        // Resolve the protocol response to get the extrinsics
        let (body, local_miss) = if let Some(protocol_response) = initial_response.protocol_response
        {
            let (body, local_miss) = self
                .resolve_extrinsics::<ConsensusRequest<Block, TxHash<Pool>>>(
                    protocol_response,
                    &network_peer_handle,
                )
                .await?;
            (Some(body), local_miss)
        } else {
            (None, 0)
        };

        // Assemble the final response
        let downloaded = vec![initial_response.partial_block.block_data(body)];
        debug!(
            target: LOG_TARGET,
            block_hash = ?initial_response.block_hash,
            download_bytes = %downloaded.encoded_size(),
            %local_miss,
            duration = ?start_ts.elapsed(),
            "block_download",
        );
        Ok(downloaded)
    }

    /// Downloads the requested blocks from the peer, without using the relay protocol.
    async fn full_download(
        &self,
        who: PeerId,
        request: BlockRequest<Block>,
    ) -> Result<Vec<BlockData<Block>>, RelayError> {
        let start_ts = Instant::now();
        let network_peer_handle = self
            .network
            .network_peer_handle(self.protocol_name.clone(), who)?;

        let server_request =
            ConsensusRequest::<Block, TxHash<Pool>>::from(FullDownloadRequest(request.clone()));
        let full_response = network_peer_handle
            .request::<_, FullDownloadResponse<Block>>(server_request)
            .await?;
        let downloaded = full_response.0;

        debug!(
            target: LOG_TARGET,
            ?request,
            download_blocks =  %downloaded.len(),
            download_bytes = %downloaded.encoded_size(),
            duration = ?start_ts.elapsed(),
            "full_download",
        );
        Ok(downloaded)
    }

    /// Resolves the extrinsics from the initial response
    async fn resolve_extrinsics<Request>(
        &self,
        protocol_response: ProtocolInitialResponse<Block, TxHash<Pool>>,
        network_peer_handle: &NetworkPeerHandle,
    ) -> Result<(Vec<Extrinsic<Block>>, usize), RelayError>
    where
        Request: From<CompactBlockHandshake<BlockHash<Block>, TxHash<Pool>>> + Encode + Send + Sync,
    {
        let ProtocolInitialResponse::CompactBlock(compact_response) = protocol_response;
        let (block_hash, resolved) = self
            .compact_block
            .resolve_initial_response::<Request>(
                compact_response,
                network_peer_handle,
                self.backend.as_ref(),
            )
            .await?;
        let mut local_miss = 0;
        let extrinsics = resolved
            .into_iter()
            .map(|entry| {
                let encoded = entry.protocol_unit.encode();
                if !entry.locally_resolved {
                    trace!(
                        target: LOG_TARGET,
                        ?block_hash,
                        tx_hash = ?entry.protocol_unit_id,
                        tx_size = %encoded.len(),
                        "resolve_extrinsics: local miss"
                    );
                    self.metrics.tx_pool_miss.inc();
                    local_miss += encoded.len();
                }
                entry.protocol_unit
            })
            .collect();
        Ok((extrinsics, local_miss))
    }
}

#[async_trait]
impl<Block, Pool> BlockDownloader<Block> for ConsensusRelayClient<Block, Pool>
where
    Block: BlockT,
    Pool: TransactionPool<Block = Block> + 'static,
{
    async fn download_blocks(
        &self,
        who: PeerId,
        request: BlockRequest<Block>,
    ) -> Result<Result<(Vec<u8>, ProtocolName), RequestFailure>, oneshot::Canceled> {
        let full_download = request.max.map_or(false, |max_blocks| max_blocks > 1);
        let ret = if full_download {
            self.full_download(who, request.clone()).await
        } else {
            self.download(who, request.clone()).await
        };
        match ret {
            Ok(blocks) => {
                self.metrics.on_download::<Block>(&blocks);
                Ok(Ok((blocks.encode(), self.protocol_name.clone())))
            }
            Err(error) => {
                debug!(
                    target: LOG_TARGET,
                    peer=?who,
                    ?request,
                    ?error,
                    "download_block failed"
                );
                self.metrics.on_download_fail(&error);
                match error {
                    RelayError::RequestResponse(error) => match error {
                        RequestResponseErr::DecodeFailed { .. } => {
                            Ok(Err(RequestFailure::Network(OutboundFailure::Timeout)))
                        }
                        RequestResponseErr::RequestFailure(err) => Ok(Err(err)),
                        RequestResponseErr::NetworkUninitialized => {
                            // TODO: This is the best error found that kind of matches
                            Ok(Err(RequestFailure::NotConnected))
                        }
                        RequestResponseErr::Canceled => Err(oneshot::Canceled),
                    },
                    _ => {
                        // Why timeout???
                        Ok(Err(RequestFailure::Network(OutboundFailure::Timeout)))
                    }
                }
            }
        }
    }

    fn block_response_into_blocks(
        &self,
        _request: &BlockRequest<Block>,
        response: Vec<u8>,
    ) -> Result<Vec<BlockData<Block>>, BlockResponseError> {
        Decode::decode(&mut response.as_ref()).map_err(|err| {
            BlockResponseError::DecodeFailed(format!(
                "Failed to decode consensus response: {err:?}"
            ))
        })
    }
}

/// The server side of the consensus block relay
struct ConsensusRelayServer<Block: BlockT, Client, Pool: TransactionPool> {
    client: Arc<Client>,
    compact_block: CompactBlockServer<BlockHash<Block>, TxHash<Pool>, Extrinsic<Block>>,
    request_receiver: async_channel::Receiver<IncomingRequest>,
    backend: Arc<ConsensusServerBackend<Client, Pool>>,
    metrics: ConsensusServerMetrics,
    _block: std::marker::PhantomData<Block>,
}

impl<Block, Client, Pool> ConsensusRelayServer<Block, Client, Pool>
where
    Block: BlockT,
    Client: HeaderBackend<Block> + BlockBackend<Block> + ProvideRuntimeApi<Block>,
    Client::Api: SubspaceApi<Block, FarmerPublicKey>,
    Pool: TransactionPool<Block = Block> + 'static,
{
    /// Creates the consensus relay server.
    fn new(
        client: Arc<Client>,
        compact_block: CompactBlockServer<BlockHash<Block>, TxHash<Pool>, Extrinsic<Block>>,
        request_receiver: async_channel::Receiver<IncomingRequest>,
        backend: Arc<ConsensusServerBackend<Client, Pool>>,
        metrics: ConsensusServerMetrics,
    ) -> Self {
        Self {
            client,
            compact_block,
            request_receiver,
            backend,
            metrics,
            _block: Default::default(),
        }
    }

    /// Handles the received request from the client side
    async fn process_incoming_request(&mut self, request: IncomingRequest) {
        // Drop the request in case of errors and let the client time out.
        // This is the behavior of the current substrate block handler.
        let IncomingRequest {
            peer,
            payload,
            pending_response,
        } = request;
        let req: ConsensusRequest<Block, TxHash<Pool>> = match Decode::decode(&mut payload.as_ref())
        {
            Ok(msg) => msg,
            Err(err) => {
                warn!(
                    target: LOG_TARGET,
                    ?peer,
                    ?err,
                    "Decode failed"
                );
                return;
            }
        };

        let ret = match req {
            ConsensusRequest::BlockDownloadV0(req) => {
                self.on_initial_request(req).map(|rsp| rsp.encode())
            }
            ConsensusRequest::ProtocolMessageV0(msg) => self.on_protocol_message(msg),
            ConsensusRequest::FullBlockDownloadV0(req) => {
                self.on_full_download_request(req).map(|rsp| rsp.encode())
            }
        };

        match ret {
            Ok(response) => {
                self.metrics.on_request();
                self.send_response(peer, response, pending_response);
                trace!(
                    target: LOG_TARGET,
                    ?peer,
                    "server: request processed from"
                );
            }
            Err(error) => {
                self.metrics.on_failed_request(&error);
                debug!(
                    target: LOG_TARGET,
                    ?peer,
                    ?error,
                    "Server error"
                );
            }
        }
    }

    /// Handles the initial request from the client
    fn on_initial_request(
        &mut self,
        initial_request: InitialRequest<Block>,
    ) -> Result<InitialResponse<Block, TxHash<Pool>>, RelayError> {
        let block_hash = self.block_hash(&initial_request.from_block)?;
        let block_attributes = initial_request.block_attributes;

        // Build the generic and the protocol specific parts of the response
        let partial_block = self.get_partial_block(&block_hash, block_attributes)?;
        let ProtocolInitialRequest::CompactBlock(compact_request) =
            initial_request.protocol_request;
        let protocol_response = if block_attributes.contains(BlockAttributes::BODY) {
            let compact_response = self.compact_block.build_initial_response(
                &block_hash,
                compact_request,
                self.backend.as_ref(),
            )?;
            Some(ProtocolInitialResponse::from(compact_response))
        } else {
            None
        };

        Ok(InitialResponse {
            block_hash,
            partial_block,
            protocol_response,
        })
    }

    /// Handles the protocol message from the client
    fn on_protocol_message(
        &mut self,
        msg: ProtocolMessage<Block, TxHash<Pool>>,
    ) -> Result<Vec<u8>, RelayError> {
        let response = match msg {
            ProtocolMessage::CompactBlock(msg) => self
                .compact_block
                .on_protocol_message(msg, self.backend.as_ref())?
                .encode(),
        };
        Ok(response)
    }

    /// Handles the full download request from the client
    fn on_full_download_request(
        &mut self,
        full_download_request: FullDownloadRequest<Block>,
    ) -> Result<FullDownloadResponse<Block>, RelayError> {
        let block_request = full_download_request.0;
        let mut blocks = Vec::new();
        let mut block_id = match block_request.from {
            FromBlock::Hash(h) => BlockId::<Block>::Hash(h),
            FromBlock::Number(n) => BlockId::<Block>::Number(n),
        };

        // This is a compact length encoding of max number of blocks to be sent in response
        let mut total_size = Compact::compact_len(&MAX_RESPONSE_BLOCKS.get());
        let max_blocks = block_request.max.map_or(MAX_RESPONSE_BLOCKS.into(), |val| {
            std::cmp::min(val, MAX_RESPONSE_BLOCKS.into())
        });
        while blocks.len() < max_blocks as usize {
            let block_hash = self.block_hash(&block_id)?;
            let partial_block = self.get_partial_block(&block_hash, block_request.fields)?;
            let body = if block_request.fields.contains(BlockAttributes::BODY) {
                Some(block_transactions(&block_hash, self.client.as_ref())?)
            } else {
                None
            };
            let block_number = partial_block.block_number;
            let parent_hash = partial_block.parent_hash;
            let block_data = partial_block.block_data(body);

            // Enforce the max limit on response size.
            let bytes = block_data.encoded_size();
            if !blocks.is_empty() && (total_size + bytes) > MAX_RESPONSE_SIZE.into() {
                break;
            }
            total_size += bytes;
            blocks.push(block_data);

            block_id = match block_request.direction {
                Direction::Ascending => BlockId::Number(block_number + One::one()),
                Direction::Descending => {
                    if block_number.is_zero() {
                        break;
                    }
                    BlockId::Hash(parent_hash)
                }
            };
        }

        Ok(FullDownloadResponse(blocks))
    }

    /// Builds the partial block response
    fn get_partial_block(
        &self,
        block_hash: &BlockHash<Block>,
        block_attributes: BlockAttributes,
    ) -> Result<PartialBlock<Block>, RelayError> {
        let block_header = match self.client.header(*block_hash) {
            Ok(Some(header)) => header,
            Ok(None) => {
                return Err(RelayError::BlockHeader(format!(
                    "Missing header: {block_hash:?}"
                )))
            }
            Err(err) => return Err(RelayError::BlockHeader(format!("{block_hash:?}, {err:?}"))),
        };
        let parent_hash = *block_header.parent_hash();
        let block_number = *block_header.number();
        let block_header_hash = block_header.hash();

        let header = if block_attributes.contains(BlockAttributes::HEADER) {
            Some(block_header)
        } else {
            None
        };

        let indexed_body = if block_attributes.contains(BlockAttributes::INDEXED_BODY) {
            self.client
                .block_indexed_body(*block_hash)
                .map_err(|err| RelayError::BlockIndexedBody(format!("{block_hash:?}, {err:?}")))?
        } else {
            None
        };

        let justifications = if block_attributes.contains(BlockAttributes::JUSTIFICATION) {
            self.client.justifications(*block_hash).map_err(|err| {
                RelayError::BlockJustifications(format!("{block_hash:?}, {err:?}"))
            })?
        } else {
            None
        };

        Ok(PartialBlock {
            parent_hash,
            block_number,
            block_header_hash,
            header,
            indexed_body,
            justifications,
        })
    }

    /// Converts the BlockId to block hash
    fn block_hash(&self, block_id: &BlockId<Block>) -> Result<BlockHash<Block>, RelayError> {
        match self.client.block_hash_from_id(block_id) {
            Ok(Some(hash)) => Ok(hash),
            Ok(None) => Err(RelayError::BlockHash(format!("Missing: {block_id:?}"))),
            Err(err) => Err(RelayError::BlockHash(format!("{block_id:?}, {err:?}"))),
        }
    }

    /// Builds/sends the response back to the client
    fn send_response(
        &self,
        peer: PeerId,
        response: Vec<u8>,
        sender: oneshot::Sender<OutgoingResponse>,
    ) {
        let response = OutgoingResponse {
            result: Ok(response),
            reputation_changes: Vec::new(),
            sent_feedback: None,
        };
        if sender.send(response).is_err() {
            warn!(
                target: LOG_TARGET,
                ?peer,
                "Failed to send response"
            );
        }
    }
}

#[async_trait]
impl<Block, Client, Pool> BlockServer<Block> for ConsensusRelayServer<Block, Client, Pool>
where
    Block: BlockT,
    Client: HeaderBackend<Block> + BlockBackend<Block> + ProvideRuntimeApi<Block>,
    Client::Api: SubspaceApi<Block, FarmerPublicKey>,
    Pool: TransactionPool<Block = Block> + 'static,
{
    async fn run(&mut self) {
        info!(
            target: LOG_TARGET,
            "relay::consensus block server: starting"
        );
        while let Some(request) = self.request_receiver.next().await {
            self.process_incoming_request(request).await;
        }
    }
}

/// The client backend.
struct ConsensusClientBackend<Pool> {
    transaction_pool: Arc<Pool>,
}

impl<Block, Pool> ClientBackend<TxHash<Pool>, Extrinsic<Block>> for ConsensusClientBackend<Pool>
where
    Block: BlockT,
    Pool: TransactionPool<Block = Block> + 'static,
{
    fn protocol_unit(&self, tx_hash: &TxHash<Pool>) -> Option<Extrinsic<Block>> {
        // Look up the transaction pool.
        self.transaction_pool
            .ready_transaction(tx_hash)
            .map(|in_pool_tx| in_pool_tx.data().clone())
    }
}

/// The server backend.
struct ConsensusServerBackend<Client, Pool> {
    client: Arc<Client>,
    transaction_pool: Arc<Pool>,
}

impl<Block, Client, Pool> ServerBackend<BlockHash<Block>, TxHash<Pool>, Extrinsic<Block>>
    for ConsensusServerBackend<Client, Pool>
where
    Block: BlockT,
    Client: HeaderBackend<Block> + BlockBackend<Block> + ProvideRuntimeApi<Block>,
    Client::Api: SubspaceApi<Block, FarmerPublicKey>,
    Pool: TransactionPool<Block = Block> + 'static,
{
    fn download_unit_members(
        &self,
        block_hash: &BlockHash<Block>,
    ) -> Result<Vec<ProtocolUnitInfo<TxHash<Pool>, Extrinsic<Block>>>, RelayError> {
        let txns = block_transactions(block_hash, self.client.as_ref())?;
        Ok(txns
            .into_iter()
            .map(|extrinsic| {
                let send_tx = extrinsic.encoded_size() <= TX_SIZE_THRESHOLD.get()
                    || self
                        .client
                        .runtime_api()
                        .is_inherent(*block_hash, &extrinsic)
                        .unwrap_or(false);
                ProtocolUnitInfo {
                    id: self.transaction_pool.hash_of(&extrinsic),
                    unit: if send_tx { Some(extrinsic) } else { None },
                }
            })
            .collect())
    }

    fn protocol_unit(
        &self,
        block_hash: &BlockHash<Block>,
        tx_hash: &TxHash<Pool>,
    ) -> Option<Extrinsic<Block>> {
        // Look up the block extrinsics.
        match block_transactions(block_hash, self.client.as_ref()) {
            Ok(extrinsics) => {
                for extrinsic in extrinsics {
                    if self.transaction_pool.hash_of(&extrinsic) == *tx_hash {
                        return Some(extrinsic);
                    }
                }
            }
            Err(err) => {
                debug!(
                    target: LOG_TARGET,
                    ?block_hash,
                    ?tx_hash,
                    ?err,
                    "consensus server protocol_unit: "
                );
            }
        }

        // Next look up the transaction pool.
        self.transaction_pool
            .ready_transaction(tx_hash)
            .map(|in_pool_tx| in_pool_tx.data().clone())
    }
}

/// Retrieves the block transactions/tx hash from the backend.
fn block_transactions<Block, Client>(
    block_hash: &BlockHash<Block>,
    client: &Client,
) -> Result<Vec<Extrinsic<Block>>, RelayError>
where
    Block: BlockT,
    Client: HeaderBackend<Block> + BlockBackend<Block>,
{
    let maybe_extrinsics = client
        .block_body(*block_hash)
        .map_err(|err| RelayError::BlockBody(format!("{block_hash:?}, {err:?}")))?;
    maybe_extrinsics.ok_or(RelayError::BlockExtrinsicsNotFound(format!(
        "{block_hash:?}"
    )))
}

#[derive(Debug, thiserror::Error)]
pub enum BlockRelayConfigurationError {
    #[error(transparent)]
    PrometheusError(#[from] PrometheusError),
}

/// Sets up the relay components.
pub fn build_consensus_relay<Block, Client, Pool>(
    network: Arc<NetworkWrapper>,
    client: Arc<Client>,
    pool: Arc<Pool>,
    registry: Option<&Registry>,
) -> Result<
    BlockRelayParams<Block, NetworkWorker<Block, <Block as BlockT>::Hash>>,
    BlockRelayConfigurationError,
>
where
    Block: BlockT,
    Client: HeaderBackend<Block> + BlockBackend<Block> + ProvideRuntimeApi<Block> + 'static,
    Client::Api: SubspaceApi<Block, FarmerPublicKey>,
    Pool: TransactionPool<Block = Block> + 'static,
{
    let (tx, request_receiver) = async_channel::bounded(NUM_PEER_HINT.get());

    let backend = Arc::new(ConsensusClientBackend {
        transaction_pool: pool.clone(),
    });
    let metrics = ConsensusClientMetrics::new(registry)
        .map_err(BlockRelayConfigurationError::PrometheusError)?;
    let relay_client: ConsensusRelayClient<Block, Pool> = ConsensusRelayClient::new(
        network,
        SYNC_PROTOCOL.into(),
        CompactBlockClient::new(),
        backend,
        metrics,
    );

    let backend = Arc::new(ConsensusServerBackend {
        client: client.clone(),
        transaction_pool: pool.clone(),
    });
    let metrics = ConsensusServerMetrics::new(registry)
        .map_err(BlockRelayConfigurationError::PrometheusError)?;
    let relay_server = ConsensusRelayServer::new(
        client,
        CompactBlockServer::new(),
        request_receiver,
        backend,
        metrics,
    );

    let mut protocol_config = ProtocolConfig {
        name: SYNC_PROTOCOL.into(),
        fallback_names: Vec::new(),
        max_request_size: 1024 * 1024,
        max_response_size: 16 * 1024 * 1024,
        request_timeout: Duration::from_secs(20),
        inbound_queue: None,
    };
    protocol_config.inbound_queue = Some(tx);

    Ok(BlockRelayParams {
        server: Box::new(relay_server),
        downloader: Arc::new(relay_client),
        request_response_config: protocol_config,
    })
}