mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 23:18:01 +00:00
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:
@@ -23,21 +23,26 @@
|
||||
|
||||
pub use crate::{
|
||||
discovery::DEFAULT_KADEMLIA_REPLICATION_FACTOR,
|
||||
peer_store::PeerStoreProvider,
|
||||
protocol::{notification_service, NotificationsSink, ProtocolHandlePair},
|
||||
request_responses::{
|
||||
IncomingRequest, OutgoingResponse, ProtocolConfig as RequestResponseConfig,
|
||||
},
|
||||
service::traits::NotificationService,
|
||||
service::{
|
||||
metrics::NotificationMetrics,
|
||||
traits::{NotificationConfig, NotificationService, PeerStore},
|
||||
},
|
||||
types::ProtocolName,
|
||||
};
|
||||
|
||||
pub use libp2p::{
|
||||
build_multiaddr,
|
||||
identity::{self, ed25519, Keypair},
|
||||
multiaddr, Multiaddr, PeerId,
|
||||
multiaddr, Multiaddr,
|
||||
};
|
||||
use sc_network_types::PeerId;
|
||||
|
||||
use crate::peer_store::PeerStoreHandle;
|
||||
use crate::service::{ensure_addresses_consistent_with_transport, traits::NetworkBackend};
|
||||
use codec::Encode;
|
||||
use prometheus_endpoint::Registry;
|
||||
use zeroize::Zeroize;
|
||||
@@ -61,6 +66,7 @@ use std::{
|
||||
path::{Path, PathBuf},
|
||||
pin::Pin,
|
||||
str::{self, FromStr},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
/// Protocol name prefix, transmitted on the wire for legacy protocol names.
|
||||
@@ -99,7 +105,7 @@ impl fmt::Debug for ProtocolId {
|
||||
/// let (peer_id, addr) = parse_str_addr(
|
||||
/// "/ip4/198.51.100.19/tcp/30333/p2p/QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV"
|
||||
/// ).unwrap();
|
||||
/// assert_eq!(peer_id, "QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV".parse::<PeerId>().unwrap());
|
||||
/// assert_eq!(peer_id, "QmSk5HQbn6LhUwDiNMseVUjuRYhEtYj4aUZ6WfWoGURpdV".parse::<PeerId>().unwrap().into());
|
||||
/// assert_eq!(addr, "/ip4/198.51.100.19/tcp/30333".parse::<Multiaddr>().unwrap());
|
||||
/// ```
|
||||
pub fn parse_str_addr(addr_str: &str) -> Result<(PeerId, Multiaddr), ParseErr> {
|
||||
@@ -569,6 +575,17 @@ impl NonDefaultSetConfig {
|
||||
}
|
||||
}
|
||||
|
||||
impl NotificationConfig for NonDefaultSetConfig {
|
||||
fn set_config(&self) -> &SetConfig {
|
||||
&self.set_config
|
||||
}
|
||||
|
||||
/// Get reference to protocol name.
|
||||
fn protocol_name(&self) -> &ProtocolName {
|
||||
&self.protocol_name
|
||||
}
|
||||
}
|
||||
|
||||
/// Network service configuration.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct NetworkConfiguration {
|
||||
@@ -655,6 +672,9 @@ pub struct NetworkConfiguration {
|
||||
/// a modification of the way the implementation works. Different nodes with different
|
||||
/// configured values remain compatible with each other.
|
||||
pub yamux_window_size: Option<u32>,
|
||||
|
||||
/// Networking backend used for P2P communication.
|
||||
pub network_backend: NetworkBackendType,
|
||||
}
|
||||
|
||||
impl NetworkConfiguration {
|
||||
@@ -687,6 +707,7 @@ impl NetworkConfiguration {
|
||||
.expect("value is a constant; constant is non-zero; qed."),
|
||||
yamux_window_size: None,
|
||||
ipfs_server: false,
|
||||
network_backend: NetworkBackendType::Libp2p,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -722,18 +743,15 @@ impl NetworkConfiguration {
|
||||
}
|
||||
|
||||
/// Network initialization parameters.
|
||||
pub struct Params<Block: BlockT> {
|
||||
pub struct Params<Block: BlockT, H: ExHashT, N: NetworkBackend<Block, H>> {
|
||||
/// Assigned role for our node (full, light, ...).
|
||||
pub role: Role,
|
||||
|
||||
/// How to spawn background tasks.
|
||||
pub executor: Box<dyn Fn(Pin<Box<dyn Future<Output = ()> + Send>>) + Send>,
|
||||
pub executor: Box<dyn Fn(Pin<Box<dyn Future<Output = ()> + Send>>) + Send + Sync>,
|
||||
|
||||
/// Network layer configuration.
|
||||
pub network_config: FullNetworkConfiguration,
|
||||
|
||||
/// Peer store with known nodes, peer reputations, etc.
|
||||
pub peer_store: PeerStoreHandle,
|
||||
pub network_config: FullNetworkConfiguration<Block, H, N>,
|
||||
|
||||
/// Legacy name of the protocol to use on the wire. Should be different for each chain.
|
||||
pub protocol_id: ProtocolId,
|
||||
@@ -749,25 +767,43 @@ pub struct Params<Block: BlockT> {
|
||||
pub metrics_registry: Option<Registry>,
|
||||
|
||||
/// Block announce protocol configuration
|
||||
pub block_announce_config: NonDefaultSetConfig,
|
||||
pub block_announce_config: N::NotificationProtocolConfig,
|
||||
|
||||
/// Bitswap configuration, if the server has been enabled.
|
||||
pub bitswap_config: Option<N::BitswapConfig>,
|
||||
|
||||
/// Notification metrics.
|
||||
pub notification_metrics: NotificationMetrics,
|
||||
}
|
||||
|
||||
/// Full network configuration.
|
||||
pub struct FullNetworkConfiguration {
|
||||
pub struct FullNetworkConfiguration<B: BlockT + 'static, H: ExHashT, N: NetworkBackend<B, H>> {
|
||||
/// Installed notification protocols.
|
||||
pub(crate) notification_protocols: Vec<NonDefaultSetConfig>,
|
||||
pub(crate) notification_protocols: Vec<N::NotificationProtocolConfig>,
|
||||
|
||||
/// List of request-response protocols that the node supports.
|
||||
pub(crate) request_response_protocols: Vec<RequestResponseConfig>,
|
||||
pub(crate) request_response_protocols: Vec<N::RequestResponseProtocolConfig>,
|
||||
|
||||
/// Network configuration.
|
||||
pub network_config: NetworkConfiguration,
|
||||
|
||||
/// [`PeerStore`](crate::peer_store::PeerStore),
|
||||
peer_store: Option<N::PeerStore>,
|
||||
|
||||
/// Handle to [`PeerStore`](crate::peer_store::PeerStore).
|
||||
peer_store_handle: Arc<dyn PeerStoreProvider>,
|
||||
}
|
||||
|
||||
impl FullNetworkConfiguration {
|
||||
impl<B: BlockT + 'static, H: ExHashT, N: NetworkBackend<B, H>> FullNetworkConfiguration<B, H, N> {
|
||||
/// Create new [`FullNetworkConfiguration`].
|
||||
pub fn new(network_config: &NetworkConfiguration) -> Self {
|
||||
let bootnodes = network_config.boot_nodes.iter().map(|bootnode| bootnode.peer_id).collect();
|
||||
let peer_store = N::peer_store(bootnodes);
|
||||
let peer_store_handle = peer_store.handle();
|
||||
|
||||
Self {
|
||||
peer_store: Some(peer_store),
|
||||
peer_store_handle,
|
||||
notification_protocols: Vec::new(),
|
||||
request_response_protocols: Vec::new(),
|
||||
network_config: network_config.clone(),
|
||||
@@ -775,19 +811,131 @@ impl FullNetworkConfiguration {
|
||||
}
|
||||
|
||||
/// Add a notification protocol.
|
||||
pub fn add_notification_protocol(&mut self, config: NonDefaultSetConfig) {
|
||||
pub fn add_notification_protocol(&mut self, config: N::NotificationProtocolConfig) {
|
||||
self.notification_protocols.push(config);
|
||||
}
|
||||
|
||||
/// Get reference to installed notification protocols.
|
||||
pub fn notification_protocols(&self) -> &Vec<NonDefaultSetConfig> {
|
||||
pub fn notification_protocols(&self) -> &Vec<N::NotificationProtocolConfig> {
|
||||
&self.notification_protocols
|
||||
}
|
||||
|
||||
/// Add a request-response protocol.
|
||||
pub fn add_request_response_protocol(&mut self, config: RequestResponseConfig) {
|
||||
pub fn add_request_response_protocol(&mut self, config: N::RequestResponseProtocolConfig) {
|
||||
self.request_response_protocols.push(config);
|
||||
}
|
||||
|
||||
/// Get handle to [`PeerStore`].
|
||||
pub fn peer_store_handle(&self) -> Arc<dyn PeerStoreProvider> {
|
||||
Arc::clone(&self.peer_store_handle)
|
||||
}
|
||||
|
||||
/// Take [`PeerStore`].
|
||||
///
|
||||
/// `PeerStore` is created when `FullNetworkConfig` is initialized so that `PeerStoreHandle`s
|
||||
/// can be passed onto notification protocols. `PeerStore` itself should be started only once
|
||||
/// and since technically it's not a libp2p task, it should be started with `SpawnHandle` in
|
||||
/// `builder.rs` instead of using the libp2p/litep2p executor in the networking backend. This
|
||||
/// function consumes `PeerStore` and starts its event loop in the appropriate place.
|
||||
pub fn take_peer_store(&mut self) -> N::PeerStore {
|
||||
self.peer_store
|
||||
.take()
|
||||
.expect("`PeerStore` can only be taken once when it's started; qed")
|
||||
}
|
||||
|
||||
/// Verify addresses are consistent with enabled transports.
|
||||
pub fn sanity_check_addresses(&self) -> Result<(), crate::error::Error> {
|
||||
ensure_addresses_consistent_with_transport(
|
||||
self.network_config.listen_addresses.iter(),
|
||||
&self.network_config.transport,
|
||||
)?;
|
||||
ensure_addresses_consistent_with_transport(
|
||||
self.network_config.boot_nodes.iter().map(|x| &x.multiaddr),
|
||||
&self.network_config.transport,
|
||||
)?;
|
||||
ensure_addresses_consistent_with_transport(
|
||||
self.network_config
|
||||
.default_peers_set
|
||||
.reserved_nodes
|
||||
.iter()
|
||||
.map(|x| &x.multiaddr),
|
||||
&self.network_config.transport,
|
||||
)?;
|
||||
|
||||
for notification_protocol in &self.notification_protocols {
|
||||
ensure_addresses_consistent_with_transport(
|
||||
notification_protocol.set_config().reserved_nodes.iter().map(|x| &x.multiaddr),
|
||||
&self.network_config.transport,
|
||||
)?;
|
||||
}
|
||||
ensure_addresses_consistent_with_transport(
|
||||
self.network_config.public_addresses.iter(),
|
||||
&self.network_config.transport,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check for duplicate bootnodes.
|
||||
pub fn sanity_check_bootnodes(&self) -> Result<(), crate::error::Error> {
|
||||
self.network_config.boot_nodes.iter().try_for_each(|bootnode| {
|
||||
if let Some(other) = self
|
||||
.network_config
|
||||
.boot_nodes
|
||||
.iter()
|
||||
.filter(|o| o.multiaddr == bootnode.multiaddr)
|
||||
.find(|o| o.peer_id != bootnode.peer_id)
|
||||
{
|
||||
Err(crate::error::Error::DuplicateBootnode {
|
||||
address: bootnode.multiaddr.clone(),
|
||||
first_id: bootnode.peer_id.into(),
|
||||
second_id: other.peer_id.into(),
|
||||
})
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Collect all reserved nodes and bootnodes addresses.
|
||||
pub fn known_addresses(&self) -> Vec<(PeerId, Multiaddr)> {
|
||||
let mut addresses: Vec<_> = self
|
||||
.network_config
|
||||
.default_peers_set
|
||||
.reserved_nodes
|
||||
.iter()
|
||||
.map(|reserved| (reserved.peer_id, reserved.multiaddr.clone()))
|
||||
.chain(self.notification_protocols.iter().flat_map(|protocol| {
|
||||
protocol
|
||||
.set_config()
|
||||
.reserved_nodes
|
||||
.iter()
|
||||
.map(|reserved| (reserved.peer_id, reserved.multiaddr.clone()))
|
||||
}))
|
||||
.chain(
|
||||
self.network_config
|
||||
.boot_nodes
|
||||
.iter()
|
||||
.map(|bootnode| (bootnode.peer_id, bootnode.multiaddr.clone())),
|
||||
)
|
||||
.collect();
|
||||
|
||||
// Remove possible duplicates.
|
||||
addresses.sort();
|
||||
addresses.dedup();
|
||||
|
||||
addresses
|
||||
}
|
||||
}
|
||||
|
||||
/// Network backend type.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum NetworkBackendType {
|
||||
/// Use libp2p for P2P networking.
|
||||
Libp2p,
|
||||
|
||||
/// Use litep2p for P2P networking.
|
||||
Litep2p,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user