From 8eb9b866788686cce63506946e1b986d4e3af789 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 2 Dec 2020 14:58:17 +1100 Subject: [PATCH] Abort swap if resumed from a step that requires further communication --- swap/src/alice/swap.rs | 95 ++++++++++++++++++++++++++++-------------- 1 file changed, 63 insertions(+), 32 deletions(-) diff --git a/swap/src/alice/swap.rs b/swap/src/alice/swap.rs index a6330205..c1afbdd7 100644 --- a/swap/src/alice/swap.rs +++ b/swap/src/alice/swap.rs @@ -47,12 +47,12 @@ pub enum AliceState { v_a: monero::PrivateViewKey, }, Negotiated { - channel: ResponseChannel, + channel: Option>, amounts: SwapAmounts, state3: State3, }, BtcLocked { - channel: ResponseChannel, + channel: Option>, amounts: SwapAmounts, state3: State3, }, @@ -105,7 +105,7 @@ pub async fn swap( swap( AliceState::Negotiated { - channel, + channel: Some(channel), amounts, state3, }, @@ -120,42 +120,73 @@ pub async fn swap( channel, amounts, } => { - let _ = wait_for_locked_bitcoin(state3.tx_lock.txid(), bitcoin_wallet.clone()).await?; + match channel { + Some(channel) => { + let _ = wait_for_locked_bitcoin(state3.tx_lock.txid(), bitcoin_wallet.clone()) + .await?; - swap( - AliceState::BtcLocked { - channel, - amounts, - state3, - }, - swarm, - bitcoin_wallet, - monero_wallet, - ) - .await + swap( + AliceState::BtcLocked { + channel: Some(channel), + amounts, + state3, + }, + swarm, + bitcoin_wallet, + monero_wallet, + ) + .await + } + None => { + tracing::info!("Cannot resume swap from negotiated state, aborting"); + + // Alice did not lock Xmr yet + swap( + AliceState::SafelyAborted, + swarm, + bitcoin_wallet, + monero_wallet, + ) + .await + } + } } AliceState::BtcLocked { channel, amounts, state3, - } => { - lock_xmr( - channel, - amounts, - state3.clone(), - &mut swarm, - monero_wallet.clone(), - ) - .await?; + } => match channel { + Some(channel) => { + lock_xmr( + channel, + amounts, + state3.clone(), + &mut swarm, + monero_wallet.clone(), + ) + .await?; - swap( - AliceState::XmrLocked { state3 }, - swarm, - bitcoin_wallet, - monero_wallet, - ) - .await - } + swap( + AliceState::XmrLocked { state3 }, + swarm, + bitcoin_wallet, + monero_wallet, + ) + .await + } + None => { + tracing::info!("Cannot resume swap from BTC locked state, aborting"); + + // Alice did not lock Xmr yet + swap( + AliceState::SafelyAborted, + swarm, + bitcoin_wallet, + monero_wallet, + ) + .await + } + }, AliceState::XmrLocked { state3 } => { // Our Monero is locked, we need to go through the cancellation process if this // step fails