From bae38a712fe6144da7bde2876e9dbe4c030d88ac Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Mon, 17 May 2021 19:10:11 +1000 Subject: [PATCH] Sync on interval instead of ping Since we don't rely on long running subscriptions anymore we can remove the ping that was used to ensure a connection refresh. --- swap/src/bitcoin/wallet.rs | 36 +++++++----------------------------- 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/swap/src/bitcoin/wallet.rs b/swap/src/bitcoin/wallet.rs index 64d1aad4..59fac072 100644 --- a/swap/src/bitcoin/wallet.rs +++ b/swap/src/bitcoin/wallet.rs @@ -528,8 +528,8 @@ impl Watchable for (Txid, Script) { pub struct Client { electrum: bdk::electrum_client::Client, latest_block_height: BlockHeight, - last_ping: Instant, - interval: Duration, + last_sync: Instant, + sync_interval: Duration, script_history: BTreeMap>, subscriptions: HashMap<(Txid, Script), Subscription>, } @@ -545,42 +545,20 @@ impl Client { Ok(Self { electrum, latest_block_height: BlockHeight::try_from(latest_block)?, - last_ping: Instant::now(), - interval, + last_sync: Instant::now(), + sync_interval: interval, script_history: Default::default(), subscriptions: Default::default(), }) } - /// Ping the electrum server unless we already did within the set interval. - /// - /// Returns a boolean indicating whether we actually pinged the server. - fn ping(&mut self) -> bool { - if self.last_ping.elapsed() <= self.interval { - return false; - } - - match self.electrum.ping() { - Ok(()) => { - self.last_ping = Instant::now(); - - true - } - Err(error) => { - tracing::debug!(?error, "Failed to ping electrum server"); - - false - } - } - } - fn update_state(&mut self) -> Result<()> { - let pinged = self.ping(); - - if !pinged { + let now = Instant::now(); + if now < self.last_sync + self.sync_interval { return Ok(()); } + self.last_sync = now; self.update_latest_block()?; self.update_script_histories()?;