Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Commit

Permalink
Name changes for GrandPa and Beefy notifications protocols (paritytec…
Browse files Browse the repository at this point in the history
…h#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>
  • Loading branch information
2 people authored and AurevoirXavier committed Jan 27, 2022
1 parent d54ed0b commit 3274a48
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 50 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bin/node-template/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub fn development_config() -> Result<ChainSpec, String> {
None,
// Protocol ID
None,
None,
// Properties
None,
// Extensions
Expand Down Expand Up @@ -117,6 +118,7 @@ pub fn local_testnet_config() -> Result<ChainSpec, String> {
None,
// Properties
None,
None,
// Extensions
None,
))
Expand Down
14 changes: 13 additions & 1 deletion bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//! Service and ServiceFactory implementation. Specialized wrapper over substrate service.

use node_template_runtime::{self, opaque::Block, RuntimeApi};
<<<<<<< HEAD
use sc_client_api::{ExecutorProvider, RemoteBackend};
=======
use sc_client_api::{BlockBackend, ExecutorProvider};
>>>>>>> 0f070c8020 (Name changes for GrandPa and Beefy notifications protocols (#10463))
use sc_consensus_aura::{ImportQueueParams, SlotProportion, StartAuraParams};
pub use sc_executor::NativeElseWasmExecutor;
use sc_finality_grandpa::SharedVoterState;
Expand Down Expand Up @@ -174,8 +178,15 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
))),
};
}
let grandpa_protocol_name = sc_finality_grandpa::protocol_standard_name(
&client.block_hash(0).ok().flatten().expect("Genesis block exists; qed"),
&config.chain_spec,
);

