mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-10-01 01:45:40 -04:00
Merge #94
94: T1Expired state for Bob and check expiries in states after both locked r=da-kami a=da-kami Co-authored-by: Daniel Karzel <daniel@comit.network>
This commit is contained in:
commit
88c29dbce5
@ -7,13 +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};
|
||||||
|
use tokio::select;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use xmr_btc::{
|
use xmr_btc::{
|
||||||
@ -33,6 +30,7 @@ pub enum BobState {
|
|||||||
XmrLocked(bob::State4, PeerId),
|
XmrLocked(bob::State4, PeerId),
|
||||||
EncSigSent(bob::State4, PeerId),
|
EncSigSent(bob::State4, PeerId),
|
||||||
BtcRedeemed(bob::State5),
|
BtcRedeemed(bob::State5),
|
||||||
|
T1Expired(bob::State4),
|
||||||
Cancelled(bob::State4),
|
Cancelled(bob::State4),
|
||||||
BtcRefunded(bob::State4),
|
BtcRefunded(bob::State4),
|
||||||
XmrRedeemed,
|
XmrRedeemed,
|
||||||
@ -49,6 +47,7 @@ impl fmt::Display for BobState {
|
|||||||
BobState::XmrLocked(..) => write!(f, "xmr_locked"),
|
BobState::XmrLocked(..) => write!(f, "xmr_locked"),
|
||||||
BobState::EncSigSent(..) => write!(f, "encsig_sent"),
|
BobState::EncSigSent(..) => write!(f, "encsig_sent"),
|
||||||
BobState::BtcRedeemed(..) => write!(f, "btc_redeemed"),
|
BobState::BtcRedeemed(..) => write!(f, "btc_redeemed"),
|
||||||
|
BobState::T1Expired(..) => write!(f, "t1_expired"),
|
||||||
BobState::Cancelled(..) => write!(f, "cancelled"),
|
BobState::Cancelled(..) => write!(f, "cancelled"),
|
||||||
BobState::BtcRefunded(..) => write!(f, "btc_refunded"),
|
BobState::BtcRefunded(..) => write!(f, "btc_refunded"),
|
||||||
BobState::XmrRedeemed => write!(f, "xmr_redeemed"),
|
BobState::XmrRedeemed => write!(f, "xmr_redeemed"),
|
||||||
@ -70,6 +69,7 @@ impl From<BobState> for state::Bob {
|
|||||||
BobState::XmrLocked(state4, peer_id) => Bob::XmrLocked { state4, peer_id },
|
BobState::XmrLocked(state4, peer_id) => Bob::XmrLocked { state4, peer_id },
|
||||||
BobState::EncSigSent(state4, peer_id) => Bob::EncSigSent { state4, peer_id },
|
BobState::EncSigSent(state4, peer_id) => Bob::EncSigSent { state4, peer_id },
|
||||||
BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5),
|
BobState::BtcRedeemed(state5) => Bob::BtcRedeemed(state5),
|
||||||
|
BobState::T1Expired(state4) => Bob::T1Expired(state4),
|
||||||
BobState::Cancelled(state4) => Bob::BtcCancelled(state4),
|
BobState::Cancelled(state4) => Bob::BtcCancelled(state4),
|
||||||
BobState::BtcRefunded(_)
|
BobState::BtcRefunded(_)
|
||||||
| BobState::XmrRedeemed
|
| BobState::XmrRedeemed
|
||||||
@ -90,6 +90,7 @@ impl TryFrom<state::Swap> for BobState {
|
|||||||
Bob::XmrLocked { state4, peer_id } => BobState::XmrLocked(state4, peer_id),
|
Bob::XmrLocked { state4, peer_id } => BobState::XmrLocked(state4, peer_id),
|
||||||
Bob::EncSigSent { state4, peer_id } => BobState::EncSigSent(state4, peer_id),
|
Bob::EncSigSent { state4, peer_id } => BobState::EncSigSent(state4, peer_id),
|
||||||
Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5),
|
Bob::BtcRedeemed(state5) => BobState::BtcRedeemed(state5),
|
||||||
|
Bob::T1Expired(state4) => BobState::T1Expired(state4),
|
||||||
Bob::BtcCancelled(state4) => BobState::Cancelled(state4),
|
Bob::BtcCancelled(state4) => BobState::Cancelled(state4),
|
||||||
Bob::SwapComplete => BobState::SafelyAborted,
|
Bob::SwapComplete => BobState::SafelyAborted,
|
||||||
};
|
};
|
||||||
@ -272,18 +273,28 @@ where
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
BobState::XmrLocked(state, alice_peer_id) => {
|
BobState::XmrLocked(state, alice_peer_id) => {
|
||||||
|
let state = if let Epoch::T0 = state.current_epoch(bitcoin_wallet.as_ref()).await? {
|
||||||
// Alice has locked Xmr
|
// Alice has locked Xmr
|
||||||
// Bob sends Alice his key
|
// Bob sends Alice his key
|
||||||
let tx_redeem_encsig = state.tx_redeem_encsig();
|
let tx_redeem_encsig = state.tx_redeem_encsig();
|
||||||
// Do we have to wait for a response?
|
|
||||||
// What if Alice fails to receive this? Should we always resend?
|
|
||||||
// todo: If we cannot dial Alice we should go to EncSigSent. Maybe dialing
|
|
||||||
// should happen in this arm?
|
|
||||||
event_loop_handle
|
|
||||||
.send_message3(alice_peer_id.clone(), tx_redeem_encsig)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let state = BobState::EncSigSent(state, alice_peer_id);
|
let state4_clone = state.clone();
|
||||||
|
let enc_sig_sent_watcher =
|
||||||
|
event_loop_handle.send_message3(alice_peer_id.clone(), tx_redeem_encsig);
|
||||||
|
let bitcoin_wallet = bitcoin_wallet.clone();
|
||||||
|
let t1_timeout = state4_clone.wait_for_t1(bitcoin_wallet.as_ref());
|
||||||
|
|
||||||
|
select! {
|
||||||
|
_ = enc_sig_sent_watcher => {
|
||||||
|
BobState::EncSigSent(state, alice_peer_id)
|
||||||
|
},
|
||||||
|
_ = t1_timeout => {
|
||||||
|
BobState::T1Expired(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
BobState::T1Expired(state)
|
||||||
|
};
|
||||||
let db_state = state.clone().into();
|
let db_state = state.clone().into();
|
||||||
db.insert_latest_state(swap_id, state::Swap::Bob(db_state))
|
db.insert_latest_state(swap_id, state::Swap::Bob(db_state))
|
||||||
.await?;
|
.await?;
|
||||||
@ -300,28 +311,21 @@ where
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
BobState::EncSigSent(state, ..) => {
|
BobState::EncSigSent(state, ..) => {
|
||||||
|
let state = if let Epoch::T0 = state.current_epoch(bitcoin_wallet.as_ref()).await? {
|
||||||
let state_clone = state.clone();
|
let state_clone = state.clone();
|
||||||
let redeem_watcher = state_clone.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_clone.wait_for_t1(bitcoin_wallet.as_ref());
|
let t1_timeout = state_clone.wait_for_t1(bitcoin_wallet.as_ref());
|
||||||
|
|
||||||
pin_mut!(redeem_watcher);
|
select! {
|
||||||
pin_mut!(t1_timeout);
|
state5 = redeem_watcher => {
|
||||||
|
BobState::BtcRedeemed(state5?)
|
||||||
let state = match select(redeem_watcher, t1_timeout).await {
|
},
|
||||||
Either::Left((val, _)) => BobState::BtcRedeemed(val?),
|
_ = t1_timeout => {
|
||||||
Either::Right((..)) => {
|
BobState::T1Expired(state)
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
BobState::T1Expired(state)
|
||||||
};
|
};
|
||||||
|
|
||||||
let db_state = state.clone().into();
|
let db_state = state.clone().into();
|
||||||
@ -359,6 +363,31 @@ where
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
BobState::T1Expired(state4) => {
|
||||||
|
if state4
|
||||||
|
.check_for_tx_cancel(bitcoin_wallet.as_ref())
|
||||||
|
.await
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
|
state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let state = BobState::Cancelled(state4);
|
||||||
|
db.insert_latest_state(swap_id, state::Swap::Bob(state.clone().into()))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
run_until(
|
||||||
|
state,
|
||||||
|
is_target_state,
|
||||||
|
event_loop_handle,
|
||||||
|
db,
|
||||||
|
bitcoin_wallet,
|
||||||
|
monero_wallet,
|
||||||
|
rng,
|
||||||
|
swap_id,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
BobState::Cancelled(state) => {
|
BobState::Cancelled(state) => {
|
||||||
// TODO
|
// TODO
|
||||||
// Bob has cancelled the swap
|
// Bob has cancelled the swap
|
||||||
|
@ -60,6 +60,7 @@ pub enum Bob {
|
|||||||
peer_id: PeerId,
|
peer_id: PeerId,
|
||||||
},
|
},
|
||||||
BtcRedeemed(bob::State5),
|
BtcRedeemed(bob::State5),
|
||||||
|
T1Expired(bob::State4),
|
||||||
BtcCancelled(bob::State4),
|
BtcCancelled(bob::State4),
|
||||||
SwapComplete,
|
SwapComplete,
|
||||||
}
|
}
|
||||||
@ -92,7 +93,7 @@ impl Display for Alice {
|
|||||||
Alice::BtcLocked(_) => f.write_str("Bitcoin locked"),
|
Alice::BtcLocked(_) => f.write_str("Bitcoin locked"),
|
||||||
Alice::XmrLocked(_) => f.write_str("Monero locked"),
|
Alice::XmrLocked(_) => f.write_str("Monero locked"),
|
||||||
Alice::BtcRedeemable { .. } => f.write_str("Bitcoin redeemable"),
|
Alice::BtcRedeemable { .. } => f.write_str("Bitcoin redeemable"),
|
||||||
Alice::T1Expired(_) => f.write_str("Submitting TxCancel"),
|
Alice::T1Expired(_) => f.write_str("Timelock T1 expired"),
|
||||||
Alice::BtcCancelled(_) => f.write_str("Bitcoin cancel transaction published"),
|
Alice::BtcCancelled(_) => f.write_str("Bitcoin cancel transaction published"),
|
||||||
Alice::BtcPunishable(_) => f.write_str("Bitcoin punishable"),
|
Alice::BtcPunishable(_) => f.write_str("Bitcoin punishable"),
|
||||||
Alice::BtcRefunded { .. } => f.write_str("Monero refundable"),
|
Alice::BtcRefunded { .. } => f.write_str("Monero refundable"),
|
||||||
@ -106,9 +107,10 @@ impl Display for Bob {
|
|||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Bob::Negotiated { .. } => f.write_str("Handshake complete"),
|
Bob::Negotiated { .. } => f.write_str("Handshake complete"),
|
||||||
Bob::BtcLocked { .. } | Bob::XmrLocked { .. } | Bob::BtcCancelled(_) => {
|
Bob::BtcLocked { .. } => f.write_str("Bitcoin locked"),
|
||||||
f.write_str("Bitcoin refundable")
|
Bob::XmrLocked { .. } => f.write_str("Monero locked"),
|
||||||
}
|
Bob::T1Expired(_) => f.write_str("Timelock T1 expired"),
|
||||||
|
Bob::BtcCancelled(_) => f.write_str("Bitcoin refundable"),
|
||||||
Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"),
|
Bob::BtcRedeemed(_) => f.write_str("Monero redeemable"),
|
||||||
Bob::SwapComplete => f.write_str("Swap complete"),
|
Bob::SwapComplete => f.write_str("Swap complete"),
|
||||||
Bob::EncSigSent { .. } => f.write_str("Encrypted signature sent"),
|
Bob::EncSigSent { .. } => f.write_str("Encrypted signature sent"),
|
||||||
|
Loading…
Reference in New Issue
Block a user