Make Monero and Bitcoin wallet use a generalized sync interval

We define the sync interval as 1/10th of the blocktime. For the
special case of our tests, we however check at max once per second.
The tests have a super fast blocktime. As such we shouldn't hammer
the nodes with a request every 100ms.
This commit is contained in:
Thomas Eizinger 2021-03-17 15:01:08 +11:00
parent 09c41f89c4
commit ce78075932
No known key found for this signature in database
GPG key ID: 651AC83A6C6C8B96
3 changed files with 43 additions and 27 deletions

View file

@ -1,4 +1,5 @@
use crate::bitcoin::{CancelTimelock, PunishTimelock};
use std::cmp::max;
use std::time::Duration;
use time::NumericalStdDurationShort;
@ -15,6 +16,16 @@ pub struct Config {
pub monero_network: monero::Network,
}
impl Config {
pub fn bitcoin_sync_interval(&self) -> Duration {
sync_interval(self.bitcoin_avg_block_time)
}
pub fn monero_sync_interval(&self) -> Duration {
sync_interval(self.monero_avg_block_time)
}
}
pub trait GetConfig {
fn get_config() -> Config;
}
@ -75,3 +86,26 @@ impl GetConfig for Regtest {
}
}
}
fn sync_interval(avg_block_time: Duration) -> Duration {
max(avg_block_time / 10, Duration::from_secs(1))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_interval_is_one_second_if_avg_blocktime_is_one_second() {
let interval = sync_interval(Duration::from_secs(1));
assert_eq!(interval, Duration::from_secs(1))
}
#[test]
fn check_interval_is_tenth_of_avg_blocktime() {
let interval = sync_interval(Duration::from_secs(100));
assert_eq!(interval, Duration::from_secs(10))
}
}