From 8c929564efd9c4a54a1740689b332ffe0b79934d Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Mon, 14 Dec 2020 20:51:20 +1100 Subject: [PATCH] Refactor container initialization --- swap/tests/alice_safe_restart.rs | 31 +++----- swap/tests/happy_path.rs | 16 ++-- swap/tests/punish.rs | 17 ++--- swap/tests/refund.rs | 22 ++---- swap/tests/testutils/mod.rs | 127 ++++++++++++++++++------------- 5 files changed, 107 insertions(+), 106 deletions(-) diff --git a/swap/tests/alice_safe_restart.rs b/swap/tests/alice_safe_restart.rs index 571629c9..cde35821 100644 --- a/swap/tests/alice_safe_restart.rs +++ b/swap/tests/alice_safe_restart.rs @@ -1,6 +1,4 @@ -use bitcoin_harness::Bitcoind; use libp2p::Multiaddr; -use monero_harness::Monero; use rand::rngs::OsRng; use swap::{alice, alice::swap::AliceState, bitcoin, bob, storage::Database}; use tempfile::tempdir; @@ -18,19 +16,18 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { let _guard = init_tracing(); let cli = Cli::default(); - let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap(); - let _ = bitcoind.init(5).await; - let (monero, _container) = - Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()]) - .await - .unwrap(); + let ( + monero, + testutils::Containers { + bitcoind, + monerods: _monerods, + }, + ) = testutils::init_containers(&cli).await; let btc_to_swap = bitcoin::Amount::from_sat(1_000_000); let xmr_to_swap = xmr_btc::monero::Amount::from_piconero(1_000_000_000_000); let bob_btc_starting_balance = btc_to_swap * 10; - let bob_xmr_starting_balance = xmr_btc::monero::Amount::ZERO; - let alice_xmr_starting_balance = xmr_to_swap * 10; let alice_multiaddr: Multiaddr = "/ip4/127.0.0.1/tcp/9877" @@ -64,7 +61,6 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { btc_to_swap, bob_btc_starting_balance, xmr_to_swap, - bob_xmr_starting_balance, config, ) .await; @@ -113,15 +109,8 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { assert!(matches!(state, swap::state::Alice::EncSignLearned {..})); } - let (_, mut event_loop_after_restart, event_loop_handle_after_restart) = - testutils::init_alice_eventloop( - btc_to_swap, - xmr_to_swap, - alice_btc_wallet.clone(), - alice_multiaddr, - config, - ) - .await; + let (mut event_loop_after_restart, event_loop_handle_after_restart) = + testutils::init_alice_eventloop(alice_multiaddr); let _alice_swarm_fut = tokio::spawn(async move { event_loop_after_restart.run().await }); let alice_state = alice::swap::recover( @@ -136,4 +125,6 @@ async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { .unwrap(); assert!(matches!(alice_state, AliceState::BtcRedeemed {..})); + + // TODO: Additionally assert balances } diff --git a/swap/tests/happy_path.rs b/swap/tests/happy_path.rs index bb19212b..a8f11b47 100644 --- a/swap/tests/happy_path.rs +++ b/swap/tests/happy_path.rs @@ -1,8 +1,6 @@ use crate::testutils::{init_alice, init_bob}; -use bitcoin_harness::Bitcoind; use futures::future::try_join; use libp2p::Multiaddr; -use monero_harness::Monero; use rand::rngs::OsRng; use swap::{alice, bob, storage::Database}; use tempfile::tempdir; @@ -20,12 +18,13 @@ async fn happy_path() { let _guard = init_tracing(); let cli = Cli::default(); - let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap(); - let _ = bitcoind.init(5).await; - let (monero, _container) = - Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()]) - .await - .unwrap(); + let ( + monero, + testutils::Containers { + bitcoind, + monerods: _monerods, + }, + ) = testutils::init_containers(&cli).await; let btc_to_swap = bitcoin::Amount::from_sat(1_000_000); let btc_alice = bitcoin::Amount::ZERO; @@ -69,7 +68,6 @@ async fn happy_path() { btc_to_swap, btc_bob, xmr_to_swap, - xmr_bob, config, ) .await; diff --git a/swap/tests/punish.rs b/swap/tests/punish.rs index 1db6a360..5254a10d 100644 --- a/swap/tests/punish.rs +++ b/swap/tests/punish.rs @@ -1,8 +1,6 @@ use crate::testutils::{init_alice, init_bob}; -use bitcoin_harness::Bitcoind; use futures::future::try_join; use libp2p::Multiaddr; -use monero_harness::Monero; use rand::rngs::OsRng; use swap::{alice, alice::swap::AliceState, bob, bob::swap::BobState, storage::Database}; use tempfile::tempdir; @@ -20,18 +18,18 @@ async fn alice_punishes_if_bob_never_acts_after_fund() { let _guard = init_tracing(); let cli = Cli::default(); - let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap(); - let _ = bitcoind.init(5).await; - let (monero, _container) = - Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()]) - .await - .unwrap(); + let ( + monero, + testutils::Containers { + bitcoind, + monerods: _monerods, + }, + ) = testutils::init_containers(&cli).await; let btc_to_swap = bitcoin::Amount::from_sat(1_000_000); let xmr_to_swap = xmr_btc::monero::Amount::from_piconero(1_000_000_000_000); let bob_btc_starting_balance = btc_to_swap * 10; - let bob_xmr_starting_balance = xmr_btc::monero::Amount::ZERO; let alice_btc_starting_balance = bitcoin::Amount::ZERO; let alice_xmr_starting_balance = xmr_to_swap * 10; @@ -68,7 +66,6 @@ async fn alice_punishes_if_bob_never_acts_after_fund() { btc_to_swap, bob_btc_starting_balance, xmr_to_swap, - bob_xmr_starting_balance, config, ) .await; diff --git a/swap/tests/refund.rs b/swap/tests/refund.rs index ce99f36e..d8e533dd 100644 --- a/swap/tests/refund.rs +++ b/swap/tests/refund.rs @@ -1,8 +1,6 @@ use crate::testutils::{init_alice, init_bob}; -use bitcoin_harness::Bitcoind; use futures::future::try_join; use libp2p::Multiaddr; -use monero_harness::Monero; use rand::rngs::OsRng; use swap::{alice, alice::swap::AliceState, bob, bob::swap::BobState, storage::Database}; use tempfile::tempdir; @@ -19,19 +17,14 @@ pub mod testutils; async fn both_refund() { let _guard = init_tracing(); - use tracing_subscriber::util::SubscriberInitExt as _; - let _guard = tracing_subscriber::fmt() - .with_env_filter("swap=info,xmr_btc=info") - .with_ansi(false) - .set_default(); - let cli = Cli::default(); - let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap(); - let _ = bitcoind.init(5).await; - let (monero, _container) = - Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()]) - .await - .unwrap(); + let ( + monero, + testutils::Containers { + bitcoind, + monerods: _monerods, + }, + ) = testutils::init_containers(&cli).await; let btc_to_swap = bitcoin::Amount::from_sat(1_000_000); let xmr_to_swap = xmr_btc::monero::Amount::from_piconero(1_000_000_000_000); @@ -72,7 +65,6 @@ async fn both_refund() { btc_to_swap, bob_btc_starting_balance, xmr_to_swap, - bob_xmr_starting_balance, Config::regtest(), ) .await; diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 90d92c0b..a352787a 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -21,7 +21,6 @@ pub async fn init_bob( btc_to_swap: bitcoin::Amount, btc_starting_balance: bitcoin::Amount, xmr_to_swap: xmr_btc::monero::Amount, - xmr_stating_balance: xmr_btc::monero::Amount, config: Config, ) -> ( BobState, @@ -31,25 +30,15 @@ pub async fn init_bob( Arc, Database, ) { - let bob_btc_wallet = Arc::new( - swap::bitcoin::Wallet::new("bob", bitcoind.node_url.clone(), config.bitcoin_network) - .await - .unwrap(), - ); - bitcoind - .mint( - bob_btc_wallet.inner.new_address().await.unwrap(), - btc_starting_balance, - ) - .await - .unwrap(); - - monero - .init(vec![("bob", xmr_stating_balance.as_piconero())]) - .await - .unwrap(); - - let bob_xmr_wallet = Arc::new(swap::monero::Wallet(monero.wallet("bob").unwrap().client())); + let (bob_btc_wallet, bob_xmr_wallet) = init_wallets( + "bob", + bitcoind, + monero, + Some(btc_starting_balance), + None, + config, + ) + .await; let amounts = SwapAmounts { btc: btc_to_swap, @@ -89,17 +78,12 @@ pub async fn init_bob( ) } -pub async fn init_alice_eventloop( +pub async fn init_alice_state( btc_to_swap: bitcoin::Amount, xmr_to_swap: monero::Amount, alice_btc_wallet: Arc, - listen: Multiaddr, config: Config, -) -> ( - AliceState, - alice::event_loop::EventLoop, - alice::event_loop::EventLoopHandle, -) { +) -> AliceState { let rng = &mut OsRng; let amounts = SwapAmounts { @@ -123,39 +107,74 @@ pub async fn init_alice_eventloop( redeem_address, punish_address, ); - let start_state = AliceState::Started { amounts, state0 }; + AliceState::Started { amounts, state0 } +} + +pub fn init_alice_eventloop( + listen: Multiaddr, +) -> ( + alice::event_loop::EventLoop, + alice::event_loop::EventLoopHandle, +) { let alice_behaviour = alice::Behaviour::default(); let alice_transport = build(alice_behaviour.identity()).unwrap(); let (swarm_driver, handle) = alice::event_loop::EventLoop::new(alice_transport, alice_behaviour, listen).unwrap(); - (start_state, swarm_driver, handle) + (swarm_driver, handle) } -pub async fn init_alice_wallets( +pub async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) { + let bitcoind = Bitcoind::new(&cli, "0.19.1").unwrap(); + let _ = bitcoind.init(5).await; + let (monero, monerods) = Monero::new(&cli, None, vec!["alice".to_string(), "bob".to_string()]) + .await + .unwrap(); + + (monero, Containers { bitcoind, monerods }) +} + +pub async fn init_wallets( + name: &str, bitcoind: &Bitcoind<'_>, monero: &Monero, - xmr_starting_balance: xmr_btc::monero::Amount, + btc_starting_balance: Option, + xmr_starting_balance: Option, config: Config, -) -> (Arc, Arc) { - monero - .init(vec![("alice", xmr_starting_balance.as_piconero())]) - .await - .unwrap(); +) -> (Arc, Arc) { + match xmr_starting_balance { + Some(amount) => { + monero + .init(vec![(name, amount.as_piconero())]) + .await + .unwrap(); + } + None => { + monero + .init(vec![(name, monero::Amount::ZERO.as_piconero())]) + .await + .unwrap(); + } + }; - let alice_xmr_wallet = Arc::new(swap::monero::Wallet( - monero.wallet("alice").unwrap().client(), - )); + let xmr_wallet = Arc::new(swap::monero::Wallet(monero.wallet(name).unwrap().client())); - let alice_btc_wallet = Arc::new( - swap::bitcoin::Wallet::new("alice", bitcoind.node_url.clone(), config.bitcoin_network) + let btc_wallet = Arc::new( + swap::bitcoin::Wallet::new(name, bitcoind.node_url.clone(), config.bitcoin_network) .await .unwrap(), ); - (alice_xmr_wallet, alice_btc_wallet) + if let Some(amount) = btc_starting_balance { + bitcoind + .mint(btc_wallet.inner.new_address().await.unwrap(), amount) + .await + .unwrap(); + } + + (btc_wallet, xmr_wallet) } #[allow(clippy::too_many_arguments)] @@ -174,17 +193,21 @@ pub async fn init_alice( Arc, Arc, ) { - let (alice_xmr_wallet, alice_btc_wallet) = - init_alice_wallets(bitcoind, monero, xmr_starting_balance, config).await; - - let (alice_start_state, swarm_driver, handle) = init_alice_eventloop( - btc_to_swap, - xmr_to_swap, - alice_btc_wallet.clone(), - listen, + let (alice_btc_wallet, alice_xmr_wallet) = init_wallets( + "alice", + bitcoind, + monero, + None, + Some(xmr_starting_balance), config, ) .await; + + let alice_start_state = + init_alice_state(btc_to_swap, xmr_to_swap, alice_btc_wallet.clone(), config).await; + + let (swarm_driver, handle) = init_alice_eventloop(listen); + ( alice_start_state, swarm_driver, @@ -197,8 +220,8 @@ pub async fn init_alice( // This is just to keep the containers alive #[allow(dead_code)] pub struct Containers<'a> { - bitcoind: Bitcoind<'a>, - monerods: Vec>, + pub bitcoind: Bitcoind<'a>, + pub monerods: Vec>, } /// Utility function to initialize logging in the test environment.