Integrate litep2p into Polkadot SDK (#2944)

[litep2p](https://github.com/altonen/litep2p) is a libp2p-compatible P2P
networking library. It supports all of the features of `rust-libp2p`
that are currently being utilized by Polkadot SDK.

Compared to `rust-libp2p`, `litep2p` has a quite different architecture
which is why the new `litep2p` network backend is only able to use a
little of the existing code in `sc-network`. The design has been mainly
influenced by how we'd wish to structure our networking-related code in
Polkadot SDK: independent higher-levels protocols directly communicating
with the network over links that support bidirectional backpressure. A
good example would be `NotificationHandle`/`RequestResponseHandle`
abstractions which allow, e.g., `SyncingEngine` to directly communicate
with peers to announce/request blocks.

I've tried running `polkadot --network-backend litep2p` with a few
different peer configurations and there is a noticeable reduction in
networking CPU usage. For high load (`--out-peers 200`), networking CPU
usage goes down from ~110% to ~30% (80 pp) and for normal load
(`--out-peers 40`), the usage goes down from ~55% to ~18% (37 pp).

These should not be taken as final numbers because:

a) there are still some low-hanging optimization fruits, such as
enabling [receive window
auto-tuning](https://github.com/libp2p/rust-yamux/pull/176), integrating
`Peerset` more closely with `litep2p` or improving memory usage of the
WebSocket transport
b) fixing bugs/instabilities that incorrectly cause `litep2p` to do less
work will increase the networking CPU usage
c) verification in a more diverse set of tests/conditions is needed

Nevertheless, these numbers should give an early estimate for CPU usage
of the new networking backend.

This PR consists of three separate changes:
* introduce a generic `PeerId` (wrapper around `Multihash`) so that we
don't have use `NetworkService::PeerId` in every part of the code that
uses a `PeerId`
* introduce `NetworkBackend` trait, implement it for the libp2p network
stack and make Polkadot SDK generic over `NetworkBackend`
  * implement `NetworkBackend` for litep2p