config.network.extra_sets.push(sc_finality_grandpa::grandpa_peers_set_config());
config
.network
.extra_sets
.push(sc_finality_grandpa::grandpa_peers_set_config(grandpa_protocol_name.clone()));
let warp_sync = Arc::new(sc_finality_grandpa::warp_proof::NetworkProvider::new(
backend.clone(),
grandpa_link.shared_authority_set().clone(),
Expand Down Expand Up @@ -300,6 +311,7 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
keystore,
local_role: role,
telemetry: telemetry.as_ref().map(|x| x.handle()),
protocol_name: grandpa_protocol_name,
};

if enable_grandpa {
Expand Down
5 changes: 5 additions & 0 deletions bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pub fn staging_testnet_config() -> ChainSpec {
),
None,
None,
None,
Default::default(),
)
}
Expand Down Expand Up @@ -386,6 +387,7 @@ pub fn development_config() -> ChainSpec {
None,
None,
None,
None,
Default::default(),
)
}
Expand All @@ -410,6 +412,7 @@ pub fn local_testnet_config() -> ChainSpec {
None,
None,
None,
None,
Default::default(),
)
}
Expand Down Expand Up @@ -441,6 +444,7 @@ pub(crate) mod tests {
None,
None,
None,
None,
Default::default(),
)
}
Expand All @@ -456,6 +460,7 @@ pub(crate) mod tests {
None,
None,
None,
None,
Default::default(),
)
}
Expand Down
10 changes: 9 additions & 1 deletion bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,15 @@ pub fn new_full_base(

let shared_voter_state = rpc_setup;
let auth_disc_publish_non_global_ips = config.network.allow_non_globals_in_dht;
let grandpa_protocol_name = grandpa::protocol_standard_name(
&client.block_hash(0).ok().flatten().expect("Genesis block exists; qed"),
&config.chain_spec,
);

config.network.extra_sets.push(grandpa::grandpa_peers_set_config());
config
.network
.extra_sets
.push(grandpa::grandpa_peers_set_config(grandpa_protocol_name.clone()));
let warp_sync = Arc::new(grandpa::warp_proof::NetworkProvider::new(
backend.clone(),
import_setup.1.shared_authority_set().clone(),
Expand Down Expand Up @@ -397,6 +404,7 @@ pub fn new_full_base(
keystore,
local_role: role,
telemetry: telemetry.as_ref().map(|x| x.handle()),
protocol_name: grandpa_protocol_name,
};

if enable_grandpa {
Expand Down
1 change: 1 addition & 0 deletions bin/utils/chain-spec-builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ fn generate_chain_spec(
None,
None,
None,
None,
Default::default(),
);

Expand Down
1 change: 1 addition & 0 deletions client/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sp-core = { version = "4.0.0-dev", path = "../../primitives/core" }
sp-keystore = { version = "0.10.0-dev", path = "../../primitives/keystore" }
sp-runtime = { version = "4.0.0-dev", path = "../../primitives/runtime" }

sc-chain-spec = { version = "4.0.0-dev", path = "../../client/chain-spec" }
sc-utils = { version = "4.0.0-dev", path = "../utils" }
sc-client-api = { version = "4.0.0-dev", path = "../api" }
sc-keystore = { version = "4.0.0-dev", path = "../keystore" }
Expand Down
41 changes: 34 additions & 7 deletions client/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,40 @@ mod round;
mod worker;

pub mod notification;

pub const BEEFY_PROTOCOL_NAME: &str = "/paritytech/beefy/1";
pub use beefy_protocol_name::standard_name as protocol_standard_name;

pub(crate) mod beefy_protocol_name {
use sc_chain_spec::ChainSpec;

const NAME: &'static str = "/beefy/1";
/// Old names for the notifications protocol, used for backward compatibility.
pub(crate) 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 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()
}
}

/// 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);
/// For standard protocol name see [`beefy_protocol_name::standard_name`].
pub fn beefy_peers_set_config(
protocol_name: std::borrow::Cow<'static, str>,
) -> sc_network::config::NonDefaultSetConfig {
let mut cfg = sc_network::config::NonDefaultSetConfig::new(protocol_name, 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 +126,8 @@ where
pub min_block_delta: u32,
/// Prometheus metric registry
pub prometheus_registry: Option<Registry>,
/// Chain specific GRANDPA protocol name. See [`beefy_protocol_name::standard_name`].
pub protocol_name: std::borrow::Cow<'static, str>,
}

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

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
12 changes: 12 additions & 0 deletions client/chain-spec/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ struct ClientSpec<E> {
boot_nodes: Vec<MultiaddrWithPeerId>,
telemetry_endpoints: Option<TelemetryEndpoints>,
protocol_id: Option<String>,
fork_id: Option<String>,
properties: Option<Properties>,
#[serde(flatten)]
extensions: E,
Expand Down Expand Up @@ -216,6 +217,11 @@ impl<G, E> ChainSpec<G, E> {
self.client_spec.protocol_id.as_ref().map(String::as_str)
}

/// Optional network fork identifier.
pub fn fork_id(&self) -> Option<&str> {
self.client_spec.fork_id.as_ref().map(String::as_str)
}

/// Additional loosly-typed properties of the chain.
///
/// Returns an empty JSON object if 'properties' not defined in config
Expand Down Expand Up @@ -247,6 +253,7 @@ impl<G, E> ChainSpec<G, E> {
boot_nodes: Vec<MultiaddrWithPeerId>,
telemetry_endpoints: Option<TelemetryEndpoints>,
protocol_id: Option<&str>,
fork_id: Option<&str>,
properties: Option<Properties>,
extensions: E,
) -> Self {
Expand All @@ -257,6 +264,7 @@ impl<G, E> ChainSpec<G, E> {
boot_nodes,
telemetry_endpoints,
protocol_id: protocol_id.map(str::to_owned),
fork_id: fork_id.map(str::to_owned),
properties,
extensions,
consensus_engine: (),
Expand Down Expand Up @@ -363,6 +371,10 @@ where
ChainSpec::protocol_id(self)
}

fn fork_id(&self) -> Option<&str> {
ChainSpec::fork_id(self)
}

fn properties(&self) -> Properties {
ChainSpec::properties(self)
}
Expand Down
2 changes: 2 additions & 0 deletions client/chain-spec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ pub trait ChainSpec: BuildStorage + Send + Sync {
fn telemetry_endpoints(&self) -> &Option<TelemetryEndpoints>;
/// Network protocol id.
fn protocol_id(&self) -> Option<&str>;
/// Optional network fork identifier. `None` by default.
fn fork_id(&self) -> Option<&str>;
/// Additional loosly-typed properties of the chain.
///
/// Returns an empty JSON object if 'properties' not defined in config
Expand Down
1 change: 1 addition & 0 deletions client/cli/src/commands/insert_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ mod tests {
None,
None,
None,
None,
NoExtension::None,
)))
}
Expand Down
9 changes: 5 additions & 4 deletions client/finality-grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ futures-timer = "3.0.1"
log = "0.4.8"
parking_lot = "0.11.1"
rand = "0.8.4"
parity-scale-codec = { version = "2.0.0", features = ["derive"] }
sp-application-crypto = { version = "4.0.0-dev", path = "../../primitives/application-crypto" }
sp-arithmetic = { version = "4.0.0-dev", path = "../../primitives/arithmetic" }
sp-runtime = { version = "4.0.0-dev", path = "../../primitives/runtime" }
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" }
Expand Down
Loading

0 comments on commit 3274a48

Please sign in to comment.