sc_network_sync/
block_relay_protocol.rs

1// Copyright Parity Technologies (UK) Ltd.
2// This file is part of Substrate.
3
4// Substrate is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Substrate is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Substrate.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Block relay protocol related definitions.
18
19use futures::channel::oneshot;
20use sc_network::{request_responses::RequestFailure, NetworkBackend, ProtocolName};
21use sc_network_common::sync::message::{BlockData, BlockRequest};
22use sc_network_types::PeerId;
23use sp_runtime::traits::Block as BlockT;
24use std::sync::Arc;
25
26/// The serving side of the block relay protocol. It runs a single instance
27/// of the server task that processes the incoming protocol messages.
28#[async_trait::async_trait]
29pub trait BlockServer<Block: BlockT>: Send {
30	/// Starts the protocol processing.
31	async fn run(&mut self);
32}
33
34/// The client side stub to download blocks from peers. This is a handle
35/// that can be used to initiate concurrent downloads.
36#[async_trait::async_trait]
37pub trait BlockDownloader<Block: BlockT>: Send + Sync {
38	/// Performs the protocol specific sequence to fetch the blocks from the peer.
39	/// Output: if the download succeeds, the response is a `Vec<u8>` which is
40	/// in a format specific to the protocol implementation. The block data
41	/// can be extracted from this response using [`BlockDownloader::block_response_into_blocks`].
42	async fn download_blocks(
43		&self,
44		who: PeerId,
45		request: BlockRequest<Block>,
46	) -> Result<Result<(Vec<u8>, ProtocolName), RequestFailure>, oneshot::Canceled>;
47
48	/// Parses the protocol specific response to retrieve the block data.
49	fn block_response_into_blocks(
50		&self,
51		request: &BlockRequest<Block>,
52		response: Vec<u8>,
53	) -> Result<Vec<BlockData<Block>>, BlockResponseError>;
54}
55
56/// Errors returned by [`BlockDownloader::block_response_into_blocks`].
57#[derive(Debug)]
58pub enum BlockResponseError {
59	/// Failed to decode the response bytes.
60	DecodeFailed(String),
61
62	/// Failed to extract the blocks from the decoded bytes.
63	ExtractionFailed(String),
64}
65
66/// Block relay specific params for network creation, specified in
67/// ['sc_service::BuildNetworkParams'].
68pub struct BlockRelayParams<Block: BlockT, N: NetworkBackend<Block, <Block as BlockT>::Hash>> {
69	pub server: Box<dyn BlockServer<Block>>,
70	pub downloader: Arc<dyn BlockDownloader<Block>>,
71	pub request_response_config: N::RequestResponseProtocolConfig,
72}