The new library should be considered experimental which is why
`rust-libp2p` will remain as the default option for the time being. This
PR currently depends on the master branch of `litep2p` but I'll cut a
new release for the library once all review comments have been
addresses.

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: Dmitry Markin <dmitry@markin.tech>
Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com>
Co-authored-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Aaro Altonen
2024-04-08 19:44:13 +03:00
committed by GitHub
parent 9543d31474
commit 80616f6d03
181 changed files with 11055 additions and 1862 deletions
@@ -104,8 +104,13 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
wasm_runtime_overrides: None,
};
node_cli::service::new_full_base(config, None, false, |_, _| ())
.expect("creating a full node doesn't fail")
node_cli::service::new_full_base::<sc_network::NetworkWorker<_, _>>(
config,
None,
false,
|_, _| (),
)
.expect("creating a full node doesn't fail")
}
fn extrinsic_set_time(now: u64) -> OpaqueExtrinsic {
@@ -101,7 +101,13 @@ fn new_node(tokio_handle: Handle) -> node_cli::service::NewFullBase {
};
tokio_handle.block_on(async move {
node_cli::service::new_full_base(config, None, false, |_, _| ()).expect("Creates node")
node_cli::service::new_full_base::<sc_network::NetworkWorker<_, _>>(
config,
None,
false,
|_, _| (),
)
.expect("Creates node")
})
}
+1 -1
View File
@@ -513,7 +513,7 @@ pub(crate) mod tests {
sc_service_test::connectivity(integration_test_config_with_two_authorities(), |config| {
let NewFullBase { task_manager, client, network, sync, transaction_pool, .. } =
new_full_base(config, None, false, |_, _| ())?;
new_full_base::<sc_network::NetworkWorker<_, _>>(config, None, false, |_, _| ())?;
Ok(sc_service_test::TestNetComponents::new(
task_manager,
client,
+67 -17
View File
@@ -29,7 +29,9 @@ use kitchensink_runtime::RuntimeApi;
use node_primitives::Block;
use sc_client_api::{Backend, BlockBackend};
use sc_consensus_babe::{self, SlotProportion};
use sc_network::{event::Event, NetworkEventStream, NetworkService};
use sc_network::{
event::Event, service::traits::NetworkService, NetworkBackend, NetworkEventStream,
};
use sc_network_sync::{strategy::warp::WarpSyncParams, SyncingService};
use sc_service::{config::Configuration, error::Error as ServiceError, RpcHandlers, TaskManager};
use sc_statement_store::Store as StatementStore;
@@ -368,7 +370,7 @@ pub struct NewFullBase {
/// The client instance of the node.
pub client: Arc<FullClient>,
/// The networking service of the node.
pub network: Arc<NetworkService<Block, <Block as BlockT>::Hash>>,
pub network: Arc<dyn NetworkService>,
/// The syncing service of the node.
pub sync: Arc<SyncingService<Block>>,
/// The transaction pool of the node.
@@ -378,7 +380,7 @@ pub struct NewFullBase {
}
/// Creates a full service from the configuration.
pub fn new_full_base(
pub fn new_full_base<N: NetworkBackend<Block, <Block as BlockT>::Hash>>(
config: Configuration,
mixnet_config: Option<sc_mixnet::Config>,
disable_hardware_benchmarks: bool,
@@ -420,15 +422,26 @@ pub fn new_full_base(
(rpc_builder, import_setup, rpc_setup, mut telemetry, statement_store, mixnet_api_backend),
} = new_partial(&config, mixnet_config.as_ref())?;
let metrics = N::register_notification_metrics(
config.prometheus_config.as_ref().map(|cfg| &cfg.registry),
);
let shared_voter_state = rpc_setup;
let auth_disc_publish_non_global_ips = config.network.allow_non_globals_in_dht;
let auth_disc_public_addresses = config.network.public_addresses.clone();
let mut net_config = sc_network::config::FullNetworkConfiguration::new(&config.network);
let mut net_config =
sc_network::config::FullNetworkConfiguration::<_, _, N>::new(&config.network);
let genesis_hash = client.block_hash(0).ok().flatten().expect("Genesis block exists; qed");
let peer_store_handle = net_config.peer_store_handle();
let grandpa_protocol_name = grandpa::protocol_standard_name(&genesis_hash, &config.chain_spec);
let (grandpa_protocol_config, grandpa_notification_service) =
grandpa::grandpa_peers_set_config(grandpa_protocol_name.clone());
grandpa::grandpa_peers_set_config::<_, N>(
grandpa_protocol_name.clone(),
metrics.clone(),
Arc::clone(&peer_store_handle),
);
net_config.add_notification_protocol(grandpa_protocol_config);
let beefy_gossip_proto_name =
@@ -436,7 +449,7 @@ pub fn new_full_base(
// `beefy_on_demand_justifications_handler` is given to `beefy-gadget` task to be run,
// while `beefy_req_resp_cfg` is added to `config.network.request_response_protocols`.
let (beefy_on_demand_justifications_handler, beefy_req_resp_cfg) =
beefy::communication::request_response::BeefyJustifsRequestHandler::new(
beefy::communication::request_response::BeefyJustifsRequestHandler::new::<_, N>(
&genesis_hash,
config.chain_spec.fork_id(),
client.clone(),
@@ -444,23 +457,33 @@ pub fn new_full_base(
);
let (beefy_notification_config, beefy_notification_service) =
beefy::communication::beefy_peers_set_config(beefy_gossip_proto_name.clone());
beefy::communication::beefy_peers_set_config::<_, N>(
beefy_gossip_proto_name.clone(),
metrics.clone(),
Arc::clone(&peer_store_handle),
);
net_config.add_notification_protocol(beefy_notification_config);
net_config.add_request_response_protocol(beefy_req_resp_cfg);
let (statement_handler_proto, statement_config) =
sc_network_statement::StatementHandlerPrototype::new(
sc_network_statement::StatementHandlerPrototype::new::<_, _, N>(
genesis_hash,
config.chain_spec.fork_id(),
metrics.clone(),
Arc::clone(&peer_store_handle),
);
net_config.add_notification_protocol(statement_config);
let mixnet_protocol_name =
sc_mixnet::protocol_name(genesis_hash.as_ref(), config.chain_spec.fork_id());
let mixnet_notification_service = mixnet_config.as_ref().map(|mixnet_config| {
let (config, notification_service) =
sc_mixnet::peers_set_config(mixnet_protocol_name.clone(), mixnet_config);
let (config, notification_service) = sc_mixnet::peers_set_config::<_, N>(
mixnet_protocol_name.clone(),
mixnet_config,
metrics.clone(),
Arc::clone(&peer_store_handle),
);
net_config.add_notification_protocol(config);
notification_service
});
@@ -482,6 +505,7 @@ pub fn new_full_base(
block_announce_validator_builder: None,
warp_sync_params: Some(WarpSyncParams::WithProvider(warp_sync)),
block_relay: None,
metrics,
})?;
if let Some(mixnet_config) = mixnet_config {
@@ -615,7 +639,7 @@ pub fn new_full_base(
..Default::default()
},
client.clone(),
network.clone(),
Arc::new(network.clone()),
Box::pin(dht_event_stream),
authority_discovery_role,
prometheus_registry.clone(),
@@ -634,7 +658,7 @@ pub fn new_full_base(
// beefy is enabled if its notification service exists
let network_params = beefy::BeefyNetworkParams {
network: network.clone(),
network: Arc::new(network.clone()),
sync: sync_service.clone(),
gossip_protocol_name: beefy_gossip_proto_name,
justifications_protocol_name: beefy_on_demand_justifications_handler.protocol_name(),
@@ -746,7 +770,7 @@ pub fn new_full_base(
transaction_pool: Some(OffchainTransactionPoolFactory::new(
transaction_pool.clone(),
)),
network_provider: network.clone(),
network_provider: Arc::new(network.clone()),
is_validator: role.is_authority(),
enable_http_requests: true,
custom_extensions: move |_| {
@@ -773,8 +797,29 @@ pub fn new_full_base(
pub fn new_full(config: Configuration, cli: Cli) -> Result<TaskManager, ServiceError> {
let mixnet_config = cli.mixnet_params.config(config.role.is_authority());
let database_path = config.database.path().map(Path::to_path_buf);
let task_manager = new_full_base(config, mixnet_config, cli.no_hardware_benchmarks, |_, _| ())
.map(|NewFullBase { task_manager, .. }| task_manager)?;
let task_manager = match config.network.network_backend {
sc_network::config::NetworkBackendType::Libp2p => {
let task_manager = new_full_base::<sc_network::NetworkWorker<_, _>>(
config,
mixnet_config,
cli.no_hardware_benchmarks,
|_, _| (),
)
.map(|NewFullBase { task_manager, .. }| task_manager)?;
task_manager
},
sc_network::config::NetworkBackendType::Litep2p => {
let task_manager = new_full_base::<sc_network::Litep2pNetworkBackend>(
config,
mixnet_config,
cli.no_hardware_benchmarks,
|_, _| (),
)
.map(|NewFullBase { task_manager, .. }| task_manager)?;
task_manager
},
};
if let Some(database_path) = database_path {
sc_storage_monitor::StorageMonitorService::try_spawn(
@@ -851,7 +896,7 @@ mod tests {
|config| {
let mut setup_handles = None;
let NewFullBase { task_manager, client, network, sync, transaction_pool, .. } =
new_full_base(
new_full_base::<sc_network::NetworkWorker<_, _>>(
config,
None,
false,
@@ -1029,7 +1074,12 @@ mod tests {
crate::chain_spec::tests::integration_test_config_with_two_authorities(),
|config| {
let NewFullBase { task_manager, client, network, sync, transaction_pool, .. } =
new_full_base(config, None, false, |_, _| ())?;
new_full_base::<sc_network::NetworkWorker<_, _>>(
config,
None,
false,
|_, _| (),
)?;
Ok(sc_service_test::TestNetComponents::new(
task_manager,
client,