sc_consensus_beefy/communication/request_response/
mod.rs1mod 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
36const 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
45pub(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		JUSTIF_REQUEST_TIMEOUT,
70		Some(tx),
71	);
72	(rx, cfg)
73}
74
75#[derive(Debug, Clone, Encode, Decode)]
77pub struct JustificationRequest<B: Block> {
78	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	#[error("Decoding request failed for peer {0}.")]
92	DecodingError(PeerId, #[source] CodecError),
93
94	#[error("Decoding request failed for peer {0}, and changing reputation failed.")]
96	DecodingErrorNoReputationChange(PeerId, #[source] CodecError),
97
98	#[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}