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

View File

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

View File

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