From 461f54be38f1897608deb5ee3a8a06578f6268d5 Mon Sep 17 00:00:00 2001 From: Roei Erez Date: Wed, 29 Jan 2025 12:07:39 +0200 Subject: [PATCH] cleanup tip endpoint --- lib/core/src/chain/bitcoin.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/core/src/chain/bitcoin.rs b/lib/core/src/chain/bitcoin.rs index 4c68a2e..149771c 100644 --- a/lib/core/src/chain/bitcoin.rs +++ b/lib/core/src/chain/bitcoin.rs @@ -107,24 +107,27 @@ impl BitcoinChainService for HybridBitcoinChainService { maybe_popped_header = Some(header) } - match maybe_popped_header { - Some(popped_header) => { - let tip: HeaderNotification = popped_header.try_into()?; - *self.tip.lock().unwrap() = tip; - } + let new_tip = match maybe_popped_header { + Some(popped_header) => Some(popped_header.try_into()?), None => { // https://github.com/bitcoindevkit/rust-electrum-client/issues/124 // It might be that the client has reconnected and subscriptions don't persist // across connections. Calling `client.ping()` won't help here because the // successful retry will prevent us knowing about the reconnect. if let Ok(header) = self.client.block_headers_subscribe_raw() { - let tip: HeaderNotification = header.try_into()?; - *self.tip.lock().unwrap() = tip; + Some(header.try_into()?) + } else { + None } } + }; + + let mut tip = self.tip.lock().unwrap(); + if let Some(new_tip) = new_tip { + *tip = new_tip; } - Ok(self.tip.lock().unwrap().clone()) + Ok(tip.clone()) } fn broadcast(&self, tx: &Transaction) -> Result {