317: Fix monero refresh interval r=thomaseizinger a=thomaseizinger

The comparison should be the MAXIMUM of the two values, not the
minimum, otherwise we always refresh at an interval of 1 second.

Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
This commit is contained in:
bors[bot] 2021-03-17 02:38:46 +00:00 committed by GitHub
commit 9ed5ca9a04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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))
}
}