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.
This commit is contained in:
Daniel Karzel 2021-05-17 19:10:11 +10:00
parent efb51820b1
commit bae38a712f
No known key found for this signature in database
GPG Key ID: 30C3FC2E438ADB6E

View File

@ -528,8 +528,8 @@ impl Watchable for (Txid, Script) {
pub struct Client { pub struct Client {
electrum: bdk::electrum_client::Client, electrum: bdk::electrum_client::Client,
latest_block_height: BlockHeight, latest_block_height: BlockHeight,
last_ping: Instant, last_sync: Instant,
interval: Duration, sync_interval: Duration,
script_history: BTreeMap<Script, Vec<GetHistoryRes>>, script_history: BTreeMap<Script, Vec<GetHistoryRes>>,
subscriptions: HashMap<(Txid, Script), Subscription>, subscriptions: HashMap<(Txid, Script), Subscription>,
} }
@ -545,42 +545,20 @@ impl Client {
Ok(Self { Ok(Self {
electrum, electrum,
latest_block_height: BlockHeight::try_from(latest_block)?, latest_block_height: BlockHeight::try_from(latest_block)?,
last_ping: Instant::now(), last_sync: Instant::now(),
interval, sync_interval: interval,
script_history: Default::default(), script_history: Default::default(),
subscriptions: 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<()> { fn update_state(&mut self) -> Result<()> {
let pinged = self.ping(); let now = Instant::now();
if now < self.last_sync + self.sync_interval {
if !pinged {
return Ok(()); return Ok(());
} }
self.last_sync = now;
self.update_latest_block()?; self.update_latest_block()?;
self.update_script_histories()?; self.update_script_histories()?;