Change expiries depending on the test goal

This commit is contained in:
Franck Royer 2021-01-27 17:03:52 +11:00
parent 5c2f83fd5d
commit b8a9356d1b
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
11 changed files with 124 additions and 38 deletions

View File

@ -16,9 +16,23 @@ pub struct Config {
pub monero_network: monero::Network, pub monero_network: monero::Network,
} }
impl Config { // TODO: This trait is not needed
pub fn mainnet() -> Self { pub trait GetConfig {
Self { 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, bob_time_to_act: *mainnet::BOB_TIME_TO_ACT,
bitcoin_finality_confirmations: mainnet::BITCOIN_FINALITY_CONFIRMATIONS, bitcoin_finality_confirmations: mainnet::BITCOIN_FINALITY_CONFIRMATIONS,
bitcoin_avg_block_time: *mainnet::BITCOIN_AVG_BLOCK_TIME, bitcoin_avg_block_time: *mainnet::BITCOIN_AVG_BLOCK_TIME,
@ -29,9 +43,11 @@ impl Config {
monero_network: monero::Network::Mainnet, monero_network: monero::Network::Mainnet,
} }
} }
}
pub fn testnet() -> Self { impl GetConfig for Testnet {
Self { fn get_config() -> Config {
Config {
bob_time_to_act: *testnet::BOB_TIME_TO_ACT, bob_time_to_act: *testnet::BOB_TIME_TO_ACT,
bitcoin_finality_confirmations: testnet::BITCOIN_FINALITY_CONFIRMATIONS, bitcoin_finality_confirmations: testnet::BITCOIN_FINALITY_CONFIRMATIONS,
bitcoin_avg_block_time: *testnet::BITCOIN_AVG_BLOCK_TIME, bitcoin_avg_block_time: *testnet::BITCOIN_AVG_BLOCK_TIME,
@ -42,9 +58,11 @@ impl Config {
monero_network: monero::Network::Stagenet, monero_network: monero::Network::Stagenet,
} }
} }
}
pub fn regtest() -> Self { impl GetConfig for Regtest {
Self { fn get_config() -> Config {
Config {
bob_time_to_act: *regtest::BOB_TIME_TO_ACT, bob_time_to_act: *regtest::BOB_TIME_TO_ACT,
bitcoin_finality_confirmations: regtest::BITCOIN_FINALITY_CONFIRMATIONS, bitcoin_finality_confirmations: regtest::BITCOIN_FINALITY_CONFIRMATIONS,
bitcoin_avg_block_time: *regtest::BITCOIN_AVG_BLOCK_TIME, bitcoin_avg_block_time: *regtest::BITCOIN_AVG_BLOCK_TIME,

View File

@ -14,7 +14,7 @@
use crate::cli::{Command, Options, Resume}; use crate::cli::{Command, Options, Resume};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use config::Config; use config::{Config, GetConfig};
use database::Database; use database::Database;
use log::LevelFilter; use log::LevelFilter;
use prettytable::{row, Table}; use prettytable::{row, Table};
@ -46,7 +46,7 @@ async fn main() -> Result<()> {
init_tracing(LevelFilter::Info).expect("initialize tracing"); init_tracing(LevelFilter::Info).expect("initialize tracing");
let opt = Options::from_args(); let opt = Options::from_args();
let config = Config::testnet(); let config = config::Testnet::get_config();
info!( info!(
"Database and Seed will be stored in directory: {}", "Database and Seed will be stored in directory: {}",

View File

@ -1,13 +1,17 @@
pub mod testutils; pub mod testutils;
use swap::protocol::{alice, bob}; use swap::{
config::GetConfig,
protocol::{alice, bob},
};
use testutils::SlowCancelConfig;
use tokio::join; use tokio::join;
/// Run the following tests with RUST_MIN_STACK=10000000 /// Run the following tests with RUST_MIN_STACK=10000000
#[tokio::test] #[tokio::test]
async fn happy_path() { 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 (alice_swap, _) = ctx.new_swap_as_alice().await;
let (bob_swap, _) = ctx.new_swap_as_bob().await; let (bob_swap, _) = ctx.new_swap_as_bob().await;

View File

@ -1,11 +1,14 @@
pub mod testutils; pub mod testutils;
use swap::protocol::{alice, alice::AliceState, bob}; use swap::{
use testutils::alice_run_until::is_encsig_learned; config::GetConfig,
protocol::{alice, alice::AliceState, bob},
};
use testutils::{alice_run_until::is_encsig_learned, SlowCancelConfig};
#[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::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 (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await;
let (bob_swap, _) = ctx.new_swap_as_bob().await; let (bob_swap, _) = ctx.new_swap_as_bob().await;

View File

@ -1,11 +1,14 @@
pub mod testutils; pub mod testutils;
use swap::protocol::{alice, bob, bob::BobState}; use swap::{
use testutils::bob_run_until::is_encsig_sent; config::GetConfig,
protocol::{alice, bob, bob::BobState},
};
use testutils::{bob_run_until::is_encsig_sent, SlowCancelConfig};
#[tokio::test] #[tokio::test]
async fn given_bob_restarts_after_encsig_is_sent_resume_swap() { 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 (alice_swap, _) = ctx.new_swap_as_alice().await;
let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;

View File

@ -1,11 +1,14 @@
pub mod testutils; pub mod testutils;
use swap::protocol::{alice, bob, bob::BobState}; use swap::{
use testutils::bob_run_until::is_lock_proof_received; config::GetConfig,
protocol::{alice, bob, bob::BobState},
};
use testutils::{bob_run_until::is_lock_proof_received, SlowCancelConfig};
#[tokio::test] #[tokio::test]
async fn given_bob_restarts_after_lock_proof_received_resume_swap() { 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 (alice_swap, _) = ctx.new_swap_as_alice().await;
let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;

View File

@ -1,11 +1,14 @@
pub mod testutils; pub mod testutils;
use swap::protocol::{alice, bob, bob::BobState}; use swap::{
use testutils::bob_run_until::is_xmr_locked; config::GetConfig,
protocol::{alice, bob, bob::BobState},
};
use testutils::{bob_run_until::is_xmr_locked, SlowCancelConfig};
#[tokio::test] #[tokio::test]
async fn given_bob_restarts_after_xmr_is_locked_resume_swap() { 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 (alice_swap, _) = ctx.new_swap_as_alice().await;
let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;

View File

@ -1,13 +1,16 @@
pub mod testutils; pub mod testutils;
use swap::protocol::{alice, bob, bob::BobState}; use swap::{
use testutils::bob_run_until::is_btc_locked; 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 /// 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. /// the encsig and fail to refund or redeem. Alice punishes.
#[tokio::test] #[tokio::test]
async fn alice_punishes_if_bob_never_acts_after_fund() { 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 (alice_swap, _) = ctx.new_swap_as_alice().await;
let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await; let (bob_swap, bob_join_handle) = ctx.new_swap_as_bob().await;

View File

@ -1,13 +1,16 @@
pub mod testutils; pub mod testutils;
use swap::protocol::{alice, alice::AliceState, bob}; use swap::{
use testutils::alice_run_until::is_xmr_locked; 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 /// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice
/// then also refunds. /// then also refunds.
#[tokio::test] #[tokio::test]
async fn given_alice_restarts_after_xmr_is_locked_refund_swap() { 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 (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await;
let (bob_swap, _) = ctx.new_swap_as_bob().await; let (bob_swap, _) = ctx.new_swap_as_bob().await;

View File

@ -1,6 +1,10 @@
pub mod testutils; 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; use testutils::alice_run_until::is_encsig_learned;
/// Bob locks btc and Alice locks xmr. Alice fails to act so Bob refunds. Alice /// 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. /// redeem had the timelock not expired.
#[tokio::test] #[tokio::test]
async fn given_alice_restarts_after_enc_sig_learned_and_bob_already_cancelled_refund_swap() { 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 (alice_swap, alice_join_handle) = ctx.new_swap_as_alice().await;
let (bob_swap, _) = ctx.new_swap_as_bob().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) let alice_state = alice::run_until(alice_swap, is_encsig_learned)
.await .await
.unwrap(); .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 // Wait for Bob to refund, because Alice does not act
let bob_state = bob_handle.await.unwrap(); 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 // 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; 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(); let alice_state = alice::run(alice_swap).await.unwrap();

View File

@ -7,7 +7,9 @@ use monero_harness::{image, Monero};
use std::{path::PathBuf, sync::Arc}; use std::{path::PathBuf, sync::Arc};
use swap::{ use swap::{
bitcoin, bitcoin,
config::Config, bitcoin::Timelock,
config,
config::{Config, GetConfig},
monero, monero,
protocol::{alice, alice::AliceState, bob, bob::BobState, SwapAmounts}, protocol::{alice, alice::AliceState, bob, bob::BobState, SwapAmounts},
seed::Seed, seed::Seed,
@ -302,7 +304,7 @@ impl TestContext {
} }
} }
pub async fn setup_test<T, F>(testfn: T) pub async fn setup_test<T, F>(config: Config, testfn: T)
where where
T: Fn(TestContext) -> F, T: Fn(TestContext) -> F,
F: Future<Output = ()>, F: Future<Output = ()>,
@ -318,8 +320,6 @@ where
xmr: monero::Amount::from_piconero(1_000_000_000_000), xmr: monero::Amount::from_piconero(1_000_000_000_000),
}; };
let config = Config::regtest();
let alice_starting_balances = StartingBalances { let alice_starting_balances = StartingBalances {
xmr: swap_amounts.xmr * 10, xmr: swap_amounts.xmr * 10,
btc: bitcoin::Amount::ZERO, btc: bitcoin::Amount::ZERO,
@ -511,3 +511,37 @@ pub mod bob_run_until {
matches!(state, BobState::EncSigSent(..)) 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()
}
}
}