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:
Thomas Eizinger 2021-03-18 13:13:30 +11:00
parent 8c9285f1f9
commit 05849505b1
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
3 changed files with 40 additions and 86 deletions

View File

@ -19,7 +19,6 @@ mod encrypted_signature;
pub mod event_loop; pub mod event_loop;
mod execution_setup; mod execution_setup;
pub mod state; pub mod state;
mod steps;
pub mod swap; pub mod swap;
mod transfer_proof; mod transfer_proof;

View File

@ -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)
}
}
}

View File

@ -6,7 +6,6 @@ use crate::env::Config;
use crate::monero_ext::ScalarExt; use crate::monero_ext::ScalarExt;
use crate::protocol::alice; use crate::protocol::alice;
use crate::protocol::alice::event_loop::EventLoopHandle; use crate::protocol::alice::event_loop::EventLoopHandle;
use crate::protocol::alice::steps::wait_for_bitcoin_refund;
use crate::protocol::alice::AliceState; use crate::protocol::alice::AliceState;
use crate::{bitcoin, database, monero}; use crate::{bitcoin, database, monero};
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
@ -309,53 +308,47 @@ async fn run_until_internal(
state3, state3,
monero_wallet_restore_blockheight, monero_wallet_restore_blockheight,
} => { } => {
let published_refund_tx = wait_for_bitcoin_refund( let tx_refund = state3.tx_refund();
&state3.tx_cancel(), let tx_cancel = state3.tx_cancel();
&state3.tx_refund(),
state3.punish_timelock,
&bitcoin_wallet,
)
.await?;
// TODO(Franck): Review error handling let seen_refund_tx =
match published_refund_tx { bitcoin_wallet.watch_until_status(&tx_refund, |status| status.has_been_seen());
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?;
run_until_internal( let punish_timelock_expired = bitcoin_wallet
state, .watch_until_status(&tx_cancel, |status| {
is_target_state, status.is_confirmed_with(state3.punish_timelock)
event_loop_handle, });
bitcoin_wallet.clone(),
monero_wallet, let state = tokio::select! {
env_config, seen_refund = seen_refund_tx => {
swap_id, seen_refund.context("Failed to monitor refund transaction")?;
db, let published_refund_tx = bitcoin_wallet.get_raw_transaction(tx_refund.txid()).await?;
)
.await let spend_key = tx_refund.extract_monero_private_key(
}
Some(published_refund_tx) => {
let spend_key = state3.tx_refund().extract_monero_private_key(
published_refund_tx, published_refund_tx,
state3.s_a, state3.s_a,
state3.a.clone(), state3.a.clone(),
state3.S_b_bitcoin, state3.S_b_bitcoin,
)?; )?;
let state = AliceState::BtcRefunded { AliceState::BtcRefunded {
spend_key, spend_key,
state3, state3,
monero_wallet_restore_blockheight, monero_wallet_restore_blockheight,
}
}
_ = punish_timelock_expired => {
AliceState::BtcPunishable {
state3,
monero_wallet_restore_blockheight,
}
}
}; };
let db_state = (&state).into(); let db_state = (&state).into();
db.insert_latest_state(swap_id, database::Swap::Alice(db_state)) db.insert_latest_state(swap_id, database::Swap::Alice(db_state))
.await?; .await?;
run_until_internal( run_until_internal(
state, state,
is_target_state, is_target_state,
@ -368,8 +361,6 @@ async fn run_until_internal(
) )
.await .await
} }
}
}
AliceState::BtcRefunded { AliceState::BtcRefunded {
spend_key, spend_key,
state3, state3,