mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-11-17 15:26:14 +00:00
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:
parent
09c41f89c4
commit
ce78075932
@ -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<Segwitv0> + Clone,
|
||||
env_config: Config,
|
||||
env_config: env::Config,
|
||||
) -> Result<Self> {
|
||||
// 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,
|
||||
})
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
@ -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<wallet::Client>,
|
||||
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<Fut>(
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user