Skip to content

Commit

Permalink
Get rid of libp2p dependency in sc-authority-discovery (#5842)
Browse files Browse the repository at this point in the history
## Issue
#4859 
## Description
This PR removes `libp2p` types in authority-discovery, and replace them
with network backend agnostic types from `sc-network-types`.
 The `sc-network` interface is therefore updated accordingly.

---------

Co-authored-by: Bastian Köcher <[email protected]>
Co-authored-by: command-bot <>
Co-authored-by: Dmitry Markin <[email protected]>
Co-authored-by: Alexandru Vasile <[email protected]>
  • Loading branch information
4 people authored Nov 13, 2024
1 parent ac2546b commit a84f259
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 55 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions prdoc/pr_5842.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Get rid of libp2p dependency in sc-authority-discovery

doc:
- audience: Node Dev
description: |
Removes `libp2p` types in authority-discovery, and replace them with network backend agnostic types from `sc-network-types`.
The `sc-network` interface is therefore updated accordingly.

crates:
- name: sc-network
bump: patch
- name: sc-network-types
bump: patch
- name: sc-authority-discovery
bump: patch
1 change: 0 additions & 1 deletion substrate/client/authority-discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ codec = { workspace = true }
futures = { workspace = true }
futures-timer = { workspace = true }
ip_network = { workspace = true }
libp2p = { features = ["ed25519", "kad"], workspace = true }
multihash = { workspace = true }
linked_hash_set = { workspace = true }
log = { workspace = true, default-features = true }
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/authority-discovery/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
};

use futures::{channel::mpsc::channel, executor::LocalPool, task::LocalSpawn};
use libp2p::identity::ed25519;
use sc_network_types::ed25519;
use std::{collections::HashSet, sync::Arc};

use sc_network::{multiaddr::Protocol, Multiaddr, PeerId};
Expand Down
6 changes: 3 additions & 3 deletions substrate/client/authority-discovery/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ use futures::{channel::mpsc, future, stream::Fuse, FutureExt, Stream, StreamExt}
use addr_cache::AddrCache;
use codec::{Decode, Encode};
use ip_network::IpNetwork;
use libp2p::kad::{PeerRecord, Record};
use linked_hash_set::LinkedHashSet;
use sc_network_types::kad::{Key, PeerRecord, Record};

use log::{debug, error, trace};
use prometheus_endpoint::{register, Counter, CounterVec, Gauge, Opts, U64};
Expand Down Expand Up @@ -682,7 +682,7 @@ where

