From c0478d7191b6401b54020f42e33533cf1b865076 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 10 Dec 2020 14:43:17 +1100 Subject: [PATCH] Move expiries to config So they can be different for mainnet and regtest --- swap/src/lib.rs | 3 --- swap/tests/e2e.rs | 22 ++++++++++++++++------ xmr-btc/src/config.rs | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/swap/src/lib.rs b/swap/src/lib.rs index d435e893..80b8d48d 100644 --- a/swap/src/lib.rs +++ b/swap/src/lib.rs @@ -14,9 +14,6 @@ pub mod state; pub mod storage; pub mod tor; -pub const REFUND_TIMELOCK: u32 = 50; // Relative timelock, this is number of blocks. TODO: What should it be? -pub const PUNISH_TIMELOCK: u32 = 50; // FIXME: What should this be? - pub type Never = std::convert::Infallible; /// Commands sent from Bob to the main task. diff --git a/swap/tests/e2e.rs b/swap/tests/e2e.rs index 248b1335..efce4319 100644 --- a/swap/tests/e2e.rs +++ b/swap/tests/e2e.rs @@ -6,7 +6,7 @@ use rand::rngs::OsRng; use std::sync::Arc; use swap::{ alice, alice::swap::AliceState, bob, bob::swap::BobState, network::transport::build, - storage::Database, SwapAmounts, PUNISH_TIMELOCK, REFUND_TIMELOCK, + storage::Database, SwapAmounts, }; use tempfile::tempdir; use testcontainers::clients::Cli; @@ -45,6 +45,8 @@ async fn happy_path() { .parse() .expect("failed to parse Alice's address"); + let config = Config::regtest(); + let ( alice_state, mut alice_swarm_driver, @@ -60,6 +62,7 @@ async fn happy_path() { xmr_to_swap, xmr_alice, alice_multiaddr.clone(), + config, ) .await; @@ -73,6 +76,7 @@ async fn happy_path() { btc_bob, xmr_to_swap, xmr_bob, + config, ) .await; @@ -81,7 +85,7 @@ async fn happy_path() { alice_swarm_handle, alice_btc_wallet.clone(), alice_xmr_wallet.clone(), - Config::regtest(), + config, ); let _alice_swarm_fut = tokio::spawn(async move { alice_swarm_driver.run().await }); @@ -149,6 +153,8 @@ async fn alice_punishes_if_bob_never_acts_after_fund() { .parse() .expect("failed to parse Alice's address"); + let config = Config::regtest(); + let ( alice_state, mut alice_swarm, @@ -164,6 +170,7 @@ async fn alice_punishes_if_bob_never_acts_after_fund() { xmr_to_swap, alice_xmr_starting_balance, alice_multiaddr.clone(), + config, ) .await; @@ -177,6 +184,7 @@ async fn alice_punishes_if_bob_never_acts_after_fund() { bob_btc_starting_balance, xmr_to_swap, bob_xmr_starting_balance, + config, ) .await; @@ -220,6 +228,7 @@ async fn init_alice( xmr_to_swap: xmr_btc::monero::Amount, xmr_starting_balance: xmr_btc::monero::Amount, listen: Multiaddr, + config: Config, ) -> ( AliceState, alice::event_loop::EventLoop, @@ -261,8 +270,8 @@ async fn init_alice( v_a, amounts.btc, amounts.xmr, - REFUND_TIMELOCK, - PUNISH_TIMELOCK, + config.bitcoin_refund_timelock, + config.bitcoin_punish_timelock, redeem_address, punish_address, ); @@ -303,6 +312,7 @@ async fn init_bob( btc_starting_balance: bitcoin::Amount, xmr_to_swap: xmr_btc::monero::Amount, xmr_stating_balance: xmr_btc::monero::Amount, + config: Config, ) -> ( BobState, bob::event_loop::EventLoop, @@ -346,8 +356,8 @@ async fn init_bob( &mut OsRng, btc_to_swap, xmr_to_swap, - REFUND_TIMELOCK, - PUNISH_TIMELOCK, + config.bitcoin_refund_timelock, + config.bitcoin_punish_timelock, refund_address, ); let bob_state = BobState::Started { diff --git a/xmr-btc/src/config.rs b/xmr-btc/src/config.rs index 3fce9f04..40b08b87 100644 --- a/xmr-btc/src/config.rs +++ b/xmr-btc/src/config.rs @@ -7,6 +7,8 @@ pub struct Config { pub bitcoin_finality_confirmations: u32, pub bitcoin_avg_block_time: Duration, pub monero_max_finality_time: Duration, + pub bitcoin_refund_timelock: u32, + pub bitcoin_punish_timelock: u32, } impl Config { @@ -19,6 +21,8 @@ impl Config { // blockchain is slow monero_max_finality_time: (*mainnet::MONERO_AVG_BLOCK_TIME).mul_f64(1.5) * mainnet::MONERO_FINALITY_CONFIRMATIONS, + bitcoin_refund_timelock: mainnet::BITCOIN_REFUND_TIMELOCK, + bitcoin_punish_timelock: mainnet::BITCOIN_PUNISH_TIMELOCK, } } @@ -31,6 +35,8 @@ impl Config { // blockchain is slow monero_max_finality_time: (*regtest::MONERO_AVG_BLOCK_TIME).mul_f64(1.5) * regtest::MONERO_FINALITY_CONFIRMATIONS, + bitcoin_refund_timelock: regtest::BITCOIN_REFUND_TIMELOCK, + bitcoin_punish_timelock: regtest::BITCOIN_PUNISH_TIMELOCK, } } } @@ -48,6 +54,10 @@ mod mainnet { pub static MONERO_FINALITY_CONFIRMATIONS: u32 = 15; pub static MONERO_AVG_BLOCK_TIME: Lazy = Lazy::new(|| Duration::from_secs(2 * 60)); + + // Set to 12 hours, arbitrary value to be reviewed properly + pub static BITCOIN_REFUND_TIMELOCK: u32 = 72; + pub static BITCOIN_PUNISH_TIMELOCK: u32 = 72; } mod regtest { @@ -63,4 +73,8 @@ mod regtest { pub static MONERO_FINALITY_CONFIRMATIONS: u32 = 1; pub static MONERO_AVG_BLOCK_TIME: Lazy = Lazy::new(|| Duration::from_secs(60)); + + pub static BITCOIN_REFUND_TIMELOCK: u32 = 50; + + pub static BITCOIN_PUNISH_TIMELOCK: u32 = 50; }