use watch channels instead of rwlock (#8950)

This commit is contained in:
Darshan Kathiriya
2024-06-19 13:09:25 -04:00
committed by GitHub
parent 5a1c99034d
commit 152b2765bd

View File

@ -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<SealedHeader> {
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<SealedHeader> {
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<BlockNumHash> {
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<BlockNumHash> {
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<SealedHeader>,
/// The block that the beacon node considers safe.
safe_block: RwLock<Option<SealedHeader>>,
safe_block: watch::Sender<Option<SealedHeader>>,
/// The block that the beacon node considers finalized.
finalized_block: RwLock<Option<SealedHeader>>,
finalized_block: watch::Sender<Option<SealedHeader>>,
}