diff --git a/node/src/endpoint_gossip/mod.rs b/node/src/endpoint_gossip/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..6824633632f2d6cecbdcda692516a9dbb66154a3 --- /dev/null +++ b/node/src/endpoint_gossip/mod.rs @@ -0,0 +1,60 @@ +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>); diff --git a/node/src/lib.rs b/node/src/lib.rs index caf6e6f327cfa0eda6ce001d9a153aae8f1f4401..a6f6aa49548813de8d802b0af8f01880588a9b48 100644 --- a/node/src/lib.rs +++ b/node/src/lib.rs @@ -17,5 +17,6 @@ pub mod chain_spec; pub mod cli; pub mod command; +pub mod endpoint_gossip; pub mod rpc; pub mod service; diff --git a/node/src/service.rs b/node/src/service.rs index 48b2c255c1149b1abfef5b7a99aed632331cfd3d..d6b23bc4ea35cf1644a48bb5c22dd831df777e13 100644 --- a/node/src/service.rs +++ b/node/src/service.rs @@ -325,15 +325,14 @@ where other: (block_import, babe_link, babe_worker_handle, grandpa_link, mut telemetry), } = new_partial::<RuntimeApi, Executor>(&config, sealing.is_manual_consensus())?; - let grandpa_protocol_name = sc_consensus_grandpa::protocol_standard_name( - &client - .block_hash(0) - .ok() - .flatten() - .expect("Genesis block exists; qed"), - &config.chain_spec, - ); - + // genesis hash used in protocol names + let genesis_hash = client + .block_hash(0) + .ok() + .flatten() + .expect("Genesis block exists; qed"); + + // shared network config let mut net_config = sc_network::config::FullNetworkConfiguration::< Block, <Block as sp_runtime::traits::Block>::Hash, @@ -341,6 +340,10 @@ where >::new(&config.network, config.prometheus_registry().cloned()); let metrics = N::register_notification_metrics(config.prometheus_registry()); 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) = sc_consensus_grandpa::grandpa_peers_set_config::<_, N>( grandpa_protocol_name.clone(), @@ -351,25 +354,33 @@ where // endpoint gossip // only active for non-authority (to save workload) - if !role.is_authority() { - // configure protocol - let (endpoint_gossip_protocol_config, endpoint_gossip_notification_service) = ((), ()); - net_config.add_notification_protocol(endpoint_gossip_protocol_config); - let endpoint_gossip_config = (); - let endpoint_gossip_params = EndpointGossipParams { - config: endpoint_gossip_config, - network, - notification_service: endpoint_gossip_notification_service, - // offchain storage - offchain_endpoint_tester: (), - }; - // spawn task - task_manager.spawn_handle().spawn( - "endpoint-gossip", - None, - run_gossip_worker(endpoint_gossip_params)?, + // if !role.is_authority() { + // configure protocol + 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); + let endpoint_gossip_config = (); + + let endpoint_gossip_params = EndpointGossipParams { + config: endpoint_gossip_config, + network, + notification_service: endpoint_gossip_notification_service, + // offchain storage + offchain_endpoint_tester: (), + }; + // spawn task + task_manager.spawn_handle().spawn( + "endpoint-gossip", + None, + run_gossip_worker(endpoint_gossip_params)?, + ); + // } let warp_sync = Arc::new(sc_consensus_grandpa::warp_proof::NetworkProvider::new( backend.clone(),