Change request-response protocol names to include genesis hash & fork id (#5870)

This commit is contained in:
Dmitry Markin
2022-08-12 15:07:13 +03:00
committed by GitHub
parent 596b5df747
commit 4def0da0dc
17 changed files with 206 additions and 72 deletions
@@ -25,7 +25,7 @@ use parity_scale_codec::{Decode, Encode};
use sc_network::{config as netconfig, config::RequestResponseConfig, PeerId};
use super::IsRequest;
use super::{IsRequest, ReqProtocolNames};
use crate::UnifiedReputationChange;
mod error;
@@ -55,8 +55,10 @@ where
///
/// This Register that config with substrate networking and receive incoming requests via the
/// returned `IncomingRequestReceiver`.
pub fn get_config_receiver() -> (IncomingRequestReceiver<Req>, RequestResponseConfig) {
let (raw, cfg) = Req::PROTOCOL.get_config();
pub fn get_config_receiver(
req_protocol_names: &ReqProtocolNames,
) -> (IncomingRequestReceiver<Req>, RequestResponseConfig) {
let (raw, cfg) = Req::PROTOCOL.get_config(req_protocol_names);
(IncomingRequestReceiver { raw, phantom: PhantomData {} }, cfg)
}
@@ -32,11 +32,11 @@
//!
//! Versioned (v1 module): The actual requests and responses as sent over the network.
use std::{borrow::Cow, time::Duration, u64};
use std::{borrow::Cow, collections::HashMap, time::Duration, u64};
use futures::channel::mpsc;
use polkadot_primitives::v2::{MAX_CODE_SIZE, MAX_POV_SIZE};
use strum::EnumIter;
use strum::{EnumIter, IntoEnumIterator};
pub use sc_network::{config as network, config::RequestResponseConfig};
@@ -126,13 +126,17 @@ impl Protocol {
///
/// Returns a receiver for messages received on this protocol and the requested
/// `ProtocolConfig`.
pub fn get_config(self) -> (mpsc::Receiver<network::IncomingRequest>, RequestResponseConfig) {
let p_name = self.into_protocol_name();
pub fn get_config(
self,
req_protocol_names: &ReqProtocolNames,
) -> (mpsc::Receiver<network::IncomingRequest>, RequestResponseConfig) {
let name = req_protocol_names.get_name(self);
let fallback_names = self.get_fallback_names();
let (tx, rx) = mpsc::channel(self.get_channel_size());
let cfg = match self {
Protocol::ChunkFetchingV1 => RequestResponseConfig {
name: p_name,
fallback_names: Vec::new(),
name,
fallback_names,
max_request_size: 1_000,
max_response_size: POV_RESPONSE_SIZE as u64 * 3,
// We are connected to all validators:
@@ -140,8 +144,8 @@ impl Protocol {
inbound_queue: Some(tx),
},
Protocol::CollationFetchingV1 => RequestResponseConfig {
name: p_name,
fallback_names: Vec::new(),
name,
fallback_names,
max_request_size: 1_000,
max_response_size: POV_RESPONSE_SIZE,
// Taken from initial implementation in collator protocol:
@@ -149,16 +153,16 @@ impl Protocol {
inbound_queue: Some(tx),
},
Protocol::PoVFetchingV1 => RequestResponseConfig {
name: p_name,
fallback_names: Vec::new(),
name,
fallback_names,
max_request_size: 1_000,
max_response_size: POV_RESPONSE_SIZE,
request_timeout: POV_REQUEST_TIMEOUT_CONNECTED,
inbound_queue: Some(tx),
},
Protocol::AvailableDataFetchingV1 => RequestResponseConfig {
name: p_name,
fallback_names: Vec::new(),
name,
fallback_names,
max_request_size: 1_000,
// Available data size is dominated by the PoV size.
max_response_size: POV_RESPONSE_SIZE,
@@ -166,8 +170,8 @@ impl Protocol {
inbound_queue: Some(tx),
},
Protocol::StatementFetchingV1 => RequestResponseConfig {
name: p_name,
fallback_names: Vec::new(),
name,
fallback_names,
max_request_size: 1_000,
// Available data size is dominated code size.
max_response_size: STATEMENT_RESPONSE_SIZE,
@@ -184,8 +188,8 @@ impl Protocol {
inbound_queue: Some(tx),
},
Protocol::DisputeSendingV1 => RequestResponseConfig {
name: p_name,
fallback_names: Vec::new(),
name,
fallback_names,
max_request_size: 1_000,
/// Responses are just confirmation, in essence not even a bit. So 100 seems
/// plenty.
@@ -243,13 +247,13 @@ impl Protocol {
}
}
/// Get the protocol name of this protocol, as understood by substrate networking.
pub fn into_protocol_name(self) -> Cow<'static, str> {
self.get_protocol_name_static().into()
/// Fallback protocol names of this protocol, as understood by substrate networking.
fn get_fallback_names(self) -> Vec<Cow<'static, str>> {
std::iter::once(self.get_legacy_name().into()).collect()
}
/// Get the protocol name associated with each peer set as static str.
pub const fn get_protocol_name_static(self) -> &'static str {
/// Legacy protocol name associated with each peer set.
const fn get_legacy_name(self) -> &'static str {
match self {
Protocol::ChunkFetchingV1 => "/polkadot/req_chunk/1",
Protocol::CollationFetchingV1 => "/polkadot/req_collation/1",
@@ -269,3 +273,51 @@ pub trait IsRequest {
/// What protocol this `Request` implements.
const PROTOCOL: Protocol;
}
/// Type for getting on the wire [`Protocol`] names using genesis hash & fork id.
pub struct ReqProtocolNames {
names: HashMap<Protocol, Cow<'static, str>>,
}
impl ReqProtocolNames {
/// Construct [`ReqProtocolNames`] from `genesis_hash` and `fork_id`.
pub fn new<Hash: AsRef<[u8]>>(genesis_hash: Hash, fork_id: Option<&str>) -> Self {
let mut names = HashMap::new();
for protocol in Protocol::iter() {
names.insert(protocol, Self::generate_name(protocol, &genesis_hash, fork_id));
}
Self { names }
}
/// Get on the wire [`Protocol`] name.
pub fn get_name(&self, protocol: Protocol) -> Cow<'static, str> {
self.names
.get(&protocol)
.expect("All `Protocol` enum variants are added above via `strum`; qed")
.clone()
}
/// Protocol name of this protocol based on `genesis_hash` and `fork_id`.
fn generate_name<Hash: AsRef<[u8]>>(
protocol: Protocol,
genesis_hash: &Hash,
fork_id: Option<&str>,
) -> Cow<'static, str> {
let prefix = if let Some(fork_id) = fork_id {
format!("/{}/{}", hex::encode(genesis_hash), fork_id)
} else {
format!("/{}", hex::encode(genesis_hash))
};
let short_name = match protocol {
Protocol::ChunkFetchingV1 => "/req_chunk/1",
Protocol::CollationFetchingV1 => "/req_collation/1",
Protocol::PoVFetchingV1 => "/req_pov/1",
Protocol::AvailableDataFetchingV1 => "/req_available_data/1",
Protocol::StatementFetchingV1 => "/req_statement/1",
Protocol::DisputeSendingV1 => "/send_dispute/1",
};
format!("{}{}", prefix, short_name).into()
}
}