Remove dupe code

This commit is contained in:
Franck Royer 2020-12-15 16:44:13 +11:00
parent 9d266fc648
commit 53d13330c2
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4

View File

@ -7,6 +7,10 @@ use crate::{
}; };
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use async_recursion::async_recursion; use async_recursion::async_recursion;
use futures::{
future::{select, Either},
pin_mut,
};
use libp2p::{core::Multiaddr, PeerId}; use libp2p::{core::Multiaddr, PeerId};
use rand::{CryptoRng, RngCore}; use rand::{CryptoRng, RngCore};
use std::{convert::TryFrom, fmt, sync::Arc}; use std::{convert::TryFrom, fmt, sync::Arc};
@ -296,53 +300,45 @@ where
.await .await
} }
BobState::EncSigSent(state, ..) => { BobState::EncSigSent(state, ..) => {
// Watch for redeem let state_clone = state.clone();
let redeem_watcher = state.watch_for_redeem_btc(bitcoin_wallet.as_ref()); let redeem_watcher = state_clone.watch_for_redeem_btc(bitcoin_wallet.as_ref());
let t1_timeout = state.wait_for_t1(bitcoin_wallet.as_ref()); let t1_timeout = state_clone.wait_for_t1(bitcoin_wallet.as_ref());
pin_mut!(redeem_watcher);
pin_mut!(t1_timeout);
let state = match select(redeem_watcher, t1_timeout).await {
Either::Left((val, _)) => BobState::BtcRedeemed(val?),
Either::Right((..)) => {
// Check whether TxCancel has been published.
// We should not fail if the transaction is already on the blockchain
if state
.check_for_tx_cancel(bitcoin_wallet.as_ref())
.await
.is_err()
{
state.submit_tx_cancel(bitcoin_wallet.as_ref()).await?;
}
BobState::Cancelled(state)
}
};
// TODO(Franck): Check if db save and run_until can be factorized
tokio::select! {
val = redeem_watcher => {
let state = BobState::BtcRedeemed(val?);
let db_state = state.clone().into(); let db_state = state.clone().into();
db.insert_latest_state(swap_id, state::Swap::Bob(db_state)).await?; db.insert_latest_state(swap_id, state::Swap::Bob(db_state))
.await?;
run_until( run_until(
state, state,
is_target_state, is_target_state,
event_loop_handle, event_loop_handle,
db, db,
bitcoin_wallet, bitcoin_wallet.clone(),
monero_wallet, monero_wallet,
rng, rng,
swap_id, swap_id,
) )
.await .await
} }
_ = t1_timeout => {
// Check whether TxCancel has been published.
// We should not fail if the transaction is already on the blockchain
if state.check_for_tx_cancel(bitcoin_wallet.as_ref()).await.is_err() {
state.submit_tx_cancel(bitcoin_wallet.as_ref()).await?;
}
let state = BobState::Cancelled(state);
let db_state = state.clone().into();
db.insert_latest_state(swap_id, state::Swap::Bob(db_state)).await?;
run_until(
state,
is_target_state,
event_loop_handle,
db,
bitcoin_wallet,
monero_wallet,
rng,
swap_id
)
.await
}
}
}
BobState::BtcRedeemed(state) => { BobState::BtcRedeemed(state) => {
// Bob redeems XMR using revealed s_a // Bob redeems XMR using revealed s_a
state.claim_xmr(monero_wallet.as_ref()).await?; state.claim_xmr(monero_wallet.as_ref()).await?;