From 2704245ed5322afa31010398a3747195cb5fda50 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 12 Mar 2021 18:58:49 +1100 Subject: [PATCH] Fix monero refresh interval The comparison should be the MAXIMUM of the two values, not the minimum, otherwise we always refresh at an interval of 1 second. --- swap/src/monero/wallet.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/swap/src/monero/wallet.rs b/swap/src/monero/wallet.rs index 441ea1f5..67a139a8 100644 --- a/swap/src/monero/wallet.rs +++ b/swap/src/monero/wallet.rs @@ -5,7 +5,7 @@ use ::monero::{Address, Network, PrivateKey, PublicKey}; use anyhow::{Context, Result}; use monero_rpc::wallet; use monero_rpc::wallet::{BlockHeight, CheckTxKey, Refreshed}; -use std::cmp::min; +use std::cmp::max; use std::future::Future; use std::str::FromStr; use std::time::Duration; @@ -173,8 +173,7 @@ impl Wallet { let address = Address::standard(self.network, public_spend_key, public_view_key.into()); - let check_interval = - tokio::time::interval(min(self.avg_block_time / 10, Duration::from_secs(1))); + let check_interval = tokio::time::interval(new_check_interval(self.avg_block_time)); let key = &transfer_proof.tx_key().to_string(); wait_for_confirmations( @@ -233,6 +232,10 @@ impl Wallet { } } +fn new_check_interval(avg_block_time: Duration) -> Duration { + max(avg_block_time / 10, Duration::from_secs(1)) +} + async fn wait_for_confirmations( txid: String, fetch_tx: impl Fn(String) -> Fut, @@ -352,4 +355,18 @@ mod tests { assert!(result.is_ok()) } + + #[test] + fn check_interval_is_one_second_if_avg_blocktime_is_one_second() { + let interval = new_check_interval(Duration::from_secs(1)); + + assert_eq!(interval, Duration::from_secs(1)) + } + + #[test] + fn check_interval_is_tenth_of_avg_blocktime() { + let interval = new_check_interval(Duration::from_secs(100)); + + assert_eq!(interval, Duration::from_secs(10)) + } }