diff --git a/swap/src/alice/execution.rs b/swap/src/alice/execution.rs index 203f8111..286709f6 100644 --- a/swap/src/alice/execution.rs +++ b/swap/src/alice/execution.rs @@ -30,17 +30,20 @@ use xmr_btc::{ pub async fn negotiate( state0: xmr_btc::alice::State0, amounts: SwapAmounts, - swarm_handle: &mut EventLoopHandle, + event_loop_handle: &mut EventLoopHandle, config: Config, ) -> Result<(ResponseChannel, State3)> { trace!("Starting negotiate"); // todo: we can move this out, we dont need to timeout here - let _peer_id = timeout(config.bob_time_to_act, swarm_handle.recv_conn_established()) - .await - .context("Failed to receive dial connection from Bob")??; + let _peer_id = timeout( + config.bob_time_to_act, + event_loop_handle.recv_conn_established(), + ) + .await + .context("Failed to receive dial connection from Bob")??; - let event = timeout(config.bob_time_to_act, swarm_handle.recv_request()) + let event = timeout(config.bob_time_to_act, event_loop_handle.recv_request()) .await .context("Failed to receive amounts from Bob")??; @@ -52,23 +55,25 @@ pub async fn negotiate( ); } - swarm_handle.send_amounts(event.channel, amounts).await?; + event_loop_handle + .send_amounts(event.channel, amounts) + .await?; - let bob_message0 = timeout(config.bob_time_to_act, swarm_handle.recv_message0()).await??; + let bob_message0 = timeout(config.bob_time_to_act, event_loop_handle.recv_message0()).await??; let state1 = state0.receive(bob_message0)?; let (bob_message1, channel) = - timeout(config.bob_time_to_act, swarm_handle.recv_message1()).await??; + timeout(config.bob_time_to_act, event_loop_handle.recv_message1()).await??; let state2 = state1.receive(bob_message1); - swarm_handle + event_loop_handle .send_message1(channel, state2.next_message()) .await?; let (bob_message2, channel) = - timeout(config.bob_time_to_act, swarm_handle.recv_message2()).await??; + timeout(config.bob_time_to_act, event_loop_handle.recv_message2()).await??; let state3 = state2.receive(bob_message2)?; @@ -103,7 +108,7 @@ pub async fn lock_xmr( channel: ResponseChannel, amounts: SwapAmounts, state3: State3, - swarm: &mut EventLoopHandle, + event_loop_handle: &mut EventLoopHandle, monero_wallet: Arc, ) -> Result<()> where @@ -122,7 +127,7 @@ where // TODO(Franck): Wait for Monero to be confirmed once - swarm + event_loop_handle .send_message2(channel, alice::Message2 { tx_lock_proof: transfer_proof, }) @@ -132,10 +137,10 @@ where } pub async fn wait_for_bitcoin_encrypted_signature( - swarm: &mut EventLoopHandle, + event_loop_handle: &mut EventLoopHandle, timeout_duration: Duration, ) -> Result { - let msg3 = timeout(timeout_duration, swarm.recv_message3()) + let msg3 = timeout(timeout_duration, event_loop_handle.recv_message3()) .await .context("Failed to receive Bitcoin encrypted signature from Bob")??; Ok(msg3.tx_redeem_encsig) diff --git a/swap/src/alice/swap.rs b/swap/src/alice/swap.rs index 9732c6fd..680c49a5 100644 --- a/swap/src/alice/swap.rs +++ b/swap/src/alice/swap.rs @@ -104,7 +104,7 @@ impl fmt::Display for AliceState { pub async fn swap( state: AliceState, - swarm: EventLoopHandle, + event_loop_handle: EventLoopHandle, bitcoin_wallet: Arc, monero_wallet: Arc, config: Config, @@ -112,7 +112,7 @@ pub async fn swap( run_until( state, is_complete, - swarm, + event_loop_handle, bitcoin_wallet, monero_wallet, config, @@ -142,18 +142,19 @@ pub fn is_xmr_locked(state: &AliceState) -> bool { pub async fn run_until( state: AliceState, is_target_state: fn(&AliceState) -> bool, - mut swarm: EventLoopHandle, + mut event_loop_handle: EventLoopHandle, bitcoin_wallet: Arc, monero_wallet: Arc, config: Config, ) -> Result<(AliceState, EventLoopHandle)> { info!("Current state:{}", state); if is_target_state(&state) { - Ok((state, swarm)) + Ok((state, event_loop_handle)) } else { match state { AliceState::Started { amounts, state0 } => { - let (channel, state3) = negotiate(state0, amounts, &mut swarm, config).await?; + let (channel, state3) = + negotiate(state0, amounts, &mut event_loop_handle, config).await?; run_until( AliceState::Negotiated { @@ -162,7 +163,7 @@ pub async fn run_until( state3, }, is_target_state, - swarm, + event_loop_handle, bitcoin_wallet, monero_wallet, config, @@ -185,7 +186,7 @@ pub async fn run_until( state3, }, is_target_state, - swarm, + event_loop_handle, bitcoin_wallet, monero_wallet, config, @@ -201,7 +202,7 @@ pub async fn run_until( channel, amounts, state3.clone(), - &mut swarm, + &mut event_loop_handle, monero_wallet.clone(), ) .await?; @@ -209,7 +210,7 @@ pub async fn run_until( run_until( AliceState::XmrLocked { state3 }, is_target_state, - swarm, + event_loop_handle, bitcoin_wallet, monero_wallet, config, @@ -220,7 +221,7 @@ pub async fn run_until( // Our Monero is locked, we need to go through the cancellation process if this // step fails match wait_for_bitcoin_encrypted_signature( - &mut swarm, + &mut event_loop_handle, config.monero_max_finality_time, ) .await @@ -232,7 +233,7 @@ pub async fn run_until( encrypted_signature, }, is_target_state, - swarm, + event_loop_handle, bitcoin_wallet, monero_wallet, config, @@ -243,7 +244,7 @@ pub async fn run_until( run_until( AliceState::WaitingToCancel { state3 }, is_target_state, - swarm, + event_loop_handle, bitcoin_wallet, monero_wallet, config, @@ -269,7 +270,7 @@ pub async fn run_until( return run_until( AliceState::WaitingToCancel { state3 }, is_target_state, - swarm, + event_loop_handle, bitcoin_wallet, monero_wallet, config, @@ -291,7 +292,7 @@ pub async fn run_until( run_until( AliceState::BtcRedeemed, is_target_state, - swarm, + event_loop_handle, bitcoin_wallet, monero_wallet, config, @@ -312,7 +313,7 @@ pub async fn run_until( run_until( AliceState::BtcCancelled { state3, tx_cancel }, is_target_state, - swarm, + event_loop_handle, bitcoin_wallet, monero_wallet, config, @@ -339,7 +340,7 @@ pub async fn run_until( run_until( AliceState::BtcPunishable { tx_refund, state3 }, is_target_state, - swarm, + event_loop_handle, bitcoin_wallet.clone(), monero_wallet, config, @@ -354,7 +355,7 @@ pub async fn run_until( state3, }, is_target_state, - swarm, + event_loop_handle, bitcoin_wallet.clone(), monero_wallet, config, @@ -381,7 +382,7 @@ pub async fn run_until( .create_and_load_wallet_for_output(spend_key, view_key) .await?; - Ok((AliceState::XmrRefunded, swarm)) + Ok((AliceState::XmrRefunded, event_loop_handle)) } AliceState::BtcPunishable { tx_refund, state3 } => { let signed_tx_punish = build_bitcoin_punish_transaction( @@ -410,7 +411,7 @@ pub async fn run_until( run_until( AliceState::Punished, is_target_state, - swarm, + event_loop_handle, bitcoin_wallet.clone(), monero_wallet, config, @@ -425,7 +426,7 @@ pub async fn run_until( state3, }, is_target_state, - swarm, + event_loop_handle, bitcoin_wallet.clone(), monero_wallet, config, @@ -434,10 +435,10 @@ pub async fn run_until( } } } - AliceState::XmrRefunded => Ok((AliceState::XmrRefunded, swarm)), - AliceState::BtcRedeemed => Ok((AliceState::BtcRedeemed, swarm)), - AliceState::Punished => Ok((AliceState::Punished, swarm)), - AliceState::SafelyAborted => Ok((AliceState::SafelyAborted, swarm)), + AliceState::XmrRefunded => Ok((AliceState::XmrRefunded, event_loop_handle)), + AliceState::BtcRedeemed => Ok((AliceState::BtcRedeemed, event_loop_handle)), + AliceState::Punished => Ok((AliceState::Punished, event_loop_handle)), + AliceState::SafelyAborted => Ok((AliceState::SafelyAborted, event_loop_handle)), } } } diff --git a/swap/src/bob/swap.rs b/swap/src/bob/swap.rs index 9f574b8d..4221b1e9 100644 --- a/swap/src/bob/swap.rs +++ b/swap/src/bob/swap.rs @@ -53,7 +53,7 @@ impl fmt::Display for BobState { pub async fn swap( state: BobState, - swarm: EventLoopHandle, + event_loop_handle: EventLoopHandle, db: Database, bitcoin_wallet: Arc, monero_wallet: Arc, @@ -66,7 +66,7 @@ where run_until( state, is_complete, - swarm, + event_loop_handle, db, bitcoin_wallet, monero_wallet, @@ -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: EventLoopHandle, + mut event_loop_handle: EventLoopHandle, db: Database, bitcoin_wallet: Arc, monero_wallet: Arc, @@ -124,7 +124,7 @@ where let state2 = negotiate( state0, amounts, - &mut swarm, + &mut event_loop_handle, addr, &mut rng, bitcoin_wallet.clone(), @@ -133,7 +133,7 @@ where run_until( BobState::Negotiated(state2, peer_id), is_target_state, - swarm, + event_loop_handle, db, bitcoin_wallet, monero_wallet, @@ -149,7 +149,7 @@ where run_until( BobState::BtcLocked(state3, alice_peer_id), is_target_state, - swarm, + event_loop_handle, db, bitcoin_wallet, monero_wallet, @@ -162,7 +162,7 @@ where // Watch for Alice to Lock Xmr or for t1 to elapse BobState::BtcLocked(state3, alice_peer_id) => { // todo: watch until t1, not indefinetely - let msg2 = swarm.recv_message2().await?; + let msg2 = event_loop_handle.recv_message2().await?; let state4 = state3 .watch_for_lock_xmr(monero_wallet.as_ref(), msg2) .await?; @@ -170,7 +170,7 @@ where run_until( BobState::XmrLocked(state4, alice_peer_id), is_target_state, - swarm, + event_loop_handle, db, bitcoin_wallet, monero_wallet, @@ -187,14 +187,14 @@ where // What if Alice fails to receive this? Should we always resend? // todo: If we cannot dial Alice we should go to EncSigSent. Maybe dialing // should happen in this arm? - swarm + event_loop_handle .send_message3(alice_peer_id.clone(), tx_redeem_encsig) .await?; run_until( BobState::EncSigSent(state, alice_peer_id), is_target_state, - swarm, + event_loop_handle, db, bitcoin_wallet, monero_wallet, @@ -213,7 +213,7 @@ where run_until( BobState::BtcRedeemed(val?), is_target_state, - swarm, + event_loop_handle, db, bitcoin_wallet, monero_wallet, @@ -232,7 +232,7 @@ where run_until( BobState::Cancelled(state), is_target_state, - swarm, + event_loop_handle, db, bitcoin_wallet, monero_wallet, @@ -250,7 +250,7 @@ where run_until( BobState::XmrRedeemed, is_target_state, - swarm, + event_loop_handle, db, bitcoin_wallet, monero_wallet,