2021-05-11 21:25:45 -04:00
|
|
|
use crate::asb;
|
2021-02-14 20:19:43 -05:00
|
|
|
use crate::bitcoin::{CancelTimelock, PunishTimelock};
|
2021-03-17 00:01:08 -04:00
|
|
|
use std::cmp::max;
|
2020-12-01 18:00:00 -05:00
|
|
|
use std::time::Duration;
|
2021-03-10 22:59:57 -05:00
|
|
|
use time::NumericalStdDurationShort;
|
2020-12-01 18:00:00 -05:00
|
|
|
|
2021-05-11 02:52:40 -04:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
2021-03-16 23:55:42 -04:00
|
|
|
pub struct Config {
|
2021-03-17 21:40:32 -04:00
|
|
|
pub bitcoin_lock_confirmed_timeout: Duration,
|
2020-12-01 18:00:00 -05:00
|
|
|
pub bitcoin_finality_confirmations: u32,
|
|
|
|
pub bitcoin_avg_block_time: Duration,
|
2021-02-14 20:19:43 -05:00
|
|
|
pub bitcoin_cancel_timelock: CancelTimelock,
|
|
|
|
pub bitcoin_punish_timelock: PunishTimelock,
|
2021-03-16 22:36:43 -04:00
|
|
|
pub bitcoin_network: bitcoin::Network,
|
2021-03-16 22:18:49 -04:00
|
|
|
pub monero_avg_block_time: Duration,
|
2021-03-28 21:05:20 -04:00
|
|
|
pub monero_finality_confirmations: u64,
|
2021-03-16 22:36:43 -04:00
|
|
|
pub monero_network: monero::Network,
|
2020-12-01 18:00:00 -05:00
|
|
|
}
|
|
|
|
|
2021-03-17 00:01:08 -04:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 23:55:42 -04:00
|
|
|
pub trait GetConfig {
|
|
|
|
fn get_config() -> Config;
|
2021-01-27 01:03:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct Mainnet;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct Testnet;
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct Regtest;
|
|
|
|
|
2021-03-16 23:55:42 -04:00
|
|
|
impl GetConfig for Mainnet {
|
|
|
|
fn get_config() -> Config {
|
|
|
|
Config {
|
2021-03-17 21:40:32 -04:00
|
|
|
bitcoin_lock_confirmed_timeout: 24.hours(),
|
2021-05-19 02:36:41 -04:00
|
|
|
bitcoin_finality_confirmations: 2,
|
2021-03-10 22:59:57 -05:00
|
|
|
bitcoin_avg_block_time: 10.minutes(),
|
2021-03-10 22:57:47 -05:00
|
|
|
bitcoin_cancel_timelock: CancelTimelock::new(72),
|
|
|
|
bitcoin_punish_timelock: PunishTimelock::new(72),
|
2021-03-16 22:36:43 -04:00
|
|
|
bitcoin_network: bitcoin::Network::Bitcoin,
|
2021-03-16 22:18:49 -04:00
|
|
|
monero_avg_block_time: 2.minutes(),
|
2021-05-19 02:36:41 -04:00
|
|
|
monero_finality_confirmations: 10,
|
2021-03-16 22:36:43 -04:00
|
|
|
monero_network: monero::Network::Mainnet,
|
2021-01-04 04:29:11 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-27 01:03:52 -05:00
|
|
|
}
|
2021-01-04 04:29:11 -05:00
|
|
|
|
2021-03-16 23:55:42 -04:00
|
|
|
impl GetConfig for Testnet {
|
|
|
|
fn get_config() -> Config {
|
|
|
|
Config {
|
2021-03-17 21:40:32 -04:00
|
|
|
bitcoin_lock_confirmed_timeout: 12.hours(),
|
2021-05-19 02:36:41 -04:00
|
|
|
bitcoin_finality_confirmations: 2,
|
2021-05-17 04:39:33 -04:00
|
|
|
bitcoin_avg_block_time: 10.minutes(),
|
2021-03-10 22:57:47 -05:00
|
|
|
bitcoin_cancel_timelock: CancelTimelock::new(12),
|
|
|
|
bitcoin_punish_timelock: PunishTimelock::new(6),
|
2021-03-16 22:36:43 -04:00
|
|
|
bitcoin_network: bitcoin::Network::Testnet,
|
2021-03-16 22:18:49 -04:00
|
|
|
monero_avg_block_time: 2.minutes(),
|
2021-05-19 02:36:41 -04:00
|
|
|
monero_finality_confirmations: 10,
|
2021-03-16 22:36:43 -04:00
|
|
|
monero_network: monero::Network::Stagenet,
|
2020-12-01 18:00:00 -05:00
|
|
|
}
|
|
|
|
}
|
2021-01-27 01:03:52 -05:00
|
|
|
}
|
2020-12-01 18:00:00 -05:00
|
|
|
|
2021-03-16 23:55:42 -04:00
|
|
|
impl GetConfig for Regtest {
|
|
|
|
fn get_config() -> Config {
|
|
|
|
Config {
|
2021-03-17 21:40:32 -04:00
|
|
|
bitcoin_lock_confirmed_timeout: 1.minutes(),
|
2021-03-10 22:57:47 -05:00
|
|
|
bitcoin_finality_confirmations: 1,
|
2021-03-10 22:59:57 -05:00
|
|
|
bitcoin_avg_block_time: 5.seconds(),
|
2021-03-10 22:57:47 -05:00
|
|
|
bitcoin_cancel_timelock: CancelTimelock::new(100),
|
|
|
|
bitcoin_punish_timelock: PunishTimelock::new(50),
|
2021-03-16 22:36:43 -04:00
|
|
|
bitcoin_network: bitcoin::Network::Regtest,
|
2021-03-16 22:18:49 -04:00
|
|
|
monero_avg_block_time: 1.seconds(),
|
|
|
|
monero_finality_confirmations: 10,
|
2021-03-16 23:55:42 -04:00
|
|
|
monero_network: monero::Network::Mainnet, // yes this is strange
|
2020-12-01 18:00:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 00:01:08 -04:00
|
|
|
|
|
|
|
fn sync_interval(avg_block_time: Duration) -> Duration {
|
|
|
|
max(avg_block_time / 10, Duration::from_secs(1))
|
|
|
|
}
|
|
|
|
|
2021-05-11 21:25:45 -04:00
|
|
|
pub fn new(is_testnet: bool, asb_config: &asb::config::Config) -> Config {
|
|
|
|
let env_config = if is_testnet {
|
|
|
|
Testnet::get_config()
|
|
|
|
} else {
|
|
|
|
Mainnet::get_config()
|
|
|
|
};
|
|
|
|
|
|
|
|
let env_config =
|
|
|
|
if let Some(bitcoin_finality_confirmations) = asb_config.bitcoin.finality_confirmations {
|
|
|
|
Config {
|
|
|
|
bitcoin_finality_confirmations,
|
|
|
|
..env_config
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
env_config
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(monero_finality_confirmations) = asb_config.monero.finality_confirmations {
|
|
|
|
Config {
|
|
|
|
monero_finality_confirmations,
|
|
|
|
..env_config
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
env_config
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 00:01:08 -04:00
|
|
|
#[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))
|
|
|
|
}
|
|
|
|
}
|