Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate litep2p into Polkadot SDK #2944

Merged
merged 59 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
70670ae
Introduce `sc-network-types` and `PeerId` for the crate
altonen Sep 25, 2023
265a674
Make Polkadot SDK generic over the networking implementation
altonen Sep 25, 2023
39a6428
Implement `litep2p` network backend for Polkadot SDK
altonen Sep 25, 2023
7472a60
Fix CI complaints
altonen Jan 18, 2024
38f0003
Apply review comments
altonen Jan 18, 2024
1ad2bdc
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
altonen Jan 22, 2024
e6fc19e
Fix clippy
altonen Jan 22, 2024
62aedb3
more clippy fixes
altonen Jan 22, 2024
39dc59c
bind to both ipv4 and ipv6
altonen Jan 22, 2024
1319e66
Apply suggestions from code review
altonen Jan 26, 2024
7d0aaee
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
altonen Jan 26, 2024
99a6705
Apply review comments
altonen Jan 26, 2024
b4cab2b
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
altonen Jan 29, 2024
0396c06
Add support for fallback request-responses
altonen Jan 29, 2024
f60faad
Apply review comments
altonen Feb 2, 2024
5f7c2a8
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
altonen Feb 2, 2024
e5bb679
fix
altonen Feb 5, 2024
2fec872
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
altonen Feb 5, 2024
b08c3d3
prdoc + workspace fix
altonen Feb 5, 2024
98379bd
fixes
altonen Feb 5, 2024
439e65e
Reimplement fallback request support
altonen Feb 8, 2024
cf2b6d5
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
altonen Feb 12, 2024
9ca02df
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
altonen Feb 14, 2024
669c428
updates
altonen Feb 27, 2024
9a68856
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
altonen Feb 27, 2024
b0e9567
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
altonen Mar 6, 2024
a0542ac
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
altonen Mar 7, 2024
7c3df5b
fixes
altonen Mar 9, 2024
83988d4
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
altonen Mar 9, 2024
cef84e6
fix metrics
altonen Mar 10, 2024
246ba63
fix prdoc
altonen Mar 10, 2024
bfbfaa0
fix url
altonen Mar 10, 2024
beae17e
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
altonen Mar 11, 2024
58722ba
fix Cargo.lock
altonen Mar 11, 2024
f212d54
Apply suggestions from code review
altonen Mar 12, 2024
33e030e
Apply suggestions from code review
dmitry-markin Mar 18, 2024
1812dc8
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
dmitry-markin Mar 18, 2024
4561871
rustfmt
dmitry-markin Mar 18, 2024
43cb163
Apply suggestion: `reserved_peer` -> `is_reserved_peer`
dmitry-markin Mar 21, 2024
d4d89c0
monor: rustfmt
dmitry-markin Mar 21, 2024
e4470e1
Upgrade litep2p identify implementation (#3785)
dmitry-markin Mar 22, 2024
62cc8b1
Merge branch 'master' into altonen-integrate-litep2p
dmitry-markin Mar 22, 2024
d88ba6d
Update `Cargo.lock`
dmitry-markin Mar 22, 2024
ddc98a2
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
lexnv Mar 27, 2024
188cf3f
node: Fix clippy
lexnv Mar 27, 2024
3496f29
bench: Remove unused imports
lexnv Mar 27, 2024
d475762
bench: Remove PeerId to fix clippy
lexnv Mar 27, 2024
73d5770
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
lexnv Apr 8, 2024
df191f4
cumulus: Merge conflict with network generic type
lexnv Apr 8, 2024
08b038c
Apply cargo fmt
lexnv Apr 8, 2024
0690577
Update prdoc/pr_2944.prdoc
lexnv Apr 8, 2024
0735488
beefy/tests: Impl missing method
lexnv Apr 8, 2024
a00b965
beefy/tests: Add #[async_trait::async_trait]
lexnv Apr 8, 2024
612cd7b
Fix clippy
lexnv Apr 8, 2024
25be974
tests: Apply rustfmt
lexnv Apr 8, 2024
02e072a
Remove unused import
lexnv Apr 8, 2024
5b9a70f
prdoc: Fix PR doc
lexnv Apr 8, 2024
f5d028a
Merge remote-tracking branch 'origin/master' into altonen-integrate-l…
lexnv Apr 8, 2024
656ac74
Remove unused bitswap (merge conflict)
lexnv Apr 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
615 changes: 490 additions & 125 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ members = [
"substrate/client/mixnet",
"substrate/client/network",
"substrate/client/network-gossip",
"substrate/client/network/bitswap",
"substrate/client/network/common",
"substrate/client/network/light",
"substrate/client/network/statement",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ use sc_network::{NetworkStateInfo, NotificationService};
use sc_service::TaskManager;
use sc_utils::mpsc::tracing_unbounded;

use cumulus_primitives_core::relay_chain::{Block, Hash as PHash};
use cumulus_relay_chain_interface::RelayChainError;

use crate::BlockChainRpcClient;
Expand All @@ -57,7 +56,7 @@ pub(crate) struct CollatorOverseerGenArgs<'a> {
/// Runtime client generic, providing the `ProvieRuntimeApi` trait besides others.
pub runtime_client: Arc<BlockChainRpcClient>,
/// Underlying network service implementation.
pub network_service: Arc<sc_network::NetworkService<Block, PHash>>,
pub network_service: Arc<dyn sc_network::service::traits::NetworkService>,
/// Syncing oracle.
pub sync_oracle: Box<dyn sp_consensus::SyncOracle + Send>,
/// Underlying authority discovery service.
Expand Down
82 changes: 56 additions & 26 deletions cumulus/client/relay-chain-minimal-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@ use polkadot_node_network_protocol::{
},
};

use polkadot_core_primitives::{Block as RelayBlock, Hash as RelayHash};
use polkadot_node_subsystem_util::metrics::prometheus::Registry;
use polkadot_primitives::CollatorPair;

use sc_authority_discovery::Service as AuthorityDiscoveryService;
use sc_network::{config::FullNetworkConfiguration, Event, NetworkEventStream, NetworkService};
use sc_network::{
config::FullNetworkConfiguration, service::traits::NetworkService, Event, NetworkBackend,
NetworkEventStream,
};
use sc_service::{config::PrometheusConfig, Configuration, TaskManager};
use sp_runtime::{app_crypto::Pair, traits::Block as BlockT};

Expand All @@ -50,7 +54,7 @@ fn build_authority_discovery_service<Block: BlockT>(
task_manager: &TaskManager,
client: Arc<BlockChainRpcClient>,
config: &Configuration,
network: Arc<NetworkService<Block, <Block as BlockT>::Hash>>,
network: Arc<dyn NetworkService>,
prometheus_registry: Option<Registry>,
) -> AuthorityDiscoveryService {
let auth_disc_publish_non_global_ips = config.network.allow_non_globals_in_dht;
Expand All @@ -69,7 +73,7 @@ fn build_authority_discovery_service<Block: BlockT>(
..Default::default()
},
client,
network,
Arc::new(network.clone()),
Box::pin(dht_event_stream),
authority_discovery_role,
prometheus_registry,
Expand All @@ -89,12 +93,22 @@ async fn build_interface(
client: RelayChainRpcClient,
) -> RelayChainResult<(Arc<(dyn RelayChainInterface + 'static)>, Option<CollatorPair>)> {
let collator_pair = CollatorPair::generate().0;
let collator_node = new_minimal_relay_chain(
polkadot_config,
collator_pair.clone(),
Arc::new(BlockChainRpcClient::new(client.clone())),
)
.await?;
let collator_node = match polkadot_config.network.network_backend {
sc_network::config::NetworkBackendType::Libp2p =>
new_minimal_relay_chain::<RelayBlock, sc_network::NetworkWorker<RelayBlock, RelayHash>>(
polkadot_config,
collator_pair.clone(),
Arc::new(BlockChainRpcClient::new(client.clone())),
)
.await?,
sc_network::config::NetworkBackendType::Litep2p =>
new_minimal_relay_chain::<RelayBlock, sc_network::Litep2pNetworkBackend>(
polkadot_config,
collator_pair.clone(),
Arc::new(BlockChainRpcClient::new(client.clone())),
)
.await?,
};
task_manager.add_child(collator_node.task_manager);
Ok((
Arc::new(RelayChainRpcInterface::new(client, collator_node.overseer_handle)),
Expand Down Expand Up @@ -140,6 +154,7 @@ pub async fn build_minimal_relay_chain_node_light_client(

build_interface(polkadot_config, task_manager, client).await
}

/// Builds a minimal relay chain node. Chain data is fetched
/// via [`BlockChainRpcClient`] and fed into the overseer and its subsystems.
///
Expand All @@ -152,13 +167,18 @@ pub async fn build_minimal_relay_chain_node_light_client(
/// - NetworkBridgeTx
/// - RuntimeApi
#[sc_tracing::logging::prefix_logs_with("Relaychain")]
async fn new_minimal_relay_chain(
async fn new_minimal_relay_chain<Block: BlockT, Network: NetworkBackend<RelayBlock, RelayHash>>(
config: Configuration,
collator_pair: CollatorPair,
relay_chain_rpc_client: Arc<BlockChainRpcClient>,
) -> Result<NewMinimalNode, RelayChainError> {
let role = config.role.clone();
let mut net_config = sc_network::config::FullNetworkConfiguration::new(&config.network);
let mut net_config =
sc_network::config::FullNetworkConfiguration::<_, _, Network>::new(&config.network);
let metrics = Network::register_notification_metrics(
config.prometheus_config.as_ref().map(|cfg| &cfg.registry),
);
let peer_store_handle = net_config.peer_store_handle();

let prometheus_registry = config.prometheus_registry();
let task_manager = TaskManager::new(config.tokio_handle.clone(), prometheus_registry)?;
Expand All @@ -175,13 +195,18 @@ async fn new_minimal_relay_chain(
let peer_set_protocol_names =
PeerSetProtocolNames::new(genesis_hash, config.chain_spec.fork_id());
let is_authority = if role.is_authority() { IsAuthority::Yes } else { IsAuthority::No };
let notification_services = peer_sets_info(is_authority, &peer_set_protocol_names)
.into_iter()
.map(|(config, (peerset, service))| {
net_config.add_notification_protocol(config);
(peerset, service)
})
.collect::<std::collections::HashMap<PeerSet, Box<dyn sc_network::NotificationService>>>();
let notification_services = peer_sets_info::<_, Network>(
is_authority,
&peer_set_protocol_names,
metrics.clone(),
Arc::clone(&peer_store_handle),
)
.into_iter()
.map(|(config, (peerset, service))| {
net_config.add_notification_protocol(config);
(peerset, service)
})
.collect::<std::collections::HashMap<PeerSet, Box<dyn sc_network::NotificationService>>>();

let request_protocol_names = ReqProtocolNames::new(genesis_hash, config.chain_spec.fork_id());
let (collation_req_receiver_v1, collation_req_receiver_v2, available_data_req_receiver) =
Expand All @@ -191,16 +216,17 @@ async fn new_minimal_relay_chain(
.chain_get_header(None)
.await?
.ok_or_else(|| RelayChainError::RpcCallError("Unable to fetch best header".to_string()))?;
let (network, network_starter, sync_oracle) = build_collator_network(
let (network, network_starter, sync_oracle) = build_collator_network::<Network>(
&config,
net_config,
task_manager.spawn_handle(),
genesis_hash,
best_header,
metrics,
)
.map_err(|e| RelayChainError::Application(Box::new(e) as Box<_>))?;

let authority_discovery_service = build_authority_discovery_service(
let authority_discovery_service = build_authority_discovery_service::<Block>(
&task_manager,
relay_chain_rpc_client.clone(),
&config,
Expand Down Expand Up @@ -232,24 +258,28 @@ async fn new_minimal_relay_chain(
Ok(NewMinimalNode { task_manager, overseer_handle })
}

fn build_request_response_protocol_receivers(
fn build_request_response_protocol_receivers<
Block: BlockT,
Network: NetworkBackend<Block, <Block as BlockT>::Hash>,
>(
request_protocol_names: &ReqProtocolNames,
config: &mut FullNetworkConfiguration,
config: &mut FullNetworkConfiguration<Block, <Block as BlockT>::Hash, Network>,
) -> (
IncomingRequestReceiver<v1::CollationFetchingRequest>,
IncomingRequestReceiver<v2::CollationFetchingRequest>,
IncomingRequestReceiver<v1::AvailableDataFetchingRequest>,
) {
let (collation_req_receiver_v1, cfg) =
IncomingRequest::get_config_receiver(request_protocol_names);
IncomingRequest::get_config_receiver::<_, Network>(request_protocol_names);
config.add_request_response_protocol(cfg);
let (collation_req_receiver_v2, cfg) =
IncomingRequest::get_config_receiver(request_protocol_names);
IncomingRequest::get_config_receiver::<_, Network>(request_protocol_names);
config.add_request_response_protocol(cfg);
let (available_data_req_receiver, cfg) =
IncomingRequest::get_config_receiver(request_protocol_names);
IncomingRequest::get_config_receiver::<_, Network>(request_protocol_names);
config.add_request_response_protocol(cfg);
let cfg = Protocol::ChunkFetchingV1.get_outbound_only_config(request_protocol_names);
let cfg =
Protocol::ChunkFetchingV1.get_outbound_only_config::<_, Network>(request_protocol_names);
config.add_request_response_protocol(cfg);
(collation_req_receiver_v1, collation_req_receiver_v2, available_data_req_receiver)
}
66 changes: 33 additions & 33 deletions cumulus/client/relay-chain-minimal-node/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,56 @@
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

use polkadot_core_primitives::{Block, Hash, Header};
use sp_runtime::traits::{Block as BlockT, NumberFor};
use sp_runtime::traits::NumberFor;

use sc_network::{
config::{
NetworkConfiguration, NonDefaultSetConfig, NonReservedPeerMode, NotificationHandshake,
ProtocolId, SetConfig,
NetworkConfiguration, NonReservedPeerMode, NotificationHandshake, PeerStore, ProtocolId,
SetConfig,
},
peer_store::PeerStore,
NetworkService,
peer_store::PeerStoreProvider,
service::traits::NetworkService,
NotificationMetrics,
};

use sc_network::{config::FullNetworkConfiguration, NotificationService};
use sc_network::{config::FullNetworkConfiguration, NetworkBackend, NotificationService};
use sc_network_common::{role::Roles, sync::message::BlockAnnouncesHandshake};
use sc_service::{error::Error, Configuration, NetworkStarter, SpawnTaskHandle};

use std::{iter, sync::Arc};

/// Build the network service, the network status sinks and an RPC sender.
pub(crate) fn build_collator_network(
pub(crate) fn build_collator_network<Network: NetworkBackend<Block, Hash>>(
config: &Configuration,
mut full_network_config: FullNetworkConfiguration,
mut network_config: FullNetworkConfiguration<Block, Hash, Network>,
spawn_handle: SpawnTaskHandle,
genesis_hash: Hash,
best_header: Header,
metrics: NotificationMetrics,
) -> Result<
(Arc<NetworkService<Block, Hash>>, NetworkStarter, Box<dyn sp_consensus::SyncOracle + Send>),
(Arc<dyn NetworkService>, NetworkStarter, Box<dyn sp_consensus::SyncOracle + Send>),
Error,
> {
let protocol_id = config.protocol_id();
let (block_announce_config, _notification_service) = get_block_announce_proto_config::<Block>(
let (block_announce_config, _notification_service) = get_block_announce_proto_config::<Network>(
protocol_id.clone(),
&None,
Roles::from(&config.role),
best_header.number,
best_header.hash(),
genesis_hash,
metrics,
network_config.peer_store_handle(),
);

// Since this node has no syncing, we do not want light-clients to connect to it.
// Here we set any potential light-client slots to 0.
adjust_network_config_light_in_peers(&mut full_network_config.network_config);

let peer_store = PeerStore::new(
full_network_config
.network_config
.boot_nodes
.iter()
.map(|bootnode| bootnode.peer_id)
.collect(),
);
let peer_store_handle = peer_store.handle();
adjust_network_config_light_in_peers(&mut network_config.network_config);

let peer_store = network_config.take_peer_store();
spawn_handle.spawn("peer-store", Some("networking"), peer_store.run());

let network_params = sc_network::config::Params::<Block> {
let network_params = sc_network::config::Params::<Block, Hash, Network> {
role: config.role.clone(),
executor: {
let spawn_handle = Clone::clone(&spawn_handle);
Expand All @@ -77,16 +73,16 @@ pub(crate) fn build_collator_network(
})
},
fork_id: None,
network_config: full_network_config,
peer_store: peer_store_handle,
network_config,
genesis_hash,
protocol_id,
metrics_registry: config.prometheus_config.as_ref().map(|config| config.registry.clone()),
block_announce_config,
bitswap_config: None,
};

let network_worker = sc_network::NetworkWorker::new(network_params)?;
let network_service = network_worker.service().clone();
let network_worker = Network::new(network_params)?;
let network_service = network_worker.network_service();

let (network_start_tx, network_start_rx) = futures::channel::oneshot::channel();

Expand Down Expand Up @@ -139,14 +135,16 @@ impl sp_consensus::SyncOracle for SyncOracle {
}
}

fn get_block_announce_proto_config<B: BlockT>(
fn get_block_announce_proto_config<Network: NetworkBackend<Block, Hash>>(
protocol_id: ProtocolId,
fork_id: &Option<String>,
roles: Roles,
best_number: NumberFor<B>,
best_hash: B::Hash,
genesis_hash: B::Hash,
) -> (NonDefaultSetConfig, Box<dyn NotificationService>) {
best_number: NumberFor<Block>,
best_hash: Hash,
genesis_hash: Hash,
metrics: NotificationMetrics,
peer_store_handle: Arc<dyn PeerStoreProvider>,
) -> (Network::NotificationProtocolConfig, Box<dyn NotificationService>) {
let block_announces_protocol = {
let genesis_hash = genesis_hash.as_ref();
if let Some(ref fork_id) = fork_id {
Expand All @@ -156,11 +154,11 @@ fn get_block_announce_proto_config<B: BlockT>(
}
};

NonDefaultSetConfig::new(
Network::notification_config(
block_announces_protocol.into(),
iter::once(format!("/{}/block-announces/1", protocol_id.as_ref()).into()).collect(),
1024 * 1024,
Some(NotificationHandshake::new(BlockAnnouncesHandshake::<B>::build(
Some(NotificationHandshake::new(BlockAnnouncesHandshake::<Block>::build(
roles,
best_number,
best_hash,
Expand All @@ -174,5 +172,7 @@ fn get_block_announce_proto_config<B: BlockT>(
reserved_nodes: Vec::new(),
non_reserved_mode: NonReservedPeerMode::Deny,
},
metrics,
peer_store_handle,
)
}
Loading
Loading