Rename swarm driver to event loop

This commit is contained in:
rishflab 2020-12-10 13:19:18 +11:00
parent f5d3d54b13
commit 9ba89194b0
9 changed files with 36 additions and 39 deletions

View File

@ -37,13 +37,13 @@ use xmr_btc::{
}; };
mod amounts; mod amounts;
pub mod event_loop;
mod execution; mod execution;
mod message0; mod message0;
mod message1; mod message1;
mod message2; mod message2;
mod message3; mod message3;
pub mod swap; pub mod swap;
pub mod swarm_driver;
pub async fn swap( pub async fn swap(
bitcoin_wallet: Arc<bitcoin::Wallet>, bitcoin_wallet: Arc<bitcoin::Wallet>,

View File

@ -29,7 +29,7 @@ impl<T> Default for Channels<T> {
} }
} }
pub struct SwarmDriverHandle { pub struct EventLoopHandle {
pub msg0: Receiver<bob::Message0>, pub msg0: Receiver<bob::Message0>,
pub msg1: Receiver<(bob::Message1, ResponseChannel<AliceToBob>)>, pub msg1: Receiver<(bob::Message1, ResponseChannel<AliceToBob>)>,
pub msg2: Receiver<(bob::Message2, ResponseChannel<AliceToBob>)>, pub msg2: Receiver<(bob::Message2, ResponseChannel<AliceToBob>)>,
@ -41,7 +41,7 @@ pub struct SwarmDriverHandle {
pub send_msg2: Sender<(ResponseChannel<AliceToBob>, alice::Message2)>, pub send_msg2: Sender<(ResponseChannel<AliceToBob>, alice::Message2)>,
} }
impl SwarmDriverHandle { impl EventLoopHandle {
pub async fn recv_conn_established(&mut self) -> Result<PeerId> { pub async fn recv_conn_established(&mut self) -> Result<PeerId> {
self.conn_established self.conn_established
.recv() .recv()
@ -111,7 +111,7 @@ impl SwarmDriverHandle {
} }
} }
pub struct SwarmDriver { pub struct EventLoop {
pub swarm: libp2p::Swarm<Behaviour>, pub swarm: libp2p::Swarm<Behaviour>,
pub msg0: Sender<bob::Message0>, pub msg0: Sender<bob::Message0>,
pub msg1: Sender<(bob::Message1, ResponseChannel<AliceToBob>)>, pub msg1: Sender<(bob::Message1, ResponseChannel<AliceToBob>)>,
@ -124,12 +124,12 @@ pub struct SwarmDriver {
pub send_msg2: Receiver<(ResponseChannel<AliceToBob>, alice::Message2)>, pub send_msg2: Receiver<(ResponseChannel<AliceToBob>, alice::Message2)>,
} }
impl SwarmDriver { impl EventLoop {
pub fn new( pub fn new(
transport: SwapTransport, transport: SwapTransport,
behaviour: Behaviour, behaviour: Behaviour,
listen: Multiaddr, listen: Multiaddr,
) -> Result<(Self, SwarmDriverHandle)> { ) -> Result<(Self, EventLoopHandle)> {
let local_peer_id = behaviour.peer_id(); let local_peer_id = behaviour.peer_id();
let mut swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, local_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_msg1 = Channels::new();
let send_msg2 = Channels::new(); let send_msg2 = Channels::new();
let driver = SwarmDriver { let driver = EventLoop {
swarm, swarm,
msg0: msg0.sender, msg0: msg0.sender,
msg1: msg1.sender, msg1: msg1.sender,
@ -164,7 +164,7 @@ impl SwarmDriver {
send_msg2: send_msg2.receiver, send_msg2: send_msg2.receiver,
}; };
let handle = SwarmDriverHandle { let handle = EventLoopHandle {
msg0: msg0.receiver, msg0: msg0.receiver,
msg1: msg1.receiver, msg1: msg1.receiver,
msg2: msg2.receiver, msg2: msg2.receiver,

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
alice::swarm_driver::SwarmDriverHandle, bitcoin, monero, network::request_response::AliceToBob, alice::event_loop::EventLoopHandle, bitcoin, monero, network::request_response::AliceToBob,
SwapAmounts, SwapAmounts,
}; };
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
@ -33,7 +33,7 @@ pub async fn negotiate(
// a: bitcoin::SecretKey, // a: bitcoin::SecretKey,
// s_a: cross_curve_dleq::Scalar, // s_a: cross_curve_dleq::Scalar,
// v_a: monero::PrivateViewKey, // v_a: monero::PrivateViewKey,
swarm_handle: &mut SwarmDriverHandle, swarm_handle: &mut EventLoopHandle,
// bitcoin_wallet: Arc<bitcoin::Wallet>, // bitcoin_wallet: Arc<bitcoin::Wallet>,
config: Config, config: Config,
) -> Result<(ResponseChannel<AliceToBob>, State3)> { ) -> Result<(ResponseChannel<AliceToBob>, State3)> {
@ -107,7 +107,7 @@ pub async fn lock_xmr<W>(
channel: ResponseChannel<AliceToBob>, channel: ResponseChannel<AliceToBob>,
amounts: SwapAmounts, amounts: SwapAmounts,
state3: State3, state3: State3,
swarm: &mut SwarmDriverHandle, swarm: &mut EventLoopHandle,
monero_wallet: Arc<W>, monero_wallet: Arc<W>,
) -> Result<()> ) -> Result<()>
where where
@ -136,7 +136,7 @@ where
} }
pub async fn wait_for_bitcoin_encrypted_signature( pub async fn wait_for_bitcoin_encrypted_signature(
swarm: &mut SwarmDriverHandle, swarm: &mut EventLoopHandle,
timeout_duration: Duration, timeout_duration: Duration,
) -> Result<EncryptedSignature> { ) -> Result<EncryptedSignature> {
let msg3 = timeout(timeout_duration, swarm.recv_message3()) let msg3 = timeout(timeout_duration, swarm.recv_message3())

View File

@ -2,13 +2,13 @@
//! Alice holds XMR and wishes receive BTC. //! Alice holds XMR and wishes receive BTC.
use crate::{ use crate::{
alice::{ alice::{
event_loop::EventLoopHandle,
execution::{ execution::{
build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction, build_bitcoin_punish_transaction, build_bitcoin_redeem_transaction,
extract_monero_private_key, lock_xmr, negotiate, publish_bitcoin_punish_transaction, extract_monero_private_key, lock_xmr, negotiate, publish_bitcoin_punish_transaction,
publish_bitcoin_redeem_transaction, publish_cancel_transaction, publish_bitcoin_redeem_transaction, publish_cancel_transaction,
wait_for_bitcoin_encrypted_signature, wait_for_bitcoin_refund, wait_for_locked_bitcoin, wait_for_bitcoin_encrypted_signature, wait_for_bitcoin_refund, wait_for_locked_bitcoin,
}, },
swarm_driver::SwarmDriverHandle,
}, },
bitcoin::EncryptedSignature, bitcoin::EncryptedSignature,
network::request_response::AliceToBob, network::request_response::AliceToBob,
@ -104,11 +104,11 @@ impl fmt::Display for AliceState {
pub async fn swap( pub async fn swap(
state: AliceState, state: AliceState,
swarm: SwarmDriverHandle, swarm: EventLoopHandle,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>, bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
monero_wallet: Arc<crate::monero::Wallet>, monero_wallet: Arc<crate::monero::Wallet>,
config: Config, config: Config,
) -> Result<(AliceState, SwarmDriverHandle)> { ) -> Result<(AliceState, EventLoopHandle)> {
run_until( run_until(
state, state,
is_complete, is_complete,
@ -142,11 +142,11 @@ pub fn is_xmr_locked(state: &AliceState) -> bool {
pub async fn run_until( pub async fn run_until(
state: AliceState, state: AliceState,
is_target_state: fn(&AliceState) -> bool, is_target_state: fn(&AliceState) -> bool,
mut swarm: SwarmDriverHandle, mut swarm: EventLoopHandle,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>, bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
monero_wallet: Arc<crate::monero::Wallet>, monero_wallet: Arc<crate::monero::Wallet>,
config: Config, config: Config,
) -> Result<(AliceState, SwarmDriverHandle)> { ) -> Result<(AliceState, EventLoopHandle)> {
info!("Current state:{}", state); info!("Current state:{}", state);
if is_target_state(&state) { if is_target_state(&state) {
Ok((state, swarm)) Ok((state, swarm))

View File

@ -17,13 +17,13 @@ use tracing::{debug, info, warn};
use uuid::Uuid; use uuid::Uuid;
mod amounts; mod amounts;
pub mod event_loop;
mod execution; mod execution;
mod message0; mod message0;
mod message1; mod message1;
mod message2; mod message2;
mod message3; mod message3;
pub mod swap; pub mod swap;
pub mod swarm_driver;
use self::{amounts::*, message0::*, message1::*, message2::*, message3::*}; use self::{amounts::*, message0::*, message1::*, message2::*, message3::*};
use crate::{ use crate::{

View File

@ -30,7 +30,7 @@ impl<T> Default for Channels<T> {
} }
} }
pub struct SwarmDriverHandle { pub struct EventLoopHandle {
pub msg0: Receiver<alice::Message0>, pub msg0: Receiver<alice::Message0>,
pub msg1: Receiver<alice::Message1>, pub msg1: Receiver<alice::Message1>,
pub msg2: Receiver<alice::Message2>, pub msg2: Receiver<alice::Message2>,
@ -43,7 +43,7 @@ pub struct SwarmDriverHandle {
pub send_msg3: Sender<(PeerId, EncryptedSignature)>, pub send_msg3: Sender<(PeerId, EncryptedSignature)>,
} }
impl SwarmDriverHandle { impl EventLoopHandle {
pub async fn recv_conn_established(&mut self) -> Result<PeerId> { pub async fn recv_conn_established(&mut self) -> Result<PeerId> {
self.conn_established self.conn_established
.recv() .recv()
@ -112,7 +112,7 @@ impl SwarmDriverHandle {
} }
} }
pub struct SwarmDriver { pub struct EventLoop {
pub swarm: libp2p::Swarm<Behaviour>, pub swarm: libp2p::Swarm<Behaviour>,
pub msg0: Sender<alice::Message0>, pub msg0: Sender<alice::Message0>,
pub msg1: Sender<alice::Message1>, pub msg1: Sender<alice::Message1>,
@ -126,11 +126,8 @@ pub struct SwarmDriver {
pub send_msg3: Receiver<(PeerId, EncryptedSignature)>, pub send_msg3: Receiver<(PeerId, EncryptedSignature)>,
} }
impl SwarmDriver { impl EventLoop {
pub fn new( pub fn new(transport: SwapTransport, behaviour: Behaviour) -> Result<(Self, EventLoopHandle)> {
transport: SwapTransport,
behaviour: Behaviour,
) -> Result<(Self, SwarmDriverHandle)> {
let local_peer_id = behaviour.peer_id(); let local_peer_id = behaviour.peer_id();
let swarm = libp2p::swarm::SwarmBuilder::new(transport, behaviour, local_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_msg2 = Channels::new();
let send_msg3 = Channels::new(); let send_msg3 = Channels::new();
let driver = SwarmDriver { let driver = EventLoop {
swarm, swarm,
request_amounts: amounts.receiver, request_amounts: amounts.receiver,
msg0: msg0.sender, msg0: msg0.sender,
@ -164,7 +161,7 @@ impl SwarmDriver {
send_msg3: send_msg3.receiver, send_msg3: send_msg3.receiver,
}; };
let handle = SwarmDriverHandle { let handle = EventLoopHandle {
request_amounts: amounts.sender, request_amounts: amounts.sender,
msg0: msg0.receiver, msg0: msg0.receiver,
msg1: msg1.receiver, msg1: msg1.receiver,

View File

@ -1,4 +1,4 @@
use crate::{bob::swarm_driver::SwarmDriverHandle, SwapAmounts}; use crate::{bob::event_loop::EventLoopHandle, SwapAmounts};
use anyhow::Result; use anyhow::Result;
use libp2p::core::Multiaddr; use libp2p::core::Multiaddr;
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
@ -8,7 +8,7 @@ use xmr_btc::bob::State2;
pub async fn negotiate<R>( pub async fn negotiate<R>(
state0: xmr_btc::bob::State0, state0: xmr_btc::bob::State0,
amounts: SwapAmounts, amounts: SwapAmounts,
swarm: &mut SwarmDriverHandle, swarm: &mut EventLoopHandle,
addr: Multiaddr, addr: Multiaddr,
mut rng: R, mut rng: R,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>, bitcoin_wallet: Arc<crate::bitcoin::Wallet>,

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
bob::{execution::negotiate, swarm_driver::SwarmDriverHandle}, bob::{event_loop::EventLoopHandle, execution::negotiate},
storage::Database, storage::Database,
SwapAmounts, SwapAmounts,
}; };
@ -53,7 +53,7 @@ impl fmt::Display for BobState {
pub async fn swap<R>( pub async fn swap<R>(
state: BobState, state: BobState,
swarm: SwarmDriverHandle, swarm: EventLoopHandle,
db: Database, db: Database,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>, bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
monero_wallet: Arc<crate::monero::Wallet>, monero_wallet: Arc<crate::monero::Wallet>,
@ -100,7 +100,7 @@ pub fn is_xmr_locked(state: &BobState) -> bool {
pub async fn run_until<R>( pub async fn run_until<R>(
state: BobState, state: BobState,
is_target_state: fn(&BobState) -> bool, is_target_state: fn(&BobState) -> bool,
mut swarm: SwarmDriverHandle, mut swarm: EventLoopHandle,
db: Database, db: Database,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>, bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
monero_wallet: Arc<crate::monero::Wallet>, monero_wallet: Arc<crate::monero::Wallet>,

View File

@ -222,8 +222,8 @@ async fn init_alice(
listen: Multiaddr, listen: Multiaddr,
) -> ( ) -> (
AliceState, AliceState,
alice::swarm_driver::SwarmDriver, alice::event_loop::EventLoop,
alice::swarm_driver::SwarmDriverHandle, alice::event_loop::EventLoopHandle,
Arc<swap::bitcoin::Wallet>, Arc<swap::bitcoin::Wallet>,
Arc<swap::monero::Wallet>, Arc<swap::monero::Wallet>,
PeerId, PeerId,
@ -281,7 +281,7 @@ async fn init_alice(
let alice_transport = build(alice_behaviour.identity()).unwrap(); let alice_transport = build(alice_behaviour.identity()).unwrap();
let (swarm_driver, handle) = 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, alice_state,
@ -305,8 +305,8 @@ async fn init_bob(
xmr_stating_balance: xmr_btc::monero::Amount, xmr_stating_balance: xmr_btc::monero::Amount,
) -> ( ) -> (
BobState, BobState,
bob::swarm_driver::SwarmDriver, bob::event_loop::EventLoop,
bob::swarm_driver::SwarmDriverHandle, bob::event_loop::EventLoopHandle,
Arc<swap::bitcoin::Wallet>, Arc<swap::bitcoin::Wallet>,
Arc<swap::monero::Wallet>, Arc<swap::monero::Wallet>,
Database, Database,
@ -358,7 +358,7 @@ async fn init_bob(
}; };
let (swarm_driver, swarm_handle) = 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, bob_state,