mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-06-29 08:02:32 +00:00
Add --force flag for cancel and refund
This commit is contained in:
parent
02f8eb7f18
commit
c930ad84a4
1
.github/workflows/ci.yml
vendored
1
.github/workflows/ci.yml
vendored
|
@ -136,6 +136,7 @@ jobs:
|
||||||
refund_restart_alice,
|
refund_restart_alice,
|
||||||
bob_refunds_using_cancel_and_refund_command,
|
bob_refunds_using_cancel_and_refund_command,
|
||||||
bob_refunds_using_cancel_and_refund_command_timelock_not_exired,
|
bob_refunds_using_cancel_and_refund_command_timelock_not_exired,
|
||||||
|
bob_refunds_using_cancel_and_refund_command_timelock_not_exired_force,
|
||||||
]
|
]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
|
|
|
@ -96,6 +96,9 @@ pub enum Cancel {
|
||||||
|
|
||||||
#[structopt(flatten)]
|
#[structopt(flatten)]
|
||||||
config: Config,
|
config: Config,
|
||||||
|
|
||||||
|
#[structopt(short, long)]
|
||||||
|
force: bool,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,6 +117,9 @@ pub enum Refund {
|
||||||
|
|
||||||
#[structopt(flatten)]
|
#[structopt(flatten)]
|
||||||
config: Config,
|
config: Config,
|
||||||
|
|
||||||
|
#[structopt(short, long)]
|
||||||
|
force: bool,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -215,6 +215,7 @@ async fn main() -> Result<()> {
|
||||||
alice_peer_id,
|
alice_peer_id,
|
||||||
alice_addr,
|
alice_addr,
|
||||||
config,
|
config,
|
||||||
|
force,
|
||||||
}) => {
|
}) => {
|
||||||
// TODO: Optimization: Only init the Bitcoin wallet, Monero wallet unnecessary
|
// TODO: Optimization: Only init the Bitcoin wallet, Monero wallet unnecessary
|
||||||
let (bitcoin_wallet, monero_wallet) =
|
let (bitcoin_wallet, monero_wallet) =
|
||||||
|
@ -234,7 +235,15 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
tokio::spawn(async move { event_loop.run().await });
|
tokio::spawn(async move { event_loop.run().await });
|
||||||
|
|
||||||
match bob::cancel(swap.swap_id, swap.state, swap.bitcoin_wallet, swap.db).await? {
|
match bob::cancel(
|
||||||
|
swap.swap_id,
|
||||||
|
swap.state,
|
||||||
|
swap.bitcoin_wallet,
|
||||||
|
swap.db,
|
||||||
|
force,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
{
|
||||||
Ok((txid, _)) => {
|
Ok((txid, _)) => {
|
||||||
info!("Cancel transaction successfully published with id {}", txid)
|
info!("Cancel transaction successfully published with id {}", txid)
|
||||||
}
|
}
|
||||||
|
@ -251,6 +260,7 @@ async fn main() -> Result<()> {
|
||||||
alice_peer_id,
|
alice_peer_id,
|
||||||
alice_addr,
|
alice_addr,
|
||||||
config,
|
config,
|
||||||
|
force,
|
||||||
}) => {
|
}) => {
|
||||||
let (bitcoin_wallet, monero_wallet) =
|
let (bitcoin_wallet, monero_wallet) =
|
||||||
init_wallets(config.path, bitcoin_network, monero_network).await?;
|
init_wallets(config.path, bitcoin_network, monero_network).await?;
|
||||||
|
@ -275,6 +285,7 @@ async fn main() -> Result<()> {
|
||||||
swap.execution_params,
|
swap.execution_params,
|
||||||
swap.bitcoin_wallet,
|
swap.bitcoin_wallet,
|
||||||
swap.db,
|
swap.db,
|
||||||
|
force,
|
||||||
)
|
)
|
||||||
.await??;
|
.await??;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ pub async fn cancel(
|
||||||
state: BobState,
|
state: BobState,
|
||||||
bitcoin_wallet: Arc<Wallet>,
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
db: Database,
|
db: Database,
|
||||||
|
force: bool,
|
||||||
) -> Result<Result<(Txid, BobState), CancelError>> {
|
) -> Result<Result<(Txid, BobState), CancelError>> {
|
||||||
let state4 = match state {
|
let state4 = match state {
|
||||||
BobState::BtcLocked(state3) => state3.state4(),
|
BobState::BtcLocked(state3) => state3.state4(),
|
||||||
|
@ -34,20 +35,22 @@ pub async fn cancel(
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let ExpiredTimelocks::None = state4.expired_timelock(bitcoin_wallet.as_ref()).await? {
|
if !force {
|
||||||
return Ok(Err(CancelError::CancelTimelockNotExpiredYet));
|
if let ExpiredTimelocks::None = state4.expired_timelock(bitcoin_wallet.as_ref()).await? {
|
||||||
}
|
return Ok(Err(CancelError::CancelTimelockNotExpiredYet));
|
||||||
|
}
|
||||||
|
|
||||||
if state4
|
if state4
|
||||||
.check_for_tx_cancel(bitcoin_wallet.as_ref())
|
.check_for_tx_cancel(bitcoin_wallet.as_ref())
|
||||||
.await
|
.await
|
||||||
.is_ok()
|
.is_ok()
|
||||||
{
|
{
|
||||||
let state = BobState::BtcCancelled(state4);
|
let state = BobState::BtcCancelled(state4);
|
||||||
let db_state = state.into();
|
let db_state = state.into();
|
||||||
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
||||||
|
|
||||||
return Ok(Err(CancelError::CancelTxAlreadyPublished));
|
return Ok(Err(CancelError::CancelTxAlreadyPublished));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let txid = state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await?;
|
let txid = state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await?;
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::{
|
||||||
execution_params::ExecutionParams,
|
execution_params::ExecutionParams,
|
||||||
protocol::bob::BobState,
|
protocol::bob::BobState,
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::{bail, Result};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
@ -18,11 +18,28 @@ pub async fn refund(
|
||||||
execution_params: ExecutionParams,
|
execution_params: ExecutionParams,
|
||||||
bitcoin_wallet: Arc<Wallet>,
|
bitcoin_wallet: Arc<Wallet>,
|
||||||
db: Database,
|
db: Database,
|
||||||
|
force: bool,
|
||||||
) -> Result<Result<BobState, SwapNotCancelledYet>> {
|
) -> Result<Result<BobState, SwapNotCancelledYet>> {
|
||||||
let state4 = match state {
|
let state4 = if force {
|
||||||
BobState::BtcCancelled(state4) => state4,
|
match state {
|
||||||
_ => {
|
BobState::BtcLocked(state3) => state3.state4(),
|
||||||
return Ok(Err(SwapNotCancelledYet(swap_id)));
|
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)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -34,6 +34,7 @@ async fn given_bob_manually_refunds_after_btc_locked_bob_refunds() {
|
||||||
bob_swap.state,
|
bob_swap.state,
|
||||||
bob_swap.bitcoin_wallet,
|
bob_swap.bitcoin_wallet,
|
||||||
bob_swap.db,
|
bob_swap.db,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -50,6 +51,7 @@ async fn given_bob_manually_refunds_after_btc_locked_bob_refunds() {
|
||||||
bob_swap.execution_params,
|
bob_swap.execution_params,
|
||||||
bob_swap.bitcoin_wallet,
|
bob_swap.bitcoin_wallet,
|
||||||
bob_swap.db,
|
bob_swap.db,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
|
@ -14,10 +14,10 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
|
||||||
tokio::spawn(alice_handle);
|
tokio::spawn(alice_handle);
|
||||||
|
|
||||||
let bob_state = bob::run_until(bob_swap, is_btc_locked).await.unwrap();
|
let bob_state = bob::run_until(bob_swap, is_btc_locked).await.unwrap();
|
||||||
assert!(matches!(bob_state, BobState::BtcLocked {..}));
|
assert!(matches!(bob_state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
||||||
assert!(matches!(bob_swap.state, BobState::BtcLocked {..}));
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
// Bob manually cancels
|
// Bob manually cancels
|
||||||
let result = bob::cancel(
|
let result = bob::cancel(
|
||||||
|
@ -25,6 +25,7 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
|
||||||
bob_swap.state,
|
bob_swap.state,
|
||||||
bob_swap.bitcoin_wallet,
|
bob_swap.bitcoin_wallet,
|
||||||
bob_swap.db,
|
bob_swap.db,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -34,7 +35,7 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
|
||||||
assert!(matches!(result, CancelError::CancelTimelockNotExpiredYet));
|
assert!(matches!(result, CancelError::CancelTimelockNotExpiredYet));
|
||||||
|
|
||||||
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
let (bob_swap, bob_join_handle) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
||||||
assert!(matches!(bob_swap.state, BobState::BtcLocked {..}));
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
|
|
||||||
// Bob manually refunds
|
// Bob manually refunds
|
||||||
bob::refund(
|
bob::refund(
|
||||||
|
@ -43,6 +44,7 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
|
||||||
bob_swap.execution_params,
|
bob_swap.execution_params,
|
||||||
bob_swap.bitcoin_wallet,
|
bob_swap.bitcoin_wallet,
|
||||||
bob_swap.db,
|
bob_swap.db,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -50,7 +52,7 @@ async fn given_bob_manually_cancels_when_timelock_not_expired_errors() {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
let (bob_swap, _) = ctx.stop_and_resume_bob_from_db(bob_join_handle).await;
|
||||||
assert!(matches!(bob_swap.state, BobState::BtcLocked {..}));
|
assert!(matches!(bob_swap.state, BobState::BtcLocked { .. }));
|
||||||
})
|
})
|
||||||
.await;
|
.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
Block a user