From b8a9356d1bf46c5c97686990c171ceb56e96bf34 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Wed, 27 Jan 2021 17:03:52 +1100 Subject: [PATCH] Change expiries depending on the test goal --- swap/src/config.rs | 32 ++++++++++---- swap/src/main.rs | 4 +- swap/tests/happy_path.rs | 8 +++- swap/tests/happy_path_restart_alice.rs | 9 ++-- .../happy_path_restart_bob_after_comm.rs | 9 ++-- ...h_restart_bob_after_lock_proof_received.rs | 9 ++-- .../happy_path_restart_bob_before_comm.rs | 9 ++-- swap/tests/punish.rs | 9 ++-- swap/tests/refund_restart_alice.rs | 9 ++-- swap/tests/refund_restart_alice_cancelled.rs | 22 +++++++--- swap/tests/testutils/mod.rs | 42 +++++++++++++++++-- 11 files changed, 124 insertions(+), 38 deletions(-) diff --git a/swap/src/config.rs b/swap/src/config.rs index d7fd1a14..1f8f9e59 100644 --- a/swap/src/config.rs +++ b/swap/src/config.rs @@ -16,9 +16,23 @@ pub struct Config { pub monero_network: monero::Network, } -impl Config { - pub fn mainnet() -> Self { - Self { +// TODO: This trait is not needed +pub trait GetConfig { + fn get_config() -> Config; +} + +#[derive(Clone, Copy)] +pub struct Mainnet; + +#[derive(Clone, Copy)] +pub struct Testnet; + +#[derive(Clone, Copy)] +pub struct Regtest; + +impl GetConfig for Mainnet { + fn get_config() -> Config { + Config { bob_time_to_act: *mainnet::BOB_TIME_TO_ACT, bitcoin_finality_confirmations: mainnet::BITCOIN_FINALITY_CONFIRMATIONS, bitcoin_avg_block_time: *mainnet::BITCOIN_AVG_BLOCK_TIME, @@ -29,9 +43,11 @@ impl Config { monero_network: monero::Network::Mainnet, } } +} - pub fn testnet() -> Self { - Self { +impl GetConfig for Testnet { + fn get_config() -> Config { + Config { bob_time_to_act: *testnet::BOB_TIME_TO_ACT, bitcoin_finality_confirmations: testnet::BITCOIN_FINALITY_CONFIRMATIONS, bitcoin_avg_block_time: *testnet::BITCOIN_AVG_BLOCK_TIME, @@ -42,9 +58,11 @@ impl Config { monero_network: monero::Network::Stagenet, } } +} - pub fn regtest() -> Self { - Self { +impl GetConfig for Regtest { + fn get_config() -> Config { + Config { bob_time_to_act: *regtest::BOB_TIME_TO_ACT, bitcoin_finality_confirmations: regtest::BITCOIN_FINALITY_CONFIRMATIONS, bitcoin_avg_block_time: *regtest::BITCOIN_AVG_BLOCK_TIME, diff --git a/swap/src/main.rs b/swap/src/main.rs index aab23e62..1e22d901 100644 --- a/swap/src/main.rs +++ b/swap/src/main.rs @@ -14,7 +14,7 @@ use crate::cli::{Command, Options, Resume}; use anyhow::{Context, Result}; -use config::Config; +use config::{Config, GetConfig}; use database::Database; use log::LevelFilter; use prettytable::{row, Table}; @@ -46,7 +46,7 @@ async fn main() -> Result<()> { init_tracing(LevelFilter::Info).expect("initialize tracing"); let opt = Options::from_args(); - let config = Config::testnet(); + let config = config::Testnet::get_config(); info!( "Database and Seed will be stored in directory: {}", diff --git a/swap/tests/happy_path.rs b/swap/tests/happy_path.rs index 02776caa..d0eb665d 100644 --- a/swap/tests/happy_path.rs +++ b/swap/tests/happy_path.rs @@ -1,13 +1,17 @@ pub mod testutils; -use swap::protocol::{alice, bob}; +use swap::{ + config::GetConfig, + protocol::{alice, bob}, +}; +use testutils::SlowCancelConfig; use tokio::join; /// Run the following tests with RUST_MIN_STACK=10000000 #[tokio::test] async fn happy_path() { - testutils::setup_test(|mut ctx| async move { + testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { let (alice_swap, _) = ctx.new_swap_as_alice().await; let (bob_swap, _) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_alice.rs b/swap/tests/happy_path_restart_alice.rs index b8acac12..fc60ff0b 100644 --- a/swap/tests/happy_path_restart_alice.rs +++ b/swap/tests/happy_path_restart_alice.rs @@ -1,11 +1,14 @@ pub mod testutils; -use swap::protocol::{alice, alice::AliceState, bob}; -use testutils::alice_run_until::is_encsig_learned; +use swap::{ + config::GetConfig, + protocol::{alice, alice::AliceState, bob}, +}; +use testutils::{alice_run_until::is_encsig_learned, SlowCancelConfig}; #[tokio::test] async fn given_alice_restarts_after_encsig_is_learned_resume_swap() { - testutils::setup_test(|mut ctx| async move { + testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { let (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await; let (bob_swap, _) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_bob_after_comm.rs b/swap/tests/happy_path_restart_bob_after_comm.rs index 63efd56d..a4667a98 100644 --- a/swap/tests/happy_path_restart_bob_after_comm.rs +++ b/swap/tests/happy_path_restart_bob_after_comm.rs @@ -1,11 +1,14 @@ pub mod testutils; -use swap::protocol::{alice, bob, bob::BobState}; -use testutils::bob_run_until::is_encsig_sent; +use swap::{ + config::GetConfig, + protocol::{alice, bob, bob::BobState}, +}; +use testutils::{bob_run_until::is_encsig_sent, SlowCancelConfig}; #[tokio::test] async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { - testutils::setup_test(|mut ctx| async move { + testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { let (alice_swap, _) = ctx.new_swap_as_alice().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs b/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs index a88f7e9a..ca5e2da3 100644 --- a/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs +++ b/swap/tests/happy_path_restart_bob_after_lock_proof_received.rs @@ -1,11 +1,14 @@ pub mod testutils; -use swap::protocol::{alice, bob, bob::BobState}; -use testutils::bob_run_until::is_lock_proof_received; +use swap::{ + config::GetConfig, + protocol::{alice, bob, bob::BobState}, +}; +use testutils::{bob_run_until::is_lock_proof_received, SlowCancelConfig}; #[tokio::test] async fn given_bob_restarts_after_lock_proof_received_resume_swap() { - testutils::setup_test(|mut ctx| async move { + testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { let (alice_swap, _) = ctx.new_swap_as_alice().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/happy_path_restart_bob_before_comm.rs b/swap/tests/happy_path_restart_bob_before_comm.rs index ab0cfa1f..f9d35e76 100644 --- a/swap/tests/happy_path_restart_bob_before_comm.rs +++ b/swap/tests/happy_path_restart_bob_before_comm.rs @@ -1,11 +1,14 @@ pub mod testutils; -use swap::protocol::{alice, bob, bob::BobState}; -use testutils::bob_run_until::is_xmr_locked; +use swap::{ + config::GetConfig, + protocol::{alice, bob, bob::BobState}, +}; +use testutils::{bob_run_until::is_xmr_locked, SlowCancelConfig}; #[tokio::test] async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { - testutils::setup_test(|mut ctx| async move { + testutils::setup_test(SlowCancelConfig::get_config(), |mut ctx| async move { let (alice_swap, _) = ctx.new_swap_as_alice().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/punish.rs b/swap/tests/punish.rs index 170dffa9..9e354a01 100644 --- a/swap/tests/punish.rs +++ b/swap/tests/punish.rs @@ -1,13 +1,16 @@ pub mod testutils; -use swap::protocol::{alice, bob, bob::BobState}; -use testutils::bob_run_until::is_btc_locked; +use swap::{ + config::GetConfig, + protocol::{alice, bob, bob::BobState}, +}; +use testutils::{bob_run_until::is_btc_locked, FastPunishConfig}; /// Bob locks Btc and Alice locks Xmr. Bob does not act; he fails to send Alice /// the encsig and fail to refund or redeem. Alice punishes. #[tokio::test] async fn alice_punishes_if_bob_never_acts_after_fund() { - testutils::setup_test(|mut ctx| async move { + testutils::setup_test(FastPunishConfig::get_config(), |mut ctx| async move { let (alice_swap, _) = ctx.new_swap_as_alice().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/refund_restart_alice.rs b/swap/tests/refund_restart_alice.rs index f77ac082..f550a247 100644 --- a/swap/tests/refund_restart_alice.rs +++ b/swap/tests/refund_restart_alice.rs @@ -1,13 +1,16 @@ pub mod testutils; -use swap::protocol::{alice, alice::AliceState, bob}; -use testutils::alice_run_until::is_xmr_locked; +use swap::{ + config::GetConfig, + protocol::{alice, alice::AliceState, bob}, +}; +use testutils::{alice_run_until::is_xmr_locked, FastCancelConfig}; /// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice /// then also refunds. #[tokio::test] async fn given_alice_restarts_after_xmr_is_locked_refund_swap() { - testutils::setup_test(|mut ctx| async move { + testutils::setup_test(FastCancelConfig::get_config(), |mut ctx| async move { let (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await; let (bob_swap, _) = ctx.new_swap_as_bob().await; diff --git a/swap/tests/refund_restart_alice_cancelled.rs b/swap/tests/refund_restart_alice_cancelled.rs index 637aec8a..47da9363 100644 --- a/swap/tests/refund_restart_alice_cancelled.rs +++ b/swap/tests/refund_restart_alice_cancelled.rs @@ -1,6 +1,10 @@ pub mod testutils; -use swap::protocol::{alice, alice::AliceState, bob}; +use swap::{ + config, + config::GetConfig, + protocol::{alice, alice::AliceState, bob}, +}; use testutils::alice_run_until::is_encsig_learned; /// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice @@ -8,7 +12,7 @@ use testutils::alice_run_until::is_encsig_learned; /// redeem had the timelock not expired. #[tokio::test] async fn given_alice_restarts_after_enc_sig_learned_and_bob_already_cancelled_refund_swap() { - testutils::setup_test(|mut ctx| async move { + testutils::setup_test(config::Regtest::get_config(), |mut ctx| async move { let (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await; let (bob_swap, _) = ctx.new_swap_as_bob().await; @@ -18,7 +22,11 @@ async fn given_alice_restarts_after_enc_sig_learned_and_bob_already_cancelled_re let alice_state = alice::run_until(alice_swap, is_encsig_learned) .await .unwrap(); - assert!(matches!(alice_state, AliceState::EncSigLearned {..})); + assert!( + matches!(alice_state, AliceState::EncSigLearned {..}), + "Alice state is not EncSigLearned: {:?}", + alice_state + ); // Wait for Bob to refund, because Alice does not act let bob_state = bob_handle.await.unwrap(); @@ -26,8 +34,12 @@ async fn given_alice_restarts_after_enc_sig_learned_and_bob_already_cancelled_re // Once bob has finished Alice is restarted and refunds as well let alice_swap = ctx.stop_and_resume_alice_from_db(alice_join_handle).await; - assert!(matches!(alice_swap.state, AliceState::EncSigLearned - {..})); + assert!( + matches!(alice_swap.state, AliceState::EncSigLearned + {..}), + "Alice state is not EncSigLearned: {:?}", + alice_state + ); let alice_state = alice::run(alice_swap).await.unwrap(); diff --git a/swap/tests/testutils/mod.rs b/swap/tests/testutils/mod.rs index 94f098d2..9513a2dd 100644 --- a/swap/tests/testutils/mod.rs +++ b/swap/tests/testutils/mod.rs @@ -7,7 +7,9 @@ use monero_harness::{image, Monero}; use std::{path::PathBuf, sync::Arc}; use swap::{ bitcoin, - config::Config, + bitcoin::Timelock, + config, + config::{Config, GetConfig}, monero, protocol::{alice, alice::AliceState, bob, bob::BobState, SwapAmounts}, seed::Seed, @@ -302,7 +304,7 @@ impl TestContext { } } -pub async fn setup_test(testfn: T) +pub async fn setup_test(config: Config, testfn: T) where T: Fn(TestContext) -> F, F: Future, @@ -318,8 +320,6 @@ where xmr: monero::Amount::from_piconero(1_000_000_000_000), }; - let config = Config::regtest(); - let alice_starting_balances = StartingBalances { xmr: swap_amounts.xmr * 10, btc: bitcoin::Amount::ZERO, @@ -511,3 +511,37 @@ pub mod bob_run_until { matches!(state, BobState::EncSigSent(..)) } } + +pub struct SlowCancelConfig; + +impl GetConfig for SlowCancelConfig { + fn get_config() -> Config { + Config { + bitcoin_cancel_timelock: Timelock::new(180), + ..config::Regtest::get_config() + } + } +} + +pub struct FastCancelConfig; + +impl GetConfig for FastCancelConfig { + fn get_config() -> Config { + Config { + bitcoin_cancel_timelock: Timelock::new(1), + ..config::Regtest::get_config() + } + } +} + +pub struct FastPunishConfig; + +impl GetConfig for FastPunishConfig { + fn get_config() -> Config { + Config { + bitcoin_cancel_timelock: Timelock::new(1), + bitcoin_punish_timelock: Timelock::new(1), + ..config::Regtest::get_config() + } + } +}