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;
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<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 msg1: Receiver<(bob::Message1, ResponseChannel<AliceToBob>)>,
pub msg2: Receiver<(bob::Message2, ResponseChannel<AliceToBob>)>,
@ -41,7 +41,7 @@ pub struct SwarmDriverHandle {
pub send_msg2: Sender<(ResponseChannel<AliceToBob>, alice::Message2)>,
}
impl SwarmDriverHandle {
impl EventLoopHandle {
pub async fn recv_conn_established(&mut self) -> Result<PeerId> {
self.conn_established
.recv()
@ -111,7 +111,7 @@ impl SwarmDriverHandle {
}
}
pub struct SwarmDriver {
pub struct EventLoop {
pub swarm: libp2p::Swarm<Behaviour>,
pub msg0: Sender<bob::Message0>,
pub msg1: Sender<(bob::Message1, ResponseChannel<AliceToBob>)>,
@ -124,12 +124,12 @@ pub struct SwarmDriver {
pub send_msg2: Receiver<(ResponseChannel<AliceToBob>, 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,

View File

@ -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<bitcoin::Wallet>,
config: Config,
) -> Result<(ResponseChannel<AliceToBob>, State3)> {
@ -107,7 +107,7 @@ pub async fn lock_xmr<W>(
channel: ResponseChannel<AliceToBob>,
amounts: SwapAmounts,
state3: State3,
swarm: &mut SwarmDriverHandle,
swarm: &mut EventLoopHandle,
monero_wallet: Arc<W>,
) -> 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<EncryptedSignature> {
let msg3 = timeout(timeout_duration, swarm.recv_message3())

View File

@ -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<crate::bitcoin::Wallet>,
monero_wallet: Arc<crate::monero::Wallet>,
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<crate::bitcoin::Wallet>,
monero_wallet: Arc<crate::monero::Wallet>,
config: Config,
) -> Result<(AliceState, SwarmDriverHandle)> {
) -> Result<(AliceState, EventLoopHandle)> {
info!("Current state:{}", state);
if is_target_state(&state) {
Ok((state, swarm))

View File

@ -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::{

View File

@ -30,7 +30,7 @@ impl<T> Default for Channels<T> {
}
}
pub struct SwarmDriverHandle {
pub struct EventLoopHandle {
pub msg0: Receiver<alice::Message0>,
pub msg1: Receiver<alice::Message1>,
pub msg2: Receiver<alice::Message2>,
@ -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<PeerId> {
self.conn_established
.recv()
@ -112,7 +112,7 @@ impl SwarmDriverHandle {
}
}
pub struct SwarmDriver {
pub struct EventLoop {
pub swarm: libp2p::Swarm<Behaviour>,
pub msg0: Sender<alice::Message0>,
pub msg1: Sender<alice::Message1>,
@ -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,

View File

@ -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<R>(
state0: xmr_btc::bob::State0,
amounts: SwapAmounts,
swarm: &mut SwarmDriverHandle,
swarm: &mut EventLoopHandle,
addr: Multiaddr,
mut rng: R,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>,

View File

@ -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<R>(
state: BobState,
swarm: SwarmDriverHandle,
swarm: EventLoopHandle,
db: Database,
bitcoin_wallet: Arc<crate::bitcoin::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>(
state: BobState,
is_target_state: fn(&BobState) -> bool,
mut swarm: SwarmDriverHandle,
mut swarm: EventLoopHandle,
db: Database,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
monero_wallet: Arc<crate::monero::Wallet>,

View File

@ -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<swap::bitcoin::Wallet>,
Arc<swap::monero::Wallet>,
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<swap::bitcoin::Wallet>,
Arc<swap::monero::Wallet>,
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,