Create BobActor

Previously an AliceActor struct was created to reduce the need
to pass around around I/O including wallets the the db. Code is
not building due to lifestime issue with Rng traits.
This commit is contained in:
rishflab 2021-01-14 11:38:28 +11:00
parent 8708d27603
commit 0fe1f7e173
3 changed files with 240 additions and 307 deletions

View File

@ -6,7 +6,6 @@ use futures::{
future::{select, Either},
pin_mut,
};
use rand::{CryptoRng, RngCore};
use std::sync::Arc;
use tracing::info;
use uuid::Uuid;
@ -32,10 +31,6 @@ use crate::{
ExpiredTimelocks,
};
trait Rng: RngCore + CryptoRng + Send {}
impl<T> Rng for T where T: RngCore + CryptoRng + Send {}
pub struct AliceActor {
event_loop_handle: EventLoopHandle,
bitcoin_wallet: Arc<bitcoin::Wallet>,

View File

@ -8,77 +8,71 @@ use uuid::Uuid;
use crate::{
bitcoin,
config::Config,
database::{Database, Swap},
monero,
protocol::bob::{self, event_loop::EventLoopHandle, state::*},
ExpiredTimelocks, SwapAmounts,
};
// TODO(Franck): Make this a method on a struct
#[allow(clippy::too_many_arguments)]
pub async fn swap<R>(
state: BobState,
pub struct BobActor<R>
where
R: RngCore + CryptoRng + Send,
{
event_loop_handle: EventLoopHandle,
db: Database,
bitcoin_wallet: Arc<bitcoin::Wallet>,
monero_wallet: Arc<monero::Wallet>,
rng: R,
db: Database,
config: Config,
swap_id: Uuid,
) -> Result<BobState>
rng: R,
}
impl<R> BobActor<R>
where
R: RngCore + CryptoRng + Send,
{
run_until(
state,
is_complete,
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,
db,
bitcoin_wallet,
monero_wallet,
rng,
db,
config,
swap_id,
)
.await
}
rng,
}
}
pub fn is_complete(state: &BobState) -> bool {
matches!(
state,
BobState::BtcRefunded(..)
| BobState::XmrRedeemed
| BobState::BtcPunished
| BobState::SafelyAborted
)
}
pub async fn swap(self, start_state: BobState) -> Result<BobState> {
self.run_until(start_state, is_complete).await
}
pub fn is_btc_locked(state: &BobState) -> bool {
matches!(state, BobState::BtcLocked(..))
}
pub fn is_xmr_locked(state: &BobState) -> bool {
matches!(state, BobState::XmrLocked(..))
}
pub fn is_encsig_sent(state: &BobState) -> bool {
matches!(state, BobState::EncSigSent(..))
}
// State machine driver for swap execution
#[allow(clippy::too_many_arguments)]
#[async_recursion]
pub async fn run_until<R>(
// State machine driver for swap execution
#[allow(clippy::too_many_arguments)]
#[async_recursion]
pub async fn run_until(
mut self,
state: BobState,
is_target_state: fn(&BobState) -> bool,
mut event_loop_handle: EventLoopHandle,
db: Database,
bitcoin_wallet: Arc<crate::bitcoin::Wallet>,
monero_wallet: Arc<crate::monero::Wallet>,
mut rng: R,
swap_id: Uuid,
) -> Result<BobState>
where
R: RngCore + CryptoRng + Send,
{
) -> 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)
@ -99,17 +93,7 @@ where
let state = BobState::Negotiated(state2);
let db_state = state.clone().into();
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
run_until(
state,
is_target_state,
event_loop_handle,
db,
bitcoin_wallet,
monero_wallet,
rng,
swap_id,
)
.await
self.run_until(state, is_target_state).await
}
BobState::Negotiated(state2) => {
// Do not lock Bitcoin if not connected to Alice.
@ -120,17 +104,7 @@ where
let state = BobState::BtcLocked(state3);
let db_state = state.clone().into();
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
run_until(
state,
is_target_state,
event_loop_handle,
db,
bitcoin_wallet,
monero_wallet,
rng,
swap_id,
)
.await
self.run_until(state, is_target_state).await
}
// Bob has locked Btc
// Watch for Alice to Lock Xmr or for cancel timelock to elapse
@ -173,17 +147,7 @@ where
};
let db_state = state.clone().into();
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
run_until(
state,
is_target_state,
event_loop_handle,
db,
bitcoin_wallet,
monero_wallet,
rng,
swap_id,
)
.await
self.run_until(state, is_target_state).await
}
BobState::XmrLocked(state) => {
let state = if let ExpiredTimelocks::None =
@ -196,10 +160,11 @@ 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 enc_sig_sent_watcher =
event_loop_handle.send_message3(tx_redeem_encsig);
let bitcoin_wallet = bitcoin_wallet.clone();
let cancel_timelock_expires =
state4_clone.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());
let cancel_timelock_expires = state4_clone
.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());
select! {
_ = enc_sig_sent_watcher => {
@ -214,24 +179,15 @@ where
};
let db_state = state.clone().into();
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
run_until(
state,
is_target_state,
event_loop_handle,
db,
bitcoin_wallet,
monero_wallet,
rng,
swap_id,
)
.await
self.run_until(state, is_target_state).await
}
BobState::EncSigSent(state) => {
let state = if let ExpiredTimelocks::None =
state.expired_timelock(bitcoin_wallet.as_ref()).await?
{
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 cancel_timelock_expires =
state_clone.wait_for_cancel_timelock_to_expire(bitcoin_wallet.as_ref());
@ -249,17 +205,7 @@ where
let db_state = state.clone().into();
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
run_until(
state,
is_target_state,
event_loop_handle,
db,
bitcoin_wallet.clone(),
monero_wallet,
rng,
swap_id,
)
.await
self.run_until(state, is_target_state).await
}
BobState::BtcRedeemed(state) => {
// Bob redeems XMR using revealed s_a
@ -268,17 +214,7 @@ where
let state = BobState::XmrRedeemed;
let db_state = state.clone().into();
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
run_until(
state,
is_target_state,
event_loop_handle,
db,
bitcoin_wallet,
monero_wallet,
rng,
swap_id,
)
.await
self.run_until(state, is_target_state).await
}
BobState::CancelTimelockExpired(state4) => {
if state4
@ -293,17 +229,7 @@ where
db.insert_latest_state(swap_id, Swap::Bob(state.clone().into()))
.await?;
run_until(
state,
is_target_state,
event_loop_handle,
db,
bitcoin_wallet,
monero_wallet,
rng,
swap_id,
)
.await
self.run_until(state, is_target_state).await
}
BobState::BtcCancelled(state) => {
// Bob has cancelled the swap
@ -320,17 +246,7 @@ where
let db_state = state.clone().into();
db.insert_latest_state(swap_id, Swap::Bob(db_state)).await?;
run_until(
state,
is_target_state,
event_loop_handle,
db,
bitcoin_wallet,
monero_wallet,
rng,
swap_id,
)
.await
self.run_until(state, is_target_state).await
}
BobState::BtcRefunded(state4) => Ok(BobState::BtcRefunded(state4)),
BobState::BtcPunished => Ok(BobState::BtcPunished),
@ -338,6 +254,29 @@ where
BobState::XmrRedeemed => Ok(BobState::XmrRedeemed),
}
}
}
}
pub fn is_complete(state: &BobState) -> bool {
matches!(
state,
BobState::BtcRefunded(..)
| BobState::XmrRedeemed
| BobState::BtcPunished
| BobState::SafelyAborted
)
}
pub fn is_btc_locked(state: &BobState) -> bool {
matches!(state, BobState::BtcLocked(..))
}
pub fn is_xmr_locked(state: &BobState) -> bool {
matches!(state, BobState::XmrLocked(..))
}
pub fn is_encsig_sent(state: &BobState) -> bool {
matches!(state, BobState::EncSigSent(..))
}
pub async fn negotiate<R>(

View File

@ -1,5 +1,3 @@
use crate::testutils::Test;
use swap::{bitcoin, monero};
use tokio::join;
pub mod testutils;
@ -13,5 +11,6 @@ async fn happy_path() {
alice.assert_btc_redeemed();
bob.assert_btc_redeemed();
}).await;
})
.await;
}