From 152b2765bdbe3674abd6a4af626916fd55086e36 Mon Sep 17 00:00:00 2001 From: Darshan Kathiriya <8559992+lakshya-sky@users.noreply.github.com> Date: Wed, 19 Jun 2024 13:09:25 -0400 Subject: [PATCH] use watch channels instead of rwlock (#8950) --- .../provider/src/providers/chain_info.rs | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/crates/storage/provider/src/providers/chain_info.rs b/crates/storage/provider/src/providers/chain_info.rs index 2e53a78ac..c696fefea 100644 --- a/crates/storage/provider/src/providers/chain_info.rs +++ b/crates/storage/provider/src/providers/chain_info.rs @@ -8,6 +8,7 @@ use std::{ }, time::Instant, }; +use tokio::sync::watch; /// Tracks the chain info: canonical head, safe block, finalized block. #[derive(Debug, Clone)] @@ -18,14 +19,16 @@ pub(crate) struct ChainInfoTracker { impl ChainInfoTracker { /// Create a new chain info container for the given canonical head. pub(crate) fn new(head: SealedHeader) -> Self { + let (finalized_block, _) = watch::channel(None); + let (safe_block, _) = watch::channel(None); Self { inner: Arc::new(ChainInfoInner { last_forkchoice_update: RwLock::new(None), last_transition_configuration_exchange: RwLock::new(None), canonical_head_number: AtomicU64::new(head.number), canonical_head: RwLock::new(head), - safe_block: RwLock::new(None), - finalized_block: RwLock::new(None), + safe_block, + finalized_block, }), } } @@ -63,12 +66,12 @@ impl ChainInfoTracker { /// Returns the safe header of the chain. pub(crate) fn get_safe_header(&self) -> Option { - self.inner.safe_block.read().clone() + self.inner.safe_block.borrow().clone() } /// Returns the finalized header of the chain. pub(crate) fn get_finalized_header(&self) -> Option { - self.inner.finalized_block.read().clone() + self.inner.finalized_block.borrow().clone() } /// Returns the canonical head of the chain. @@ -85,14 +88,14 @@ impl ChainInfoTracker { /// Returns the safe header of the chain. #[allow(dead_code)] pub(crate) fn get_safe_num_hash(&self) -> Option { - let h = self.inner.safe_block.read(); + let h = self.inner.safe_block.borrow(); h.as_ref().map(|h| h.num_hash()) } /// Returns the finalized header of the chain. #[allow(dead_code)] pub(crate) fn get_finalized_num_hash(&self) -> Option { - let h = self.inner.finalized_block.read(); + let h = self.inner.finalized_block.borrow(); h.as_ref().map(|h| h.num_hash()) } @@ -107,12 +110,16 @@ impl ChainInfoTracker { /// Sets the safe header of the chain. pub(crate) fn set_safe(&self, header: SealedHeader) { - self.inner.safe_block.write().replace(header); + self.inner.safe_block.send_modify(|h| { + let _ = h.replace(header); + }); } /// Sets the finalized header of the chain. pub(crate) fn set_finalized(&self, header: SealedHeader) { - self.inner.finalized_block.write().replace(header); + self.inner.finalized_block.send_modify(|h| { + let _ = h.replace(header); + }); } } @@ -132,7 +139,7 @@ struct ChainInfoInner { /// The canonical head of the chain. canonical_head: RwLock, /// The block that the beacon node considers safe. - safe_block: RwLock>, + safe_block: watch::Sender>, /// The block that the beacon node considers finalized. - finalized_block: RwLock>, + finalized_block: watch::Sender>, }