Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
beefy: add genesis hash prefix to protocol name
Browse files Browse the repository at this point in the history
  • Loading branch information
acatangiu committed Dec 13, 2021
1 parent 2cb1593 commit fcb9c8a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
29 changes: 23 additions & 6 deletions client/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,28 @@ mod worker;

pub mod notification;

pub const BEEFY_PROTOCOL_NAME: &str = "/paritytech/beefy/1";
pub(crate) mod beefy_protocol_name {
const NAME: &'static str = "/beefy/1";
/// Old names for the notifications protocol, used for backward compatibility.
pub const LEGACY_NAMES: [&'static str; 1] = ["/paritytech/beefy/1"];

/// Name of the notifications protocol used by Beefy.
/// Must be registered towards the networking in order for Beefy to properly function.
pub fn with_prefix(prefix: &str) -> String {
format!("{}{}", prefix, NAME)
}
}

/// Returns the configuration value to put in
/// [`sc_network::config::NetworkConfiguration::extra_sets`].
pub fn beefy_peers_set_config() -> sc_network::config::NonDefaultSetConfig {
let mut cfg =
sc_network::config::NonDefaultSetConfig::new(BEEFY_PROTOCOL_NAME.into(), 1024 * 1024);
pub fn beefy_peers_set_config(chain_prefix: &str) -> sc_network::config::NonDefaultSetConfig {
let mut cfg = sc_network::config::NonDefaultSetConfig::new(
beefy_protocol_name::with_prefix(chain_prefix).into(),
1024 * 1024,
);

cfg.allow_non_reserved(25, 25);
cfg.add_fallback_names(beefy_protocol_name::LEGACY_NAMES.iter().map(|&n| n.into()).collect());
cfg
}

Expand Down Expand Up @@ -101,6 +115,8 @@ where
pub min_block_delta: u32,
/// Prometheus metric registry
pub prometheus_registry: Option<Registry>,
/// Protocol name prefix - usually genesis hash and optional fork id.
pub protocol_name_prefix: String,
}

/// Start the BEEFY gadget.
Expand All @@ -122,11 +138,12 @@ where
signed_commitment_sender,
min_block_delta,
prometheus_registry,
protocol_name_prefix,
} = beefy_params;

let protocol_name = beefy_protocol_name::with_prefix(&protocol_name_prefix);
let gossip_validator = Arc::new(gossip::GossipValidator::new());
let gossip_engine =
GossipEngine::new(network, BEEFY_PROTOCOL_NAME, gossip_validator.clone(), None);
let gossip_engine = GossipEngine::new(network, protocol_name, gossip_validator.clone(), None);

let metrics =
prometheus_registry.as_ref().map(metrics::Metrics::register).and_then(
Expand Down
5 changes: 5 additions & 0 deletions client/network/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ impl NonDefaultSetConfig {
pub fn add_reserved(&mut self, peer: MultiaddrWithPeerId) {
self.set_config.reserved_nodes.push(peer);
}

/// Add a list of protocol names used for backward compatibility.
pub fn add_fallback_names(&mut self, fallback_names: Vec<Cow<'static, str>>) {
self.fallback_names.extend(fallback_names);
}
}

/// Configuration for the transport layer.
Expand Down

0 comments on commit fcb9c8a

Please sign in to comment.