perf: store canonical number in atomic (#2859)

This commit is contained in:
Matthias Seitz
2023-05-26 15:02:07 +02:00
committed by GitHub
parent be5850488c
commit e05f655928
3 changed files with 20 additions and 13 deletions

View File

@ -1,6 +1,12 @@
use parking_lot::RwLock;
use reth_primitives::{BlockNumHash, BlockNumber, ChainInfo, SealedHeader};
use std::{sync::Arc, time::Instant};
use std::{
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
time::Instant,
};
/// Tracks the chain info: canonical head, safe block, finalized block.
#[derive(Debug, Clone)]
@ -14,6 +20,7 @@ impl ChainInfoTracker {
Self {
inner: Arc::new(ChainInfoInner {
last_forkchoice_update: 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),
@ -64,7 +71,7 @@ impl ChainInfoTracker {
/// Returns the canonical head of the chain.
pub(crate) fn get_canonical_block_number(&self) -> BlockNumber {
self.inner.canonical_head.read().number
self.inner.canonical_head_number.load(Ordering::Relaxed)
}
/// Returns the safe header of the chain.
@ -83,7 +90,11 @@ impl ChainInfoTracker {
/// Sets the canonical head of the chain.
pub(crate) fn set_canonical_head(&self, header: SealedHeader) {
let number = header.number;
*self.inner.canonical_head.write() = header;
// also update the atomic number.
self.inner.canonical_head_number.store(number, Ordering::Relaxed);
}
/// Sets the safe header of the chain.
@ -104,6 +115,8 @@ struct ChainInfoInner {
///
/// This is mainly used to track if we're connected to a beacon node.
last_forkchoice_update: RwLock<Option<Instant>>,
/// Tracks the number of the `canonical_head`.
canonical_head_number: AtomicU64,
/// The canonical head of the chain.
canonical_head: RwLock<SealedHeader>,
/// The block that the beacon node considers safe.

View File

@ -173,15 +173,15 @@ where
DB: Database,
Tree: BlockchainTreeViewer + Send + Sync,
{
fn pending_block_num_hash(&self) -> Result<Option<reth_primitives::BlockNumHash>> {
fn pending_block_num_hash(&self) -> Result<Option<BlockNumHash>> {
Ok(self.tree.pending_block_num_hash())
}
fn safe_block_num_hash(&self) -> Result<Option<reth_primitives::BlockNumHash>> {
fn safe_block_num_hash(&self) -> Result<Option<BlockNumHash>> {
Ok(self.chain_info.get_safe_num_hash())
}
fn finalized_block_num_hash(&self) -> Result<Option<reth_primitives::BlockNumHash>> {
fn finalized_block_num_hash(&self) -> Result<Option<BlockNumHash>> {
Ok(self.chain_info.get_finalized_num_hash())
}
}

View File

@ -48,10 +48,7 @@ pub trait BlockNumProvider: BlockHashProvider + Send + Sync {
#[auto_impl::auto_impl(&, Arc)]
pub trait BlockIdProvider: BlockNumProvider + Send + Sync {
/// Converts the `BlockNumberOrTag` variants to a block number.
fn convert_block_number(
&self,
num: BlockNumberOrTag,
) -> Result<Option<reth_primitives::BlockNumber>> {
fn convert_block_number(&self, num: BlockNumberOrTag) -> Result<Option<BlockNumber>> {
let num = match num {
BlockNumberOrTag::Latest => self.best_block_number()?,
BlockNumberOrTag::Earliest => 0,
@ -91,10 +88,7 @@ pub trait BlockIdProvider: BlockNumProvider + Send + Sync {
}
/// Get the number of the block by matching the given id.
fn block_number_for_id(
&self,
block_id: BlockId,
) -> Result<Option<reth_primitives::BlockNumber>> {
fn block_number_for_id(&self, block_id: BlockId) -> Result<Option<BlockNumber>> {
match block_id {
BlockId::Hash(hash) => self.block_number(hash.into()),
BlockId::Number(num) => self.convert_block_number(num),