Merge #1263
1263: feat(swap): merge cancel/refund commands into one command r=binarybaron a=delta1 Merges the `cancel`/`refund` commands in the swap cli into one `cancel-and-refund` command closes #1020 Co-authored-by: Byron Hambly <bhambly@blockstream.com>pull/1270/head
commit
cd6bc20e02
@ -1,56 +0,0 @@
|
|||||||
use crate::bitcoin::{parse_rpc_error_code, RpcErrorCode, Txid, Wallet};
|
|
||||||
use crate::protocol::bob::BobState;
|
|
||||||
use crate::protocol::Database;
|
|
||||||
use anyhow::{bail, Result};
|
|
||||||
use std::convert::TryInto;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub async fn cancel(
|
|
||||||
swap_id: Uuid,
|
|
||||||
bitcoin_wallet: Arc<Wallet>,
|
|
||||||
db: Arc<dyn Database>,
|
|
||||||
) -> Result<(Txid, BobState)> {
|
|
||||||
let state = db.get_state(swap_id).await?.try_into()?;
|
|
||||||
|
|
||||||
let state6 = match state {
|
|
||||||
BobState::BtcLocked { state3, .. } => state3.cancel(),
|
|
||||||
BobState::XmrLockProofReceived { state, .. } => state.cancel(),
|
|
||||||
BobState::XmrLocked(state4) => state4.cancel(),
|
|
||||||
BobState::EncSigSent(state4) => state4.cancel(),
|
|
||||||
BobState::CancelTimelockExpired(state6) => state6,
|
|
||||||
BobState::BtcRefunded(state6) => state6,
|
|
||||||
BobState::BtcCancelled(state6) => state6,
|
|
||||||
|
|
||||||
BobState::Started { .. }
|
|
||||||
| BobState::SwapSetupCompleted(_)
|
|
||||||
| BobState::BtcRedeemed(_)
|
|
||||||
| BobState::XmrRedeemed { .. }
|
|
||||||
| BobState::BtcPunished { .. }
|
|
||||||
| BobState::SafelyAborted => bail!(
|
|
||||||
"Cannot cancel swap {} because it is in state {} which is not refundable.",
|
|
||||||
swap_id,
|
|
||||||
state
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
tracing::info!(%swap_id, "Manually cancelling swap");
|
|
||||||
|
|
||||||
let txid = match state6.submit_tx_cancel(bitcoin_wallet.as_ref()).await {
|
|
||||||
Ok(txid) => txid,
|
|
||||||
Err(err) => {
|
|
||||||
if let Ok(code) = parse_rpc_error_code(&err) {
|
|
||||||
if code == i64::from(RpcErrorCode::RpcVerifyAlreadyInChain) {
|
|
||||||
tracing::info!("Cancel transaction has already been confirmed on chain")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bail!(err);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let state = BobState::BtcCancelled(state6);
|
|
||||||
db.insert_latest_state(swap_id, state.clone().into())
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok((txid, state))
|
|
||||||
}
|
|
@ -0,0 +1,115 @@
|
|||||||
|
use crate::bitcoin::wallet::Subscription;
|
||||||
|
use crate::bitcoin::{parse_rpc_error_code, RpcErrorCode, Wallet};
|
||||||
|
use crate::protocol::bob::BobState;
|
||||||
|
use crate::protocol::Database;
|
||||||
|
use anyhow::{bail, Result};
|
||||||
|
use bitcoin::Txid;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub async fn cancel_and_refund(
|
||||||
|
swap_id: Uuid,
|
||||||
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
|
db: Arc<dyn Database>,
|
||||||
|
) -> Result<BobState> {
|
||||||
|
if let Err(err) = cancel(swap_id, bitcoin_wallet.clone(), db.clone()).await {
|
||||||
|
tracing::info!(%err, "Could not submit cancel transaction");
|
||||||
|
};
|
||||||
|
|
||||||
|
let state = match refund(swap_id, bitcoin_wallet, db).await {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(e) => bail!(e),
|
||||||
|
};
|
||||||
|
|
||||||
|
tracing::info!("Refund transaction submitted");
|
||||||
|
Ok(state)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn cancel(
|
||||||
|
swap_id: Uuid,
|
||||||
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
|
db: Arc<dyn Database>,
|
||||||
|
) -> Result<(Txid, Subscription, BobState)> {
|
||||||
|
let state = db.get_state(swap_id).await?.try_into()?;
|
||||||
|
|
||||||
|
let state6 = match state {
|
||||||
|
BobState::BtcLocked { state3, .. } => state3.cancel(),
|
||||||
|
BobState::XmrLockProofReceived { state, .. } => state.cancel(),
|
||||||
|
BobState::XmrLocked(state4) => state4.cancel(),
|
||||||
|
BobState::EncSigSent(state4) => state4.cancel(),
|
||||||
|
BobState::CancelTimelockExpired(state6) => state6,
|
||||||
|
BobState::BtcRefunded(state6) => state6,
|
||||||
|
BobState::BtcCancelled(state6) => state6,
|
||||||
|
|
||||||
|
BobState::Started { .. }
|
||||||
|
| BobState::SwapSetupCompleted(_)
|
||||||
|
| BobState::BtcRedeemed(_)
|
||||||
|
| BobState::XmrRedeemed { .. }
|
||||||
|
| BobState::BtcPunished { .. }
|
||||||
|
| BobState::SafelyAborted => bail!(
|
||||||
|
"Cannot cancel swap {} because it is in state {} which is not refundable.",
|
||||||
|
swap_id,
|
||||||
|
state
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
tracing::info!(%swap_id, "Manually cancelling swap");
|
||||||
|
|
||||||
|
let (txid, subscription) = match state6.submit_tx_cancel(bitcoin_wallet.as_ref()).await {
|
||||||
|
Ok(txid) => txid,
|
||||||
|
Err(err) => {
|
||||||
|
if let Ok(error_code) = parse_rpc_error_code(&err) {
|
||||||
|
tracing::debug!(%error_code, "parse rpc error");
|
||||||
|
if error_code == i64::from(RpcErrorCode::RpcVerifyAlreadyInChain) {
|
||||||
|
tracing::info!("Cancel transaction has already been confirmed on chain");
|
||||||
|
} else if error_code == i64::from(RpcErrorCode::RpcVerifyError) {
|
||||||
|
tracing::info!("General error trying to submit cancel transaction");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bail!(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let state = BobState::BtcCancelled(state6);
|
||||||
|
db.insert_latest_state(swap_id, state.clone().into())
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok((txid, subscription, state))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn refund(
|
||||||
|
swap_id: Uuid,
|
||||||
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
|
db: Arc<dyn Database>,
|
||||||
|
) -> Result<BobState> {
|
||||||
|
let state = db.get_state(swap_id).await?.try_into()?;
|
||||||
|
|
||||||
|
let state6 = match state {
|
||||||
|
BobState::BtcLocked { state3, .. } => state3.cancel(),
|
||||||
|
BobState::XmrLockProofReceived { state, .. } => state.cancel(),
|
||||||
|
BobState::XmrLocked(state4) => state4.cancel(),
|
||||||
|
BobState::EncSigSent(state4) => state4.cancel(),
|
||||||
|
BobState::CancelTimelockExpired(state6) => state6,
|
||||||
|
BobState::BtcCancelled(state6) => state6,
|
||||||
|
BobState::Started { .. }
|
||||||
|
| BobState::SwapSetupCompleted(_)
|
||||||
|
| BobState::BtcRedeemed(_)
|
||||||
|
| BobState::BtcRefunded(_)
|
||||||
|
| BobState::XmrRedeemed { .. }
|
||||||
|
| BobState::BtcPunished { .. }
|
||||||
|
| BobState::SafelyAborted => bail!(
|
||||||
|
"Cannot refund swap {} because it is in state {} which is not refundable.",
|
||||||
|
swap_id,
|
||||||
|
state
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
|
tracing::info!(%swap_id, "Manually refunding swap");
|
||||||
|
state6.publish_refund_btc(bitcoin_wallet.as_ref()).await?;
|
||||||
|
|
||||||
|
let state = BobState::BtcRefunded(state6);
|
||||||
|
db.insert_latest_state(swap_id, state.clone().into())
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(state)
|
||||||
|
}
|
@ -1,43 +0,0 @@
|
|||||||
use crate::bitcoin::Wallet;
|
|
||||||
use crate::protocol::bob::BobState;
|
|
||||||
use crate::protocol::Database;
|
|
||||||
use anyhow::{bail, Result};
|
|
||||||
use std::convert::TryInto;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
pub async fn refund(
|
|
||||||
swap_id: Uuid,
|
|
||||||
bitcoin_wallet: Arc<Wallet>,
|
|
||||||
db: Arc<dyn Database>,
|
|
||||||
) -> Result<BobState> {
|
|
||||||
let state = db.get_state(swap_id).await?.try_into()?;
|
|
||||||
|
|
||||||
let state6 = match state {
|
|
||||||
BobState::BtcLocked { state3, .. } => state3.cancel(),
|
|
||||||
BobState::XmrLockProofReceived { state, .. } => state.cancel(),
|
|
||||||
BobState::XmrLocked(state4) => state4.cancel(),
|
|
||||||
BobState::EncSigSent(state4) => state4.cancel(),
|
|
||||||
BobState::CancelTimelockExpired(state6) => state6,
|
|
||||||
BobState::BtcCancelled(state6) => state6,
|
|
||||||
BobState::Started { .. }
|
|
||||||
| BobState::SwapSetupCompleted(_)
|
|
||||||
| BobState::BtcRedeemed(_)
|
|
||||||
| BobState::BtcRefunded(_)
|
|
||||||
| BobState::XmrRedeemed { .. }
|
|
||||||
| BobState::BtcPunished { .. }
|
|
||||||
| BobState::SafelyAborted => bail!(
|
|
||||||
"Cannot refund swap {} because it is in state {} which is not refundable.",
|
|
||||||
swap_id,
|
|
||||||
state
|
|
||||||
),
|
|
||||||
};
|
|
||||||
|
|
||||||
state6.publish_refund_btc(bitcoin_wallet.as_ref()).await?;
|
|
||||||
|
|
||||||
let state = BobState::BtcRefunded(state6);
|
|
||||||
db.insert_latest_state(swap_id, state.clone().into())
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(state)
|
|
||||||
}
|
|
@ -0,0 +1,74 @@
|
|||||||
|
pub mod harness;
|
||||||
|
|
||||||
|
use harness::alice_run_until::is_xmr_lock_transaction_sent;
|
||||||
|
use harness::bob_run_until::is_btc_locked;
|
||||||
|
use harness::FastCancelConfig;
|
||||||
|
use swap::asb::FixedRate;
|
||||||
|
use swap::protocol::alice::AliceState;
|
||||||
|
use swap::protocol::bob::BobState;
|
||||||
|
use swap::protocol::{alice, bob};
|
||||||
|
use swap::{asb, cli};
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn given_alice_and_bob_manually_cancel_and_refund_after_funds_locked_both_refund() {
|
||||||
|
harness::setup_test(FastCancelConfig, |mut ctx| async move {
|
||||||
|
let (bob_swap, bob_join_handle) = ctx.bob_swap().await;
|
||||||
|
let bob_swap_id = bob_swap.id;
|
||||||
|
let bob_swap = tokio::spawn(bob::run_until(bob_swap, is_btc_locked));
|
||||||
|
|
||||||
|
let alice_swap = ctx.alice_next_swap().await;
|
||||||
|
let alice_swap = tokio::spawn(alice::run_until(
|
||||||
|
alice_swap,
|
||||||
|
is_xmr_lock_transaction_sent,
|
||||||
|
FixedRate::default(),
|
||||||
|
));
|
||||||
|
|
||||||
|
let bob_state = bob_swap.await??;
|
||||||
|
assert!(matches!(bob_state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
|
let alice_state = alice_swap.await??;
|
||||||
|
assert!(matches!(
|
||||||
|
alice_state,
|
||||||
|
AliceState::XmrLockTransactionSent { .. }
|
||||||
|
));
|
||||||
|
|
||||||
|
let (bob_swap, bob_join_handle) = ctx
|
||||||
|
.stop_and_resume_bob_from_db(bob_join_handle, bob_swap_id)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
// Ensure cancel timelock is expired
|
||||||
|
if let BobState::BtcLocked { state3, .. } = bob_swap.state.clone() {
|
||||||
|
bob_swap
|
||||||
|
.bitcoin_wallet
|
||||||
|
.subscribe_to(state3.tx_lock)
|
||||||
|
.await
|
||||||
|
.wait_until_confirmed_with(state3.cancel_timelock)
|
||||||
|
.await?;
|
||||||
|
} else {
|
||||||
|
panic!("Bob in unexpected state {}", bob_swap.state);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bob manually cancels and refunds
|
||||||
|
bob_join_handle.abort();
|
||||||
|
let bob_state =
|
||||||
|
cli::cancel_and_refund(bob_swap.id, bob_swap.bitcoin_wallet, bob_swap.db).await?;
|
||||||
|
|
||||||
|
ctx.assert_bob_refunded(bob_state).await;
|
||||||
|
|
||||||
|
// manually refund Alice's swap
|
||||||
|
ctx.restart_alice().await;
|
||||||
|
let alice_swap = ctx.alice_next_swap().await;
|
||||||
|
let alice_state = asb::refund(
|
||||||
|
alice_swap.swap_id,
|
||||||
|
alice_swap.bitcoin_wallet,
|
||||||
|
alice_swap.monero_wallet,
|
||||||
|
alice_swap.db,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
ctx.assert_alice_refunded(alice_state).await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
Loading…
Reference in New Issue