Name changes for GrandPa and Beefy notifications protocols (#10463)

* grandpa: update notif protocol name

* grandpa: add chain id prefix to protocol name

* grandpa: beautify protocol name handling

* grandpa: prepend genesis hash to protocol name

* chain-spec: add optional 'fork_id'

'fork_id' is used to uniquely identify forks of the same chain/network
'ChainSpec' trait provides default 'None' implementation, meaning this
chain hasn't been forked.

* grandpa: protocol_name mod instead of struct

* beefy: add genesis hash prefix to protocol name

* chainspec: add fork_id

* grandpa: simplify protocol name

* grandpa: contain protocol name building logic

* beefy: contain protocol name building logic

* grandpa: fix tests

* fix merge damage

* fix docs reference visibility

Signed-off-by: acatangiu <adrian@parity.io>

* Update client/finality-grandpa/src/lib.rs

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Update client/finality-grandpa/src/communication/mod.rs

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Update client/beefy/src/lib.rs

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Update client/beefy/src/lib.rs

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* avoid using hash default, even for protocol names

Co-authored-by: Tomasz Drwięga <tomusdrw@users.noreply.github.com>
This commit is contained in:
Adrian Catangiu
2022-01-05 19:01:44 +02:00
committed by GitHub
parent 223d929f86
commit c1865988df
18 changed files with 163 additions and 46 deletions
@@ -27,6 +27,7 @@ parity-scale-codec = { version = "2.3.1", features = ["derive"] }
sp-application-crypto = { version = "4.0.0", path = "../../primitives/application-crypto" }
sp-arithmetic = { version = "4.0.0", path = "../../primitives/arithmetic" }
sp-runtime = { version = "4.0.0", path = "../../primitives/runtime" }
sc-chain-spec = { version = "4.0.0-dev", path = "../../client/chain-spec" }
sc-utils = { version = "4.0.0-dev", path = "../utils" }
sp-consensus = { version = "0.10.0-dev", path = "../../primitives/consensus/common" }
sc-consensus = { version = "0.10.0-dev", path = "../consensus/common" }
@@ -1664,6 +1664,7 @@ pub(super) struct PeerReport {
#[cfg(test)]
mod tests {
use super::{environment::SharedVoterSetState, *};
use crate::communication;
use sc_network::config::Role;
use sc_network_gossip::Validator as GossipValidatorT;
use sc_network_test::Block;
@@ -1679,6 +1680,7 @@ mod tests {
local_role: Role::Authority,
observer_enabled: true,
telemetry: None,
protocol_name: communication::grandpa_protocol_name::NAME.into(),
}
}
@@ -1840,13 +1842,13 @@ mod tests {
// messages from old rounds are expired.
for round_num in 1u64..last_kept_round {
let topic = crate::communication::round_topic::<Block>(round_num, 1);
let topic = communication::round_topic::<Block>(round_num, 1);
assert!(is_expired(topic, &[1, 2, 3]));
}
// messages from not-too-old rounds are not expired.
for round_num in last_kept_round..10 {
let topic = crate::communication::round_topic::<Block>(round_num, 1);
let topic = communication::round_topic::<Block>(round_num, 1);
assert!(!is_expired(topic, &[1, 2, 3]));
}
}
@@ -2262,7 +2264,7 @@ mod tests {
// we accept messages from rounds 9, 10 and 11
// therefore neither of those should be considered expired
for round in &[9, 10, 11] {
assert!(!is_expired(crate::communication::round_topic::<Block>(*round, 1), &[]))
assert!(!is_expired(communication::round_topic::<Block>(*round, 1), &[]))
}
}
@@ -2310,7 +2312,7 @@ mod tests {
if message_allowed(
peer,
MessageIntent::Broadcast,
&crate::communication::round_topic::<Block>(1, 0),
&communication::round_topic::<Block>(1, 0),
&[],
) {
allowed += 1;
@@ -2374,7 +2376,7 @@ mod tests {
assert!(!val.message_allowed()(
&light_peer,
MessageIntent::Broadcast,
&crate::communication::round_topic::<Block>(1, 0),
&communication::round_topic::<Block>(1, 0),
&[],
));
@@ -2388,7 +2390,7 @@ mod tests {
assert!(!val.message_allowed()(
&light_peer,
MessageIntent::Broadcast,
&crate::communication::round_topic::<Block>(1, 0),
&communication::round_topic::<Block>(1, 0),
&[],
));
@@ -2412,8 +2414,8 @@ mod tests {
auth_data: Vec::new(),
};
crate::communication::gossip::GossipMessage::<Block>::Commit(
crate::communication::gossip::FullCommitMessage {
communication::gossip::GossipMessage::<Block>::Commit(
communication::gossip::FullCommitMessage {
round: Round(2),
set_id: SetId(0),
message: commit,
@@ -2426,7 +2428,7 @@ mod tests {
assert!(val.message_allowed()(
&light_peer,
MessageIntent::Broadcast,
&crate::communication::global_topic::<Block>(0),
&communication::global_topic::<Block>(0),
&commit,
));
}
@@ -2466,8 +2468,8 @@ mod tests {
auth_data: Vec::new(),
};
crate::communication::gossip::GossipMessage::<Block>::Commit(
crate::communication::gossip::FullCommitMessage {
communication::gossip::GossipMessage::<Block>::Commit(
communication::gossip::FullCommitMessage {
round: Round(1),
set_id: SetId(1),
message: commit,
@@ -2485,7 +2487,7 @@ mod tests {
assert!(message_allowed(
&peer1,
MessageIntent::Broadcast,
&crate::communication::global_topic::<Block>(1),
&communication::global_topic::<Block>(1),
&commit,
));
@@ -2494,7 +2496,7 @@ mod tests {
assert!(!message_allowed(
&peer2,
MessageIntent::Broadcast,
&crate::communication::global_topic::<Block>(1),
&communication::global_topic::<Block>(1),
&commit,
));
}
@@ -2511,8 +2513,8 @@ mod tests {
auth_data: Vec::new(),
};
crate::communication::gossip::GossipMessage::<Block>::Commit(
crate::communication::gossip::FullCommitMessage {
communication::gossip::GossipMessage::<Block>::Commit(
communication::gossip::FullCommitMessage {
round: Round(round),
set_id: SetId(set_id),
message: commit,
@@ -2532,15 +2534,13 @@ mod tests {
// a commit message for round 1 that finalizes the same height as we
// have observed previously should not be expired
assert!(
!message_expired(crate::communication::global_topic::<Block>(1), &commit(1, 1, 2),)
);
assert!(!message_expired(communication::global_topic::<Block>(1), &commit(1, 1, 2),));
// it should be expired if it is for a lower block
assert!(message_expired(crate::communication::global_topic::<Block>(1), &commit(1, 1, 1)));
assert!(message_expired(communication::global_topic::<Block>(1), &commit(1, 1, 1)));
// or the same block height but from the previous round
assert!(message_expired(crate::communication::global_topic::<Block>(1), &commit(0, 1, 2)));
assert!(message_expired(communication::global_topic::<Block>(1), &commit(0, 1, 2)));
}
#[test]
@@ -67,9 +67,27 @@ mod periodic;
#[cfg(test)]
pub(crate) mod tests;
/// Name of the notifications protocol used by Grandpa. Must be registered towards the networking
/// in order for Grandpa to properly function.
pub const GRANDPA_PROTOCOL_NAME: &'static str = "/paritytech/grandpa/1";
pub mod grandpa_protocol_name {
use sc_chain_spec::ChainSpec;
pub(crate) const NAME: &'static str = "/grandpa/1";
/// Old names for the notifications protocol, used for backward compatibility.
pub(crate) const LEGACY_NAMES: [&'static str; 1] = ["/paritytech/grandpa/1"];
/// Name of the notifications protocol used by GRANDPA.
///
/// Must be registered towards the networking in order for GRANDPA to properly function.
pub fn standard_name<Hash: std::fmt::Display>(
genesis_hash: &Hash,
chain_spec: &Box<dyn ChainSpec>,
) -> std::borrow::Cow<'static, str> {
let chain_prefix = match chain_spec.fork_id() {
Some(fork_id) => format!("/{}/{}", genesis_hash, fork_id),
None => format!("/{}", genesis_hash),
};
format!("{}{}", chain_prefix, NAME).into()
}
}
// cost scalars for reporting peers.
mod cost {
@@ -220,13 +238,14 @@ impl<B: BlockT, N: Network<B>> NetworkBridge<B, N> {
prometheus_registry: Option<&Registry>,
telemetry: Option<TelemetryHandle>,
) -> Self {
let protocol = config.protocol_name.clone();
let (validator, report_stream) =
GossipValidator::new(config, set_state.clone(), prometheus_registry, telemetry.clone());
let validator = Arc::new(validator);
let gossip_engine = Arc::new(Mutex::new(GossipEngine::new(
service.clone(),
GRANDPA_PROTOCOL_NAME,
protocol,
validator.clone(),
prometheus_registry,
)));
@@ -22,7 +22,7 @@ use super::{
gossip::{self, GossipValidator},
Round, SetId, VoterSet,
};
use crate::{communication::GRANDPA_PROTOCOL_NAME, environment::SharedVoterSetState};
use crate::{communication::grandpa_protocol_name, environment::SharedVoterSetState};
use futures::prelude::*;
use parity_scale_codec::Encode;
use sc_network::{config::Role, Event as NetworkEvent, ObservedRole, PeerId};
@@ -97,7 +97,7 @@ impl sc_network_gossip::ValidatorContext<Block> for TestNetwork {
<Self as sc_network_gossip::Network<Block>>::write_notification(
self,
who.clone(),
GRANDPA_PROTOCOL_NAME.into(),
grandpa_protocol_name::NAME.into(),
data,
);
}
@@ -148,6 +148,7 @@ fn config() -> crate::Config {
local_role: Role::Authority,
observer_enabled: true,
telemetry: None,
protocol_name: grandpa_protocol_name::NAME.into(),
}
}
@@ -286,7 +287,7 @@ fn good_commit_leads_to_relay() {
// Add the sending peer and send the commit
let _ = sender.unbounded_send(NetworkEvent::NotificationStreamOpened {
remote: sender_id.clone(),
protocol: GRANDPA_PROTOCOL_NAME.into(),
protocol: grandpa_protocol_name::NAME.into(),
negotiated_fallback: None,
role: ObservedRole::Full,
});
@@ -294,7 +295,7 @@ fn good_commit_leads_to_relay() {
let _ = sender.unbounded_send(NetworkEvent::NotificationsReceived {
remote: sender_id.clone(),
messages: vec![(
GRANDPA_PROTOCOL_NAME.into(),
grandpa_protocol_name::NAME.into(),
commit_to_send.clone().into(),
)],
});
@@ -303,7 +304,7 @@ fn good_commit_leads_to_relay() {
let receiver_id = sc_network::PeerId::random();
let _ = sender.unbounded_send(NetworkEvent::NotificationStreamOpened {
remote: receiver_id.clone(),
protocol: GRANDPA_PROTOCOL_NAME.into(),
protocol: grandpa_protocol_name::NAME.into(),
negotiated_fallback: None,
role: ObservedRole::Full,
});
@@ -321,7 +322,10 @@ fn good_commit_leads_to_relay() {
sender.unbounded_send(NetworkEvent::NotificationsReceived {
remote: receiver_id,
messages: vec![(GRANDPA_PROTOCOL_NAME.into(), msg.encode().into())],
messages: vec![(
grandpa_protocol_name::NAME.into(),
msg.encode().into(),
)],
})
};
@@ -433,14 +437,14 @@ fn bad_commit_leads_to_report() {
Event::EventStream(sender) => {
let _ = sender.unbounded_send(NetworkEvent::NotificationStreamOpened {
remote: sender_id.clone(),
protocol: GRANDPA_PROTOCOL_NAME.into(),
protocol: grandpa_protocol_name::NAME.into(),
negotiated_fallback: None,
role: ObservedRole::Full,
});
let _ = sender.unbounded_send(NetworkEvent::NotificationsReceived {
remote: sender_id.clone(),
messages: vec![(
GRANDPA_PROTOCOL_NAME.into(),
grandpa_protocol_name::NAME.into(),
commit_to_send.clone().into(),
)],
});
+10 -3
View File
@@ -123,6 +123,7 @@ pub mod warp_proof;
pub use authorities::{AuthoritySet, AuthoritySetChanges, SharedAuthoritySet};
pub use aux_schema::best_justification;
pub use communication::grandpa_protocol_name::standard_name as protocol_standard_name;
pub use finality_grandpa::voter::report;
pub use finality_proof::{FinalityProof, FinalityProofError, FinalityProofProvider};
pub use import::{find_forced_change, find_scheduled_change, GrandpaBlockImport};
@@ -263,6 +264,8 @@ pub struct Config {
pub keystore: Option<SyncCryptoStorePtr>,
/// TelemetryHandle instance.
pub telemetry: Option<TelemetryHandle>,
/// Chain specific GRANDPA protocol name. See [`crate::protocol_standard_name`].
pub protocol_name: std::borrow::Cow<'static, str>,
}
impl Config {
@@ -714,10 +717,14 @@ pub struct GrandpaParams<Block: BlockT, C, N, SC, VR> {
/// Returns the configuration value to put in
/// [`sc_network::config::NetworkConfiguration::extra_sets`].
pub fn grandpa_peers_set_config() -> sc_network::config::NonDefaultSetConfig {
/// For standard protocol name see [`crate::protocol_standard_name`].
pub fn grandpa_peers_set_config(
protocol_name: std::borrow::Cow<'static, str>,
) -> sc_network::config::NonDefaultSetConfig {
use communication::grandpa_protocol_name;
sc_network::config::NonDefaultSetConfig {
notifications_protocol: communication::GRANDPA_PROTOCOL_NAME.into(),
fallback_names: Vec::new(),
notifications_protocol: protocol_name,
fallback_names: grandpa_protocol_name::LEGACY_NAMES.iter().map(|&n| n.into()).collect(),
// Notifications reach ~256kiB in size at the time of writing on Kusama and Polkadot.
max_notification_size: 1024 * 1024,
set_config: sc_network::config::SetConfig {
+12 -2
View File
@@ -56,6 +56,7 @@ use substrate_test_runtime_client::runtime::BlockNumber;
use tokio::runtime::{Handle, Runtime};
use authorities::AuthoritySet;
use communication::grandpa_protocol_name;
use sc_block_builder::BlockBuilderProvider;
use sc_consensus::LongestChain;
use sc_keystore::LocalKeystore;
@@ -97,7 +98,7 @@ impl GrandpaTestNet {
impl GrandpaTestNet {
fn add_authority_peer(&mut self) {
self.add_full_peer_with_config(FullPeerConfig {
notifications_protocols: vec![communication::GRANDPA_PROTOCOL_NAME.into()],
notifications_protocols: vec![grandpa_protocol_name::NAME.into()],
is_authority: true,
..Default::default()
})
@@ -121,7 +122,7 @@ impl TestNetFactory for GrandpaTestNet {
fn add_full_peer(&mut self) {
self.add_full_peer_with_config(FullPeerConfig {
notifications_protocols: vec![communication::GRANDPA_PROTOCOL_NAME.into()],
notifications_protocols: vec![grandpa_protocol_name::NAME.into()],
is_authority: false,
..Default::default()
})
@@ -274,6 +275,7 @@ fn initialize_grandpa(
local_role: Role::Authority,
observer_enabled: true,
telemetry: None,
protocol_name: grandpa_protocol_name::NAME.into(),
},
link,
network: net_service,
@@ -423,6 +425,7 @@ fn finalize_3_voters_1_full_observer() {
local_role: Role::Authority,
observer_enabled: true,
telemetry: None,
protocol_name: grandpa_protocol_name::NAME.into(),
},
link,
network: net_service,
@@ -513,6 +516,7 @@ fn transition_3_voters_twice_1_full_observer() {
local_role: Role::Authority,
observer_enabled: true,
telemetry: None,
protocol_name: grandpa_protocol_name::NAME.into(),
},
link,
network: net_service,
@@ -971,6 +975,7 @@ fn voter_persists_its_votes() {
local_role: Role::Authority,
observer_enabled: true,
telemetry: None,
protocol_name: grandpa_protocol_name::NAME.into(),
};
let set_state = {
@@ -1010,6 +1015,7 @@ fn voter_persists_its_votes() {
local_role: Role::Authority,
observer_enabled: true,
telemetry: None,
protocol_name: grandpa_protocol_name::NAME.into(),
},
link,
network: net_service,
@@ -1050,6 +1056,7 @@ fn voter_persists_its_votes() {
local_role: Role::Authority,
observer_enabled: true,
telemetry: None,
protocol_name: grandpa_protocol_name::NAME.into(),
},
link,
network: net_service,
@@ -1213,6 +1220,7 @@ fn finalize_3_voters_1_light_observer() {
local_role: Role::Full,
observer_enabled: true,
telemetry: None,
protocol_name: grandpa_protocol_name::NAME.into(),
},
net.peers[3].data.lock().take().expect("link initialized at startup; qed"),
net.peers[3].network_service().clone(),
@@ -1259,6 +1267,7 @@ fn voter_catches_up_to_latest_round_when_behind() {
local_role: Role::Authority,
observer_enabled: true,
telemetry: None,
protocol_name: grandpa_protocol_name::NAME.into(),
},
link,
network: net.lock().peer(peer_id).network_service().clone(),
@@ -1376,6 +1385,7 @@ where
local_role: Role::Authority,
observer_enabled: true,
telemetry: None,
protocol_name: grandpa_protocol_name::NAME.into(),
};
let network =