mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-30 01:28:21 -05:00
feat: add best_block_number call to BlockIdProvider (#2358)
This commit is contained in:
@@ -228,7 +228,7 @@ where
|
||||
|
||||
/// Installs a new filter and returns the new identifier.
|
||||
async fn install_filter(&self, kind: FilterKind) -> RpcResult<FilterId> {
|
||||
let last_poll_block_number = self.client.chain_info().to_rpc_result()?.best_number;
|
||||
let last_poll_block_number = self.client.best_block_number().to_rpc_result()?;
|
||||
let id = FilterId::from(self.id_provider.next_id());
|
||||
let mut filters = self.active_filters.inner.lock().await;
|
||||
filters.insert(
|
||||
|
||||
@@ -139,13 +139,17 @@ impl<DB: Database> BlockHashProvider for ShareableDatabase<DB> {
|
||||
|
||||
impl<DB: Database> BlockIdProvider for ShareableDatabase<DB> {
|
||||
fn chain_info(&self) -> Result<ChainInfo> {
|
||||
let best_number = self
|
||||
let best_number = self.best_block_number()?;
|
||||
let best_hash = self.block_hash(best_number)?.unwrap_or_default();
|
||||
Ok(ChainInfo { best_hash, best_number, last_finalized: None, safe_finalized: None })
|
||||
}
|
||||
|
||||
fn best_block_number(&self) -> Result<BlockNumber> {
|
||||
Ok(self
|
||||
.db
|
||||
.view(|tx| tx.get::<tables::SyncStage>("Finish".to_string()))?
|
||||
.map_err(Into::<reth_interfaces::db::Error>::into)?
|
||||
.unwrap_or_default();
|
||||
let best_hash = self.block_hash(best_number)?.unwrap_or_default();
|
||||
Ok(ChainInfo { best_hash, best_number, last_finalized: None, safe_finalized: None })
|
||||
.unwrap_or_default())
|
||||
}
|
||||
|
||||
fn block_number(&self, hash: H256) -> Result<Option<BlockNumber>> {
|
||||
|
||||
@@ -99,6 +99,10 @@ where
|
||||
self.database.chain_info()
|
||||
}
|
||||
|
||||
fn best_block_number(&self) -> Result<BlockNumber> {
|
||||
self.database.best_block_number()
|
||||
}
|
||||
|
||||
fn convert_block_number(&self, num: BlockNumberOrTag) -> Result<Option<BlockNumber>> {
|
||||
let num = match num {
|
||||
BlockNumberOrTag::Latest => self.chain_info()?.best_number,
|
||||
|
||||
@@ -222,10 +222,12 @@ impl BlockHashProvider for MockEthProvider {
|
||||
|
||||
impl BlockIdProvider for MockEthProvider {
|
||||
fn chain_info(&self) -> Result<ChainInfo> {
|
||||
let best_block_number = self.best_block_number()?;
|
||||
let lock = self.headers.lock();
|
||||
|
||||
Ok(lock
|
||||
.iter()
|
||||
.max_by_key(|h| h.1.number)
|
||||
.find(|(_, header)| header.number == best_block_number)
|
||||
.map(|(hash, header)| ChainInfo {
|
||||
best_hash: *hash,
|
||||
best_number: header.number,
|
||||
@@ -235,6 +237,15 @@ impl BlockIdProvider for MockEthProvider {
|
||||
.expect("provider is empty"))
|
||||
}
|
||||
|
||||
fn best_block_number(&self) -> Result<BlockNumber> {
|
||||
let lock = self.headers.lock();
|
||||
Ok(lock
|
||||
.iter()
|
||||
.max_by_key(|h| h.1.number)
|
||||
.map(|(_, header)| header.number)
|
||||
.expect("provider is empty"))
|
||||
}
|
||||
|
||||
fn block_number(&self, hash: H256) -> Result<Option<reth_primitives::BlockNumber>> {
|
||||
let lock = self.blocks.lock();
|
||||
let num = lock.iter().find_map(|(h, b)| (*h == hash).then_some(b.number));
|
||||
|
||||
@@ -33,6 +33,10 @@ impl BlockIdProvider for NoopProvider {
|
||||
Ok(ChainInfo::default())
|
||||
}
|
||||
|
||||
fn best_block_number(&self) -> Result<BlockNumber> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
fn block_number(&self, _hash: H256) -> Result<Option<BlockNumber>> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use super::BlockHashProvider;
|
||||
use reth_interfaces::Result;
|
||||
use reth_primitives::{BlockId, BlockNumberOrTag, ChainInfo, H256};
|
||||
use reth_primitives::{BlockId, BlockNumber, BlockNumberOrTag, ChainInfo, H256};
|
||||
|
||||
/// Client trait for transforming [BlockId].
|
||||
#[auto_impl::auto_impl(&, Arc)]
|
||||
@@ -8,6 +8,9 @@ pub trait BlockIdProvider: BlockHashProvider + Send + Sync {
|
||||
/// Returns the current info for the chain.
|
||||
fn chain_info(&self) -> Result<ChainInfo>;
|
||||
|
||||
/// Returns the best block number in the chain.
|
||||
fn best_block_number(&self) -> Result<BlockNumber>;
|
||||
|
||||
/// Converts the `BlockNumberOrTag` variants.
|
||||
fn convert_block_number(
|
||||
&self,
|
||||
|
||||
Reference in New Issue
Block a user