mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Inline wait_for_bitcoin_refund
This function is essentially a single select! statement and can easily be inlined into the swap state machine.
This commit is contained in:
parent
8c9285f1f9
commit
05849505b1
@ -19,7 +19,6 @@ mod encrypted_signature;
|
||||
pub mod event_loop;
|
||||
mod execution_setup;
|
||||
pub mod state;
|
||||
mod steps;
|
||||
pub mod swap;
|
||||
mod transfer_proof;
|
||||
|
||||
|
@ -1,36 +0,0 @@
|
||||
use crate::bitcoin;
|
||||
use crate::bitcoin::{PunishTimelock, TxCancel, TxRefund};
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
pub async fn wait_for_bitcoin_refund(
|
||||
tx_cancel: &TxCancel,
|
||||
tx_refund: &TxRefund,
|
||||
punish_timelock: PunishTimelock,
|
||||
bitcoin_wallet: &bitcoin::Wallet,
|
||||
) -> Result<Option<bitcoin::Transaction>> {
|
||||
let refund_tx_id = tx_refund.txid();
|
||||
let seen_refund_tx =
|
||||
bitcoin_wallet.watch_until_status(tx_refund, |status| status.has_been_seen());
|
||||
|
||||
let punish_timelock_expired = bitcoin_wallet.watch_until_status(tx_cancel, |status| {
|
||||
status.is_confirmed_with(punish_timelock)
|
||||
});
|
||||
|
||||
tokio::select! {
|
||||
seen_refund = seen_refund_tx => {
|
||||
match seen_refund {
|
||||
Ok(()) => {
|
||||
let published_refund_tx = bitcoin_wallet.get_raw_transaction(refund_tx_id).await?;
|
||||
|
||||
Ok(Some(published_refund_tx))
|
||||
}
|
||||
Err(e) => {
|
||||
bail!(e.context("Failed to monitor refund transaction"))
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = punish_timelock_expired => {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
@ -6,7 +6,6 @@ use crate::env::Config;
|
||||
use crate::monero_ext::ScalarExt;
|
||||
use crate::protocol::alice;
|
||||
use crate::protocol::alice::event_loop::EventLoopHandle;
|
||||
use crate::protocol::alice::steps::wait_for_bitcoin_refund;
|
||||
use crate::protocol::alice::AliceState;
|
||||
use crate::{bitcoin, database, monero};
|
||||
use anyhow::{bail, Context, Result};
|
||||
@ -309,53 +308,47 @@ async fn run_until_internal(
|
||||
state3,
|
||||
monero_wallet_restore_blockheight,
|
||||
} => {
|
||||
let published_refund_tx = wait_for_bitcoin_refund(
|
||||
&state3.tx_cancel(),
|
||||
&state3.tx_refund(),
|
||||
state3.punish_timelock,
|
||||
&bitcoin_wallet,
|
||||
)
|
||||
.await?;
|
||||
let tx_refund = state3.tx_refund();
|
||||
let tx_cancel = state3.tx_cancel();
|
||||
|
||||
// TODO(Franck): Review error handling
|
||||
match published_refund_tx {
|
||||
None => {
|
||||
let state = AliceState::BtcPunishable {
|
||||
state3,
|
||||
monero_wallet_restore_blockheight,
|
||||
};
|
||||
let db_state = (&state).into();
|
||||
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
|
||||
.await?;
|
||||
let seen_refund_tx =
|
||||
bitcoin_wallet.watch_until_status(&tx_refund, |status| status.has_been_seen());
|
||||
|
||||
run_until_internal(
|
||||
state,
|
||||
is_target_state,
|
||||
event_loop_handle,
|
||||
bitcoin_wallet.clone(),
|
||||
monero_wallet,
|
||||
env_config,
|
||||
swap_id,
|
||||
db,
|
||||
)
|
||||
.await
|
||||
}
|
||||
Some(published_refund_tx) => {
|
||||
let spend_key = state3.tx_refund().extract_monero_private_key(
|
||||
let punish_timelock_expired = bitcoin_wallet
|
||||
.watch_until_status(&tx_cancel, |status| {
|
||||
status.is_confirmed_with(state3.punish_timelock)
|
||||
});
|
||||
|
||||
let state = tokio::select! {
|
||||
seen_refund = seen_refund_tx => {
|
||||
seen_refund.context("Failed to monitor refund transaction")?;
|
||||
let published_refund_tx = bitcoin_wallet.get_raw_transaction(tx_refund.txid()).await?;
|
||||
|
||||
let spend_key = tx_refund.extract_monero_private_key(
|
||||
published_refund_tx,
|
||||
state3.s_a,
|
||||
state3.a.clone(),
|
||||
state3.S_b_bitcoin,
|
||||
)?;
|
||||
|
||||
let state = AliceState::BtcRefunded {
|
||||
AliceState::BtcRefunded {
|
||||
spend_key,
|
||||
state3,
|
||||
monero_wallet_restore_blockheight,
|
||||
}
|
||||
}
|
||||
_ = punish_timelock_expired => {
|
||||
AliceState::BtcPunishable {
|
||||
state3,
|
||||
monero_wallet_restore_blockheight,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let db_state = (&state).into();
|
||||
db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
|
||||
.await?;
|
||||
|
||||
run_until_internal(
|
||||
state,
|
||||
is_target_state,
|
||||
@ -368,8 +361,6 @@ async fn run_until_internal(
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
AliceState::BtcRefunded {
|
||||
spend_key,
|
||||
state3,
|
||||
|
Loading…
Reference in New Issue
Block a user