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

Commit 104d269

Browse files
altonendmitry-markin
authored andcommitted
Prepare sc-network for ProtocolController/NotificationService (#14080)
* Prepare `sc-network` for `ProtocolController`/`NotificationService` The upcoming notification protocol refactoring requires that protocols are able to communicate with `sc-network` over unique and direct links. This means that `sc-network` side of the link has to be created before `sc-network` is initialized and that it is allowed to consume the object as the receiver half of the link may not implement `Clone`. Remove request-response and notification protocols from `NetworkConfiguration` and create a new object that contains the configurations of these protocols and which is consumable by `sc-network`. This is needed needed because, e.g., the receiver half of `NotificationService` is not clonable so `sc-network` must consume it when it's initializing the protocols in `Notifications`. Similar principe applies to `PeerStore`/`ProtocolController`: as per current design, protocols are created before the network so `Protocol` cannot be the one creating the `PeerStore` object. `FullNetworkConfiguration` will be used to store the objects that `sc-network` will use to communicate with protocols and it will also allow protocols to allocate handles so they can directly communicate with `sc-network`. * Fixes * Update client/service/src/builder.rs Co-authored-by: Dmitry Markin <dmitry@markin.tech> * Updates * Doc updates + cargo-fmt --------- Co-authored-by: Dmitry Markin <dmitry@markin.tech>
1 parent e9057bc commit 104d269

File tree

14 files changed

+302
-252
lines changed

14 files changed

+302
-252
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node-template/node/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ futures = { version = "0.3.21", features = ["thread-pool"]}
2323
sc-cli = { version = "0.10.0-dev", path = "../../../client/cli" }
2424
sp-core = { version = "7.0.0", path = "../../../primitives/core" }
2525
sc-executor = { version = "0.10.0-dev", path = "../../../client/executor" }
26+
sc-network = { version = "0.10.0-dev", path = "../../../client/network" }
2627
sc-service = { version = "0.10.0-dev", path = "../../../client/service" }
2728
sc-telemetry = { version = "4.0.0-dev", path = "../../../client/telemetry" }
2829
sc-keystore = { version = "4.0.0-dev", path = "../../../client/keystore" }

bin/node-template/node/src/service.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ pub fn new_partial(
138138
}
139139

140140
/// Builds a new service for a full client.
141-
pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError> {
141+
pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
142142
let sc_service::PartialComponents {
143143
client,
144144
backend,
@@ -150,15 +150,16 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
150150
other: (block_import, grandpa_link, mut telemetry),
151151
} = new_partial(&config)?;
152152

153+
let mut net_config = sc_network::config::FullNetworkConfiguration::new(&config.network);
154+
153155
let grandpa_protocol_name = sc_consensus_grandpa::protocol_standard_name(
154156
&client.block_hash(0).ok().flatten().expect("Genesis block exists; qed"),
155157
&config.chain_spec,
156158
);
159+
net_config.add_notification_protocol(sc_consensus_grandpa::grandpa_peers_set_config(
160+
grandpa_protocol_name.clone(),
161+
));
157162

158-
config
159-
.network
160-
.extra_sets
161-
.push(sc_consensus_grandpa::grandpa_peers_set_config(grandpa_protocol_name.clone()));
162163
let warp_sync = Arc::new(sc_consensus_grandpa::warp_proof::NetworkProvider::new(
163164
backend.clone(),
164165
grandpa_link.shared_authority_set().clone(),
@@ -168,6 +169,7 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
168169
let (network, system_rpc_tx, tx_handler_controller, network_starter, sync_service) =
169170
sc_service::build_network(sc_service::BuildNetworkParams {
170171
config: &config,
172+
net_config,
171173
client: client.clone(),
172174
transaction_pool: transaction_pool.clone(),
173175
spawn_handle: task_manager.spawn_handle(),

bin/node/cli/src/service.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ pub struct NewFullBase {
316316

317317
/// Creates a full service from the configuration.
318318
pub fn new_full_base(
319-
mut config: Configuration,
319+
config: Configuration,
320320
disable_hardware_benchmarks: bool,
321321
with_startup_data: impl FnOnce(
322322
&sc_consensus_babe::BabeBlockImport<Block, FullClient, FullGrandpaBlockImport>,
@@ -343,10 +343,15 @@ pub fn new_full_base(
343343

344344
let shared_voter_state = rpc_setup;
345345
let auth_disc_publish_non_global_ips = config.network.allow_non_globals_in_dht;
346+
let mut net_config = sc_network::config::FullNetworkConfiguration::new(&config.network);
347+
346348
let grandpa_protocol_name = grandpa::protocol_standard_name(
347349
&client.block_hash(0).ok().flatten().expect("Genesis block exists; qed"),
348350
&config.chain_spec,
349351
);
352+
net_config.add_notification_protocol(grandpa::grandpa_peers_set_config(
353+
grandpa_protocol_name.clone(),
354+
));
350355

351356
let statement_handler_proto = sc_network_statement::StatementHandlerPrototype::new(
352357
client
@@ -356,12 +361,8 @@ pub fn new_full_base(
356361
.expect("Genesis block exists; qed"),
357362
config.chain_spec.fork_id(),
358363
);
359-
config.network.extra_sets.push(statement_handler_proto.set_config());
364+
net_config.add_notification_protocol(statement_handler_proto.set_config());
360365

361-
config
362-
.network
363-
.extra_sets
364-
.push(grandpa::grandpa_peers_set_config(grandpa_protocol_name.clone()));
365366
let warp_sync = Arc::new(grandpa::warp_proof::NetworkProvider::new(
366367
backend.clone(),
367368
import_setup.1.shared_authority_set().clone(),
@@ -371,6 +372,7 @@ pub fn new_full_base(
371372
let (network, system_rpc_tx, tx_handler_controller, network_starter, sync_service) =
372373
sc_service::build_network(sc_service::BuildNetworkParams {
373374
config: &config,
375+
net_config,
374376
client: client.clone(),
375377
transaction_pool: transaction_pool.clone(),
376378
spawn_handle: task_manager.spawn_handle(),

client/cli/src/params/network_params.rs

-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ impl NetworkParams {
221221
default_peers_set_num_full: self.in_peers + self.out_peers,
222222
listen_addresses,
223223
public_addresses,
224-
extra_sets: Vec::new(),
225-
request_response_protocols: Vec::new(),
226224
node_key,
227225
node_name: node_name.to_string(),
228226
client_version: client_id.to_string(),

client/consensus/beefy/src/communication/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub(crate) mod beefy_protocol_name {
6363
}
6464

6565
/// Returns the configuration value to put in
66-
/// [`sc_network::config::NetworkConfiguration::extra_sets`].
66+
/// [`sc_network::config::FullNetworkConfiguration`].
6767
/// For standard protocol name see [`beefy_protocol_name::gossip_protocol_name`].
6868
pub fn beefy_peers_set_config(
6969
gossip_protocol_name: sc_network::ProtocolName,

client/consensus/grandpa/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ pub struct GrandpaParams<Block: BlockT, C, N, S, SC, VR> {
691691
}
692692

693693
/// Returns the configuration value to put in
694-
/// [`sc_network::config::NetworkConfiguration::extra_sets`].
694+
/// [`sc_network::config::FullNetworkConfiguration`].
695695
/// For standard protocol name see [`crate::protocol_standard_name`].
696696
pub fn grandpa_peers_set_config(
697697
protocol_name: ProtocolName,

client/network/src/config.rs

+41-13
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ pub use crate::{
3232
use codec::Encode;
3333
use libp2p::{identity::Keypair, multiaddr, Multiaddr, PeerId};
3434
use prometheus_endpoint::Registry;
35+
use zeroize::Zeroize;
36+
3537
pub use sc_network_common::{
3638
role::{Role, Roles},
3739
sync::warp::WarpSyncProvider,
3840
ExHashT,
3941
};
4042
use sc_utils::mpsc::TracingUnboundedSender;
41-
use zeroize::Zeroize;
42-
4343
use sp_runtime::traits::Block as BlockT;
44+
4445
use std::{
4546
error::Error,
4647
fmt, fs,
@@ -564,9 +565,6 @@ pub struct NetworkConfiguration {
564565
/// The node key configuration, which determines the node's network identity keypair.
565566
pub node_key: NodeKeyConfig,
566567

567-
/// List of request-response protocols that the node supports.
568-
pub request_response_protocols: Vec<RequestResponseConfig>,
569-
570568
/// Configuration for the default set of nodes used for block syncing and transactions.
571569
pub default_peers_set: SetConfig,
572570

@@ -576,9 +574,6 @@ pub struct NetworkConfiguration {
576574
/// This value is implicitly capped to `default_set.out_peers + default_set.in_peers`.
577575
pub default_peers_set_num_full: u32,
578576

579-
/// Configuration for extra sets of nodes.
580-
pub extra_sets: Vec<NonDefaultSetConfig>,
581-
582577
/// Client identifier. Sent over the wire for debugging purposes.
583578
pub client_version: String,
584579

@@ -649,10 +644,8 @@ impl NetworkConfiguration {
649644
public_addresses: Vec::new(),
650645
boot_nodes: Vec::new(),
651646
node_key,
652-
request_response_protocols: Vec::new(),
653647
default_peers_set_num_full: default_peers_set.in_peers + default_peers_set.out_peers,
654648
default_peers_set,
655-
extra_sets: Vec::new(),
656649
client_version: client_version.into(),
657650
node_name: node_name.into(),
658651
transport: TransportConfig::Normal { enable_mdns: false, allow_private_ip: true },
@@ -707,7 +700,7 @@ pub struct Params<Block: BlockT> {
707700
pub executor: Box<dyn Fn(Pin<Box<dyn Future<Output = ()> + Send>>) + Send>,
708701

709702
/// Network layer configuration.
710-
pub network_config: NetworkConfiguration,
703+
pub network_config: FullNetworkConfiguration,
711704

712705
/// Legacy name of the protocol to use on the wire. Should be different for each chain.
713706
pub protocol_id: ProtocolId,
@@ -727,9 +720,44 @@ pub struct Params<Block: BlockT> {
727720

728721
/// TX channel for direct communication with `SyncingEngine` and `Protocol`.
729722
pub tx: TracingUnboundedSender<crate::event::SyncEvent<Block>>,
723+
}
724+
725+
/// Full network configuration.
726+
pub struct FullNetworkConfiguration {
727+
/// Installed notification protocols.
728+
pub(crate) notification_protocols: Vec<NonDefaultSetConfig>,
729+
730+
/// List of request-response protocols that the node supports.
731+
pub(crate) request_response_protocols: Vec<RequestResponseConfig>,
730732

731-
/// Request response protocol configurations
732-
pub request_response_protocol_configs: Vec<RequestResponseConfig>,
733+
/// Network configuration.
734+
pub network_config: NetworkConfiguration,
735+
}
736+
737+
impl FullNetworkConfiguration {
738+
/// Create new [`FullNetworkConfiguration`].
739+
pub fn new(network_config: &NetworkConfiguration) -> Self {
740+
Self {
741+
notification_protocols: Vec::new(),
742+
request_response_protocols: Vec::new(),
743+
network_config: network_config.clone(),
744+
}
745+
}
746+
747+
/// Add a notification protocol.
748+
pub fn add_notification_protocol(&mut self, config: NonDefaultSetConfig) {
749+
self.notification_protocols.push(config);
750+
}
751+
752+
/// Get reference to installed notification protocols.
753+
pub fn notification_protocols(&self) -> &Vec<NonDefaultSetConfig> {
754+
&self.notification_protocols
755+
}
756+
757+
/// Add a request-response protocol.
758+
pub fn add_request_response_protocol(&mut self, config: RequestResponseConfig) {
759+
self.request_response_protocols.push(config);
760+
}
733761
}
734762

735763
#[cfg(test)]

client/network/src/protocol.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,15 @@ impl<B: BlockT> Protocol<B> {
102102
pub fn new(
103103
roles: Roles,
104104
network_config: &config::NetworkConfiguration,
105+
notification_protocols: Vec<config::NonDefaultSetConfig>,
105106
block_announces_protocol: config::NonDefaultSetConfig,
106107
tx: TracingUnboundedSender<crate::event::SyncEvent<B>>,
107108
) -> error::Result<(Self, sc_peerset::PeersetHandle, Vec<(PeerId, Multiaddr)>)> {
108109
let mut known_addresses = Vec::new();
109110

110111
let (peerset, peerset_handle) = {
111112
let mut sets =
112-
Vec::with_capacity(NUM_HARDCODED_PEERSETS + network_config.extra_sets.len());
113+
Vec::with_capacity(NUM_HARDCODED_PEERSETS + notification_protocols.len());
113114

114115
let mut default_sets_reserved = HashSet::new();
115116
for reserved in network_config.default_peers_set.reserved_nodes.iter() {
@@ -135,7 +136,7 @@ impl<B: BlockT> Protocol<B> {
135136
NonReservedPeerMode::Deny,
136137
});
137138

138-
for set_cfg in &network_config.extra_sets {
139+
for set_cfg in &notification_protocols {
139140
let mut reserved_nodes = HashSet::new();
140141
for reserved in set_cfg.set_config.reserved_nodes.iter() {
141142
reserved_nodes.insert(reserved.peer_id);
@@ -169,7 +170,7 @@ impl<B: BlockT> Protocol<B> {
169170
handshake: block_announces_protocol.handshake.as_ref().unwrap().to_vec(),
170171
max_notification_size: block_announces_protocol.max_notification_size,
171172
})
172-
.chain(network_config.extra_sets.iter().map(|s| notifications::ProtocolConfig {
173+
.chain(notification_protocols.iter().map(|s| notifications::ProtocolConfig {
173174
name: s.notifications_protocol.clone(),
174175
fallback_names: s.fallback_names.clone(),
175176
handshake: s.handshake.as_ref().map_or(roles.encode(), |h| (*h).to_vec()),
@@ -182,7 +183,7 @@ impl<B: BlockT> Protocol<B> {
182183
peerset_handle: peerset_handle.clone(),
183184
behaviour,
184185
notification_protocols: iter::once(block_announces_protocol.notifications_protocol)
185-
.chain(network_config.extra_sets.iter().map(|s| s.notifications_protocol.clone()))
186+
.chain(notification_protocols.iter().map(|s| s.notifications_protocol.clone()))
186187
.collect(),
187188
bad_handshake_substreams: Default::default(),
188189
peers: HashMap::new(),

0 commit comments

Comments
 (0)