From 87be9aeb2a71aa625f1420524aff8d5c3039b164 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Mon, 8 Feb 2021 14:49:36 +1100 Subject: [PATCH] Prepare separation of event loop initialisation to swap initialisation As for Alice, the event loop will be started with the program and will be the one starting swaps (`run_until`) based on libp2p events (swap request). --- swap/src/protocol/alice.rs | 18 ++++-------------- swap/src/protocol/alice/event_loop.rs | 8 +++++--- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/swap/src/protocol/alice.rs b/swap/src/protocol/alice.rs index cf9b883b..c228b136 100644 --- a/swap/src/protocol/alice.rs +++ b/swap/src/protocol/alice.rs @@ -7,7 +7,6 @@ use crate::{ monero, network::{ peer_tracker::{self, PeerTracker}, - transport::build, Seed as NetworkSeed, }, protocol::{bob::EncryptedSignature, SwapAmounts}, @@ -113,7 +112,8 @@ impl Builder { .make_initial_state(swap_amounts.btc, swap_amounts.xmr) .await?; - let (event_loop, event_loop_handle) = self.init_event_loop()?; + let (event_loop, event_loop_handle) = + EventLoop::new(self.identity.clone(), self.listen_address(), self.peer_id)?; Ok(( Swap { @@ -139,7 +139,8 @@ impl Builder { ) }; - let (event_loop, event_loop_handle) = self.init_event_loop()?; + let (event_loop, event_loop_handle) = + EventLoop::new(self.identity.clone(), self.listen_address(), self.peer_id)?; Ok(( Swap { @@ -186,17 +187,6 @@ impl Builder { Ok(AliceState::Started { amounts, state0 }) } - - fn init_event_loop(&self) -> Result<(EventLoop, EventLoopHandle)> { - let alice_behaviour = Behaviour::default(); - let alice_transport = build(self.identity.clone())?; - EventLoop::new( - alice_transport, - alice_behaviour, - self.listen_address(), - self.peer_id, - ) - } } #[derive(Debug)] diff --git a/swap/src/protocol/alice/event_loop.rs b/swap/src/protocol/alice/event_loop.rs index b2b3873c..2cc0af2a 100644 --- a/swap/src/protocol/alice/event_loop.rs +++ b/swap/src/protocol/alice/event_loop.rs @@ -1,6 +1,6 @@ use crate::{ execution_params::ExecutionParams, - network::{transport::SwapTransport, TokioExecutor}, + network::{transport, TokioExecutor}, protocol::{ alice::{Behaviour, OutEvent, State0, State3, SwapResponse, TransferProof}, bob::{EncryptedSignature, SwapRequest}, @@ -135,11 +135,13 @@ pub struct EventLoop { impl EventLoop { pub fn new( - transport: SwapTransport, - behaviour: Behaviour, + identity: libp2p::identity::Keypair, listen: Multiaddr, peer_id: PeerId, ) -> Result<(Self, EventLoopHandle)> { + let behaviour = Behaviour::default(); + let transport = transport::build(identity)?; + let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, peer_id) .executor(Box::new(TokioExecutor { handle: tokio::runtime::Handle::current(),