|
|
|
@ -1,8 +1,7 @@
|
|
|
|
|
use libp2p::futures::future;
|
|
|
|
|
use libp2p::futures::future::FutureExt;
|
|
|
|
|
use libp2p::{
|
|
|
|
|
core::{
|
|
|
|
|
muxing::StreamMuxerBox, transport::memory::MemoryTransport, upgrade::Version,
|
|
|
|
|
},
|
|
|
|
|
core::{muxing::StreamMuxerBox, transport::memory::MemoryTransport, upgrade::Version},
|
|
|
|
|
identity,
|
|
|
|
|
noise::{self, NoiseConfig, X25519Spec},
|
|
|
|
|
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
|
|
|
@ -10,9 +9,8 @@ use libp2p::{
|
|
|
|
|
Multiaddr, PeerId, Swarm, Transport,
|
|
|
|
|
};
|
|
|
|
|
use std::{fmt::Debug, future::Future, time::Duration};
|
|
|
|
|
use tokio::time;
|
|
|
|
|
use tokio::runtime::Handle;
|
|
|
|
|
use libp2p::futures::future::FutureExt;
|
|
|
|
|
use tokio::time;
|
|
|
|
|
|
|
|
|
|
#[allow(missing_debug_implementations)]
|
|
|
|
|
pub struct Actor<B: NetworkBehaviour> {
|
|
|
|
@ -25,7 +23,8 @@ pub async fn new_connected_swarm_pair<B, F>(behaviour_fn: F, handle: Handle) ->
|
|
|
|
|
where
|
|
|
|
|
B: NetworkBehaviour,
|
|
|
|
|
F: Fn(PeerId, identity::Keypair) -> B + Clone,
|
|
|
|
|
<B as NetworkBehaviour>::OutEvent: Debug{
|
|
|
|
|
<B as NetworkBehaviour>::OutEvent: Debug,
|
|
|
|
|
{
|
|
|
|
|
let (swarm, addr, peer_id) = new_swarm(behaviour_fn.clone(), handle.clone());
|
|
|
|
|
let mut alice = Actor {
|
|
|
|
|
swarm,
|
|
|
|
@ -45,7 +44,10 @@ where
|
|
|
|
|
(alice, bob)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new_swarm<B: NetworkBehaviour, F: Fn(PeerId, identity::Keypair) -> B>(behaviour_fn: F, handle: Handle) -> (Swarm<B>, Multiaddr, PeerId) {
|
|
|
|
|
pub fn new_swarm<B: NetworkBehaviour, F: Fn(PeerId, identity::Keypair) -> B>(
|
|
|
|
|
behaviour_fn: F,
|
|
|
|
|
handle: Handle,
|
|
|
|
|
) -> (Swarm<B>, Multiaddr, PeerId) {
|
|
|
|
|
let id_keys = identity::Keypair::generate_ed25519();
|
|
|
|
|
let peer_id = PeerId::from(id_keys.public());
|
|
|
|
|
|
|
|
|
@ -61,15 +63,11 @@ pub fn new_swarm<B: NetworkBehaviour, F: Fn(PeerId, identity::Keypair) -> B>(beh
|
|
|
|
|
.map(|(peer, muxer), _| (peer, StreamMuxerBox::new(muxer)))
|
|
|
|
|
.boxed();
|
|
|
|
|
|
|
|
|
|
let mut swarm: Swarm<B> = SwarmBuilder::new(
|
|
|
|
|
transport,
|
|
|
|
|
behaviour_fn(peer_id.clone(), id_keys),
|
|
|
|
|
peer_id.clone(),
|
|
|
|
|
)
|
|
|
|
|
.executor(Box::new(move |f| {
|
|
|
|
|
handle.spawn(f);
|
|
|
|
|
}))
|
|
|
|
|
.build();
|
|
|
|
|
let mut swarm: Swarm<B> = SwarmBuilder::new(transport, behaviour_fn(peer_id, id_keys), peer_id)
|
|
|
|
|
.executor(Box::new(move |f| {
|
|
|
|
|
handle.spawn(f);
|
|
|
|
|
}))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let address_port = rand::random::<u64>();
|
|
|
|
|
let addr = format!("/memory/{}", address_port)
|
|
|
|
@ -104,9 +102,10 @@ pub async fn await_events_or_timeout<A, B>(
|
|
|
|
|
/// connection phase. Any event emitted is considered a bug from this functions
|
|
|
|
|
/// PoV because they would be lost.
|
|
|
|
|
pub async fn connect<B>(alice: &mut Swarm<B>, bob: &mut Swarm<B>)
|
|
|
|
|
where
|
|
|
|
|
B: NetworkBehaviour,
|
|
|
|
|
<B as NetworkBehaviour>::OutEvent: Debug{
|
|
|
|
|
where
|
|
|
|
|
B: NetworkBehaviour,
|
|
|
|
|
<B as NetworkBehaviour>::OutEvent: Debug,
|
|
|
|
|
{
|
|
|
|
|
let mut alice_connected = false;
|
|
|
|
|
let mut bob_connected = false;
|
|
|
|
|
|
|
|
|
|