Change on-the-wire protocol names to include genesis hash & fork id (#11938)

* Rename transactions protocol to include genesis hash

* Add protocol name generation to sc_network::utils

* Use utils functions for transactions protocol name generation

* Extract protocol name generation into public module

* Use sc_network::protocol_name::standard_protocol_name() for BEEFY and GRANDPA

* minor: add missing newline at EOF

* Change block-announces protocol name to include genesis_hash & fork_id

* Change protocol names to include genesis hash and fork id

Protocols changed:
    - sync
    - state
    - light
    - sync/warp

* Revert "Use sc_network::protocol_name::standard_protocol_name() for BEEFY and GRANDPA"

This reverts commit cd60a95a3face397e1b67f4bc95dd0f2b581bfae.

* Get rid of `protocol_name` module
This commit is contained in:
Dmitry Markin
2022-08-05 09:50:57 +03:00
committed by GitHub
parent 946f6a2818
commit 6eda842cf0
17 changed files with 226 additions and 50 deletions
@@ -62,9 +62,15 @@ mod rep {
}
/// Generates a [`ProtocolConfig`] for the block request protocol, refusing incoming requests.
pub fn generate_protocol_config(protocol_id: &ProtocolId) -> ProtocolConfig {
pub fn generate_protocol_config<Hash: AsRef<[u8]>>(
protocol_id: &ProtocolId,
genesis_hash: Hash,
fork_id: Option<&str>,
) -> ProtocolConfig {
ProtocolConfig {
name: generate_protocol_name(protocol_id).into(),
name: generate_protocol_name(genesis_hash, fork_id).into(),
fallback_names: std::iter::once(generate_legacy_protocol_name(protocol_id).into())
.collect(),
max_request_size: 1024 * 1024,
max_response_size: 16 * 1024 * 1024,
request_timeout: Duration::from_secs(20),
@@ -72,8 +78,17 @@ pub fn generate_protocol_config(protocol_id: &ProtocolId) -> ProtocolConfig {
}
}
/// Generate the block protocol name from chain specific protocol identifier.
fn generate_protocol_name(protocol_id: &ProtocolId) -> String {
/// Generate the block protocol name from the genesis hash and fork id.
fn generate_protocol_name<Hash: AsRef<[u8]>>(genesis_hash: Hash, fork_id: Option<&str>) -> String {
if let Some(fork_id) = fork_id {
format!("/{}/{}/sync/2", hex::encode(genesis_hash), fork_id)
} else {
format!("/{}/sync/2", hex::encode(genesis_hash))
}
}
/// Generate the legacy block protocol name from chain specific protocol identifier.
fn generate_legacy_protocol_name(protocol_id: &ProtocolId) -> String {
format!("/{}/sync/2", protocol_id.as_ref())
}
@@ -129,6 +144,7 @@ where
/// Create a new [`BlockRequestHandler`].
pub fn new(
protocol_id: &ProtocolId,
fork_id: Option<&str>,
client: Arc<Client>,
num_peer_hint: usize,
) -> (Self, ProtocolConfig) {
@@ -136,7 +152,15 @@ where
// number of peers.
let (tx, request_receiver) = mpsc::channel(num_peer_hint);
let mut protocol_config = generate_protocol_config(protocol_id);
let mut protocol_config = generate_protocol_config(
protocol_id,
client
.block_hash(0u32.into())
.ok()
.flatten()
.expect("Genesis block exists; qed"),
fork_id,
);
protocol_config.inbound_queue = Some(tx);
let seen_requests = LruCache::new(num_peer_hint * 2);
@@ -27,7 +27,7 @@ use libp2p::PeerId;
use log::{debug, trace};
use lru::LruCache;
use prost::Message;
use sc_client_api::ProofProvider;
use sc_client_api::{BlockBackend, ProofProvider};
use sc_network_common::{
config::ProtocolId,
request_responses::{IncomingRequest, OutgoingResponse, ProtocolConfig},
@@ -50,10 +50,16 @@ mod rep {
pub const SAME_REQUEST: Rep = Rep::new(i32::MIN, "Same state request multiple times");
}
/// Generates a [`ProtocolConfig`] for the block request protocol, refusing incoming requests.
pub fn generate_protocol_config(protocol_id: &ProtocolId) -> ProtocolConfig {
/// Generates a [`ProtocolConfig`] for the state request protocol, refusing incoming requests.
pub fn generate_protocol_config<Hash: AsRef<[u8]>>(
protocol_id: &ProtocolId,
genesis_hash: Hash,
fork_id: Option<&str>,
) -> ProtocolConfig {
ProtocolConfig {
name: generate_protocol_name(protocol_id).into(),
name: generate_protocol_name(genesis_hash, fork_id).into(),
fallback_names: std::iter::once(generate_legacy_protocol_name(protocol_id).into())
.collect(),
max_request_size: 1024 * 1024,
max_response_size: 16 * 1024 * 1024,
request_timeout: Duration::from_secs(40),
@@ -61,8 +67,17 @@ pub fn generate_protocol_config(protocol_id: &ProtocolId) -> ProtocolConfig {
}
}
/// Generate the state protocol name from chain specific protocol identifier.
fn generate_protocol_name(protocol_id: &ProtocolId) -> String {
/// Generate the state protocol name from the genesis hash and fork id.
fn generate_protocol_name<Hash: AsRef<[u8]>>(genesis_hash: Hash, fork_id: Option<&str>) -> String {
if let Some(fork_id) = fork_id {
format!("/{}/{}/state/2", hex::encode(genesis_hash), fork_id)
} else {
format!("/{}/state/2", hex::encode(genesis_hash))
}
}
/// Generate the legacy state protocol name from chain specific protocol identifier.
fn generate_legacy_protocol_name(protocol_id: &ProtocolId) -> String {
format!("/{}/state/2", protocol_id.as_ref())
}
@@ -104,11 +119,12 @@ pub struct StateRequestHandler<B: BlockT, Client> {
impl<B, Client> StateRequestHandler<B, Client>
where
B: BlockT,
Client: ProofProvider<B> + Send + Sync + 'static,
Client: BlockBackend<B> + ProofProvider<B> + Send + Sync + 'static,
{
/// Create a new [`StateRequestHandler`].
pub fn new(
protocol_id: &ProtocolId,
fork_id: Option<&str>,
client: Arc<Client>,
num_peer_hint: usize,
) -> (Self, ProtocolConfig) {
@@ -116,7 +132,15 @@ where
// number of peers.
let (tx, request_receiver) = mpsc::channel(num_peer_hint);
let mut protocol_config = generate_protocol_config(protocol_id);
let mut protocol_config = generate_protocol_config(
protocol_id,
client
.block_hash(0u32.into())
.ok()
.flatten()
.expect("Genesis block exists; qed"),
fork_id,
);
protocol_config.inbound_queue = Some(tx);
let seen_requests = LruCache::new(num_peer_hint * 2);
@@ -36,9 +36,15 @@ const MAX_RESPONSE_SIZE: u64 = 16 * 1024 * 1024;
/// Generates a [`RequestResponseConfig`] for the grandpa warp sync request protocol, refusing
/// incoming requests.
pub fn generate_request_response_config(protocol_id: ProtocolId) -> RequestResponseConfig {
pub fn generate_request_response_config<Hash: AsRef<[u8]>>(
protocol_id: ProtocolId,
genesis_hash: Hash,
fork_id: Option<&str>,
) -> RequestResponseConfig {
RequestResponseConfig {
name: generate_protocol_name(protocol_id).into(),
name: generate_protocol_name(genesis_hash, fork_id).into(),
fallback_names: std::iter::once(generate_legacy_protocol_name(protocol_id).into())
.collect(),
max_request_size: 32,
max_response_size: MAX_RESPONSE_SIZE,
request_timeout: Duration::from_secs(10),
@@ -46,8 +52,17 @@ pub fn generate_request_response_config(protocol_id: ProtocolId) -> RequestRespo
}
}
/// Generate the grandpa warp sync protocol name from chain specific protocol identifier.
fn generate_protocol_name(protocol_id: ProtocolId) -> String {
/// Generate the grandpa warp sync protocol name from the genesi hash and fork id.
fn generate_protocol_name<Hash: AsRef<[u8]>>(genesis_hash: Hash, fork_id: Option<&str>) -> String {
if let Some(fork_id) = fork_id {
format!("/{}/{}/sync/warp", hex::encode(genesis_hash), fork_id)
} else {
format!("/{}/sync/warp", hex::encode(genesis_hash))
}
}
/// Generate the legacy grandpa warp sync protocol name from chain specific protocol identifier.
fn generate_legacy_protocol_name(protocol_id: ProtocolId) -> String {
format!("/{}/sync/warp", protocol_id.as_ref())
}
@@ -59,13 +74,16 @@ pub struct RequestHandler<TBlock: BlockT> {
impl<TBlock: BlockT> RequestHandler<TBlock> {
/// Create a new [`RequestHandler`].
pub fn new(
pub fn new<Hash: AsRef<[u8]>>(
protocol_id: ProtocolId,
genesis_hash: Hash,
fork_id: Option<&str>,
backend: Arc<dyn WarpSyncProvider<TBlock>>,
) -> (Self, RequestResponseConfig) {
let (tx, request_receiver) = mpsc::channel(20);
let mut request_response_config = generate_request_response_config(protocol_id);
let mut request_response_config =
generate_request_response_config(protocol_id, genesis_hash, fork_id);
request_response_config.inbound_queue = Some(tx);
(Self { backend, request_receiver }, request_response_config)