subspace_networking/protocols/request_response/handlers/
piece_by_index.rs

1//! Helper for incoming piece requests.
2//!
3//! Request handler can be created with [`PieceByIndexRequestHandler`].
4
5use crate::protocols::request_response::handlers::generic_request_handler::{
6    GenericRequest, GenericRequestHandler,
7};
8use parity_scale_codec::{Decode, Encode};
9use std::sync::Arc;
10use subspace_core_primitives::pieces::{Piece, PieceIndex};
11
12/// Piece-by-index request
13#[derive(Debug, Clone, Eq, PartialEq, Encode, Decode)]
14pub struct PieceByIndexRequest {
15    /// Request key - piece index
16    pub piece_index: PieceIndex,
17    /// Additional pieces that requester is interested in if they are cached locally
18    // TODO: Use `Arc<[PieceIndex]>` once
19    //  https://github.com/paritytech/parity-scale-codec/issues/633 is resolved
20    pub cached_pieces: Arc<Vec<PieceIndex>>,
21}
22
23impl GenericRequest for PieceByIndexRequest {
24    const PROTOCOL_NAME: &'static str = "/subspace/piece-by-index/0.1.0";
25    const LOG_TARGET: &'static str = "piece-by-index-request-response-handler";
26    type Response = PieceByIndexResponse;
27}
28
29impl PieceByIndexRequest {
30    /// Max number of cached pieces to accept per request, equals to the number of source shards in
31    /// a sector and fits nicely into a single TCP packet
32    pub const RECOMMENDED_LIMIT: usize = 128;
33}
34
35/// Piece-by-index response, may be cached piece or stored in one of the farms
36#[derive(Debug, PartialEq, Eq, Clone, Encode, Decode)]
37pub struct PieceByIndexResponse {
38    /// Piece, if found
39    pub piece: Option<Piece>,
40    /// Additional pieces that requester is interested in and are cached locally, order from request
41    /// is not preserved
42    pub cached_pieces: Vec<PieceIndex>,
43}
44
45/// Piece-by-index request handler
46pub type PieceByIndexRequestHandler = GenericRequestHandler<PieceByIndexRequest>;