From 9ba89194b0242fd01b39836239a87a2da2cc9e6c Mon Sep 17 00:00:00 2001 From: rishflab Date: Thu, 10 Dec 2020 13:19:18 +1100 Subject: [PATCH] Rename swarm driver to event loop --- swap/src/alice.rs | 2 +- .../alice/{swarm_driver.rs => event_loop.rs} | 14 +++++++------- swap/src/alice/execution.rs | 8 ++++---- swap/src/alice/swap.rs | 10 +++++----- swap/src/bob.rs | 2 +- swap/src/bob/{swarm_driver.rs => event_loop.rs} | 17 +++++++---------- swap/src/bob/execution.rs | 4 ++-- swap/src/bob/swap.rs | 6 +++--- swap/tests/e2e.rs | 12 ++++++------ 9 files changed, 36 insertions(+), 39 deletions(-) rename swap/src/alice/{swarm_driver.rs => event_loop.rs} (97%) rename swap/src/bob/{swarm_driver.rs => event_loop.rs} (96%) diff --git a/swap/src/alice.rs b/swap/src/alice.rs index 72d3ec0c..8ccba8a7 100644 --- a/swap/src/alice.rs +++ b/swap/src/alice.rs @@ -37,13 +37,13 @@ use xmr_btc::{ }; mod amounts; +pub mod event_loop; mod execution; mod message0; mod message1; mod message2; mod message3; pub mod swap; -pub mod swarm_driver; pub async fn swap( bitcoin_wallet: Arc, diff --git a/swap/src/alice/swarm_driver.rs b/swap/src/alice/event_loop.rs similarity index 97% rename from swap/src/alice/swarm_driver.rs rename to swap/src/alice/event_loop.rs index e631457c..e8c6857c 100644 --- a/swap/src/alice/swarm_driver.rs +++ b/swap/src/alice/event_loop.rs @@ -29,7 +29,7 @@ impl Default for Channels { } } -pub struct SwarmDriverHandle { +pub struct EventLoopHandle { pub msg0: Receiver, pub msg1: Receiver<(bob::Message1, ResponseChannel)>, pub msg2: Receiver<(bob::Message2, ResponseChannel)>, @@ -41,7 +41,7 @@ pub struct SwarmDriverHandle { pub send_msg2: Sender<(ResponseChannel, alice::Message2)>, } -impl SwarmDriverHandle { +impl EventLoopHandle { pub async fn recv_conn_established(&mut self) -> Result { self.conn_established .recv() @@ -111,7 +111,7 @@ impl SwarmDriverHandle { } } -pub struct SwarmDriver { +pub struct EventLoop { pub swarm: libp2p::Swarm, pub msg0: Sender, pub msg1: Sender<(bob::Message1, ResponseChannel)>, @@ -124,12 +124,12 @@ pub struct SwarmDriver { pub send_msg2: Receiver<(ResponseChannel, alice::Message2)>, } -impl SwarmDriver { +impl EventLoop { pub fn new( transport: SwapTransport, behaviour: Behaviour, listen: Multiaddr, - ) -> Result<(Self, SwarmDriverHandle)> { + ) -> Result<(Self, EventLoopHandle)> { let local_peer_id = behaviour.peer_id(); let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, local_peer_id) @@ -151,7 +151,7 @@ impl SwarmDriver { let send_msg1 = Channels::new(); let send_msg2 = Channels::new(); - let driver = SwarmDriver { + let driver = EventLoop { swarm, msg0: msg0.sender, msg1: msg1.sender, @@ -164,7 +164,7 @@ impl SwarmDriver { send_msg2: send_msg2.receiver, }; - let handle = SwarmDriverHandle { + let handle = EventLoopHandle { msg0: msg0.receiver, msg1: msg1.receiver, msg2: msg2.receiver, diff --git a/swap/src/alice/execution.rs b/swap/src/alice/execution.rs index 76b05a5a..10490310 100644 --- a/swap/src/alice/execution.rs +++ b/swap/src/alice/execution.rs @@ -1,5 +1,5 @@ use crate::{ - alice::swarm_driver::SwarmDriverHandle, bitcoin, monero, network::request_response::AliceToBob, + alice::event_loop::EventLoopHandle, bitcoin, monero, network::request_response::AliceToBob, SwapAmounts, }; use anyhow::{bail, Context, Result}; @@ -33,7 +33,7 @@ pub async fn negotiate( // a: bitcoin::SecretKey, // s_a: cross_curve_dleq::Scalar, // v_a: monero::PrivateViewKey, - swarm_handle: &mut SwarmDriverHandle, + swarm_handle: &mut EventLoopHandle, // bitcoin_wallet: Arc, config: Config, ) -> Result<(ResponseChannel, State3)> { @@ -107,7 +107,7 @@ pub async fn lock_xmr( channel: ResponseChannel, amounts: SwapAmounts, state3: State3, - swarm: &mut SwarmDriverHandle, + swarm: &mut EventLoopHandle, monero_wallet: Arc, ) -> Result<()> where @@ -136,7 +136,7 @@ where } pub async fn wait_for_bitcoin_encrypted_signature( - swarm: &mut SwarmDriverHandle, + swarm: &mut EventLoopHandle, timeout_duration: Duration, ) -> Result { let msg3 = timeout(timeout_duration, swarm.recv_message3()) diff --git a/swap/src/alice/swap.rs b/swap/src/alice/swap.rs index a12ba94e..9732c6fd 100644 --- a/swap/src/alice/swap.rs +++ b/swap/src/alice/swap.rs @@ -2,13 +2,13 @@ //! Alice holds XMR and wishes receive BTC. use crate::{ alice::{ + event_loop::EventLoopHandle, execution::{ build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction, extract_monero_private_key, lock_xmr, negotiate, publish_bitcoin_punish_transaction, publish_bitcoin_redeem_transaction, publish_cancel_transaction, wait_for_bitcoin_encrypted_signature, wait_for_bitcoin_refund, wait_for_locked_bitcoin, }, - swarm_driver::SwarmDriverHandle, }, bitcoin::EncryptedSignature, network::request_response::AliceToBob, @@ -104,11 +104,11 @@ impl fmt::Display for AliceState { pub async fn swap( state: AliceState, - swarm: SwarmDriverHandle, + swarm: EventLoopHandle, bitcoin_wallet: Arc, monero_wallet: Arc, config: Config, -) -> Result<(AliceState, SwarmDriverHandle)> { +) -> Result<(AliceState, EventLoopHandle)> { run_until( state, is_complete, @@ -142,11 +142,11 @@ pub fn is_xmr_locked(state: &AliceState) -> bool { pub async fn run_until( state: AliceState, is_target_state: fn(&AliceState) -> bool, - mut swarm: SwarmDriverHandle, + mut swarm: EventLoopHandle, bitcoin_wallet: Arc, monero_wallet: Arc, config: Config, -) -> Result<(AliceState, SwarmDriverHandle)> { +) -> Result<(AliceState, EventLoopHandle)> { info!("Current state:{}", state); if is_target_state(&state) { Ok((state, swarm)) diff --git a/swap/src/bob.rs b/swap/src/bob.rs index bf7cd48a..40bf6c7c 100644 --- a/swap/src/bob.rs +++ b/swap/src/bob.rs @@ -17,13 +17,13 @@ use tracing::{debug, info, warn}; use uuid::Uuid; mod amounts; +pub mod event_loop; mod execution; mod message0; mod message1; mod message2; mod message3; pub mod swap; -pub mod swarm_driver; use self::{amounts::*, message0::*, message1::*, message2::*, message3::*}; use crate::{ diff --git a/swap/src/bob/swarm_driver.rs b/swap/src/bob/event_loop.rs similarity index 96% rename from swap/src/bob/swarm_driver.rs rename to swap/src/bob/event_loop.rs index f36a9090..2f31b757 100644 --- a/swap/src/bob/swarm_driver.rs +++ b/swap/src/bob/event_loop.rs @@ -30,7 +30,7 @@ impl Default for Channels { } } -pub struct SwarmDriverHandle { +pub struct EventLoopHandle { pub msg0: Receiver, pub msg1: Receiver, pub msg2: Receiver, @@ -43,7 +43,7 @@ pub struct SwarmDriverHandle { pub send_msg3: Sender<(PeerId, EncryptedSignature)>, } -impl SwarmDriverHandle { +impl EventLoopHandle { pub async fn recv_conn_established(&mut self) -> Result { self.conn_established .recv() @@ -112,7 +112,7 @@ impl SwarmDriverHandle { } } -pub struct SwarmDriver { +pub struct EventLoop { pub swarm: libp2p::Swarm, pub msg0: Sender, pub msg1: Sender, @@ -126,11 +126,8 @@ pub struct SwarmDriver { pub send_msg3: Receiver<(PeerId, EncryptedSignature)>, } -impl SwarmDriver { - pub fn new( - transport: SwapTransport, - behaviour: Behaviour, - ) -> Result<(Self, SwarmDriverHandle)> { +impl EventLoop { + pub fn new(transport: SwapTransport, behaviour: Behaviour) -> Result<(Self, EventLoopHandle)> { let local_peer_id = behaviour.peer_id(); let swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, local_peer_id) @@ -150,7 +147,7 @@ impl SwarmDriver { let send_msg2 = Channels::new(); let send_msg3 = Channels::new(); - let driver = SwarmDriver { + let driver = EventLoop { swarm, request_amounts: amounts.receiver, msg0: msg0.sender, @@ -164,7 +161,7 @@ impl SwarmDriver { send_msg3: send_msg3.receiver, }; - let handle = SwarmDriverHandle { + let handle = EventLoopHandle { request_amounts: amounts.sender, msg0: msg0.receiver, msg1: msg1.receiver, diff --git a/swap/src/bob/execution.rs b/swap/src/bob/execution.rs index f74987b8..486a3e0a 100644 --- a/swap/src/bob/execution.rs +++ b/swap/src/bob/execution.rs @@ -1,4 +1,4 @@ -use crate::{bob::swarm_driver::SwarmDriverHandle, SwapAmounts}; +use crate::{bob::event_loop::EventLoopHandle, SwapAmounts}; use anyhow::Result; use libp2p::core::Multiaddr; use rand::{CryptoRng, RngCore}; @@ -8,7 +8,7 @@ use xmr_btc::bob::State2; pub async fn negotiate( state0: xmr_btc::bob::State0, amounts: SwapAmounts, - swarm: &mut SwarmDriverHandle, + swarm: &mut EventLoopHandle, addr: Multiaddr, mut rng: R, bitcoin_wallet: Arc, diff --git a/swap/src/bob/swap.rs b/swap/src/bob/swap.rs index 35c3c3be..9f574b8d 100644 --- a/swap/src/bob/swap.rs +++ b/swap/src/bob/swap.rs @@ -1,5 +1,5 @@ use crate::{ - bob::{execution::negotiate, swarm_driver::SwarmDriverHandle}, + bob::{event_loop::EventLoopHandle, execution::negotiate}, storage::Database, SwapAmounts, }; @@ -53,7 +53,7 @@ impl fmt::Display for BobState { pub async fn swap( state: BobState, - swarm: SwarmDriverHandle, + swarm: EventLoopHandle, db: Database, bitcoin_wallet: Arc, monero_wallet: Arc, @@ -100,7 +100,7 @@ pub fn is_xmr_locked(state: &BobState) -> bool { pub async fn run_until( state: BobState, is_target_state: fn(&BobState) -> bool, - mut swarm: SwarmDriverHandle, + mut swarm: EventLoopHandle, db: Database, bitcoin_wallet: Arc, monero_wallet: Arc, diff --git a/swap/tests/e2e.rs b/swap/tests/e2e.rs index 9dd35589..248b1335 100644 --- a/swap/tests/e2e.rs +++ b/swap/tests/e2e.rs @@ -222,8 +222,8 @@ async fn init_alice( listen: Multiaddr, ) -> ( AliceState, - alice::swarm_driver::SwarmDriver, - alice::swarm_driver::SwarmDriverHandle, + alice::event_loop::EventLoop, + alice::event_loop::EventLoopHandle, Arc, Arc, PeerId, @@ -281,7 +281,7 @@ async fn init_alice( let alice_transport = build(alice_behaviour.identity()).unwrap(); let (swarm_driver, handle) = - alice::swarm_driver::SwarmDriver::new(alice_transport, alice_behaviour, listen).unwrap(); + alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen).unwrap(); ( alice_state, @@ -305,8 +305,8 @@ async fn init_bob( xmr_stating_balance: xmr_btc::monero::Amount, ) -> ( BobState, - bob::swarm_driver::SwarmDriver, - bob::swarm_driver::SwarmDriverHandle, + bob::event_loop::EventLoop, + bob::event_loop::EventLoopHandle, Arc, Arc, Database, @@ -358,7 +358,7 @@ async fn init_bob( }; let (swarm_driver, swarm_handle) = - bob::swarm_driver::SwarmDriver::new(bob_transport, bob_behaviour).unwrap(); + bob::event_loop::EventLoop::new(bob_transport, bob_behaviour).unwrap(); ( bob_state,