From afcc66c39a62c8a7fe412de992e85b864f282421 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 12 May 2023 18:15:16 +0200 Subject: [PATCH] feat: add getter for last FCU timestamp (#2647) --- crates/storage/provider/src/providers/chain_info.rs | 8 ++++---- crates/storage/provider/src/providers/mod.rs | 5 +++++ crates/storage/provider/src/traits/chain_info.rs | 5 +++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/crates/storage/provider/src/providers/chain_info.rs b/crates/storage/provider/src/providers/chain_info.rs index 1527ebdd7d..a1a8977817 100644 --- a/crates/storage/provider/src/providers/chain_info.rs +++ b/crates/storage/provider/src/providers/chain_info.rs @@ -13,7 +13,7 @@ impl ChainInfoTracker { pub(crate) fn new(head: SealedHeader) -> Self { Self { inner: Arc::new(ChainInfoInner { - last_forkchoice_update: RwLock::new(Instant::now()), + last_forkchoice_update: RwLock::new(None), canonical_head: RwLock::new(head), safe_block: RwLock::new(None), finalized_block: RwLock::new(None), @@ -23,12 +23,12 @@ impl ChainInfoTracker { /// Update the timestamp when we received a forkchoice update. pub(crate) fn on_forkchoice_update_received(&self) { - *self.inner.last_forkchoice_update.write() = Instant::now(); + self.inner.last_forkchoice_update.write().replace(Instant::now()); } /// Returns the instant when we received the latest forkchoice update. #[allow(unused)] - pub(crate) fn last_forkchoice_update_received_at(&self) -> Instant { + pub(crate) fn last_forkchoice_update_received_at(&self) -> Option { *self.inner.last_forkchoice_update.read() } @@ -92,7 +92,7 @@ struct ChainInfoInner { /// Timestamp when we received the last fork choice update. /// /// This is mainly used to track if we're connected to a beacon node. - last_forkchoice_update: RwLock, + last_forkchoice_update: RwLock>, /// The canonical head of the chain. canonical_head: RwLock, /// The block that the beacon node considers safe. diff --git a/crates/storage/provider/src/providers/mod.rs b/crates/storage/provider/src/providers/mod.rs index f80a44dff2..e23a4d4fc7 100644 --- a/crates/storage/provider/src/providers/mod.rs +++ b/crates/storage/provider/src/providers/mod.rs @@ -24,6 +24,7 @@ pub use state::{ use std::{ collections::{BTreeMap, HashSet}, ops::RangeBounds, + time::Instant, }; use tracing::trace; @@ -453,6 +454,10 @@ where fn set_canonical_head(&self, header: SealedHeader) { self.chain_info.set_canonical_head(header); } + + fn last_received_update_timestamp(&self) -> Option { + self.chain_info.last_forkchoice_update_received_at() + } } impl BlockProviderIdExt for BlockchainProvider diff --git a/crates/storage/provider/src/traits/chain_info.rs b/crates/storage/provider/src/traits/chain_info.rs index 6fdb338574..587a08682e 100644 --- a/crates/storage/provider/src/traits/chain_info.rs +++ b/crates/storage/provider/src/traits/chain_info.rs @@ -1,11 +1,16 @@ use reth_interfaces::consensus::ForkchoiceState; use reth_primitives::SealedHeader; +use std::time::Instant; /// A type that can track updates related to fork choice updates. pub trait CanonChainTracker: Send + Sync { /// Notify the tracker about a received fork choice update. fn on_forkchoice_update_received(&self, update: &ForkchoiceState); + /// Returns the last time a fork choice update was received from the CL + /// ([CanonChainTracker::on_forkchoice_update_received]) + fn last_received_update_timestamp(&self) -> Option; + /// Sets the canonical head of the chain. fn set_canonical_head(&self, header: SealedHeader);