Merge #170
170: Cancel and refund commands r=da-kami a=da-kami
I plugged the cancel and refund logic into the current state/state-machine logic of the swap.
## Follow ups (out of scope)
We might want to record issues to be tackled later, since we are on a tight time budget :)
Please let me know what you think @D4nte @rishflab
### Problems with `ack` after sending a message
Alice was waiting forever when awaiting the `ack` from bob when sending the lock proof in case she runs into a dial error. It seems the `acks` can cause the program to hang. This is a severe problem that we most probably will encountered in production at some point. For this PR I wrapped the `ack` of Alice upon sending the `encsig` in a timeout to work around this problem, see 7463081f88
- but **we might want to consider to remove all `ack` message. I don't see much value in them if we don't have a resilient retry strategy.**
### Do not require Monero wallet for cancel/refund
The cancel/refund commands don't require a monero wallet.
In this PR we re-uses the builder which requires the monero wallet as well - and we check for the monero balance upon wallet initialization, so the command will fail if no monero wallet is started.
### Save Alice connection info in Bob DB
Save Alice's peer-id/address in DB: It's cumbersome for the user to lookup those details again.
Co-authored-by: Daniel Karzel <daniel@comit.network>
fix-confirmations
commit
27bb9498d5
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,63 @@
|
|||||||
|
use crate::{
|
||||||
|
bitcoin::{timelocks::ExpiredTimelocks, Txid, Wallet},
|
||||||
|
database::{Database, Swap},
|
||||||
|
protocol::bob::BobState,
|
||||||
|
};
|
||||||
|
use anyhow::{bail, Result};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error, Clone, Copy)]
|
||||||
|
pub enum CancelError {
|
||||||
|
#[error("The cancel timelock has not expired yet.")]
|
||||||
|
CancelTimelockNotExpiredYet,
|
||||||
|
#[error("The cancel transaction has already been published.")]
|
||||||
|
CancelTxAlreadyPublished,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn cancel(
|
||||||
|
swap_id: Uuid,
|
||||||
|
state: BobState,
|
||||||
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
|
db: Database,
|
||||||
|
force: bool,
|
||||||
|
) -> Result<Result<(Txid, BobState), CancelError>> {
|
||||||
|
let state4 = match state {
|
||||||
|
BobState::BtcLocked(state3) => state3.state4(),
|
||||||
|
BobState::XmrLockProofReceived { state, .. } => state.state4(),
|
||||||
|
BobState::XmrLocked(state4) => state4,
|
||||||
|
BobState::EncSigSent(state4) => state4,
|
||||||
|
BobState::CancelTimelockExpired(state4) => state4,
|
||||||
|
_ => bail!(
|
||||||
|
"Cannot cancel swap {} because it is in state {} which is not refundable.",
|
||||||
|
swap_id,
|
||||||
|
state
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
if !force {
|
||||||
|
if let ExpiredTimelocks::None = state4.expired_timelock(bitcoin_wallet.as_ref()).await? {
|
||||||
|
return Ok(Err(CancelError::CancelTimelockNotExpiredYet));
|
||||||
|
}
|
||||||
|
|
||||||
|
if state4
|
||||||
|
.check_for_tx_cancel(bitcoin_wallet.as_ref())
|
||||||
|
.await
|
||||||
|
.is_ok()
|
||||||
|
{
|
||||||
|
let state = BobState::BtcCancelled(state4);
|
||||||
|
let db_state = state.into();
|
||||||
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
||||||
|
|
||||||
|
return Ok(Err(CancelError::CancelTxAlreadyPublished));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let txid = state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await?;
|
||||||
|
|
||||||
|
let state = BobState::BtcCancelled(state4);
|
||||||
|
let db_state = state.clone().into();
|
||||||
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
||||||
|
|
||||||
|
Ok(Ok((txid, state)))
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
use crate::{
|
||||||
|
bitcoin::Wallet,
|
||||||
|
database::{Database, Swap},
|
||||||
|
execution_params::ExecutionParams,
|
||||||
|
protocol::bob::BobState,
|
||||||
|
};
|
||||||
|
use anyhow::{bail, Result};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(thiserror::Error, Debug, Clone, Copy)]
|
||||||
|
#[error("Cannot refund because swap {0} was not cancelled yet. Make sure to cancel the swap before trying to refund.")]
|
||||||
|
pub struct SwapNotCancelledYet(Uuid);
|
||||||
|
|
||||||
|
pub async fn refund(
|
||||||
|
swap_id: Uuid,
|
||||||
|
state: BobState,
|
||||||
|
execution_params: ExecutionParams,
|
||||||
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
|
db: Database,
|
||||||
|
force: bool,
|
||||||
|
) -> Result<Result<BobState, SwapNotCancelledYet>> {
|
||||||
|
let state4 = if force {
|
||||||
|
match state {
|
||||||
|
BobState::BtcLocked(state3) => state3.state4(),
|
||||||
|
BobState::XmrLockProofReceived { state, .. } => state.state4(),
|
||||||
|
BobState::XmrLocked(state4) => state4,
|
||||||
|
BobState::EncSigSent(state4) => state4,
|
||||||
|
BobState::CancelTimelockExpired(state4) => state4,
|
||||||
|
BobState::BtcCancelled(state4) => state4,
|
||||||
|
_ => bail!(
|
||||||
|
"Cannot refund swap {} because it is in state {} which is not refundable.",
|
||||||
|
swap_id,
|
||||||
|
state
|
||||||
|
),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match state {
|
||||||
|
BobState::BtcCancelled(state4) => state4,
|
||||||
|
_ => {
|
||||||
|
return Ok(Err(SwapNotCancelledYet(swap_id)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
state4
|
||||||
|
.refund_btc(bitcoin_wallet.as_ref(), execution_params)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let state = BobState::BtcRefunded(state4);
|
||||||
|
let db_state = state.clone().into();
|
||||||
|
|
||||||
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
||||||
|
|
||||||
|
Ok(Ok(state))
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
pub mod testutils;
|
||||||
|
|
||||||
|
use swap::protocol::{alice, bob, bob::BobState};
|
||||||
|
use testutils::{bob_run_until::is_btc_locked, FastCancelConfig};
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn given_bob_manually_refunds_after_btc_locked_bob_refunds() {
|
||||||
|
testutils::setup_test(FastCancelConfig, |mut ctx| async move {
|
||||||
|
let (alice_swap, _) = ctx.new_swap_as_alice().await;
|
||||||
|
let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;
|
||||||
|
|
||||||
|
let alice_handle = alice::run(alice_swap);
|
||||||
|
let alice_swap_handle = tokio::spawn(alice_handle);
|
||||||
|
|
||||||
|
let bob_state = bob::run_until(bob_swap, is_btc_locked).await.unwrap();
|
||||||
|
|
||||||
|
assert!(matches!(bob_state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
|
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
||||||
|
|
||||||
|
// Ensure Bob's timelock is expired
|
||||||
|
if let BobState::BtcLocked(state3) = bob_swap.state.clone() {
|
||||||
|
state3
|
||||||
|
.wait_for_cancel_timelock_to_expire(bob_swap.bitcoin_wallet.as_ref())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
} else {
|
||||||
|
panic!("Bob in unexpected state {}", bob_swap.state);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bob manually cancels
|
||||||
|
let (_, state) = bob::cancel(
|
||||||
|
bob_swap.swap_id,
|
||||||
|
bob_swap.state,
|
||||||
|
bob_swap.bitcoin_wallet,
|
||||||
|
bob_swap.db,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
assert!(matches!(state, BobState::BtcCancelled { .. }));
|
||||||
|
|
||||||
|
let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
||||||
|
assert!(matches!(bob_swap.state, BobState::BtcCancelled { .. }));
|
||||||
|
|
||||||
|
// Bob manually refunds
|
||||||
|
let bob_state = bob::refund(
|
||||||
|
bob_swap.swap_id,
|
||||||
|
bob_swap.state,
|
||||||
|
bob_swap.execution_params,
|
||||||
|
bob_swap.bitcoin_wallet,
|
||||||
|
bob_swap.db,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
ctx.assert_bob_refunded(bob_state).await;
|
||||||
|
|
||||||
|
let alice_state = alice_swap_handle.await.unwrap().unwrap();
|
||||||
|
ctx.assert_alice_refunded(alice_state).await;
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
pub mod testutils;
|
||||||
|
|
||||||
|
use bob::cancel::CancelError;
|
||||||
|
use swap::protocol::{alice, bob, bob::BobState};
|
||||||
|
use testutils::{bob_run_until::is_btc_locked, SlowCancelConfig};
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
|
||||||
|
testutils::setup_test(SlowCancelConfig, |mut ctx| async move {
|
||||||
|
let (alice_swap, _) = ctx.new_swap_as_alice().await;
|
||||||
|
let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;
|
||||||
|
|
||||||
|
let alice_handle = alice::run(alice_swap);
|
||||||
|
tokio::spawn(alice_handle);
|
||||||
|
|
||||||
|
let bob_state = bob::run_until(bob_swap, is_btc_locked).await.unwrap();
|
||||||
|
assert!(matches!(bob_state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
|
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
||||||
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
|
// Bob tries but fails to manually cancel
|
||||||
|
let result = bob::cancel(
|
||||||
|
bob_swap.swap_id,
|
||||||
|
bob_swap.state,
|
||||||
|
bob_swap.bitcoin_wallet,
|
||||||
|
bob_swap.db,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.err()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert!(matches!(result, CancelError::CancelTimelockNotExpiredYet));
|
||||||
|
|
||||||
|
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
||||||
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
|
// Bob tries but fails to manually refund
|
||||||
|
bob::refund(
|
||||||
|
bob_swap.swap_id,
|
||||||
|
bob_swap.state,
|
||||||
|
bob_swap.execution_params,
|
||||||
|
bob_swap.bitcoin_wallet,
|
||||||
|
bob_swap.db,
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.err()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
||||||
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
pub mod testutils;
|
||||||
|
|
||||||
|
use swap::protocol::{alice, bob, bob::BobState};
|
||||||
|
use testutils::{bob_run_until::is_btc_locked, SlowCancelConfig};
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn given_bob_manually_forces_cancel_when_timelock_not_expired_errors() {
|
||||||
|
testutils::setup_test(SlowCancelConfig, |mut ctx| async move {
|
||||||
|
let (alice_swap, _) = ctx.new_swap_as_alice().await;
|
||||||
|
let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;
|
||||||
|
|
||||||
|
let alice_handle = alice::run(alice_swap);
|
||||||
|
tokio::spawn(alice_handle);
|
||||||
|
|
||||||
|
let bob_state = bob::run_until(bob_swap, is_btc_locked).await.unwrap();
|
||||||
|
assert!(matches!(bob_state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
|
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
||||||
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
|
// Bob forces a cancel that will fail
|
||||||
|
let is_error = bob::cancel(
|
||||||
|
bob_swap.swap_id,
|
||||||
|
bob_swap.state,
|
||||||
|
bob_swap.bitcoin_wallet,
|
||||||
|
bob_swap.db,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.is_err();
|
||||||
|
|
||||||
|
assert!(is_error);
|
||||||
|
|
||||||
|
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
||||||
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
|
// Bob forces a refund that will fail
|
||||||
|
let is_error = bob::refund(
|
||||||
|
bob_swap.swap_id,
|
||||||
|
bob_swap.state,
|
||||||
|
bob_swap.execution_params,
|
||||||
|
bob_swap.bitcoin_wallet,
|
||||||
|
bob_swap.db,
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.is_err();
|
||||||
|
|
||||||
|
assert!(is_error);
|
||||||
|
let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
||||||
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
}
|
Loading…
Reference in New Issue