From ce780759327a5277e9ad7b3d6c774f392ed5e4bb Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 17 Mar 2021 15:01:08 +1100 Subject: [PATCH] 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. --- swap/src/bitcoin/wallet.rs | 11 ++++++----- swap/src/env.rs | 34 ++++++++++++++++++++++++++++++++++ swap/src/monero/wallet.rs | 25 +++---------------------- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/swap/src/bitcoin/wallet.rs b/swap/src/bitcoin/wallet.rs index b5483fd6..dbd38a3f 100644 --- a/swap/src/bitcoin/wallet.rs +++ b/swap/src/bitcoin/wallet.rs @@ -1,6 +1,6 @@ use crate::bitcoin::timelocks::BlockHeight; use crate::bitcoin::{Address, Amount, Transaction}; -use crate::env::Config; +use crate::env; use ::bitcoin::util::psbt::PartiallySignedTransaction; use ::bitcoin::Txid; use anyhow::{anyhow, bail, Context, Result}; @@ -33,7 +33,7 @@ impl Wallet { electrum_rpc_url: Url, wallet_dir: &Path, key: impl DerivableKey + Clone, - env_config: Config, + env_config: env::Config, ) -> Result { // Workaround for https://github.com/bitcoindevkit/rust-electrum-client/issues/47. let config = electrum_client::ConfigBuilder::default().retry(2).build(); @@ -55,11 +55,12 @@ impl Wallet { let electrum = bdk::electrum_client::Client::from_config(electrum_rpc_url.as_str(), config) .map_err(|e| anyhow!("Failed to init electrum rpc client {:?}", e))?; - let interval = Duration::from_secs(5); - Ok(Self { wallet: Arc::new(Mutex::new(bdk_wallet)), - client: Arc::new(Mutex::new(Client::new(electrum, interval)?)), + client: Arc::new(Mutex::new(Client::new( + electrum, + env_config.bitcoin_sync_interval(), + )?)), finality_confirmations: env_config.bitcoin_finality_confirmations, }) } diff --git a/swap/src/env.rs b/swap/src/env.rs index cae01dea..d60ae858 100644 --- a/swap/src/env.rs +++ b/swap/src/env.rs @@ -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)) + } +} diff --git a/swap/src/monero/wallet.rs b/swap/src/monero/wallet.rs index bd7afcda..764235c9 100644 --- a/swap/src/monero/wallet.rs +++ b/swap/src/monero/wallet.rs @@ -6,7 +6,6 @@ use ::monero::{Address, Network, PrivateKey, PublicKey}; use anyhow::{Context, Result}; use monero_rpc::wallet; use monero_rpc::wallet::{BlockHeight, CheckTxKey, Client, Refreshed}; -use std::cmp::max; use std::future::Future; use std::str::FromStr; use std::time::Duration; @@ -20,7 +19,7 @@ pub struct Wallet { inner: Mutex, network: Network, name: String, - avg_block_time: Duration, + sync_interval: Duration, } impl Wallet { @@ -33,7 +32,7 @@ impl Wallet { inner: Mutex::new(client), network: env_config.monero_network, name, - avg_block_time: env_config.monero_avg_block_time, + sync_interval: env_config.monero_sync_interval(), } } @@ -164,7 +163,7 @@ impl Wallet { let address = Address::standard(self.network, public_spend_key, public_view_key.into()); - let check_interval = tokio::time::interval(new_check_interval(self.avg_block_time)); + let check_interval = tokio::time::interval(self.sync_interval); let key = &transfer_proof.tx_key().to_string(); wait_for_confirmations( @@ -223,10 +222,6 @@ 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, @@ -346,18 +341,4 @@ 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)) - } }