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
@@ -28,6 +28,7 @@ sc-consensus = { path = "../common" }
sc-network = { path = "../../network" }
sc-network-gossip = { path = "../../network-gossip" }
sc-network-sync = { path = "../../network/sync" }
sc-network-types = { path = "../../network/types" }
sc-utils = { path = "../../utils" }
sp-api = { path = "../../../primitives/api" }
sp-application-crypto = { path = "../../../primitives/application-crypto" }
@@ -18,8 +18,9 @@
use std::{collections::BTreeSet, sync::Arc, time::Duration};
use sc_network::{NetworkPeers, PeerId, ReputationChange};
use sc_network::{NetworkPeers, ReputationChange};
use sc_network_gossip::{MessageIntent, ValidationResult, Validator, ValidatorContext};
use sc_network_types::PeerId;
use sp_runtime::traits::{Block, Hash, Header, NumberFor};
use codec::{Decode, DecodeAll, Encode};
@@ -506,6 +507,7 @@ pub(crate) mod tests {
}
}
#[async_trait::async_trait]
impl NetworkPeers for TestNetwork {
fn set_authorized_peers(&self, _: std::collections::HashSet<PeerId>) {
unimplemented!()
@@ -581,6 +583,10 @@ pub(crate) mod tests {
fn peer_role(&self, _: PeerId, _: Vec<u8>) -> Option<sc_network::ObservedRole> {
unimplemented!()
}
async fn reserved_peers(&self) -> Result<Vec<PeerId>, ()> {
unimplemented!();
}
}
struct TestContext;
@@ -591,11 +597,11 @@ pub(crate) mod tests {
fn broadcast_message(&mut self, _topic: B::Hash, _message: Vec<u8>, _force: bool) {}
fn send_message(&mut self, _who: &sc_network::PeerId, _message: Vec<u8>) {
fn send_message(&mut self, _who: &sc_network_types::PeerId, _message: Vec<u8>) {
unimplemented!()
}
fn send_topic(&mut self, _who: &sc_network::PeerId, _topic: B::Hash, _force: bool) {
fn send_topic(&mut self, _who: &sc_network_types::PeerId, _topic: B::Hash, _force: bool) {
unimplemented!()
}
}
@@ -772,7 +778,7 @@ pub(crate) mod tests {
Arc::new(TestNetwork::new().0),
);
gv.update_filter(GossipFilterCfg { start: 0, end: 10, validator_set: &validator_set });
let sender = sc_network::PeerId::random();
let sender = sc_network_types::PeerId::random();
let topic = Default::default();
let intent = MessageIntent::Broadcast;
@@ -852,7 +858,7 @@ pub(crate) mod tests {
Arc::new(TestNetwork::new().0),
);
gv.update_filter(GossipFilterCfg { start: 0, end: 10, validator_set: &validator_set });
let sender = sc_network::PeerId::random();
let sender = sc_network_types::PeerId::random();
let topic = Default::default();
let vote = dummy_vote(1);
@@ -65,17 +65,28 @@ pub(crate) mod beefy_protocol_name {
/// Returns the configuration value to put in
/// [`sc_network::config::FullNetworkConfiguration`].
/// For standard protocol name see [`beefy_protocol_name::gossip_protocol_name`].
pub fn beefy_peers_set_config(
pub fn beefy_peers_set_config<
B: sp_runtime::traits::Block,
N: sc_network::NetworkBackend<B, <B as sp_runtime::traits::Block>::Hash>,
>(
gossip_protocol_name: sc_network::ProtocolName,
) -> (sc_network::config::NonDefaultSetConfig, Box<dyn sc_network::NotificationService>) {
let (mut cfg, notification_service) = sc_network::config::NonDefaultSetConfig::new(
metrics: sc_network::service::NotificationMetrics,
peer_store_handle: std::sync::Arc<dyn sc_network::peer_store::PeerStoreProvider>,
) -> (N::NotificationProtocolConfig, Box<dyn sc_network::NotificationService>) {
let (cfg, notification_service) = N::notification_config(
gossip_protocol_name,
Vec::new(),
1024 * 1024,
None,
Default::default(),
sc_network::config::SetConfig {
in_peers: 25,
out_peers: 25,
reserved_nodes: Vec::new(),
non_reserved_mode: sc_network::config::NonReservedPeerMode::Accept,
},
metrics,
peer_store_handle,
);
cfg.allow_non_reserved(25, 25);
(cfg, notification_service)
}
@@ -18,7 +18,8 @@
//! Logic for keeping track of BEEFY peers.
use sc_network::{PeerId, ReputationChange};
use sc_network::ReputationChange;
use sc_network_types::PeerId;
use sp_runtime::traits::{Block, NumberFor, Zero};
use std::collections::{HashMap, VecDeque};
@@ -21,9 +21,10 @@ use futures::{channel::oneshot, StreamExt};
use log::{debug, trace};
use sc_client_api::BlockBackend;
use sc_network::{
config as netconfig, config::RequestResponseConfig, types::ProtocolName, PeerId,
ReputationChange,
config as netconfig, service::traits::RequestResponseConfig, types::ProtocolName,
NetworkBackend, ReputationChange,
};
use sc_network_types::PeerId;
use sp_consensus_beefy::BEEFY_ENGINE_ID;
use sp_runtime::traits::Block;
use std::{marker::PhantomData, sync::Arc};
@@ -139,15 +140,15 @@ where
Client: BlockBackend<B> + Send + Sync,
{
/// Create a new [`BeefyJustifsRequestHandler`].
pub fn new<Hash: AsRef<[u8]>>(
pub fn new<Hash: AsRef<[u8]>, Network: NetworkBackend<B, <B as Block>::Hash>>(
genesis_hash: Hash,
fork_id: Option<&str>,
client: Arc<Client>,
prometheus_registry: Option<prometheus::Registry>,
) -> (Self, RequestResponseConfig) {
let (request_receiver, config) =
on_demand_justifications_protocol_config(genesis_hash, fork_id);
let justif_protocol_name = config.name.clone();
) -> (Self, Network::RequestResponseProtocolConfig) {
let (request_receiver, config): (_, Network::RequestResponseProtocolConfig) =
on_demand_justifications_protocol_config::<_, _, Network>(genesis_hash, fork_id);
let justif_protocol_name = config.protocol_name().clone();
let metrics = register_metrics(prometheus_registry);
(
Self { request_receiver, justif_protocol_name, client, metrics, _block: PhantomData },
@@ -26,7 +26,8 @@ pub use incoming_requests_handler::BeefyJustifsRequestHandler;
use std::time::Duration;
use codec::{Decode, Encode, Error as CodecError};
use sc_network::{config::RequestResponseConfig, PeerId};
use sc_network::NetworkBackend;
use sc_network_types::PeerId;
use sp_runtime::traits::{Block, NumberFor};
use crate::communication::{beefy_protocol_name::justifications_protocol_name, peers::PeerReport};
@@ -47,23 +48,27 @@ const BEEFY_SYNC_LOG_TARGET: &str = "beefy::sync";
/// `ProtocolConfig`.
///
/// Consider using [`BeefyJustifsRequestHandler`] instead of this low-level function.
pub(crate) fn on_demand_justifications_protocol_config<Hash: AsRef<[u8]>>(
pub(crate) fn on_demand_justifications_protocol_config<
Hash: AsRef<[u8]>,
B: Block,
Network: NetworkBackend<B, <B as Block>::Hash>,
>(
genesis_hash: Hash,
fork_id: Option<&str>,
) -> (IncomingRequestReceiver, RequestResponseConfig) {
) -> (IncomingRequestReceiver, Network::RequestResponseProtocolConfig) {
let name = justifications_protocol_name(genesis_hash, fork_id);
let fallback_names = vec![];
let (tx, rx) = async_channel::bounded(JUSTIF_CHANNEL_SIZE);
let rx = IncomingRequestReceiver::new(rx);
let cfg = RequestResponseConfig {
let cfg = Network::request_response_config(
name,
fallback_names,
max_request_size: 32,
max_response_size: MAX_RESPONSE_SIZE,
32,
MAX_RESPONSE_SIZE,
// We are connected to all validators:
request_timeout: JUSTIF_REQUEST_TIMEOUT,
inbound_queue: Some(tx),
};
JUSTIF_REQUEST_TIMEOUT,
Some(tx),
);
(rx, cfg)
}
@@ -24,8 +24,9 @@ use log::{debug, warn};
use parking_lot::Mutex;
use sc_network::{
request_responses::{IfDisconnected, RequestFailure},
NetworkRequest, PeerId, ProtocolName,
NetworkRequest, ProtocolName,
};
use sc_network_types::PeerId;
use sp_consensus_beefy::{ecdsa_crypto::AuthorityId, ValidatorSet};
use sp_runtime::traits::{Block, NumberFor};
use std::{collections::VecDeque, result::Result, sync::Arc};
@@ -125,7 +125,11 @@ impl BeefyTestNet {
let mut net = BeefyTestNet { peers: Vec::with_capacity(n_authority), beefy_genesis };
for i in 0..n_authority {
let (rx, cfg) = on_demand_justifications_protocol_config(GENESIS_HASH, None);
let (rx, cfg) = on_demand_justifications_protocol_config::<
_,
Block,
sc_network::NetworkWorker<_, _>,
>(GENESIS_HASH, None);
let justif_protocol_name = cfg.name.clone();
net.add_authority_peer(vec![cfg]);
+1 -1
View File
@@ -19,7 +19,6 @@ targets = ["x86_64-unknown-linux-gnu"]
async-trait = "0.1.79"
futures = { version = "0.3.30", features = ["thread-pool"] }
futures-timer = "3.0.1"
libp2p-identity = { version = "0.1.3", features = ["ed25519", "peerid"] }
log = { workspace = true, default-features = true }
mockall = "0.11.3"
parking_lot = "0.12.1"
@@ -27,6 +26,7 @@ serde = { features = ["derive"], workspace = true, default-features = true }
thiserror = { workspace = true }
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus" }
sc-client-api = { path = "../../api" }
sc-network-types = { path = "../../network/types" }
sc-utils = { path = "../../utils" }
sp-api = { path = "../../../primitives/api" }
sp-blockchain = { path = "../../../primitives/blockchain" }
@@ -64,7 +64,7 @@ pub type BoxJustificationImport<B> =
Box<dyn JustificationImport<B, Error = ConsensusError> + Send + Sync>;
/// Maps to the RuntimeOrigin used by the network.
pub type RuntimeOrigin = libp2p_identity::PeerId;
pub type RuntimeOrigin = sc_network_types::PeerId;
/// Block data used by the queue.
#[derive(Debug, PartialEq, Eq, Clone)]
@@ -632,7 +632,7 @@ mod tests {
let hash = Hash::random();
finality_sender
.unbounded_send(worker_messages::ImportJustification(
libp2p_identity::PeerId::random(),
sc_network_types::PeerId::random(),
hash,
1,
(*b"TEST", Vec::new()),
@@ -41,6 +41,7 @@ sc-network = { path = "../../network" }
sc-network-gossip = { path = "../../network-gossip" }
sc-network-common = { path = "../../network/common" }
sc-network-sync = { path = "../../network/sync" }
sc-network-types = { path = "../../network/types" }
sc-telemetry = { path = "../../telemetry" }
sc-utils = { path = "../../utils" }
sp-api = { path = "../../../primitives/api" }
@@ -90,9 +90,10 @@ use log::{debug, trace};
use parity_scale_codec::{Decode, DecodeAll, Encode};
use prometheus_endpoint::{register, CounterVec, Opts, PrometheusError, Registry, U64};
use rand::seq::SliceRandom;
use sc_network::{PeerId, ReputationChange};
use sc_network::ReputationChange;
use sc_network_common::role::ObservedRole;
use sc_network_gossip::{MessageIntent, ValidatorContext};
use sc_network_types::PeerId;
use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG};
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use sp_consensus_grandpa::AuthorityId;
@@ -488,7 +488,7 @@ impl<B: BlockT, N: Network<B>, S: Syncing<B>> NetworkBridge<B, N, S> {
/// connected to (NOTE: this assumption will change in the future #3629).
pub(crate) fn set_sync_fork_request(
&self,
peers: Vec<sc_network::PeerId>,
peers: Vec<sc_network_types::PeerId>,
hash: B::Hash,
number: NumberFor<B>,
) {
@@ -27,7 +27,7 @@ use std::{
time::Duration,
};
use sc_network::PeerId;
use sc_network_types::PeerId;
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use sp_runtime::traits::{Block as BlockT, NumberFor};
@@ -44,7 +44,7 @@ impl<B: BlockT> NeighborPacketSender<B> {
/// Send a neighbor packet for the background worker to gossip to peers.
pub fn send(
&self,
who: Vec<sc_network::PeerId>,
who: Vec<sc_network_types::PeerId>,
neighbor_packet: NeighborPacket<NumberFor<B>>,
) {
if let Err(err) = self.0.unbounded_send((who, neighbor_packet)) {
@@ -30,14 +30,14 @@ use sc_network::{
event::Event as NetworkEvent,
service::traits::{Direction, MessageSink, NotificationEvent, NotificationService},
types::ProtocolName,
Multiaddr, NetworkBlock, NetworkEventStream, NetworkNotification, NetworkPeers,
NetworkSyncForkRequest, NotificationSenderError, NotificationSenderT as NotificationSender,
PeerId, ReputationChange,
Multiaddr, NetworkBlock, NetworkEventStream, NetworkPeers, NetworkSyncForkRequest,
ReputationChange,
};
use sc_network_common::role::{ObservedRole, Roles};
use sc_network_gossip::Validator;
use sc_network_sync::{SyncEvent as SyncStreamEvent, SyncEventStream};
use sc_network_test::{Block, Hash};
use sc_network_types::PeerId;
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender};
use sp_consensus_grandpa::AuthorityList;
use sp_keyring::Ed25519Keyring;
@@ -62,6 +62,7 @@ pub(crate) struct TestNetwork {
sender: TracingUnboundedSender<Event>,
}
#[async_trait::async_trait]
impl NetworkPeers for TestNetwork {
fn set_authorized_peers(&self, _peers: HashSet<PeerId>) {
unimplemented!();
@@ -134,6 +135,10 @@ impl NetworkPeers for TestNetwork {
.ok()
.and_then(|role| Some(ObservedRole::from(role)))
}
async fn reserved_peers(&self) -> Result<Vec<PeerId>, ()> {
unimplemented!();
}
}
impl NetworkEventStream for TestNetwork {
@@ -147,24 +152,6 @@ impl NetworkEventStream for TestNetwork {
}
}
impl NetworkNotification for TestNetwork {
fn write_notification(&self, target: PeerId, _protocol: ProtocolName, message: Vec<u8>) {
let _ = self.sender.unbounded_send(Event::WriteNotification(target, message));
}
fn notification_sender(
&self,
_target: PeerId,
_protocol: ProtocolName,
) -> Result<Box<dyn NotificationSender>, NotificationSenderError> {
unimplemented!();
}
fn set_notification_handshake(&self, _protocol: ProtocolName, _handshake: Vec<u8>) {
unimplemented!();
}
}
impl NetworkBlock<Hash, NumberFor<Block>> for TestNetwork {
fn announce_block(&self, hash: Hash, _data: Option<Vec<u8>>) {
let _ = self.sender.unbounded_send(Event::Announce(hash));
@@ -185,12 +172,7 @@ impl sc_network_gossip::ValidatorContext<Block> for TestNetwork {
fn broadcast_message(&mut self, _: Hash, _: Vec<u8>, _: bool) {}
fn send_message(&mut self, who: &PeerId, data: Vec<u8>) {
<Self as NetworkNotification>::write_notification(
self,
*who,
grandpa_protocol_name::NAME.into(),
data,
);
let _ = self.sender.unbounded_send(Event::WriteNotification(*who, data));
}
fn send_topic(&mut self, _: &PeerId, _: Hash, _: bool) {}
@@ -241,13 +223,13 @@ impl NotificationService for TestNotificationService {
}
/// Send synchronous `notification` to `peer`.
fn send_sync_notification(&self, peer: &PeerId, notification: Vec<u8>) {
fn send_sync_notification(&mut self, peer: &PeerId, notification: Vec<u8>) {
let _ = self.sender.unbounded_send(Event::WriteNotification(*peer, notification));
}
/// Send asynchronous `notification` to `peer`, allowing sender to exercise backpressure.
async fn send_async_notification(
&self,
&mut self,
_peer: &PeerId,
_notification: Vec<u8>,
) -> Result<(), sc_network::error::Error> {
+10 -6
View File
@@ -67,7 +67,7 @@ use sc_client_api::{
BlockchainEvents, CallExecutor, ExecutorProvider, Finalizer, LockImportRun, StorageProvider,
};
use sc_consensus::BlockImport;
use sc_network::{types::ProtocolName, NotificationService};
use sc_network::{types::ProtocolName, NetworkBackend, NotificationService};
use sc_telemetry::{telemetry, TelemetryHandle, CONSENSUS_DEBUG, CONSENSUS_INFO};
use sc_transaction_pool_api::OffchainTransactionPoolFactory;
use sc_utils::mpsc::{tracing_unbounded, TracingUnboundedReceiver};
@@ -343,7 +343,7 @@ pub(crate) trait BlockSyncRequester<Block: BlockT> {
/// connected to (NOTE: this assumption will change in the future #3629).
fn set_sync_fork_request(
&self,
peers: Vec<sc_network::PeerId>,
peers: Vec<sc_network_types::PeerId>,
hash: Block::Hash,
number: NumberFor<Block>,
);
@@ -357,7 +357,7 @@ where
{
fn set_sync_fork_request(
&self,
peers: Vec<sc_network::PeerId>,
peers: Vec<sc_network_types::PeerId>,
hash: Block::Hash,
number: NumberFor<Block>,
) {
@@ -707,11 +707,13 @@ pub struct GrandpaParams<Block: BlockT, C, N, S, SC, VR> {
/// Returns the configuration value to put in
/// [`sc_network::config::FullNetworkConfiguration`].
/// For standard protocol name see [`crate::protocol_standard_name`].
pub fn grandpa_peers_set_config(
pub fn grandpa_peers_set_config<B: BlockT, N: NetworkBackend<B, <B as BlockT>::Hash>>(
protocol_name: ProtocolName,
) -> (sc_network::config::NonDefaultSetConfig, Box<dyn NotificationService>) {
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;
sc_network::config::NonDefaultSetConfig::new(
N::notification_config(
protocol_name,
grandpa_protocol_name::LEGACY_NAMES.iter().map(|&n| n.into()).collect(),
// Notifications reach ~256kiB in size at the time of writing on Kusama and Polkadot.
@@ -723,6 +725,8 @@ pub fn grandpa_peers_set_config(
reserved_nodes: Vec::new(),
non_reserved_mode: sc_network::config::NonReservedPeerMode::Deny,
},
metrics,
peer_store_handle,
)
}
@@ -410,7 +410,7 @@ mod tests {
communication::tests::{make_test_network, Event},
};
use assert_matches::assert_matches;
use sc_network::PeerId;
use sc_network_types::PeerId;
use sc_utils::mpsc::tracing_unbounded;
use sp_blockchain::HeaderBackend as _;
use substrate_test_runtime_client::{TestClientBuilder, TestClientBuilderExt};
@@ -632,7 +632,7 @@ mod tests {
impl BlockSyncRequesterT<Block> for TestBlockSyncRequester {
fn set_sync_fork_request(
&self,
_peers: Vec<sc_network::PeerId>,
_peers: Vec<sc_network_types::PeerId>,
hash: Hash,
number: NumberFor<Block>,
) {