Rename Alice's factory to harness and include redeem assertions

This makes the redeem assertion reusable for all tests with a redeem scenario.
Since the factory was not a clean factory before and is now doing even more it was renamed to harness.
This commit is contained in:
Daniel Karzel 2021-01-15 18:34:46 +11:00
parent 152c8d7eba
commit 87edec0d50
3 changed files with 61 additions and 80 deletions

View File

@ -1,8 +1,5 @@
use rand::rngs::OsRng; use rand::rngs::OsRng;
use swap::{ use swap::protocol::{alice, bob, bob::BobState};
bitcoin,
protocol::{alice, alice::AliceState, bob, bob::BobState},
};
use tokio::join; use tokio::join;
pub mod testutils; pub mod testutils;
@ -11,13 +8,13 @@ pub mod testutils;
#[tokio::test] #[tokio::test]
async fn happy_path() { async fn happy_path() {
testutils::test(|alice_factory, bob, swap_amounts| async move { testutils::test(|alice_harness, bob, swap_amounts| async move {
let alice = alice_factory.new_alice().await; let alice = alice_harness.new_alice().await;
let alice_swap_fut = alice::swap( let alice_swap_fut = alice::swap(
alice.state, alice.state,
alice.event_loop_handle, alice.event_loop_handle,
alice.btc_wallet.clone(), alice.bitcoin_wallet.clone(),
alice.xmr_wallet.clone(), alice.monero_wallet.clone(),
alice.config, alice.config,
alice.swap_id, alice.swap_id,
alice.db, alice.db,
@ -33,25 +30,15 @@ async fn happy_path() {
); );
let (alice_state, bob_state) = join!(alice_swap_fut, bob_swap_fut); let (alice_state, bob_state) = join!(alice_swap_fut, bob_swap_fut);
let btc_alice_final = alice.btc_wallet.as_ref().balance().await.unwrap(); alice_harness.assert_redeemed(alice_state.unwrap()).await;
let btc_bob_final = bob.bitcoin_wallet.as_ref().balance().await.unwrap(); let btc_bob_final = bob.bitcoin_wallet.as_ref().balance().await.unwrap();
let xmr_alice_final = alice.xmr_wallet.as_ref().get_balance().await.unwrap();
bob.monero_wallet.as_ref().inner.refresh().await.unwrap(); bob.monero_wallet.as_ref().inner.refresh().await.unwrap();
let xmr_bob_final = bob.monero_wallet.as_ref().get_balance().await.unwrap(); let xmr_bob_final = bob.monero_wallet.as_ref().get_balance().await.unwrap();
assert!(matches!(alice_state.unwrap(), AliceState::BtcRedeemed));
assert!(matches!(bob_state.unwrap(), BobState::XmrRedeemed)); assert!(matches!(bob_state.unwrap(), BobState::XmrRedeemed));
assert_eq!(
btc_alice_final,
alice.btc_starting_balance + swap_amounts.btc
- bitcoin::Amount::from_sat(bitcoin::TX_FEE)
);
assert!(btc_bob_final <= bob.btc_starting_balance - swap_amounts.btc); assert!(btc_bob_final <= bob.btc_starting_balance - swap_amounts.btc);
assert!(xmr_alice_final <= alice.xmr_starting_balance - swap_amounts.xmr);
assert_eq!(xmr_bob_final, bob.xmr_starting_balance + swap_amounts.xmr); assert_eq!(xmr_bob_final, bob.xmr_starting_balance + swap_amounts.xmr);
}) })
.await; .await;

View File

@ -1,15 +1,12 @@
use rand::rngs::OsRng; use rand::rngs::OsRng;
use swap::{ use swap::protocol::{alice, alice::AliceState, bob, bob::BobState};
bitcoin,
protocol::{alice, alice::AliceState, bob, bob::BobState},
};
pub mod testutils; pub mod testutils;
#[tokio::test] #[tokio::test]
async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
testutils::test(|alice_factory, bob, swap_amounts| async move { testutils::test(|alice_harness, bob, swap_amounts| async move {
let alice = alice_factory.new_alice().await; let alice = alice_harness.new_alice().await;
let bob_swap_fut = bob::swap( let bob_swap_fut = bob::swap(
bob.state, bob.state,
@ -26,8 +23,8 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
alice.state, alice.state,
alice::swap::is_encsig_learned, alice::swap::is_encsig_learned,
alice.event_loop_handle, alice.event_loop_handle,
alice.btc_wallet.clone(), alice.bitcoin_wallet.clone(),
alice.xmr_wallet.clone(), alice.monero_wallet.clone(),
alice.config, alice.config,
alice.swap_id, alice.swap_id,
alice.db, alice.db,
@ -36,14 +33,14 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
.unwrap(); .unwrap();
assert!(matches!(alice_state, AliceState::EncSigLearned {..})); assert!(matches!(alice_state, AliceState::EncSigLearned {..}));
let alice = alice_factory.recover_alice_from_db().await; let alice = alice_harness.recover_alice_from_db().await;
assert!(matches!(alice.state, AliceState::EncSigLearned {..})); assert!(matches!(alice.state, AliceState::EncSigLearned {..}));
let alice_state = alice::swap( let alice_state = alice::swap(
alice.state, alice.state,
alice.event_loop_handle, alice.event_loop_handle,
alice.btc_wallet.clone(), alice.bitcoin_wallet.clone(),
alice.xmr_wallet.clone(), alice.monero_wallet.clone(),
alice.config, alice.config,
alice.swap_id, alice.swap_id,
alice.db, alice.db,
@ -51,27 +48,14 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() {
.await .await
.unwrap(); .unwrap();
alice_harness.assert_redeemed(alice_state).await;
let bob_state = bob_swap_handle.await.unwrap(); let bob_state = bob_swap_handle.await.unwrap();
let btc_alice_final = alice.btc_wallet.as_ref().balance().await.unwrap();
let btc_bob_final = bob.bitcoin_wallet.as_ref().balance().await.unwrap(); let btc_bob_final = bob.bitcoin_wallet.as_ref().balance().await.unwrap();
let xmr_alice_final = alice.xmr_wallet.as_ref().get_balance().await.unwrap();
bob.monero_wallet.as_ref().inner.refresh().await.unwrap(); bob.monero_wallet.as_ref().inner.refresh().await.unwrap();
let xmr_bob_final = bob.monero_wallet.as_ref().get_balance().await.unwrap(); let xmr_bob_final = bob.monero_wallet.as_ref().get_balance().await.unwrap();
assert!(matches!(alice_state, AliceState::BtcRedeemed));
assert!(matches!(bob_state.unwrap(), BobState::XmrRedeemed)); assert!(matches!(bob_state.unwrap(), BobState::XmrRedeemed));
assert_eq!(
btc_alice_final,
alice.btc_starting_balance + swap_amounts.btc
- bitcoin::Amount::from_sat(bitcoin::TX_FEE)
);
assert!(btc_bob_final <= bob.btc_starting_balance - swap_amounts.btc); assert!(btc_bob_final <= bob.btc_starting_balance - swap_amounts.btc);
assert!(xmr_alice_final <= alice.xmr_starting_balance - swap_amounts.xmr);
assert_eq!(xmr_bob_final, bob.xmr_starting_balance + swap_amounts.xmr); assert_eq!(xmr_bob_final, bob.xmr_starting_balance + swap_amounts.xmr);
}) })
.await; .await;

View File

@ -22,20 +22,19 @@ use tracing_core::dispatcher::DefaultGuard;
use tracing_log::LogTracer; use tracing_log::LogTracer;
use uuid::Uuid; use uuid::Uuid;
pub struct StartingBalances {
pub xmr: monero::Amount,
pub btc: bitcoin::Amount,
}
pub struct Alice { pub struct Alice {
pub event_loop_handle: alice::EventLoopHandle,
pub btc_wallet: Arc<bitcoin::Wallet>,
pub xmr_wallet: Arc<monero::Wallet>,
pub config: Config,
pub db: Database,
pub state: AliceState, pub state: AliceState,
pub event_loop_handle: alice::EventLoopHandle,
pub xmr_starting_balance: monero::Amount, pub bitcoin_wallet: Arc<bitcoin::Wallet>,
pub btc_starting_balance: bitcoin::Amount, pub monero_wallet: Arc<monero::Wallet>,
pub config: Config,
// test context (state we have to keep to simulate restart)
pub swap_id: Uuid, pub swap_id: Uuid,
pub db: Database,
} }
pub struct Bob { pub struct Bob {
@ -49,7 +48,7 @@ pub struct Bob {
pub xmr_starting_balance: monero::Amount, pub xmr_starting_balance: monero::Amount,
} }
pub struct AliceFactory { pub struct AliceHarness {
listen_address: Multiaddr, listen_address: Multiaddr,
peer_id: PeerId, peer_id: PeerId,
@ -62,11 +61,10 @@ pub struct AliceFactory {
btc_wallet: Arc<bitcoin::Wallet>, btc_wallet: Arc<bitcoin::Wallet>,
xmr_wallet: Arc<monero::Wallet>, xmr_wallet: Arc<monero::Wallet>,
config: Config, config: Config,
xmr_starting_balance: monero::Amount, starting_balances: StartingBalances,
btc_starting_balance: bitcoin::Amount,
} }
impl AliceFactory { impl AliceHarness {
pub fn peer_id(&self) -> PeerId { pub fn peer_id(&self) -> PeerId {
self.peer_id.clone() self.peer_id.clone()
} }
@ -75,14 +73,28 @@ impl AliceFactory {
self.listen_address.clone() self.listen_address.clone()
} }
pub async fn assert_redeemed(&self, state: AliceState) {
assert!(matches!(state, AliceState::BtcRedeemed));
let btc_alice_final = self.btc_wallet.as_ref().balance().await.unwrap();
assert_eq!(
btc_alice_final,
self.starting_balances.btc + self.swap_amounts.btc
- bitcoin::Amount::from_sat(bitcoin::TX_FEE)
);
let xmr_alice_final = self.xmr_wallet.as_ref().get_balance().await.unwrap();
assert!(xmr_alice_final <= self.starting_balances.xmr - self.swap_amounts.xmr);
}
pub async fn new( pub async fn new(
config: Config, config: Config,
swap_amounts: SwapAmounts, swap_amounts: SwapAmounts,
swap_id: Uuid, swap_id: Uuid,
monero: &Monero, monero: &Monero,
bitcoind: &Bitcoind<'_>, bitcoind: &Bitcoind<'_>,
xmr_starting_balance: monero::Amount, starting_balances: StartingBalances,
btc_starting_balance: bitcoin::Amount,
) -> Self { ) -> Self {
let port = get_port().expect("Failed to find a free port"); let port = get_port().expect("Failed to find a free port");
@ -94,13 +106,13 @@ impl AliceFactory {
let db_path = tempdir().unwrap().path().to_path_buf(); let db_path = tempdir().unwrap().path().to_path_buf();
let alice_xmr_starting_balance = swap_amounts.xmr * 10; let xmr_starting_balance = swap_amounts.xmr * 10;
let (btc_wallet, xmr_wallet) = init_wallets( let (btc_wallet, xmr_wallet) = init_wallets(
"alice", "alice",
bitcoind, bitcoind,
monero, monero,
None, None,
Some(alice_xmr_starting_balance), Some(xmr_starting_balance),
config, config,
) )
.await; .await;
@ -120,8 +132,7 @@ impl AliceFactory {
btc_wallet, btc_wallet,
xmr_wallet, xmr_wallet,
config, config,
xmr_starting_balance, starting_balances,
btc_starting_balance,
} }
} }
@ -143,13 +154,11 @@ impl AliceFactory {
Alice { Alice {
event_loop_handle, event_loop_handle,
btc_wallet: self.btc_wallet.clone(), bitcoin_wallet: self.btc_wallet.clone(),
xmr_wallet: self.xmr_wallet.clone(), monero_wallet: self.xmr_wallet.clone(),
config: self.config, config: self.config,
db, db,
state: initial_state, state: initial_state,
xmr_starting_balance: self.xmr_starting_balance,
btc_starting_balance: self.btc_starting_balance,
swap_id: self.swap_id, swap_id: self.swap_id,
} }
} }
@ -178,20 +187,18 @@ impl AliceFactory {
Alice { Alice {
state: resume_state, state: resume_state,
event_loop_handle, event_loop_handle,
btc_wallet: self.btc_wallet.clone(), bitcoin_wallet: self.btc_wallet.clone(),
xmr_wallet: self.xmr_wallet.clone(), monero_wallet: self.xmr_wallet.clone(),
config: self.config, config: self.config,
swap_id: self.swap_id, swap_id: self.swap_id,
db, db,
xmr_starting_balance: self.xmr_starting_balance,
btc_starting_balance: self.btc_starting_balance,
} }
} }
} }
pub async fn test<T, F>(testfn: T) pub async fn test<T, F>(testfn: T)
where where
T: Fn(AliceFactory, Bob, SwapAmounts) -> F, T: Fn(AliceHarness, Bob, SwapAmounts) -> F,
F: Future<Output = ()>, F: Future<Output = ()>,
{ {
let cli = Cli::default(); let cli = Cli::default();
@ -207,14 +214,17 @@ where
let config = Config::regtest(); let config = Config::regtest();
let alice_factory = AliceFactory::new( let alice_starting_balances = StartingBalances {
xmr: swap_amounts.xmr * 10,
btc: bitcoin::Amount::ZERO,
};
let alice_factory = AliceHarness::new(
config, config,
swap_amounts, swap_amounts,
Uuid::new_v4(), Uuid::new_v4(),
&monero, &monero,
&containers.bitcoind, &containers.bitcoind,
swap_amounts.xmr * 10, alice_starting_balances,
bitcoin::Amount::ZERO,
) )
.await; .await;