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
+5 -1
View File
@@ -87,9 +87,13 @@ where
/// the network.
pub transaction_pool: Arc<dyn TransactionPool<H, B>>,
/// Name of the protocol to use on the wire. Should be different for each chain.
/// Legacy name of the protocol to use on the wire. Should be different for each chain.
pub protocol_id: ProtocolId,
/// Fork ID to distinguish protocols of different hard forks. Part of the standard protocol
/// name on the wire.
pub fork_id: Option<String>,
/// Import queue to use.
///
/// The import queue is the component that verifies that blocks received from other nodes are
+14 -4
View File
@@ -276,6 +276,7 @@ where
roles: Roles,
chain: Arc<Client>,
protocol_id: ProtocolId,
fork_id: &Option<String>,
network_config: &config::NetworkConfiguration,
notifications_protocols_handshakes: Vec<Vec<u8>>,
metrics_registry: Option<&Registry>,
@@ -371,8 +372,17 @@ where
sc_peerset::Peerset::from_config(sc_peerset::PeersetConfig { sets })
};
let block_announces_protocol: Cow<'static, str> =
format!("/{}/block-announces/1", protocol_id.as_ref()).into();
let block_announces_protocol = {
let genesis_hash =
chain.block_hash(0u32.into()).ok().flatten().expect("Genesis block exists; qed");
if let Some(fork_id) = fork_id {
format!("/{}/{}/block-announces/1", hex::encode(genesis_hash), fork_id)
} else {
format!("/{}/block-announces/1", hex::encode(genesis_hash))
}
};
let legacy_ba_protocol_name = format!("/{}/block-announces/1", protocol_id.as_ref());
let behaviour = {
let best_number = info.best_number;
@@ -384,8 +394,8 @@ where
.encode();
let sync_protocol_config = notifications::ProtocolConfig {
name: block_announces_protocol,
fallback_names: Vec::new(),
name: block_announces_protocol.into(),
fallback_names: iter::once(legacy_ba_protocol_name.into()).collect(),
handshake: block_announces_handshake,
max_notification_size: MAX_BLOCK_ANNOUNCE_SIZE,
};
@@ -220,7 +220,9 @@ impl RequestResponsesBehaviour {
max_request_size: protocol.max_request_size,
max_response_size: protocol.max_response_size,
},
iter::once((protocol.name.as_bytes().to_vec(), protocol_support)),
iter::once(protocol.name.as_bytes().to_vec())
.chain(protocol.fallback_names.iter().map(|name| name.as_bytes().to_vec()))
.zip(iter::repeat(protocol_support)),
cfg,
);
@@ -1027,6 +1029,7 @@ mod tests {
let protocol_config = ProtocolConfig {
name: From::from(protocol_name),
fallback_names: Vec::new(),
max_request_size: 1024,
max_response_size: 1024 * 1024,
request_timeout: Duration::from_secs(30),
@@ -1127,6 +1130,7 @@ mod tests {
let protocol_config = ProtocolConfig {
name: From::from(protocol_name),
fallback_names: Vec::new(),
max_request_size: 1024,
max_response_size: 8, // <-- important for the test
request_timeout: Duration::from_secs(30),
@@ -1223,6 +1227,7 @@ mod tests {
let protocol_configs = vec![
ProtocolConfig {
name: From::from(protocol_name_1),
fallback_names: Vec::new(),
max_request_size: 1024,
max_response_size: 1024 * 1024,
request_timeout: Duration::from_secs(30),
@@ -1230,6 +1235,7 @@ mod tests {
},
ProtocolConfig {
name: From::from(protocol_name_2),
fallback_names: Vec::new(),
max_request_size: 1024,
max_response_size: 1024 * 1024,
request_timeout: Duration::from_secs(30),
@@ -1247,6 +1253,7 @@ mod tests {
let protocol_configs = vec![
ProtocolConfig {
name: From::from(protocol_name_1),
fallback_names: Vec::new(),
max_request_size: 1024,
max_response_size: 1024 * 1024,
request_timeout: Duration::from_secs(30),
@@ -1254,6 +1261,7 @@ mod tests {
},
ProtocolConfig {
name: From::from(protocol_name_2),
fallback_names: Vec::new(),
max_request_size: 1024,
max_response_size: 1024 * 1024,
request_timeout: Duration::from_secs(30),
+11 -2
View File
@@ -224,8 +224,16 @@ where
fs::create_dir_all(path)?;
}
let transactions_handler_proto =
transactions::TransactionsHandlerPrototype::new(params.protocol_id.clone());
let transactions_handler_proto = transactions::TransactionsHandlerPrototype::new(
params.protocol_id.clone(),
params
.chain
.block_hash(0u32.into())
.ok()
.flatten()
.expect("Genesis block exists; qed"),
params.fork_id.clone(),
);
params
.network_config
.extra_sets
@@ -243,6 +251,7 @@ where
From::from(&params.role),
params.chain.clone(),
params.protocol_id.clone(),
&params.fork_id,
&params.network_config,
iter::once(Vec::new())
.chain(
@@ -92,21 +92,25 @@ fn build_test_full_node(
let protocol_id = ProtocolId::from("/test-protocol-name");
let fork_id = Some(String::from("test-fork-id"));
let block_request_protocol_config = {
let (handler, protocol_config) = BlockRequestHandler::new(&protocol_id, client.clone(), 50);
let (handler, protocol_config) =
BlockRequestHandler::new(&protocol_id, None, client.clone(), 50);
async_std::task::spawn(handler.run().boxed());
protocol_config
};
let state_request_protocol_config = {
let (handler, protocol_config) = StateRequestHandler::new(&protocol_id, client.clone(), 50);
let (handler, protocol_config) =
StateRequestHandler::new(&protocol_id, None, client.clone(), 50);
async_std::task::spawn(handler.run().boxed());
protocol_config
};
let light_client_request_protocol_config = {
let (handler, protocol_config) =
LightClientRequestHandler::new(&protocol_id, client.clone());
LightClientRequestHandler::new(&protocol_id, None, client.clone());
async_std::task::spawn(handler.run().boxed());
protocol_config
};
@@ -134,6 +138,7 @@ fn build_test_full_node(
chain: client.clone(),
transaction_pool: Arc::new(config::EmptyTransactionPool),
protocol_id,
fork_id,
import_queue,
chain_sync: Box::new(chain_sync),
metrics_registry: None,
+18 -3
View File
@@ -127,19 +127,34 @@ impl<H: ExHashT> Future for PendingTransaction<H> {
/// Prototype for a [`TransactionsHandler`].
pub struct TransactionsHandlerPrototype {
protocol_name: Cow<'static, str>,
fallback_protocol_names: Vec<Cow<'static, str>>,
}
impl TransactionsHandlerPrototype {
/// Create a new instance.
pub fn new(protocol_id: ProtocolId) -> Self {
Self { protocol_name: format!("/{}/transactions/1", protocol_id.as_ref()).into() }
pub fn new<Hash: AsRef<[u8]>>(
protocol_id: ProtocolId,
genesis_hash: Hash,
fork_id: Option<String>,
) -> Self {
let protocol_name = if let Some(fork_id) = fork_id {
format!("/{}/{}/transactions/1", hex::encode(genesis_hash), fork_id)
} else {
format!("/{}/transactions/1", hex::encode(genesis_hash))
};
let legacy_protocol_name = format!("/{}/transactions/1", protocol_id.as_ref());
Self {
protocol_name: protocol_name.into(),
fallback_protocol_names: iter::once(legacy_protocol_name.into()).collect(),
}
}
/// Returns the configuration of the set to put in the network configuration.
pub fn set_config(&self) -> config::NonDefaultSetConfig {
config::NonDefaultSetConfig {
notifications_protocol: self.protocol_name.clone(),
fallback_names: Vec::new(),
fallback_names: self.fallback_protocol_names.clone(),
max_notification_size: MAX_TRANSACTIONS_SIZE,
set_config: config::SetConfig {
in_peers: 0,