Add --force flag for cancel and refund

This commit is contained in:
Daniel Karzel 2021-02-01 22:32:54 +11:00
parent 02f8eb7f18
commit c930ad84a4
8 changed files with 118 additions and 22 deletions

View file

@ -96,6 +96,9 @@ pub enum Cancel {
#[structopt(flatten)]
config: Config,
#[structopt(short, long)]
force: bool,
},
}
@ -114,6 +117,9 @@ pub enum Refund {
#[structopt(flatten)]
config: Config,
#[structopt(short, long)]
force: bool,
},
}

View file

@ -215,6 +215,7 @@ async fn main() -> Result<()> {
alice_peer_id,
alice_addr,
config,
force,
}) => {
// TODO: Optimization: Only init the Bitcoin wallet, Monero wallet unnecessary
let (bitcoin_wallet, monero_wallet) =
@ -234,7 +235,15 @@ async fn main() -> Result<()> {
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, _)) => {
info!("Cancel transaction successfully published with id {}", txid)
}
@ -251,6 +260,7 @@ async fn main() -> Result<()> {
alice_peer_id,
alice_addr,
config,
force,
}) => {
let (bitcoin_wallet, monero_wallet) =
init_wallets(config.path, bitcoin_network, monero_network).await?;
@ -275,6 +285,7 @@ async fn main() -> Result<()> {
swap.execution_params,
swap.bitcoin_wallet,
swap.db,
force,
)
.await??;
}

View file

@ -20,6 +20,7 @@ pub async fn cancel(
state: BobState,
bitcoin_wallet: Arc<Wallet>,
db: Database,
force: bool,
) -> Result<Result<(Txid, BobState), CancelError>> {
let state4 = match state {
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? {
return Ok(Err(CancelError::CancelTimelockNotExpiredYet));
}
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?;
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));
return Ok(Err(CancelError::CancelTxAlreadyPublished));
}
}
let txid = state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await?;

View file

@ -4,7 +4,7 @@ use crate::{
execution_params::ExecutionParams,
protocol::bob::BobState,
};
use anyhow::Result;
use anyhow::{bail, Result};
use std::sync::Arc;
use uuid::Uuid;
@ -18,11 +18,28 @@ pub async fn refund(
execution_params: ExecutionParams,
bitcoin_wallet: Arc<Wallet>,
db: Database,
force: bool,
) -> Result<Result<BobState, SwapNotCancelledYet>> {
let state4 = match state {
BobState::BtcCancelled(state4) => state4,
_ => {
return Ok(Err(SwapNotCancelledYet(swap_id)));
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)));
}
}
};