Skip to content

Commit

Permalink
remove reliance on USE_TEST_LOCKTIME env var
Browse files Browse the repository at this point in the history
  • Loading branch information
shamardy committed Dec 12, 2024
1 parent 5eca01d commit 557ebe2
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 120 deletions.
10 changes: 5 additions & 5 deletions mm2src/mm2_main/src/lp_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ use std::sync::{Arc, Mutex, Weak};
use std::time::Duration;
use uuid::Uuid;

#[cfg(feature = "custom-swap-locktime")]
#[cfg(any(feature = "custom-swap-locktime", test, feature = "run-docker-tests"))]
use std::sync::atomic::{AtomicU64, Ordering};

mod check_balance;
Expand Down Expand Up @@ -420,13 +420,13 @@ async fn recv_swap_msg<T>(
/// in order to give different and/or heavy communication channels a chance.
const BASIC_COMM_TIMEOUT: u64 = 90;

#[cfg(not(feature = "custom-swap-locktime"))]
#[cfg(not(any(feature = "custom-swap-locktime", test, feature = "run-docker-tests")))]
/// Default atomic swap payment locktime, in seconds.
/// Maker sends payment with LOCKTIME * 2
/// Taker sends payment with LOCKTIME
const PAYMENT_LOCKTIME: u64 = 3600 * 2 + 300 * 2;

#[cfg(feature = "custom-swap-locktime")]
#[cfg(any(feature = "custom-swap-locktime", test, feature = "run-docker-tests"))]
/// Default atomic swap payment locktime, in seconds.
/// Maker sends payment with LOCKTIME * 2
/// Taker sends payment with LOCKTIME
Expand All @@ -435,9 +435,9 @@ pub(crate) static PAYMENT_LOCKTIME: AtomicU64 = AtomicU64::new(super::CUSTOM_PAY
#[inline]
/// Returns `PAYMENT_LOCKTIME`
pub fn get_payment_locktime() -> u64 {
#[cfg(not(feature = "custom-swap-locktime"))]
#[cfg(not(any(feature = "custom-swap-locktime", test, feature = "run-docker-tests")))]
return PAYMENT_LOCKTIME;
#[cfg(feature = "custom-swap-locktime")]
#[cfg(any(feature = "custom-swap-locktime", test, feature = "run-docker-tests"))]
PAYMENT_LOCKTIME.load(Ordering::Relaxed)
}

Expand Down
31 changes: 13 additions & 18 deletions mm2src/mm2_main/src/lp_swap/swap_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,7 @@ impl State for ValidateTakerPayment {
let validate_input = WatcherValidatePaymentInput {
payment_tx: taker_payment_hex.clone(),
taker_payment_refund_preimage: watcher_ctx.data.taker_payment_refund_preimage.clone(),
time_lock: match std::env::var("USE_TEST_LOCKTIME") {
Ok(_) => watcher_ctx.data.swap_started_at,
Err(_) => watcher_ctx.taker_locktime(),
},
time_lock: watcher_ctx.taker_locktime(),
taker_pub: watcher_ctx.verified_pub.clone(),
maker_pub: watcher_ctx.data.maker_pub.clone(),
secret_hash: watcher_ctx.data.secret_hash.clone(),
Expand Down Expand Up @@ -451,20 +448,18 @@ impl State for RefundTakerPayment {

async fn on_changed(self: Box<Self>, watcher_ctx: &mut WatcherStateMachine) -> StateResult<WatcherStateMachine> {
debug!("Watcher refund taker payment");
if std::env::var("USE_TEST_LOCKTIME").is_err() {
loop {
match watcher_ctx
.taker_coin
.can_refund_htlc(watcher_ctx.taker_locktime())
.await
{
Ok(CanRefundHtlc::CanRefundNow) => break,
Ok(CanRefundHtlc::HaveToWait(to_sleep)) => Timer::sleep(to_sleep as f64).await,
Err(e) => {
error!("Error {} on can_refund_htlc, retrying in 30 seconds", e);
Timer::sleep(30.).await;
},
}
loop {
match watcher_ctx
.taker_coin
.can_refund_htlc(watcher_ctx.taker_locktime())
.await
{
Ok(CanRefundHtlc::CanRefundNow) => break,
Ok(CanRefundHtlc::HaveToWait(to_sleep)) => Timer::sleep(to_sleep as f64).await,
Err(e) => {
error!("Error {} on can_refund_htlc, retrying in 30 seconds", e);
Timer::sleep(30.).await;
},
}
}

Expand Down
11 changes: 2 additions & 9 deletions mm2src/mm2_main/src/lp_swap/taker_restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,7 @@ pub async fn check_taker_payment_spend(swap: &TakerSwap) -> Result<Option<FoundS
let other_taker_coin_htlc_pub = swap.r().other_taker_coin_htlc_pub;
let taker_coin_start_block = swap.r().data.taker_coin_start_block;
let taker_coin_swap_contract_address = swap.r().data.taker_coin_swap_contract_address.clone();

let taker_payment_lock = match std::env::var("USE_TEST_LOCKTIME") {
Ok(_) => swap.r().data.started_at,
Err(_) => swap.r().data.taker_payment_lock,
};
let taker_payment_lock = swap.r().data.taker_payment_lock;
let secret_hash = swap.r().secret_hash.0.clone();
let unique_data = swap.unique_swap_data();
let watcher_reward = swap.r().watcher_reward;
Expand Down Expand Up @@ -223,10 +219,7 @@ pub async fn add_taker_payment_refunded_by_watcher_event(
) -> Result<TakerSwapCommand, String> {
let other_maker_coin_htlc_pub = swap.r().other_maker_coin_htlc_pub;
let taker_coin_swap_contract_address = swap.r().data.taker_coin_swap_contract_address.clone();
let taker_payment_lock = match std::env::var("USE_TEST_LOCKTIME") {
Ok(_) => swap.r().data.started_at,
Err(_) => swap.r().data.taker_payment_lock,
};
let taker_payment_lock = swap.r().data.taker_payment_lock;
let secret_hash = swap.r().secret_hash.0.clone();

let validate_input = ValidateWatcherSpendInput {
Expand Down
20 changes: 3 additions & 17 deletions mm2src/mm2_main/src/lp_swap/taker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1557,14 +1557,9 @@ impl TakerSwap {
&self.unique_swap_data()[..],
);

let time_lock = match std::env::var("USE_TEST_LOCKTIME") {
Ok(_) => self.r().data.started_at,
Err(_) => self.r().data.taker_payment_lock,
};

let taker_payment_refund_preimage_fut = self.taker_coin.create_taker_payment_refund_preimage(
&transaction.tx_hex(),
time_lock,
self.r().data.taker_payment_lock,
&*self.r().other_taker_coin_htlc_pub,
&self.r().secret_hash.0,
&self.r().data.taker_coin_swap_contract_address,
Expand Down Expand Up @@ -1676,17 +1671,12 @@ impl TakerSwap {
let transaction = match maybe_existing_payment {
Some(tx) => tx,
None => {
let time_lock = match std::env::var("USE_TEST_LOCKTIME") {
Ok(_) => self.r().data.started_at,
Err(_) => taker_payment_lock,
};

let lock_duration = self.r().data.lock_duration;
match self
.taker_coin
.send_taker_payment(SendPaymentArgs {
time_lock_duration: lock_duration,
time_lock,
time_lock: taker_payment_lock,
other_pubkey: &*other_taker_coin_htlc_pub,
secret_hash: &secret_hash.0,
amount: taker_amount_decimal,
Expand Down Expand Up @@ -1779,11 +1769,7 @@ impl TakerSwap {

info!("Waiting for maker to spend taker payment!");

let wait_until = match std::env::var("USE_TEST_LOCKTIME") {
Ok(_) => self.r().data.started_at,
Err(_) => self.r().data.taker_payment_lock,
};

let wait_until = self.r().data.taker_payment_lock;
let secret_hash = self.r().secret_hash.clone();
let taker_coin_start_block = self.r().data.taker_coin_start_block;
let taker_coin_swap_contract_address = self.r().data.taker_coin_swap_contract_address.clone();
Expand Down
13 changes: 7 additions & 6 deletions mm2src/mm2_main/src/mm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ use common::log::LogLevel;
use common::password_policy::password_policy;
use mm2_core::mm_ctx::MmCtxBuilder;

#[cfg(feature = "custom-swap-locktime")] use common::log::warn;
#[cfg(feature = "custom-swap-locktime")]
#[cfg(any(feature = "custom-swap-locktime", test, feature = "run-docker-tests"))]
use common::log::warn;
#[cfg(any(feature = "custom-swap-locktime", test, feature = "run-docker-tests"))]
use lp_swap::PAYMENT_LOCKTIME;
#[cfg(feature = "custom-swap-locktime")]
#[cfg(any(feature = "custom-swap-locktime", test, feature = "run-docker-tests"))]
use std::sync::atomic::Ordering;

use gstuff::slurp;
Expand Down Expand Up @@ -85,7 +86,7 @@ pub mod rpc;

pub const PASSWORD_MAXIMUM_CONSECUTIVE_CHARACTERS: usize = 3;

#[cfg(feature = "custom-swap-locktime")]
#[cfg(any(feature = "custom-swap-locktime", test, feature = "run-docker-tests"))]
const CUSTOM_PAYMENT_LOCKTIME_DEFAULT: u64 = 900;

pub struct LpMainParams {
Expand All @@ -102,7 +103,7 @@ impl LpMainParams {
}
}

#[cfg(feature = "custom-swap-locktime")]
#[cfg(any(feature = "custom-swap-locktime", test, feature = "run-docker-tests"))]
/// Reads `payment_locktime` from conf arg and assigns it into `PAYMENT_LOCKTIME` in lp_swap.
/// Assigns 900 if `payment_locktime` is invalid or not provided.
fn initialize_payment_locktime(conf: &Json) {
Expand Down Expand Up @@ -150,7 +151,7 @@ pub async fn lp_main(
}
}

#[cfg(feature = "custom-swap-locktime")]
#[cfg(any(feature = "custom-swap-locktime", test, feature = "run-docker-tests"))]
initialize_payment_locktime(&conf);

let ctx = MmCtxBuilder::new()
Expand Down
Loading

0 comments on commit 557ebe2

Please sign in to comment.