async fn handle_put_record_requested(
&mut self,
record_key: KademliaKey,
record_key: Key,
record_value: Vec<u8>,
publisher: Option<PeerId>,
expires: Option<std::time::Instant>,
Expand Down Expand Up @@ -943,7 +943,7 @@ where
authority_id, new_record.creation_time, current_record_info.creation_time,
);
self.network.put_record_to(
current_record_info.record.clone(),
current_record_info.record.clone().into(),
new_record.peers_with_record.clone(),
// If this is empty it means we received the answer from our node local
// storage, so we need to update that as well.
Expand Down
14 changes: 7 additions & 7 deletions substrate/client/authority-discovery/src/worker/schema/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ mod schema_v2 {

use super::*;
use codec::Encode;
use libp2p::identity::Keypair;
use prost::Message;
use sc_network::{Multiaddr, PeerId};
use sc_network_types::ed25519::Keypair;

#[test]
fn v2_decodes_v1() {
Expand Down Expand Up @@ -61,7 +61,7 @@ fn v2_decodes_v1() {

#[test]
fn v1_decodes_v2() {
let peer_secret = Keypair::generate_ed25519();
let peer_secret = Keypair::generate();
let peer_public = peer_secret.public();
let peer_id = peer_public.to_peer_id();
let multiaddress: Multiaddr =
Expand All @@ -73,7 +73,7 @@ fn v1_decodes_v2() {
let record_v2 = schema_v2::AuthorityRecord { addresses: vec_addresses.clone() };
let mut vec_record_v2 = vec![];
record_v2.encode(&mut vec_record_v2).unwrap();
let vec_peer_public = peer_public.encode_protobuf();
let vec_peer_public = peer_public.to_bytes().to_vec();
let peer_signature_v2 =
PeerSignature { public_key: vec_peer_public, signature: vec_peer_signature };
let signed_record_v2 = SignedAuthorityRecord {
Expand All @@ -97,7 +97,7 @@ fn v1_decodes_v2() {

#[test]
fn v1_decodes_v3() {
let peer_secret = Keypair::generate_ed25519();
let peer_secret = Keypair::generate();
let peer_public = peer_secret.public();
let peer_id = peer_public.to_peer_id();
let multiaddress: Multiaddr =
Expand All @@ -112,7 +112,7 @@ fn v1_decodes_v3() {
};
let mut vec_record_v3 = vec![];
record_v3.encode(&mut vec_record_v3).unwrap();
let vec_peer_public = peer_public.encode_protobuf();
let vec_peer_public = peer_public.to_bytes().to_vec();
let peer_signature_v3 =
PeerSignature { public_key: vec_peer_public, signature: vec_peer_signature };
let signed_record_v3 = SignedAuthorityRecord {
Expand All @@ -136,7 +136,7 @@ fn v1_decodes_v3() {

#[test]
fn v3_decodes_v2() {
let peer_secret = Keypair::generate_ed25519();
let peer_secret = Keypair::generate();
let peer_public = peer_secret.public();
let peer_id = peer_public.to_peer_id();
let multiaddress: Multiaddr =
Expand All @@ -148,7 +148,7 @@ fn v3_decodes_v2() {
let record_v2 = schema_v2::AuthorityRecord { addresses: vec_addresses.clone() };
let mut vec_record_v2 = vec![];
record_v2.encode(&mut vec_record_v2).unwrap();
let vec_peer_public = peer_public.encode_protobuf();
let vec_peer_public = peer_public.to_bytes().to_vec();
let peer_signature_v2 =
schema_v2::PeerSignature { public_key: vec_peer_public, signature: vec_peer_signature };
let signed_record_v2 = schema_v2::SignedAuthorityRecord {
Expand Down
12 changes: 7 additions & 5 deletions substrate/client/authority-discovery/src/worker/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ use futures::{
sink::SinkExt,
task::LocalSpawn,
};
use libp2p::{identity::SigningError, kad::record::Key as KademliaKey};
use prometheus_endpoint::prometheus::default_registry;

use sc_client_api::HeaderBackend;
use sc_network::{service::signature::Keypair, Signature};
use sc_network::{
service::signature::{Keypair, SigningError},
PublicKey, Signature,
};
use sc_network_types::{
kad::Key as KademliaKey,
multiaddr::{Multiaddr, Protocol},
PeerId,
};
Expand Down Expand Up @@ -178,8 +180,8 @@ impl NetworkSigner for TestNetwork {
signature: &Vec<u8>,
message: &Vec<u8>,
) -> std::result::Result<bool, String> {
let public_key = libp2p::identity::PublicKey::try_decode_protobuf(&public_key)
.map_err(|error| error.to_string())?;
let public_key =
PublicKey::try_decode_protobuf(&public_key).map_err(|error| error.to_string())?;
let peer_id: PeerId = peer_id.into();
let remote: PeerId = public_key.to_peer_id().into();

Expand Down
10 changes: 5 additions & 5 deletions substrate/client/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,18 +375,18 @@ impl From<DiscoveryOut> for BehaviourOut {
},
DiscoveryOut::Discovered(peer_id) => BehaviourOut::Discovered(peer_id),
DiscoveryOut::ValueFound(results, duration) =>
BehaviourOut::Dht(DhtEvent::ValueFound(results), Some(duration)),
BehaviourOut::Dht(DhtEvent::ValueFound(results.into()), Some(duration)),
DiscoveryOut::ValueNotFound(key, duration) =>
BehaviourOut::Dht(DhtEvent::ValueNotFound(key), Some(duration)),
BehaviourOut::Dht(DhtEvent::ValueNotFound(key.into()), Some(duration)),
DiscoveryOut::ValuePut(key, duration) =>
BehaviourOut::Dht(DhtEvent::ValuePut(key), Some(duration)),
BehaviourOut::Dht(DhtEvent::ValuePut(key.into()), Some(duration)),
DiscoveryOut::PutRecordRequest(record_key, record_value, publisher, expires) =>
BehaviourOut::Dht(
DhtEvent::PutRecordRequest(record_key, record_value, publisher, expires),
DhtEvent::PutRecordRequest(record_key.into(), record_value, publisher, expires),
None,
),
DiscoveryOut::ValuePutFailed(key, duration) =>
BehaviourOut::Dht(DhtEvent::ValuePutFailed(key), Some(duration)),
BehaviourOut::Dht(DhtEvent::ValuePutFailed(key.into()), Some(duration)),
DiscoveryOut::RandomKademliaStarted => BehaviourOut::RandomKademliaStarted,
}
}
Expand Down
8 changes: 4 additions & 4 deletions substrate/client/network/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
use crate::types::ProtocolName;

use bytes::Bytes;
use libp2p::{
kad::{record::Key, PeerRecord},
PeerId,
};

use sc_network_common::role::ObservedRole;
use sc_network_types::{
kad::{Key, PeerRecord},
PeerId,
};

/// Events generated by DHT as a response to get_value and put_value requests.
#[derive(Debug, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/network/src/litep2p/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use array_bytes::bytes2hex;
use futures::{FutureExt, Stream};
use futures_timer::Delay;
use ip_network::IpNetwork;
use libp2p::kad::record::Key as KademliaKey;
use litep2p::{
protocol::{
libp2p::{
Expand All @@ -45,6 +44,7 @@ use litep2p::{
PeerId, ProtocolName,
};
use parking_lot::RwLock;
use sc_network_types::kad::Key as KademliaKey;
use schnellru::{ByLength, LruMap};

use std::{
Expand Down
16 changes: 8 additions & 8 deletions substrate/client/network/src/litep2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ use crate::{

use codec::Encode;
use futures::StreamExt;
use libp2p::kad::{PeerRecord, Record as P2PRecord, RecordKey};
use litep2p::{
config::ConfigBuilder,
crypto::ed25519::Keypair,
Expand All @@ -74,6 +73,7 @@ use litep2p::{
Litep2p, Litep2pEvent, ProtocolName as Litep2pProtocolName,
};
use prometheus_endpoint::Registry;
use sc_network_types::kad::{Key as RecordKey, PeerRecord, Record as P2PRecord};

use sc_client_api::BlockBackend;
use sc_network_common::{role::Roles, ExHashT};
Expand Down Expand Up @@ -711,8 +711,8 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac
self.pending_put_values.insert(query_id, (key, Instant::now()));
}
NetworkServiceCommand::PutValueTo { record, peers, update_local_storage} => {
let kademlia_key = record.key.to_vec().into();
let query_id = self.discovery.put_value_to_peers(record, peers, update_local_storage).await;
let kademlia_key = record.key.clone();
let query_id = self.discovery.put_value_to_peers(record.into(), peers, update_local_storage).await;
self.pending_put_values.insert(query_id, (kademlia_key, Instant::now()));
}

Expand Down Expand Up @@ -836,7 +836,7 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac
self.event_streams.send(
Event::Dht(
DhtEvent::ValueFound(
record
record.into()
)
)
);
Expand Down Expand Up @@ -864,7 +864,7 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac
);

self.event_streams.send(Event::Dht(
DhtEvent::ValuePut(libp2p::kad::RecordKey::new(&key))
DhtEvent::ValuePut(key)
));

if let Some(ref metrics) = self.metrics {
Expand All @@ -890,7 +890,7 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac
);

self.event_streams.send(Event::Dht(
DhtEvent::ValuePutFailed(libp2p::kad::RecordKey::new(&key))
DhtEvent::ValuePutFailed(key)
));

if let Some(ref metrics) = self.metrics {
Expand All @@ -908,7 +908,7 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac
);

self.event_streams.send(Event::Dht(
DhtEvent::ValueNotFound(libp2p::kad::RecordKey::new(&key))
DhtEvent::ValueNotFound(key)
));

if let Some(ref metrics) = self.metrics {
Expand Down Expand Up @@ -964,7 +964,7 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac
Some(DiscoveryEvent::IncomingRecord { record: Record { key, value, publisher, expires }} ) => {
self.event_streams.send(Event::Dht(
DhtEvent::PutRecordRequest(
libp2p::kad::RecordKey::new(&key),
key.into(),
value,
publisher.map(Into::into),
expires,
Expand Down
11 changes: 3 additions & 8 deletions substrate/client/network/src/litep2p/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ use crate::{
RequestFailure, Signature,
};

use crate::litep2p::Record;
use codec::DecodeAll;
use futures::{channel::oneshot, stream::BoxStream};
use libp2p::{identity::SigningError, kad::record::Key as KademliaKey};
use libp2p::identity::SigningError;
use litep2p::{
addresses::PublicAddresses, crypto::ed25519::Keypair,
types::multiaddr::Multiaddr as LiteP2pMultiaddr,
};
use parking_lot::RwLock;
use sc_network_types::kad::{Key as KademliaKey, Record};

use sc_network_common::{
role::{ObservedRole, Roles},
Expand Down Expand Up @@ -266,12 +266,7 @@ impl NetworkDHTProvider for Litep2pNetworkService {
let _ = self.cmd_tx.unbounded_send(NetworkServiceCommand::PutValue { key, value });
}

fn put_record_to(
&self,
record: libp2p::kad::Record,
peers: HashSet<PeerId>,
update_local_storage: bool,
) {
fn put_record_to(&self, record: Record, peers: HashSet<PeerId>, update_local_storage: bool) {
let _ = self.cmd_tx.unbounded_send(NetworkServiceCommand::PutValueTo {
record: Record {
key: record.key.to_vec().into(),
Expand Down
10 changes: 5 additions & 5 deletions substrate/client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ use libp2p::{
core::{upgrade, ConnectedPoint, Endpoint},
identify::Info as IdentifyInfo,
identity::ed25519,
kad::{record::Key as KademliaKey, Record},
multiaddr::{self, Multiaddr},
swarm::{
Config as SwarmConfig, ConnectionError, ConnectionId, DialError, Executor, ListenError,
Expand All @@ -80,6 +79,7 @@ use log::{debug, error, info, trace, warn};
use metrics::{Histogram, MetricSources, Metrics};
use parking_lot::Mutex;
use prometheus_endpoint::Registry;
use sc_network_types::kad::{Key as KademliaKey, Record};

use sc_client_api::BlockBackend;
use sc_network_common::{
Expand Down Expand Up @@ -1455,17 +1455,17 @@ where
fn handle_worker_message(&mut self, msg: ServiceToWorkerMsg) {
match msg {
ServiceToWorkerMsg::GetValue(key) =>
self.network_service.behaviour_mut().get_value(key),
self.network_service.behaviour_mut().get_value(key.into()),
ServiceToWorkerMsg::PutValue(key, value) =>
self.network_service.behaviour_mut().put_value(key, value),
self.network_service.behaviour_mut().put_value(key.into(), value),
ServiceToWorkerMsg::PutRecordTo { record, peers, update_local_storage } => self
.network_service
.behaviour_mut()
.put_record_to(record, peers, update_local_storage),
.put_record_to(record.into(), peers, update_local_storage),
ServiceToWorkerMsg::StoreRecord(key, value, publisher, expires) => self
.network_service
.behaviour_mut()
.store_record(key, value, publisher, expires),
.store_record(key.into(), value, publisher, expires),
ServiceToWorkerMsg::AddKnownAddress(peer_id, addr) =>
self.network_service.behaviour_mut().add_known_address(peer_id, addr),
ServiceToWorkerMsg::EventStream(sender) => self.event_streams.push(sender),
Expand Down
Loading

0 comments on commit a84f259

Please sign in to comment.