Use early return to reduce one level of indentation

This commit is contained in:
Thomas Eizinger 2021-03-18 13:16:21 +11:00
parent 05849505b1
commit 0d8962762a
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96
2 changed files with 676 additions and 681 deletions

View File

@ -70,14 +70,14 @@ async fn run_until_internal(
) -> Result<AliceState> { ) -> Result<AliceState> {
info!("Current state: {}", state); info!("Current state: {}", state);
if is_target_state(&state) { if is_target_state(&state) {
Ok(state) return Ok(state);
} else { }
match state { match state {
AliceState::Started { state3 } => { AliceState::Started { state3 } => {
timeout( timeout(
env_config.bob_time_to_act, env_config.bob_time_to_act,
bitcoin_wallet bitcoin_wallet.watch_until_status(&state3.tx_lock, |status| status.has_been_seen()),
.watch_until_status(&state3.tx_lock, |status| status.has_been_seen()),
) )
.await .await
.context("Failed to find lock Bitcoin tx")??; .context("Failed to find lock Bitcoin tx")??;
@ -267,11 +267,7 @@ async fn run_until_internal(
.is_err() .is_err()
{ {
let transaction = tx_cancel let transaction = tx_cancel
.complete_as_alice( .complete_as_alice(state3.a.clone(), state3.B, state3.tx_cancel_sig_bob.clone())
state3.a.clone(),
state3.B,
state3.tx_cancel_sig_bob.clone(),
)
.context("Failed to complete Bitcoin cancel transaction")?; .context("Failed to complete Bitcoin cancel transaction")?;
if let Err(e) = bitcoin_wallet.broadcast(transaction, "cancel").await { if let Err(e) = bitcoin_wallet.broadcast(transaction, "cancel").await {
@ -314,8 +310,7 @@ async fn run_until_internal(
let seen_refund_tx = let seen_refund_tx =
bitcoin_wallet.watch_until_status(&tx_refund, |status| status.has_been_seen()); bitcoin_wallet.watch_until_status(&tx_refund, |status| status.has_been_seen());
let punish_timelock_expired = bitcoin_wallet let punish_timelock_expired = bitcoin_wallet.watch_until_status(&tx_cancel, |status| {
.watch_until_status(&tx_cancel, |status| {
status.is_confirmed_with(state3.punish_timelock) status.is_confirmed_with(state3.punish_timelock)
}); });
@ -389,8 +384,7 @@ async fn run_until_internal(
)?; )?;
let punish_tx_finalised = async { let punish_tx_finalised = async {
let (txid, finality) = let (txid, finality) = bitcoin_wallet.broadcast(signed_tx_punish, "punish").await?;
bitcoin_wallet.broadcast(signed_tx_punish, "punish").await?;
finality.await?; finality.await?;
@ -463,4 +457,3 @@ async fn run_until_internal(
AliceState::SafelyAborted => Ok(AliceState::SafelyAborted), AliceState::SafelyAborted => Ok(AliceState::SafelyAborted),
} }
} }
}

View File

@ -63,8 +63,9 @@ async fn run_until_internal(
) -> Result<BobState> { ) -> Result<BobState> {
trace!("Current state: {}", state); trace!("Current state: {}", state);
if is_target_state(&state) { if is_target_state(&state) {
Ok(state) return Ok(state);
} else { }
match state { match state {
BobState::Started { btc_amount } => { BobState::Started { btc_amount } => {
let bitcoin_refund_address = bitcoin_wallet.new_address().await?; let bitcoin_refund_address = bitcoin_wallet.new_address().await?;
@ -373,7 +374,9 @@ async fn run_until_internal(
// Bob has cancelled the swap // Bob has cancelled the swap
let state = match state.expired_timelock(bitcoin_wallet.as_ref()).await? { let state = match state.expired_timelock(bitcoin_wallet.as_ref()).await? {
ExpiredTimelocks::None => { ExpiredTimelocks::None => {
bail!("Internal error: canceled state reached before cancel timelock was expired"); bail!(
"Internal error: canceled state reached before cancel timelock was expired"
);
} }
ExpiredTimelocks::Cancel => { ExpiredTimelocks::Cancel => {
state.refund_btc(bitcoin_wallet.as_ref()).await?; state.refund_btc(bitcoin_wallet.as_ref()).await?;
@ -405,7 +408,6 @@ async fn run_until_internal(
BobState::XmrRedeemed { tx_lock_id } => Ok(BobState::XmrRedeemed { tx_lock_id }), BobState::XmrRedeemed { tx_lock_id } => Ok(BobState::XmrRedeemed { tx_lock_id }),
} }
} }
}
pub async fn request_price_and_setup( pub async fn request_price_and_setup(
btc: bitcoin::Amount, btc: bitcoin::Amount,