From 3c68026cfeddb18237cf316971dcbe2c17824031 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Tue, 19 Jan 2021 14:22:09 +1100 Subject: [PATCH] Move init event loop in `bob::SwapFactory` --- swap/src/protocol/bob.rs | 47 +++++++++++++++------------------------- 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/swap/src/protocol/bob.rs b/swap/src/protocol/bob.rs index 560d69e9..b0f5a563 100644 --- a/swap/src/protocol/bob.rs +++ b/swap/src/protocol/bob.rs @@ -107,12 +107,7 @@ impl SwapFactory { ) .await?; - let (event_loop, event_loop_handle) = init_bob_event_loop( - self.identity.clone(), - self.peer_id.clone(), - self.alice_connect_peer_id.clone(), - self.alice_connect_address.clone(), - )?; + let (event_loop, event_loop_handle) = self.init_event_loop()?; let db = Database::open(self.db_path.as_path())?; @@ -142,12 +137,7 @@ impl SwapFactory { ) }; - let (event_loop, event_loop_handle) = init_bob_event_loop( - self.identity.clone(), - self.peer_id.clone(), - self.alice_connect_peer_id.clone(), - self.alice_connect_address.clone(), - )?; + let (event_loop, event_loop_handle) = self.init_event_loop()?; Ok(( Swap { @@ -161,6 +151,21 @@ impl SwapFactory { event_loop, )) } + + fn init_event_loop( + &self, + ) -> Result<(bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle)> { + let bob_behaviour = bob::Behaviour::default(); + let bob_transport = build(self.identity.clone())?; + + bob::event_loop::EventLoop::new( + bob_transport, + bob_behaviour, + self.peer_id.clone(), + self.alice_connect_peer_id.clone(), + self.alice_connect_address.clone(), + ) + } } async fn init_bob_state( @@ -188,24 +193,6 @@ async fn init_bob_state( Ok(BobState::Started { state0, amounts }) } -fn init_bob_event_loop( - identity: Keypair, - peer_id: PeerId, - alice_peer_id: PeerId, - alice_addr: Multiaddr, -) -> Result<(bob::event_loop::EventLoop, bob::event_loop::EventLoopHandle)> { - let bob_behaviour = bob::Behaviour::default(); - let bob_transport = build(identity)?; - - bob::event_loop::EventLoop::new( - bob_transport, - bob_behaviour, - peer_id, - alice_peer_id, - alice_addr, - ) -} - #[derive(Debug, Clone)] pub enum OutEvent { ConnectionEstablished(PeerId),