diff --git a/swap/src/protocol/alice.rs b/swap/src/protocol/alice.rs index 14cd220c..2c7fdd13 100644 --- a/swap/src/protocol/alice.rs +++ b/swap/src/protocol/alice.rs @@ -4,18 +4,18 @@ use std::sync::Arc; use uuid::Uuid; -use crate::{bitcoin, monero}; use crate::database::Database; use crate::env::Config; +use crate::{bitcoin, monero}; pub use self::behaviour::{Behaviour, OutEvent}; pub use self::event_loop::{EventLoop, EventLoopHandle}; -pub use self::recovery::{cancel, punish, redeem, refund, safely_abort}; pub use self::recovery::cancel::cancel; pub use self::recovery::punish::punish; pub use self::recovery::redeem::redeem; pub use self::recovery::refund::refund; pub use self::recovery::safely_abort::safely_abort; +pub use self::recovery::{cancel, punish, redeem, refund, safely_abort}; pub use self::state::*; pub use self::swap::{run, run_until}; diff --git a/swap/src/protocol/alice/behaviour.rs b/swap/src/protocol/alice/behaviour.rs index 7e9804fa..b0507472 100644 --- a/swap/src/protocol/alice/behaviour.rs +++ b/swap/src/protocol/alice/behaviour.rs @@ -1,35 +1,35 @@ use anyhow::{anyhow, Error}; -use libp2p::{NetworkBehaviour, PeerId}; use libp2p::ping::{Ping, PingEvent}; use libp2p::request_response::{RequestId, ResponseChannel}; +use libp2p::{NetworkBehaviour, PeerId}; use uuid::Uuid; -use crate::{env, monero}; -use crate::network::{encrypted_signature, quote, transfer_proof}; use crate::network::quote::BidQuote; -use crate::protocol::alice::{execution_setup, State3, swap_setup}; +use crate::network::{encrypted_signature, quote, transfer_proof}; use crate::protocol::alice::event_loop::LatestRate; use crate::protocol::alice::swap_setup::WalletSnapshot; +use crate::protocol::alice::{execution_setup, swap_setup, State3}; +use crate::{env, monero}; use tokio::sync::oneshot; #[derive(Debug)] pub enum OutEvent { - SwapRequestDeclined { + SwapSetupInitiated { + send_wallet_snapshot: oneshot::Sender, + }, + SwapSetupCompleted { + peer_id: PeerId, + swap_id: Uuid, + state3: Box, + }, + SwapDeclined { peer: PeerId, error: swap_setup::Error, }, - SwapInitiated { - send_wallet_snapshot: oneshot::Sender - }, QuoteRequested { channel: ResponseChannel, peer: PeerId, }, - ExecutionSetupDone { - bob_peer_id: PeerId, - swap_id: Uuid, - state3: Box, - }, TransferProofAcknowledged { peer: PeerId, id: RequestId, diff --git a/swap/src/protocol/alice/event_loop.rs b/swap/src/protocol/alice/event_loop.rs index c210807d..475fa54f 100644 --- a/swap/src/protocol/alice/event_loop.rs +++ b/swap/src/protocol/alice/event_loop.rs @@ -3,6 +3,7 @@ use crate::database::Database; use crate::env::Config; use crate::network::quote::BidQuote; use crate::network::transfer_proof; +use crate::protocol::alice::swap_setup::WalletSnapshot; use crate::protocol::alice::{AliceState, Behaviour, OutEvent, State0, State3, Swap}; use crate::{bitcoin, kraken, monero}; use anyhow::{Context, Result}; @@ -20,7 +21,6 @@ use std::fmt::Debug; use std::sync::Arc; use tokio::sync::mpsc; use uuid::Uuid; -use crate::protocol::alice::swap_setup::WalletSnapshot; /// A future that resolves to a tuple of `PeerId`, `transfer_proof::Request` and /// `Responder`. @@ -151,7 +151,7 @@ where tokio::select! { swarm_event = self.swarm.next_event() => { match swarm_event { - SwarmEvent::Behaviour(OutEvent::SwapInitiated { send_wallet_snapshot }) => { + SwarmEvent::Behaviour(OutEvent::SwapSetupInitiated { send_wallet_snapshot }) => { let wallet_snapshot = match WalletSnapshot::capture(&self.bitcoin_wallet, &self.monero_wallet).await { Ok(wallet_snapshot) => wallet_snapshot, @@ -163,28 +163,11 @@ where match send_wallet_snapshot.send().await { Ok() } - - // TODO: Move into execution setup as part of swap_setup - // let state0 = match State0::new( - // btc, - // xmr, - // self.env_config, - // redeem_address, - // punish_address, - // tx_redeem_fee, - // tx_punish_fee, - // &mut OsRng - // ) { - // Ok(state) => state, - // Err(error) => { - // tracing::warn!(%peer, "Failed to make State0 for execution setup. Error {:#}", error); - // continue; - // } - // }; - // - // self.swarm.behaviour_mut().execution_setup.run(peer, state0); } - SwarmEvent::Behaviour(OutEvent::SwapRequestDeclined { peer, error }) => { + SwarmEvent::Behaviour(OutEvent::SwapSetupCompleted{peer_id, swap_id, state3}) => { + let _ = self.handle_execution_setup_done(bob_peer_id, swap_id, *state3).await; + } + SwarmEvent::Behaviour(OutEvent::SwapDeclined { peer, error }) => { tracing::warn!(%peer, "Ignoring spot price request because: {}", error); } SwarmEvent::Behaviour(OutEvent::QuoteRequested { channel, peer }) => { @@ -211,9 +194,6 @@ where tracing::debug!(%peer, "Failed to respond with quote"); } } - SwarmEvent::Behaviour(OutEvent::ExecutionSetupDone{bob_peer_id, swap_id, state3}) => { - let _ = self.handle_execution_setup_done(bob_peer_id, swap_id, *state3).await; - } SwarmEvent::Behaviour(OutEvent::TransferProofAcknowledged { peer, id }) => { tracing::debug!(%peer, "Bob acknowledged transfer proof"); if let Some(responder) = self.inflight_transfer_proofs.remove(&id) { diff --git a/swap/src/protocol/alice/execution_setup.rs b/swap/src/protocol/alice/execution_setup.rs index 19389072..2be13327 100644 --- a/swap/src/protocol/alice/execution_setup.rs +++ b/swap/src/protocol/alice/execution_setup.rs @@ -51,11 +51,7 @@ impl Default for Behaviour { impl Behaviour { pub fn run(&mut self, bob: PeerId, state0: State0) { self.inner.do_protocol_listener(bob, move |mut substream| { - let protocol = async move { - - - Ok((bob, (swap_id, state3))) - }; + let protocol = async move { Ok((bob, (swap_id, state3))) }; async move { tokio::time::timeout(Duration::from_secs(60), protocol).await? } }); @@ -69,8 +65,8 @@ impl From for alice::OutEvent { bob_peer_id, state3, swap_id, - } => Self::ExecutionSetupDone { - bob_peer_id, + } => Self::SwapSetupCompleted { + peer_id: bob_peer_id, state3: Box::new(state3), swap_id, }, diff --git a/swap/src/protocol/alice/swap_setup.rs b/swap/src/protocol/alice/swap_setup.rs index 6ce80919..ba36ac80 100644 --- a/swap/src/protocol/alice/swap_setup.rs +++ b/swap/src/protocol/alice/swap_setup.rs @@ -91,8 +91,6 @@ where LR: LatestRate + Send + 'static, { pub fn new( - balance: monero::Amount, - lock_fee: monero::Amount, min_buy: bitcoin::Amount, max_buy: bitcoin::Amount, env_config: env::Config, @@ -101,8 +99,6 @@ where ) -> Self { Self { events: Default::default(), - balance, - lock_fee, min_buy, max_buy, env_config, @@ -110,10 +106,6 @@ where resume_only, } } - - pub fn update(&mut self, monero_balance: monero::Amount, redeem_address: bitcoin::Address, punish_address: bitcoin::Address) { - self.balance = monero_balance; - } } impl NetworkBehaviour for Behaviour {