From 958e5b12bc2e28e6fa3fb2189c0a568f2d128e0e Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 1 Apr 2021 16:18:16 +1100 Subject: [PATCH] Don't match on expired_timelocks and race it in a select in parallel There is no point in first checking for the expired timelocks and then constructing a `select!` that also watches for the timelock to expiry. We can simply only have the select! invocation to achieve the same effect. In case the timelock is already expired, this future will resolve immediately. Normally, the polling order of `select!` is pseudo-random. We configure it to be _biased_ here to make sure the futures are polled in order. --- swap/src/protocol/alice/swap.rs | 41 ++++++++++++++------------------- 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/swap/src/protocol/alice/swap.rs b/swap/src/protocol/alice/swap.rs index a26474fb..c7acbc66 100644 --- a/swap/src/protocol/alice/swap.rs +++ b/swap/src/protocol/alice/swap.rs @@ -146,33 +146,26 @@ async fn next_state( } => { let tx_lock_status = bitcoin_wallet.subscribe_to(state3.tx_lock.clone()).await; - match state3.expired_timelocks(bitcoin_wallet).await? { - ExpiredTimelocks::None => { - select! { - _ = tx_lock_status.wait_until_confirmed_with(state3.cancel_timelock) => { - AliceState::CancelTimelockExpired { - monero_wallet_restore_blockheight, - transfer_proof, - state3, - } - } - enc_sig = event_loop_handle.recv_encrypted_signature() => { - tracing::info!("Received encrypted signature"); + select! { + biased; // make sure the cancel timelock expiry future is polled first - AliceState::EncSigLearned { - monero_wallet_restore_blockheight, - transfer_proof, - encrypted_signature: Box::new(enc_sig?), - state3, - } - } + _ = tx_lock_status.wait_until_confirmed_with(state3.cancel_timelock) => { + AliceState::CancelTimelockExpired { + monero_wallet_restore_blockheight, + transfer_proof, + state3, + } + } + enc_sig = event_loop_handle.recv_encrypted_signature() => { + tracing::info!("Received encrypted signature"); + + AliceState::EncSigLearned { + monero_wallet_restore_blockheight, + transfer_proof, + encrypted_signature: Box::new(enc_sig?), + state3, } } - _ => AliceState::CancelTimelockExpired { - monero_wallet_restore_blockheight, - transfer_proof, - state3, - }, } } AliceState::EncSigLearned {