mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: add header by number provider fn (#299)
* feat: add header by number provider fn * Update crates/interfaces/src/provider/block.rs Co-authored-by: rakita <rakita@users.noreply.github.com> Co-authored-by: rakita <rakita@users.noreply.github.com>
This commit is contained in:
@ -417,6 +417,10 @@ mod tests {
|
||||
fn header(&self, _block_number: &BlockHash) -> Result<Option<Header>> {
|
||||
Ok(self.parent.clone())
|
||||
}
|
||||
|
||||
fn header_by_number(&self, _num: u64) -> Result<Option<Header>> {
|
||||
Ok(self.parent.clone())
|
||||
}
|
||||
}
|
||||
/// got test block
|
||||
fn mock_block() -> (BlockLocked, Header) {
|
||||
|
||||
@ -2,7 +2,7 @@ use crate::Result;
|
||||
use auto_impl::auto_impl;
|
||||
use reth_primitives::{
|
||||
rpc::{BlockId, BlockNumber},
|
||||
Block, BlockHash, Header, H256, U256,
|
||||
Block, BlockHash, BlockHashOrNumber, Header, H256, U256,
|
||||
};
|
||||
|
||||
/// Client trait for fetching `Header` related data.
|
||||
@ -15,6 +15,17 @@ pub trait HeaderProvider: Send + Sync {
|
||||
|
||||
/// Get header by block hash
|
||||
fn header(&self, block_hash: &BlockHash) -> Result<Option<Header>>;
|
||||
|
||||
/// Get header by block number
|
||||
fn header_by_number(&self, num: u64) -> Result<Option<Header>>;
|
||||
|
||||
/// Get header by block number or hash
|
||||
fn header_by_hash_or_number(&self, hash_or_num: BlockHashOrNumber) -> Result<Option<Header>> {
|
||||
match hash_or_num {
|
||||
BlockHashOrNumber::Hash(hash) => self.header(&hash),
|
||||
BlockHashOrNumber::Number(num) => self.header_by_number(num),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Api trait for fetching `Block` related data.
|
||||
|
||||
@ -2,16 +2,18 @@ use crate::{
|
||||
db::{tables, Database, DbTx},
|
||||
provider::{HeaderProvider, ProviderImpl},
|
||||
};
|
||||
use reth_primitives::{BlockNumber, Header};
|
||||
|
||||
impl<DB: Database> HeaderProvider for ProviderImpl<DB> {
|
||||
fn header(
|
||||
&self,
|
||||
block_hash: &reth_primitives::BlockHash,
|
||||
) -> crate::Result<Option<reth_primitives::Header>> {
|
||||
fn header(&self, block_hash: &reth_primitives::BlockHash) -> crate::Result<Option<Header>> {
|
||||
self.db.view(|tx| tx.get::<tables::Headers>((0, *block_hash).into()))?.map_err(Into::into)
|
||||
}
|
||||
|
||||
fn is_known(&self, block_hash: &reth_primitives::BlockHash) -> crate::Result<bool> {
|
||||
self.header(block_hash).map(|header| header.is_some())
|
||||
fn header_by_number(&self, num: BlockNumber) -> crate::Result<Option<Header>> {
|
||||
if let Some(hash) = self.db.view(|tx| tx.get::<tables::CanonicalHeaders>(num))?? {
|
||||
self.header(&hash)
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user