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.
This commit is contained in:
Thomas Eizinger 2021-03-12 18:58:49 +11:00
parent 904312d1e9
commit 2704245ed5
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96

View File

@ -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<Fut>(
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))
}
}