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

Expose API for PublicAddresses #212

Merged
merged 33 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8502645
identify: Expose API to inject listen addresses
lexnv Aug 20, 2024
e452745
tests: Check identify public addresses are propagated
lexnv Aug 20, 2024
0e42027
listen-addr: Add listen address struct to a separate module
lexnv Aug 28, 2024
890df79
listen-addr/tests: Check listen addr interface functionality
lexnv Aug 28, 2024
59668c9
listen-addr: Use a lock guard for custom iteration over the addresses
lexnv Aug 28, 2024
56ed02a
litep2p: Store shared listenAddresses on Litep2p object
lexnv Aug 28, 2024
a4bbf06
identify: Use the new interface of public addresses
lexnv Aug 28, 2024
f2a1c51
listen-addr: Check listen address contains p2p protocol
lexnv Aug 28, 2024
67b617d
Polish up the API
lexnv Aug 28, 2024
38d8ca3
listen-addr: Register lsiten addresses with adding the local peerID
lexnv Aug 28, 2024
2a45422
Adjust testing to the new interface
lexnv Aug 28, 2024
fc0c700
Use ListenAddresses everywhere
lexnv Aug 28, 2024
7f07e9d
listen-addr: Add better documentation
lexnv Aug 28, 2024
794eba5
listen-addr: Add contains and remove partial methods
lexnv Aug 29, 2024
5e9930a
Merge remote-tracking branch 'origin/master' into lexnv/indentify-con…
lexnv Aug 29, 2024
e11a996
Rename to ExternalAddresses for clarity
lexnv Aug 30, 2024
09b6fee
Refactor and adjust testing
lexnv Sep 2, 2024
c11f468
Merge remote-tracking branch 'origin/master' into lexnv/indentify-con…
lexnv Sep 2, 2024
68c76d2
Apply fmt
lexnv Sep 2, 2024
6161233
Fix cargo doc and clippy
lexnv Sep 2, 2024
dacd665
pub-addr: Rename API methods
lexnv Sep 3, 2024
1194a29
Introduce ListenAddresses object
lexnv Sep 3, 2024
ba6e198
Use listenAddresses added interface
lexnv Sep 3, 2024
e7a7d80
identify: Use user-provided, listen and public addresses
lexnv Sep 3, 2024
9ad762b
manager: Remove transport_manager::register_listen_address
lexnv Sep 3, 2024
d0ebcd2
Adjust testing
lexnv Sep 3, 2024
41c1e23
Fix documentation
lexnv Sep 3, 2024
fd23d15
address: Introduce insertion error for better reporting
lexnv Sep 3, 2024
be8d663
Remove ListenAddresses
lexnv Sep 3, 2024
af8fe60
Adjust testing
lexnv Sep 3, 2024
39153f1
Adjust testing
lexnv Sep 3, 2024
6b7b7b1
identify: Remove public addr from list config
lexnv Sep 4, 2024
a633b68
Merge branch 'master' into lexnv/indentify-confirmed-addresses
lexnv Sep 4, 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
2 changes: 1 addition & 1 deletion examples/custom_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async fn main() {

// dial `litep2p1`
litep2p2
.dial_address(litep2p1.listen_addresses().next().unwrap().clone())
.dial_address(litep2p1.public_addresses().get_addresses().get(0).unwrap().clone())
.await
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion examples/custom_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ async fn main() {
let (mut litep2p2, mut handle2) = make_litep2p();

let peer2 = *litep2p2.local_peer_id();
let listen_address = litep2p2.listen_addresses().next().unwrap().clone();
let listen_address = litep2p2.public_addresses().get_addresses().get(0).unwrap().clone();
litep2p1.add_known_address(peer2, std::iter::once(listen_address));

tokio::spawn(async move {
Expand Down
2 changes: 1 addition & 1 deletion examples/echo_notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async fn main() {

// get the first (and only) listen address for the second peer
// and add it as a known address for `litep2p1`
let listen_address = litep2p2.listen_addresses().next().unwrap().clone();
let listen_address = litep2p2.public_addresses().get_addresses().get(0).unwrap().clone();
let peer = *litep2p2.local_peer_id();

litep2p1.add_known_address(peer, vec![listen_address].into_iter());
Expand Down
2 changes: 1 addition & 1 deletion examples/gossiping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ async fn main() {

// establish connection to litep2p for all other litep2ps
let peer2 = *litep2p2.local_peer_id();
let listen_address = litep2p2.listen_addresses().next().unwrap().clone();
let listen_address = litep2p2.public_addresses().get_addresses().get(0).unwrap().clone();

litep2p1.add_known_address(peer2, vec![listen_address.clone()].into_iter());
litep2p3.add_known_address(peer2, vec![listen_address.clone()].into_iter());
Expand Down
38 changes: 14 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@

use crate::{
config::Litep2pConfig,
error::DialError,
protocol::{
libp2p::{bitswap::Bitswap, identify::Identify, kademlia::Kademlia, ping::Ping},
mdns::Mdns,
notification::NotificationProtocol,
request_response::RequestResponseProtocol,
},
public_addresses::PublicAddresses,
transport::{
manager::{SupportedTransport, TransportManager},
tcp::TcpTransport,
Expand All @@ -50,9 +52,7 @@ use crate::transport::webrtc::WebRtcTransport;
#[cfg(feature = "websocket")]
use crate::transport::websocket::WebSocketTransport;

use error::DialError;
use multiaddr::{Multiaddr, Protocol};
use multihash::Multihash;
use multiaddr::Multiaddr;
use transport::Endpoint;
use types::ConnectionId;

Expand All @@ -71,6 +71,7 @@ pub mod crypto;
pub mod error;
pub mod executor;
pub mod protocol;
pub mod public_addresses;
pub mod substream;
pub mod transport;
pub mod types;
Expand Down Expand Up @@ -138,7 +139,7 @@ pub struct Litep2p {
local_peer_id: PeerId,

/// Listen addresses.
listen_addresses: Vec<Multiaddr>,
listen_addresses: PublicAddresses,
dmitry-markin marked this conversation as resolved.
Show resolved Hide resolved

/// Transport manager.
transport_manager: TransportManager,
Expand All @@ -152,7 +153,6 @@ impl Litep2p {
pub fn new(mut litep2p_config: Litep2pConfig) -> crate::Result<Litep2p> {
let local_peer_id = PeerId::from_public_key(&litep2p_config.keypair.public().into());
let bandwidth_sink = BandwidthSink::new();
let mut listen_addresses = vec![];

let supported_transports = Self::supported_transports(&litep2p_config);
let (mut transport_manager, transport_handle) = TransportManager::new(
Expand Down Expand Up @@ -315,9 +315,6 @@ impl Litep2p {

for address in transport_listen_addresses {
transport_manager.register_listen_address(address.clone());
listen_addresses.push(address.with(Protocol::P2p(
lexnv marked this conversation as resolved.
Show resolved Hide resolved
Multihash::from_bytes(&local_peer_id.to_bytes()).unwrap(),
)));
}

transport_manager.register_transport(SupportedTransport::Tcp, Box::new(transport));
Expand All @@ -332,9 +329,6 @@ impl Litep2p {

for address in transport_listen_addresses {
transport_manager.register_listen_address(address.clone());
listen_addresses.push(address.with(Protocol::P2p(
Multihash::from_bytes(&local_peer_id.to_bytes()).unwrap(),
)));
}

transport_manager.register_transport(SupportedTransport::Quic, Box::new(transport));
Expand All @@ -349,9 +343,6 @@ impl Litep2p {

for address in transport_listen_addresses {
transport_manager.register_listen_address(address.clone());
listen_addresses.push(address.with(Protocol::P2p(
Multihash::from_bytes(&local_peer_id.to_bytes()).unwrap(),
)));
}

transport_manager.register_transport(SupportedTransport::WebRtc, Box::new(transport));
Expand All @@ -366,18 +357,17 @@ impl Litep2p {

for address in transport_listen_addresses {
transport_manager.register_listen_address(address.clone());
listen_addresses.push(address.with(Protocol::P2p(
Multihash::from_bytes(&local_peer_id.to_bytes()).unwrap(),
)));
}

transport_manager
.register_transport(SupportedTransport::WebSocket, Box::new(transport));
}

let listen_addresses = transport_manager.public_addresses();

// enable mdns if the config exists
if let Some(config) = litep2p_config.mdns.take() {
let mdns = Mdns::new(transport_handle, config, listen_addresses.clone())?;
let mdns = Mdns::new(transport_handle, config, listen_addresses.get_addresses())?;

litep2p_config.executor.run(Box::pin(async move {
let _ = mdns.start().await;
Expand All @@ -387,7 +377,7 @@ impl Litep2p {
// if identify was enabled, give it the enabled protocols and listen addresses and start it
if let Some((service, mut identify_config)) = identify_info.take() {
identify_config.protocols = transport_manager.protocols().cloned().collect();
let identify = Identify::new(service, identify_config, listen_addresses.clone());
let identify = Identify::new(service, identify_config);

litep2p_config.executor.run(Box::pin(async move {
let _ = identify.run().await;
Expand All @@ -399,7 +389,7 @@ impl Litep2p {
}

// verify that at least one transport is specified
if listen_addresses.is_empty() {
if listen_addresses.inner.read().is_empty() {
tracing::warn!(
target: LOG_TARGET,
"litep2p started with no listen addresses, cannot accept inbound connections",
Expand Down Expand Up @@ -450,9 +440,9 @@ impl Litep2p {
&self.local_peer_id
}

/// Get listen address of litep2p.
pub fn listen_addresses(&self) -> impl Iterator<Item = &Multiaddr> {
self.listen_addresses.iter()
/// Get the list of public addresses of the node.
pub fn public_addresses(&self) -> PublicAddresses {
self.listen_addresses.clone()
}

/// Get handle to bandwidth sink.
Expand All @@ -473,7 +463,7 @@ impl Litep2p {
/// Add one ore more known addresses for peer.
///
/// Return value denotes how many addresses were added for the peer.
// Addresses belonging to disabled/unsupported transports will be ignored.
/// Addresses belonging to disabled/unsupported transports will be ignored.
pub fn add_known_address(
&mut self,
peer: PeerId,
Expand Down
116 changes: 103 additions & 13 deletions src/protocol/libp2p/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ use crate::{
crypto::PublicKey,
error::{Error, SubstreamError},
protocol::{Direction, TransportEvent, TransportService},
public_addresses::PublicAddresses,
substream::Substream,
transport::Endpoint,
types::{protocol::ProtocolName, SubstreamId},
PeerId, DEFAULT_CHANNEL_SIZE,
};

use futures::{future::BoxFuture, stream::FuturesUnordered, Stream, StreamExt};
use multiaddr::Multiaddr;
use multiaddr::{Multiaddr, Protocol};
use prost::Message;
use tokio::sync::mpsc::{channel, Sender};
use tokio_stream::wrappers::ReceiverStream;
Expand Down Expand Up @@ -183,7 +184,7 @@ pub(crate) struct Identify {
user_agent: String,

/// Public addresses.
listen_addresses: HashSet<Multiaddr>,
listen_addresses: PublicAddresses,

/// Protocols supported by the local node, filled by `Litep2p`.
protocols: Vec<String>,
Expand All @@ -200,16 +201,31 @@ pub(crate) struct Identify {

impl Identify {
/// Create new [`Identify`] protocol.
pub(crate) fn new(
service: TransportService,
config: Config,
listen_addresses: Vec<Multiaddr>,
) -> Self {
pub(crate) fn new(service: TransportService, config: Config) -> Self {
let listen_addresses = service.public_addresses();

let local_peer_id = service.local_peer_id();
let filtered_public_addr = config.public_addresses.into_iter().filter_map(|address| {
if address.is_empty() {
return None;
}
if let Some(peer_id) = PeerId::try_from_multiaddr(&address) {
if peer_id != local_peer_id {
return None;
}

return Some(address);
}

Some(address.with(Protocol::P2p(local_peer_id.into())))
});
listen_addresses.inner.write().extend(filtered_public_addr);
lexnv marked this conversation as resolved.
Show resolved Hide resolved

Self {
service,
tx: config.tx_event,
peers: HashMap::new(),
listen_addresses: config.public_addresses.into_iter().chain(listen_addresses).collect(),
listen_addresses,
public: config.public.expect("public key to be supplied"),
protocol_version: config.protocol_version,
user_agent: config.user_agent.unwrap_or(DEFAULT_AGENT.to_string()),
Expand Down Expand Up @@ -265,15 +281,13 @@ impl Identify {
}
};

let listen_addrs =
lexnv marked this conversation as resolved.
Show resolved Hide resolved
self.listen_addresses.inner.read().iter().map(|addr| addr.to_vec()).collect();
let identify = identify_schema::Identify {
protocol_version: Some(self.protocol_version.clone()),
agent_version: Some(self.user_agent.clone()),
public_key: Some(self.public.to_protobuf_encoding()),
listen_addrs: self
.listen_addresses
.iter()
.map(|address| address.to_vec())
.collect::<Vec<_>>(),
listen_addrs,
observed_addr,
protocols: self.protocols.clone(),
};
Expand Down Expand Up @@ -413,3 +427,79 @@ impl Identify {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{config::ConfigBuilder, transport::tcp::config::Config as TcpConfig, Litep2p};
use multiaddr::{Multiaddr, Protocol};

fn create_litep2p() -> (
Litep2p,
Box<dyn Stream<Item = IdentifyEvent> + Send + Unpin>,
PeerId,
) {
let (identify_config, identify) = Config::new(
"1.0.0".to_string(),
Some("litep2p/1.0.0".to_string()),
vec![Multiaddr::empty()],
);

let keypair = crate::crypto::ed25519::Keypair::generate();
let peer = PeerId::from_public_key(&crate::crypto::PublicKey::Ed25519(keypair.public()));
let config = ConfigBuilder::new()
.with_keypair(keypair)
.with_tcp(TcpConfig {
listen_addresses: vec!["/ip6/::1/tcp/0".parse().unwrap()],
..Default::default()
})
.with_libp2p_identify(identify_config)
.build();

(Litep2p::new(config).unwrap(), identify, peer)
}

#[tokio::test]
async fn update_identify_addresses() {
// Create two instances of litep2p
let (mut litep2p1, mut event_stream1, peer1) = create_litep2p();
let (mut litep2p2, mut event_stream2, _peer2) = create_litep2p();
let litep2p1_address =
litep2p1.public_addresses().get_addresses().into_iter().next().unwrap();

let multiaddr: Multiaddr = "/ip6/::9/tcp/111".parse().unwrap();
// Litep2p1 is now reporting the new address.
assert!(litep2p1.public_addresses().add_public_address(multiaddr.clone()).unwrap());

// Dial `litep2p1`
litep2p2.dial_address(litep2p1_address).await.unwrap();

let expected_multiaddr = multiaddr.with(Protocol::P2p(peer1.into()));

tokio::spawn(async move {
loop {
tokio::select! {
_ = litep2p1.next_event() => {}
_event = event_stream1.next() => {}
}
}
});

loop {
tokio::select! {
_ = litep2p2.next_event() => {}
event = event_stream2.next() => match event {
Some(IdentifyEvent::PeerIdentified {
listen_addresses,
..
}) => {
println!(" listen_addresses: {:?}", listen_addresses);
assert!(listen_addresses.iter().any(|address| address == &expected_multiaddr));
break;
}
_ => {}
}
}
}
}
}
10 changes: 8 additions & 2 deletions src/protocol/transport_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use crate::{
error::Error,
protocol::{connection::ConnectionHandle, InnerTransportEvent, TransportEvent},
public_addresses::PublicAddresses,
transport::{manager::TransportManagerHandle, Endpoint},
types::{protocol::ProtocolName, ConnectionId, SubstreamId},
PeerId, DEFAULT_CHANNEL_SIZE,
Expand Down Expand Up @@ -150,13 +151,18 @@ impl TransportService {
transport_handle,
next_substream_id,
connections: HashMap::new(),
keep_alive_timeout: keep_alive_timeout,
keep_alive_timeout,
pending_keep_alive_timeouts: FuturesUnordered::new(),
},
tx,
)
}

/// Get the list of public addresses of the node.
pub fn public_addresses(&self) -> PublicAddresses {
self.transport_handle.public_addresses()
}

/// Handle connection established event.
fn on_connection_established(
&mut self,
Expand Down Expand Up @@ -444,7 +450,7 @@ mod tests {
Arc::new(RwLock::new(HashMap::new())),
cmd_tx,
HashSet::new(),
Default::default(),
PublicAddresses::new(peer),
);

let (service, sender) = TransportService::new(
Expand Down
Loading