referrerpolicy=no-referrer-when-downgrade

sc_consensus_beefy/communication/request_response/
mod.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Request/response protocol for syncing BEEFY justifications.
20
21mod incoming_requests_handler;
22pub(crate) mod outgoing_requests_engine;
23
24pub use incoming_requests_handler::BeefyJustifsRequestHandler;
25
26use std::time::Duration;
27
28use codec::{Decode, Encode, Error as CodecError};
29use sc_network::NetworkBackend;
30use sc_network_types::PeerId;
31use sp_runtime::traits::{Block, NumberFor};
32
33use crate::communication::{beefy_protocol_name::justifications_protocol_name, peers::PeerReport};
34use incoming_requests_handler::IncomingRequestReceiver;
35
36// 10 seems reasonable, considering justifs are explicitly requested only
37// for mandatory blocks, by nodes that are syncing/catching-up.
38const JUSTIF_CHANNEL_SIZE: usize = 10;
39
40const MAX_RESPONSE_SIZE: u64 = 1024 * 1024;
41const JUSTIF_REQUEST_TIMEOUT: Duration = Duration::from_secs(3);
42
43const BEEFY_SYNC_LOG_TARGET: &str = "beefy::sync";
44
45/// Get the configuration for the BEEFY justifications Request/response protocol.
46///
47/// Returns a receiver for messages received on this protocol and the requested
48/// `ProtocolConfig`.
49///
50/// Consider using [`BeefyJustifsRequestHandler`] instead of this low-level function.
51pub(crate) fn on_demand_justifications_protocol_config<
52	Hash: AsRef<[u8]>,
53	B: Block,
54	Network: NetworkBackend<B, <B as Block>::Hash>,
55>(
56	genesis_hash: Hash,
57	fork_id: Option<&str>,
58) -> (IncomingRequestReceiver, Network::RequestResponseProtocolConfig) {
59	let name = justifications_protocol_name(genesis_hash, fork_id);
60	let fallback_names = vec![];
61	let (tx, rx) = async_channel::bounded(JUSTIF_CHANNEL_SIZE);
62	let rx = IncomingRequestReceiver::new(rx);
63	let cfg = Network::request_response_config(
64		name,
65		fallback_names,
66		32,
67		MAX_RESPONSE_SIZE,
68		// We are connected to all validators:
69		JUSTIF_REQUEST_TIMEOUT,
70		Some(tx),
71	);
72	(rx, cfg)
73}
74
75/// BEEFY justification request.
76#[derive(Debug, Clone, Encode, Decode)]
77pub struct JustificationRequest<B: Block> {
78	/// Start collecting proofs from this block.
79	pub begin: NumberFor<B>,
80}
81
82#[derive(Debug, thiserror::Error)]
83pub enum Error {
84	#[error(transparent)]
85	Client(#[from] sp_blockchain::Error),
86
87	#[error(transparent)]
88	RuntimeApi(#[from] sp_api::ApiError),
89
90	/// Decoding failed, we were able to change the peer's reputation accordingly.
91	#[error("Decoding request failed for peer {0}.")]
92	DecodingError(PeerId, #[source] CodecError),
93
94	/// Decoding failed, but sending reputation change failed.
95	#[error("Decoding request failed for peer {0}, and changing reputation failed.")]
96	DecodingErrorNoReputationChange(PeerId, #[source] CodecError),
97
98	/// Incoming request stream exhausted. Should only happen on shutdown.
99	#[error("Incoming request channel got closed.")]
100	RequestChannelExhausted,
101
102	#[error("Failed to send response.")]
103	SendResponse,
104
105	#[error("Received invalid response.")]
106	InvalidResponse(PeerReport),
107
108	#[error("Internal error while getting response.")]
109	ResponseError,
110
111	#[error("On-demand requests receiver stream terminated.")]
112	RequestsReceiverStreamClosed,
113}