check for punish timelock in fn refund, not yet expired timelock in cancel and remove breaking database changes

This commit is contained in:
patrini32 2024-06-09 14:19:11 +03:00 committed by patrini32
parent 7f70f23106
commit e7fb332c72
2 changed files with 9 additions and 49 deletions

View File

@ -1,4 +1,4 @@
use crate::bitcoin::{parse_rpc_error_code, RpcErrorCode, Wallet};
use crate::bitcoin::{parse_rpc_error_code, ExpiredTimelocks, RpcErrorCode, Wallet};
use crate::protocol::bob::BobState;
use crate::protocol::Database;
use anyhow::{bail, Result};
@ -72,6 +72,11 @@ pub async fn cancel(
tracing::info!("Cancel transaction has already been confirmed on chain. The swap has therefore already been cancelled by Alice.");
return Ok((tx.txid(), state));
}
if let ExpiredTimelocks::None { .. } =
state6.expired_timelock(bitcoin_wallet.as_ref()).await?
{
tracing::info!("Timelock hasn't expired yet.");
}
if let Ok(error_code) = parse_rpc_error_code(&err) {
tracing::debug!(%error_code, "parse rpc error");
if error_code == i64::from(RpcErrorCode::RpcVerifyAlreadyInChain) {
@ -122,10 +127,8 @@ pub async fn refund(
Ok(state)
}
Err(error) => {
if state6
.check_for_tx_punish(bitcoin_wallet.as_ref())
.await
.is_ok()
if let ExpiredTimelocks::Punish { .. } =
state6.expired_timelock(bitcoin_wallet.as_ref()).await?
{
// Alice already punished Bob, so Bob sets his state to BtcPunished.
let state = BobState::BtcPunished {
@ -134,7 +137,7 @@ pub async fn refund(
db.insert_latest_state(swap_id, state.clone().into())
.await?;
tracing::info!("Cannot refund because BTC is punished by Alice.");
tracing::info!("Cannot refund because BTC is punished.");
return Ok(state);
}
bail!(error);

View File

@ -267,14 +267,12 @@ impl State1 {
punish_timelock: self.punish_timelock,
refund_address: self.refund_address,
redeem_address: self.redeem_address,
punish_address: self.punish_address,
tx_lock: self.tx_lock,
tx_cancel_sig_a: msg.tx_cancel_sig,
tx_refund_encsig: msg.tx_refund_encsig,
min_monero_confirmations: self.min_monero_confirmations,
tx_redeem_fee: self.tx_redeem_fee,
tx_refund_fee: self.tx_refund_fee,
tx_punish_fee: self.tx_punish_fee,
tx_cancel_fee: self.tx_cancel_fee,
})
}
@ -293,7 +291,6 @@ pub struct State2 {
pub punish_timelock: PunishTimelock,
pub refund_address: bitcoin::Address,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
pub tx_lock: bitcoin::TxLock,
tx_cancel_sig_a: Signature,
tx_refund_encsig: bitcoin::EncryptedSignature,
@ -301,8 +298,6 @@ pub struct State2 {
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
tx_redeem_fee: bitcoin::Amount,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
tx_punish_fee: bitcoin::Amount,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub tx_refund_fee: bitcoin::Amount,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub tx_cancel_fee: bitcoin::Amount,
@ -347,7 +342,6 @@ impl State2 {
punish_timelock: self.punish_timelock,
refund_address: self.refund_address,
redeem_address: self.redeem_address,
punish_address: self.punish_address,
tx_lock: self.tx_lock.clone(),
tx_cancel_sig_a: self.tx_cancel_sig_a,
tx_refund_encsig: self.tx_refund_encsig,
@ -355,7 +349,6 @@ impl State2 {
tx_redeem_fee: self.tx_redeem_fee,
tx_refund_fee: self.tx_refund_fee,
tx_cancel_fee: self.tx_cancel_fee,
tx_punish_fee: self.tx_punish_fee,
},
self.tx_lock,
))
@ -375,7 +368,6 @@ pub struct State3 {
punish_timelock: PunishTimelock,
refund_address: bitcoin::Address,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
pub tx_lock: bitcoin::TxLock,
tx_cancel_sig_a: Signature,
tx_refund_encsig: bitcoin::EncryptedSignature,
@ -386,8 +378,6 @@ pub struct State3 {
tx_refund_fee: bitcoin::Amount,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
tx_cancel_fee: bitcoin::Amount,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
tx_punish_fee: bitcoin::Amount,
}
impl State3 {
@ -416,7 +406,6 @@ impl State3 {
punish_timelock: self.punish_timelock,
refund_address: self.refund_address,
redeem_address: self.redeem_address,
punish_address: self.punish_address,
tx_lock: self.tx_lock,
tx_cancel_sig_a: self.tx_cancel_sig_a,
tx_refund_encsig: self.tx_refund_encsig,
@ -424,7 +413,6 @@ impl State3 {
tx_redeem_fee: self.tx_redeem_fee,
tx_refund_fee: self.tx_refund_fee,
tx_cancel_fee: self.tx_cancel_fee,
tx_punish_fee: self.tx_punish_fee,
}
}
@ -436,13 +424,11 @@ impl State3 {
cancel_timelock: self.cancel_timelock,
punish_timelock: self.punish_timelock,
refund_address: self.refund_address.clone(),
punish_address: self.punish_address.clone(),
tx_lock: self.tx_lock.clone(),
tx_cancel_sig_a: self.tx_cancel_sig_a.clone(),
tx_refund_encsig: self.tx_refund_encsig.clone(),
tx_refund_fee: self.tx_refund_fee,
tx_cancel_fee: self.tx_cancel_fee,
tx_punish_fee: self.tx_punish_fee,
}
}
@ -485,7 +471,6 @@ pub struct State4 {
punish_timelock: PunishTimelock,
refund_address: bitcoin::Address,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
pub tx_lock: bitcoin::TxLock,
tx_cancel_sig_a: Signature,
tx_refund_encsig: bitcoin::EncryptedSignature,
@ -496,8 +481,6 @@ pub struct State4 {
tx_refund_fee: bitcoin::Amount,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
tx_cancel_fee: bitcoin::Amount,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
tx_punish_fee: bitcoin::Amount,
}
impl State4 {
@ -586,13 +569,11 @@ impl State4 {
cancel_timelock: self.cancel_timelock,
punish_timelock: self.punish_timelock,
refund_address: self.refund_address,
punish_address: self.punish_address,
tx_lock: self.tx_lock,
tx_cancel_sig_a: self.tx_cancel_sig_a,
tx_refund_encsig: self.tx_refund_encsig,
tx_refund_fee: self.tx_refund_fee,
tx_cancel_fee: self.tx_cancel_fee,
tx_punish_fee: self.tx_punish_fee,
}
}
}
@ -628,7 +609,6 @@ pub struct State6 {
cancel_timelock: CancelTimelock,
punish_timelock: PunishTimelock,
refund_address: bitcoin::Address,
punish_address: bitcoin::Address,
tx_lock: bitcoin::TxLock,
tx_cancel_sig_a: Signature,
tx_refund_encsig: bitcoin::EncryptedSignature,
@ -636,8 +616,6 @@ pub struct State6 {
pub tx_refund_fee: bitcoin::Amount,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub tx_cancel_fee: bitcoin::Amount,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
pub tx_punish_fee: bitcoin::Amount,
}
impl State6 {
@ -681,27 +659,6 @@ impl State6 {
Ok(tx)
}
pub async fn check_for_tx_punish(
&self,
bitcoin_wallet: &bitcoin::Wallet,
) -> Result<Transaction> {
let tx_cancel = bitcoin::TxCancel::new(
&self.tx_lock,
self.cancel_timelock,
self.A,
self.b.public(),
self.tx_cancel_fee,
)?;
let tx_punish = bitcoin::TxPunish::new(
&tx_cancel,
&self.punish_address,
self.punish_timelock,
self.tx_punish_fee,
);
let tx = bitcoin_wallet.get_raw_transaction(tx_punish.txid()).await?;
Ok(tx)
}
pub async fn submit_tx_cancel(
&self,
bitcoin_wallet: &bitcoin::Wallet,