|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
use anyhow::{bail, Result};
|
|
|
|
|
use async_recursion::async_recursion;
|
|
|
|
|
use rand::{CryptoRng, RngCore};
|
|
|
|
|
use rand::{rngs::OsRng, CryptoRng, RngCore};
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use tokio::select;
|
|
|
|
|
use tracing::info;
|
|
|
|
@ -8,122 +8,103 @@ use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
bitcoin,
|
|
|
|
|
config::Config,
|
|
|
|
|
database::{Database, Swap},
|
|
|
|
|
monero,
|
|
|
|
|
protocol::bob::{self, event_loop::EventLoopHandle, state::*},
|
|
|
|
|
ExpiredTimelocks, SwapAmounts,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub struct BobActor<R>
|
|
|
|
|
where
|
|
|
|
|
R: RngCore + CryptoRng + Send,
|
|
|
|
|
{
|
|
|
|
|
pub struct BobActor {
|
|
|
|
|
event_loop_handle: EventLoopHandle,
|
|
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
|
monero_wallet: Arc<monero::Wallet>,
|
|
|
|
|
db: Database,
|
|
|
|
|
config: Config,
|
|
|
|
|
swap_id: Uuid,
|
|
|
|
|
rng: R,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<R> BobActor<R>
|
|
|
|
|
where
|
|
|
|
|
R: RngCore + CryptoRng + Send,
|
|
|
|
|
{
|
|
|
|
|
impl BobActor {
|
|
|
|
|
pub fn new(
|
|
|
|
|
event_loop_handle: EventLoopHandle,
|
|
|
|
|
bitcoin_wallet: Arc<bitcoin::Wallet>,
|
|
|
|
|
monero_wallet: Arc<monero::Wallet>,
|
|
|
|
|
db: Database,
|
|
|
|
|
config: Config,
|
|
|
|
|
swap_id: Uuid,
|
|
|
|
|
rng: R,
|
|
|
|
|
) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
event_loop_handle,
|
|
|
|
|
bitcoin_wallet,
|
|
|
|
|
monero_wallet,
|
|
|
|
|
db,
|
|
|
|
|
config,
|
|
|
|
|
swap_id,
|
|
|
|
|
rng,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn swap(self, start_state: BobState) -> Result<BobState> {
|
|
|
|
|
self.run_until(start_state, is_complete).await
|
|
|
|
|
// TODO: Make a swap abstraction that contains the state and swap id
|
|
|
|
|
pub async fn swap(&mut self, start_state: BobState, swap_id: Uuid) -> Result<BobState> {
|
|
|
|
|
self.run_until(start_state, is_complete, swap_id).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// State machine driver for swap execution
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
|
#[async_recursion]
|
|
|
|
|
pub async fn run_until(
|
|
|
|
|
mut self,
|
|
|
|
|
&mut self,
|
|
|
|
|
state: BobState,
|
|
|
|
|
is_target_state: fn(&BobState) -> bool,
|
|
|
|
|
swap_id: Uuid,
|
|
|
|
|
) -> Result<BobState> {
|
|
|
|
|
let BobActor {
|
|
|
|
|
mut event_loop_handle,
|
|
|
|
|
bitcoin_wallet,
|
|
|
|
|
monero_wallet,
|
|
|
|
|
db,
|
|
|
|
|
config,
|
|
|
|
|
swap_id,
|
|
|
|
|
mut rng,
|
|
|
|
|
} = self;
|
|
|
|
|
info!("Current state: {}", state);
|
|
|
|
|
if is_target_state(&state) {
|
|
|
|
|
Ok(state)
|
|
|
|
|
} else {
|
|
|
|
|
match state {
|
|
|
|
|
BobState::Started { state0, amounts } => {
|
|
|
|
|
event_loop_handle.dial().await?;
|
|
|
|
|
self.event_loop_handle.dial().await?;
|
|
|
|
|
|
|
|
|
|
let state2 = negotiate(
|
|
|
|
|
state0,
|
|
|
|
|
amounts,
|
|
|
|
|
&mut event_loop_handle,
|
|
|
|
|
&mut rng,
|
|
|
|
|
bitcoin_wallet.clone(),
|
|
|
|
|
&mut self.event_loop_handle,
|
|
|
|
|
&mut OsRng,
|
|
|
|
|
self.bitcoin_wallet.clone(),
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
let state = BobState::Negotiated(state2);
|
|
|
|
|
let db_state = state.clone().into();
|
|
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
|
|
|
|
self.run_until(state, is_target_state).await
|
|
|
|
|
self.db
|
|
|
|
|
.insert_latest_state(swap_id, Swap::Bob(db_state))
|
|
|
|
|
.await?;
|
|
|
|
|
self.run_until(state, is_target_state, swap_id).await
|
|
|
|
|
}
|
|
|
|
|
BobState::Negotiated(state2) => {
|
|
|
|
|
// Do not lock Bitcoin if not connected to Alice.
|
|
|
|
|
event_loop_handle.dial().await?;
|
|
|
|
|
self.event_loop_handle.dial().await?;
|
|
|
|
|
// Alice and Bob have exchanged info
|
|
|
|
|
let state3 = state2.lock_btc(bitcoin_wallet.as_ref()).await?;
|
|
|
|
|
let state3 = state2.lock_btc(self.bitcoin_wallet.as_ref()).await?;
|
|
|
|
|
|
|
|
|
|
let state = BobState::BtcLocked(state3);
|
|
|
|
|
let db_state = state.clone().into();
|
|
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
|
|
|
|
self.run_until(state, is_target_state).await
|
|
|
|
|
self.db
|
|
|
|
|
.insert_latest_state(swap_id, Swap::Bob(db_state))
|
|
|
|
|
.await?;
|
|
|
|
|
self.run_until(state, is_target_state, swap_id).await
|
|
|
|
|
}
|
|
|
|
|
// Bob has locked Btc
|
|
|
|
|
// Watch for Alice to Lock Xmr or for cancel timelock to elapse
|
|
|
|
|
BobState::BtcLocked(state3) => {
|
|
|
|
|
let state = if let ExpiredTimelocks::None =
|
|
|
|
|
state3.current_epoch(bitcoin_wallet.as_ref()).await?
|
|
|
|
|
state3.current_epoch(self.bitcoin_wallet.as_ref()).await?
|
|
|
|
|
{
|
|
|
|
|
event_loop_handle.dial().await?;
|
|
|
|
|
self.event_loop_handle.dial().await?;
|
|
|
|
|
|
|
|
|
|
let msg2_watcher = event_loop_handle.recv_message2();
|
|
|
|
|
let msg2_watcher = self.event_loop_handle.recv_message2();
|
|
|
|
|
let cancel_timelock_expires =
|
|
|
|
|
state3.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());
|
|
|
|
|
state3.wait_for_cancel_timelock_to_expire(self.bitcoin_wallet.as_ref());
|
|
|
|
|
|
|
|
|
|
select! {
|
|
|
|
|
msg2 = msg2_watcher => {
|
|
|
|
|
|
|
|
|
|
let xmr_lock_watcher = state3.clone()
|
|
|
|
|
.watch_for_lock_xmr(monero_wallet.as_ref(), msg2?);
|
|
|
|
|
let cancel_timelock_expires = state3.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());
|
|
|
|
|
.watch_for_lock_xmr(self.monero_wallet.as_ref(), msg2?);
|
|
|
|
|
let cancel_timelock_expires = state3.wait_for_cancel_timelock_to_expire(self.bitcoin_wallet.as_ref());
|
|
|
|
|
|
|
|
|
|
select! {
|
|
|
|
|
state4 = xmr_lock_watcher => {
|
|
|
|
@ -146,14 +127,16 @@ where
|
|
|
|
|
BobState::CancelTimelockExpired(state4)
|
|
|
|
|
};
|
|
|
|
|
let db_state = state.clone().into();
|
|
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
|
|
|
|
self.run_until(state, is_target_state).await
|
|
|
|
|
self.db
|
|
|
|
|
.insert_latest_state(swap_id, Swap::Bob(db_state))
|
|
|
|
|
.await?;
|
|
|
|
|
self.run_until(state, is_target_state, swap_id).await
|
|
|
|
|
}
|
|
|
|
|
BobState::XmrLocked(state) => {
|
|
|
|
|
let state = if let ExpiredTimelocks::None =
|
|
|
|
|
state.expired_timelock(bitcoin_wallet.as_ref()).await?
|
|
|
|
|
state.expired_timelock(self.bitcoin_wallet.as_ref()).await?
|
|
|
|
|
{
|
|
|
|
|
event_loop_handle.dial().await?;
|
|
|
|
|
self.event_loop_handle.dial().await?;
|
|
|
|
|
// Alice has locked Xmr
|
|
|
|
|
// Bob sends Alice his key
|
|
|
|
|
let tx_redeem_encsig = state.tx_redeem_encsig();
|
|
|
|
@ -161,8 +144,8 @@ where
|
|
|
|
|
let state4_clone = state.clone();
|
|
|
|
|
// TODO(Franck): Refund if message cannot be sent.
|
|
|
|
|
let enc_sig_sent_watcher =
|
|
|
|
|
event_loop_handle.send_message3(tx_redeem_encsig);
|
|
|
|
|
let bitcoin_wallet = bitcoin_wallet.clone();
|
|
|
|
|
self.event_loop_handle.send_message3(tx_redeem_encsig);
|
|
|
|
|
let bitcoin_wallet = self.bitcoin_wallet.clone();
|
|
|
|
|
let cancel_timelock_expires = state4_clone
|
|
|
|
|
.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());
|
|
|
|
|
|
|
|
|
@ -178,18 +161,20 @@ where
|
|
|
|
|
BobState::CancelTimelockExpired(state)
|
|
|
|
|
};
|
|
|
|
|
let db_state = state.clone().into();
|
|
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
|
|
|
|
self.run_until(state, is_target_state).await
|
|
|
|
|
self.db
|
|
|
|
|
.insert_latest_state(swap_id, Swap::Bob(db_state))
|
|
|
|
|
.await?;
|
|
|
|
|
self.run_until(state, is_target_state, swap_id).await
|
|
|
|
|
}
|
|
|
|
|
BobState::EncSigSent(state) => {
|
|
|
|
|
let state = if let ExpiredTimelocks::None =
|
|
|
|
|
state.expired_timelock(bitcoin_wallet.as_ref()).await?
|
|
|
|
|
state.expired_timelock(self.bitcoin_wallet.as_ref()).await?
|
|
|
|
|
{
|
|
|
|
|
let state_clone = state.clone();
|
|
|
|
|
let redeem_watcher =
|
|
|
|
|
state_clone.watch_for_redeem_btc(bitcoin_wallet.as_ref());
|
|
|
|
|
let cancel_timelock_expires =
|
|
|
|
|
state_clone.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());
|
|
|
|
|
state_clone.watch_for_redeem_btc(self.bitcoin_wallet.as_ref());
|
|
|
|
|
let cancel_timelock_expires = state_clone
|
|
|
|
|
.wait_for_cancel_timelock_to_expire(self.bitcoin_wallet.as_ref());
|
|
|
|
|
|
|
|
|
|
select! {
|
|
|
|
|
state5 = redeem_watcher => {
|
|
|
|
@ -204,49 +189,58 @@ where
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let db_state = state.clone().into();
|
|
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
|
|
|
|
self.run_until(state, is_target_state).await
|
|
|
|
|
self.db
|
|
|
|
|
.insert_latest_state(swap_id, Swap::Bob(db_state))
|
|
|
|
|
.await?;
|
|
|
|
|
self.run_until(state, is_target_state, swap_id).await
|
|
|
|
|
}
|
|
|
|
|
BobState::BtcRedeemed(state) => {
|
|
|
|
|
// Bob redeems XMR using revealed s_a
|
|
|
|
|
state.claim_xmr(monero_wallet.as_ref()).await?;
|
|
|
|
|
state.claim_xmr(self.monero_wallet.as_ref()).await?;
|
|
|
|
|
|
|
|
|
|
let state = BobState::XmrRedeemed;
|
|
|
|
|
let db_state = state.clone().into();
|
|
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
|
|
|
|
self.run_until(state, is_target_state).await
|
|
|
|
|
self.db
|
|
|
|
|
.insert_latest_state(swap_id, Swap::Bob(db_state))
|
|
|
|
|
.await?;
|
|
|
|
|
self.run_until(state, is_target_state, swap_id).await
|
|
|
|
|
}
|
|
|
|
|
BobState::CancelTimelockExpired(state4) => {
|
|
|
|
|
if state4
|
|
|
|
|
.check_for_tx_cancel(bitcoin_wallet.as_ref())
|
|
|
|
|
.check_for_tx_cancel(self.bitcoin_wallet.as_ref())
|
|
|
|
|
.await
|
|
|
|
|
.is_err()
|
|
|
|
|
{
|
|
|
|
|
state4.submit_tx_cancel(bitcoin_wallet.as_ref()).await?;
|
|
|
|
|
state4
|
|
|
|
|
.submit_tx_cancel(self.bitcoin_wallet.as_ref())
|
|
|
|
|
.await?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let state = BobState::BtcCancelled(state4);
|
|
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(state.clone().into()))
|
|
|
|
|
self.db
|
|
|
|
|
.insert_latest_state(swap_id, Swap::Bob(state.clone().into()))
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
self.run_until(state, is_target_state).await
|
|
|
|
|
self.run_until(state, is_target_state, swap_id).await
|
|
|
|
|
}
|
|
|
|
|
BobState::BtcCancelled(state) => {
|
|
|
|
|
// Bob has cancelled the swap
|
|
|
|
|
let state = match state.expired_timelock(bitcoin_wallet.as_ref()).await? {
|
|
|
|
|
let state = match state.expired_timelock(self.bitcoin_wallet.as_ref()).await? {
|
|
|
|
|
ExpiredTimelocks::None => {
|
|
|
|
|
bail!("Internal error: canceled state reached before cancel timelock was expired");
|
|
|
|
|
}
|
|
|
|
|
ExpiredTimelocks::Cancel => {
|
|
|
|
|
state.refund_btc(bitcoin_wallet.as_ref()).await?;
|
|
|
|
|
state.refund_btc(self.bitcoin_wallet.as_ref()).await?;
|
|
|
|
|
BobState::BtcRefunded(state)
|
|
|
|
|
}
|
|
|
|
|
ExpiredTimelocks::Punish => BobState::BtcPunished,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let db_state = state.clone().into();
|
|
|
|
|
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
|
|
|
|
|
self.run_until(state, is_target_state).await
|
|
|
|
|
self.db
|
|
|
|
|
.insert_latest_state(swap_id, Swap::Bob(db_state))
|
|
|
|
|
.await?;
|
|
|
|
|
self.run_until(state, is_target_state, swap_id).await
|
|
|
|
|
}
|
|
|
|
|
BobState::BtcRefunded(state4) => Ok(BobState::BtcRefunded(state4)),
|
|
|
|
|
BobState::BtcPunished => Ok(BobState::BtcPunished),
|
|
|
|
|