7799d888f6
* chore: upgrade the libp2p to 0.18.(0|1) * refactor: fix most of the swarm with todos mainly leaving the multiple connections case to panic. * feat: add ability to configure listening addr the original random listening address is still in place. * add test case for the panic with two connections * fix: allow multiple connections in p2p/swarm * doc: Connection and the any connecting address * chore: cleanup libp2p patching examples * refactor: rename SwarmApi::stats perhaps roundtrip_times is more readable. * chore: upgrade to libp2p 0.19 * fix: awaiting for binding new listening addresses the initial implementation didn't actually wait for anything other than asking the swarm to start listening, but now the await will complete only after a listening address has actually been bound. this required some nasty matching on the multiaddr protocol parts which will hopefully ever be used by test code but ... it works now, at least a bit better. * fix test on macos, fix unspecified addr handling
126 lines
3.6 KiB
Rust
126 lines
3.6 KiB
Rust
use async_std::task;
|
|
|
|
/// Make sure two instances of ipfs can be connected.
|
|
#[test]
|
|
fn connect_two_nodes() {
|
|
// env_logger::init();
|
|
|
|
// make sure the connection will only happen through explicit connect
|
|
let mdns = false;
|
|
|
|
let (tx, rx) = futures::channel::oneshot::channel();
|
|
|
|
let node_a = task::spawn(async move {
|
|
let opts = ipfs::IpfsOptions::inmemory_with_generated_keys(mdns);
|
|
let (ipfs, fut) = ipfs::UninitializedIpfs::new(opts)
|
|
.await
|
|
.start()
|
|
.await
|
|
.unwrap();
|
|
|
|
let jh = task::spawn(fut);
|
|
|
|
let (pk, addrs) = ipfs
|
|
.identity()
|
|
.await
|
|
.expect("failed to read identity() on node_a");
|
|
assert!(!addrs.is_empty());
|
|
tx.send((pk, addrs, ipfs, jh)).unwrap();
|
|
});
|
|
|
|
task::block_on(async move {
|
|
let (other_pk, other_addrs, other_ipfs, other_jh) = rx.await.unwrap();
|
|
|
|
println!("got back from the other node: {:?}", other_addrs);
|
|
|
|
let opts = ipfs::IpfsOptions::inmemory_with_generated_keys(mdns);
|
|
let (ipfs, fut) = ipfs::UninitializedIpfs::new(opts)
|
|
.await
|
|
.start()
|
|
.await
|
|
.unwrap();
|
|
let jh = task::spawn(fut);
|
|
|
|
let _other_peerid = other_pk.into_peer_id();
|
|
|
|
let mut connected = None;
|
|
|
|
for addr in other_addrs {
|
|
println!("trying {}", addr);
|
|
match ipfs.connect(addr.clone()).await {
|
|
Ok(_) => {
|
|
connected = Some(addr);
|
|
break;
|
|
}
|
|
Err(e) => {
|
|
println!("Failed connecting to {}: {}", addr, e);
|
|
}
|
|
}
|
|
}
|
|
|
|
let connected = connected.expect("Failed to connect to anything");
|
|
println!("connected to {}", connected);
|
|
|
|
other_ipfs.exit_daemon().await;
|
|
other_jh.await;
|
|
node_a.await;
|
|
|
|
ipfs.exit_daemon().await;
|
|
jh.await;
|
|
});
|
|
}
|
|
|
|
/// More complicated one to the above; first node will have two listening addresses and the second
|
|
/// one should dial both of the addresses, resulting in two connections.
|
|
#[test]
|
|
fn connect_two_nodes_with_two_connections_doesnt_panic() {
|
|
const MDNS: bool = false;
|
|
|
|
task::block_on(async move {
|
|
let node_a = ipfs::Node::new(MDNS).await;
|
|
let node_b = ipfs::Node::new(MDNS).await;
|
|
|
|
node_a
|
|
.add_listening_address(libp2p::build_multiaddr!(Ip4([127, 0, 0, 1]), Tcp(0u16)))
|
|
.await
|
|
.unwrap();
|
|
|
|
let addresses = node_a.addrs_local().await.unwrap();
|
|
assert_eq!(
|
|
addresses.len(),
|
|
2,
|
|
"there should had been two local addresses, found {:?}",
|
|
addresses
|
|
);
|
|
|
|
for addr in addresses {
|
|
node_b.connect(addr).await.unwrap();
|
|
}
|
|
|
|
// not too sure on this, since there'll be a single peer but two connections; the return
|
|
// type is `Vec<Connection>` but it's peer with any connection.
|
|
let mut peers = node_a.peers().await.unwrap();
|
|
assert_eq!(
|
|
peers.len(),
|
|
1,
|
|
"there should had been one peer, found {:?}",
|
|
peers
|
|
);
|
|
|
|
// sadly we are unable to currently verify that there exists two connections for the node_b
|
|
// peer..
|
|
|
|
node_a
|
|
.disconnect(peers.remove(0).address)
|
|
.await
|
|
.expect("failed to disconnect peer_b at peer_a");
|
|
|
|
let peers = node_a.peers().await.unwrap();
|
|
assert!(
|
|
peers.is_empty(),
|
|
"node_b was still connected after disconnect: {:?}",
|
|
peers
|
|
);
|
|
});
|
|
}
|