Skip to content
Snippets Groups Projects
Commit ff284273 authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

wip endpoint_gossip module

parent 35eb66d9
No related branches found
No related tags found
No related merge requests found
use sc_chain_spec::ChainSpec;
use sc_network::types::ProtocolName;
// endpoint gossip is a single protocol used to gossip different kinds of endpoints like
// - rpc endpoints
// - squid indexer endpoints
// ...
pub(crate) const NAME: &str = "/endpoint_gossip/1";
/// Name of the notifications protocol used by endpoint gossip.
// Prefix by fork id to avoid gossiping endpoints of wrong network
pub fn protocol_name<Hash: AsRef<[u8]>>(
genesis_hash: &Hash,
chain_spec: &Box<dyn ChainSpec>,
) -> ProtocolName {
let genesis_hash = genesis_hash.as_ref();
let chain_prefix = match chain_spec.fork_id() {
Some(fork_id) => format!("/{}/{}", array_bytes::bytes2hex("", genesis_hash), fork_id),
None => format!("/{}", array_bytes::bytes2hex("", genesis_hash)),
};
format!("{}{}", chain_prefix, NAME).into()
}
/// Returns the configuration value to put in
/// [`sc_network::config::FullNetworkConfiguration`].
pub fn peers_set_config<B: BlockT, N: NetworkBackend<B, <B as BlockT>::Hash>>(
protocol_name: ProtocolName,
metrics: sc_network::service::NotificationMetrics,
peer_store_handle: Arc<dyn sc_network::peer_store::PeerStoreProvider>,
) -> (N::NotificationProtocolConfig, Box<dyn NotificationService>) {
use communication::grandpa_protocol_name;
N::notification_config(
protocol_name,
vec![],
// Inspired by GRANDPA notifications which are ~256kiB
1024 * 1024,
None,
sc_network::config::SetConfig {
in_peers: 0,
out_peers: 0,
reserved_nodes: Vec::new(),
non_reserved_mode: sc_network::config::NonReservedPeerMode::Deny,
},
metrics,
peer_store_handle,
)
}
// /// Create notification protocol configuration and an associated `NotificationService`
// /// for the protocol.
// fn notification_config(
// protocol_name: ProtocolName,
// fallback_names: Vec<ProtocolName>,
// max_notification_size: u64,
// handshake: Option<NotificationHandshake>,
// set_config: SetConfig,
// metrics: NotificationMetrics,
// peerstore_handle: Arc<dyn PeerStoreProvider>,
// ) -> (Self::NotificationProtocolConfig, Box<dyn NotificationService>);
...@@ -17,5 +17,6 @@ ...@@ -17,5 +17,6 @@
pub mod chain_spec; pub mod chain_spec;
pub mod cli; pub mod cli;
pub mod command; pub mod command;
pub mod endpoint_gossip;
pub mod rpc; pub mod rpc;
pub mod service; pub mod service;
...@@ -325,15 +325,14 @@ where ...@@ -325,15 +325,14 @@ where
other: (block_import, babe_link, babe_worker_handle, grandpa_link, mut telemetry), other: (block_import, babe_link, babe_worker_handle, grandpa_link, mut telemetry),
} = new_partial::<RuntimeApi, Executor>(&config, sealing.is_manual_consensus())?; } = new_partial::<RuntimeApi, Executor>(&config, sealing.is_manual_consensus())?;
let grandpa_protocol_name = sc_consensus_grandpa::protocol_standard_name( // genesis hash used in protocol names
&client let genesis_hash = client
.block_hash(0) .block_hash(0)
.ok() .ok()
.flatten() .flatten()
.expect("Genesis block exists; qed"), .expect("Genesis block exists; qed");
&config.chain_spec,
);
// shared network config
let mut net_config = sc_network::config::FullNetworkConfiguration::< let mut net_config = sc_network::config::FullNetworkConfiguration::<
Block, Block,
<Block as sp_runtime::traits::Block>::Hash, <Block as sp_runtime::traits::Block>::Hash,
...@@ -341,6 +340,10 @@ where ...@@ -341,6 +340,10 @@ where
>::new(&config.network, config.prometheus_registry().cloned()); >::new(&config.network, config.prometheus_registry().cloned());
let metrics = N::register_notification_metrics(config.prometheus_registry()); let metrics = N::register_notification_metrics(config.prometheus_registry());
let peer_store_handle = net_config.peer_store_handle(); let peer_store_handle = net_config.peer_store_handle();
// grandpa network config
let grandpa_protocol_name =
sc_consensus_grandpa::protocol_standard_name(&genesis_hash, &config.chain_spec);
let (grandpa_protocol_config, grandpa_notification_service) = let (grandpa_protocol_config, grandpa_notification_service) =
sc_consensus_grandpa::grandpa_peers_set_config::<_, N>( sc_consensus_grandpa::grandpa_peers_set_config::<_, N>(
grandpa_protocol_name.clone(), grandpa_protocol_name.clone(),
...@@ -351,11 +354,19 @@ where ...@@ -351,11 +354,19 @@ where
// endpoint gossip // endpoint gossip
// only active for non-authority (to save workload) // only active for non-authority (to save workload)
if !role.is_authority() { // if !role.is_authority() {
// configure protocol // configure protocol
let (endpoint_gossip_protocol_config, endpoint_gossip_notification_service) = ((), ()); let endpoint_gossip_protocol_name =
endpoint_gossip::protocol_name(&genesis_hash, &config.chain_spec);
let (endpoint_gossip_protocol_config, endpoint_gossip_notification_service) =
endpoint_gossip::peers_set_config::<_, N>(
endpoint_gossip_protocol_name.clone(),
metrics.clone(),
peer_store_handle,
);
net_config.add_notification_protocol(endpoint_gossip_protocol_config); net_config.add_notification_protocol(endpoint_gossip_protocol_config);
let endpoint_gossip_config = (); let endpoint_gossip_config = ();
let endpoint_gossip_params = EndpointGossipParams { let endpoint_gossip_params = EndpointGossipParams {
config: endpoint_gossip_config, config: endpoint_gossip_config,
network, network,
...@@ -369,7 +380,7 @@ where ...@@ -369,7 +380,7 @@ where
None, None,
run_gossip_worker(endpoint_gossip_params)?, run_gossip_worker(endpoint_gossip_params)?,
); );
} // }
let warp_sync = Arc::new(sc_consensus_grandpa::warp_proof::NetworkProvider::new( let warp_sync = Arc::new(sc_consensus_grandpa::warp_proof::NetworkProvider::new(
backend.clone(), backend.clone(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment