forked from libp2p/rust-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer_address.rs
61 lines (49 loc) · 2.01 KB
/
peer_address.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use libp2p_core::ConnectedPoint;
use libp2p_request_response as request_response;
use libp2p_request_response::ProtocolSupport;
use libp2p_swarm::{StreamProtocol, Swarm, SwarmEvent};
use libp2p_swarm_test::SwarmExt;
use serde::{Deserialize, Serialize};
use std::iter;
use tracing_subscriber::EnvFilter;
#[async_std::test]
#[cfg(feature = "cbor")]
async fn dial_succeeds_after_adding_peers_address() {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.try_init();
let protocols = iter::once((StreamProtocol::new("/ping/1"), ProtocolSupport::Full));
let config = request_response::Config::default();
let mut swarm = Swarm::new_ephemeral(|_| {
request_response::cbor::Behaviour::<Ping, Pong>::new(protocols.clone(), config.clone())
});
let mut swarm2 = Swarm::new_ephemeral(|_| {
request_response::cbor::Behaviour::<Ping, Pong>::new(protocols.clone(), config.clone())
});
let peer_id2 = *swarm2.local_peer_id();
let (listen_addr, _) = swarm2.listen().with_memory_addr_external().await;
swarm.add_peer_address(peer_id2, listen_addr.clone());
swarm.dial(peer_id2).unwrap();
async_std::task::spawn(swarm2.loop_on_next());
let (connected_peer_id, connected_address) = swarm
.wait(|event| match event {
SwarmEvent::ConnectionEstablished {
peer_id, endpoint, ..
} => {
let address = match endpoint {
ConnectedPoint::Dialer { address, .. } => Some(address),
_ => None,
};
Some((peer_id, address))
}
_ => None,
})
.await;
let expected_address = listen_addr.with_p2p(peer_id2).unwrap();
assert_eq!(connected_peer_id, peer_id2);
assert_eq!(expected_address, connected_address.unwrap());
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct Ping(Vec<u8>);
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct Pong(Vec<u8>);