mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat(rpc): add block_by_number (#1467)
This commit is contained in:
@ -156,9 +156,9 @@ impl Decodable for BlockHashOrNumber {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
/// A Block Identifier
|
||||
/// <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1898.md>
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub enum BlockId {
|
||||
/// A block hash and an optional bool that defines if it's canonical
|
||||
Hash(BlockHash),
|
||||
@ -166,6 +166,18 @@ pub enum BlockId {
|
||||
Number(BlockNumberOrTag),
|
||||
}
|
||||
|
||||
// === impl BlockId ===
|
||||
|
||||
impl BlockId {
|
||||
/// Returns the block hash if it is [BlockId::Hash]
|
||||
pub fn as_block_hash(&self) -> Option<H256> {
|
||||
match self {
|
||||
BlockId::Hash(hash) => Some(hash.block_hash),
|
||||
BlockId::Number(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u64> for BlockId {
|
||||
fn from(num: u64) -> Self {
|
||||
BlockNumberOrTag::Number(num).into()
|
||||
|
||||
@ -68,13 +68,11 @@ where
|
||||
EthApiClient::transaction_count(client, address, None).await.unwrap();
|
||||
EthApiClient::storage_at(client, address, U256::default(), None).await.unwrap();
|
||||
EthApiClient::block_by_hash(client, hash, false).await.unwrap();
|
||||
EthApiClient::block_by_number(client, block_number, false).await.unwrap();
|
||||
|
||||
// Unimplemented
|
||||
assert!(is_unimplemented(EthApiClient::syncing(client).await.err().unwrap()));
|
||||
assert!(is_unimplemented(EthApiClient::author(client).await.err().unwrap()));
|
||||
assert!(is_unimplemented(
|
||||
EthApiClient::block_by_number(client, block_number, false).await.err().unwrap()
|
||||
));
|
||||
assert!(is_unimplemented(
|
||||
EthApiClient::block_transaction_count_by_hash(client, hash).await.err().unwrap()
|
||||
));
|
||||
|
||||
@ -4,7 +4,7 @@ use crate::{
|
||||
eth::error::{EthApiError, EthResult},
|
||||
EthApi,
|
||||
};
|
||||
use reth_primitives::{BlockId, H256};
|
||||
use reth_primitives::BlockId;
|
||||
use reth_provider::{BlockProvider, StateProviderFactory};
|
||||
use reth_rpc_types::{Block, RichBlock};
|
||||
|
||||
@ -12,34 +12,27 @@ impl<Client, Pool, Network> EthApi<Client, Pool, Network>
|
||||
where
|
||||
Client: BlockProvider + StateProviderFactory + 'static,
|
||||
{
|
||||
pub(crate) async fn block_by_hash(
|
||||
pub(crate) async fn block(
|
||||
&self,
|
||||
hash: H256,
|
||||
block_id: impl Into<BlockId>,
|
||||
full: bool,
|
||||
) -> EthResult<Option<RichBlock>> {
|
||||
if let Some(block) = self.client().block_by_hash(hash)? {
|
||||
let total_difficulty =
|
||||
self.client().header_td(&hash)?.ok_or_else(|| EthApiError::UnknownBlockNumber)?;
|
||||
let block_id = block_id.into();
|
||||
// TODO support pending block
|
||||
|
||||
if let Some(block) = self.client().block(block_id)? {
|
||||
let block_hash = self
|
||||
.client()
|
||||
.block_hash_for_id(block_id)?
|
||||
.ok_or_else(|| EthApiError::UnknownBlockNumber)?;
|
||||
let total_difficulty = self
|
||||
.client()
|
||||
.header_td(&block_hash)?
|
||||
.ok_or_else(|| EthApiError::UnknownBlockNumber)?;
|
||||
let block = Block::from_block(block, total_difficulty, full.into())?;
|
||||
Ok(Some(block.into()))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn block_by_number(
|
||||
&self,
|
||||
number: u64,
|
||||
_full: bool,
|
||||
) -> EthResult<Option<RichBlock>> {
|
||||
let block = self.client().block(BlockId::Number(number.into()))?;
|
||||
if let Some(_block) = block {
|
||||
// TODO: GET TD FOR BLOCK - needs block provider? or header provider?
|
||||
// let total_difficulty = todo!();
|
||||
// let rich_block = Block::from_block_full(block, total_difficulty);
|
||||
todo!()
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,15 +56,15 @@ where
|
||||
}
|
||||
|
||||
async fn block_by_hash(&self, hash: H256, full: bool) -> Result<Option<RichBlock>> {
|
||||
Ok(EthApi::block_by_hash(self, hash, full).await?)
|
||||
Ok(EthApi::block(self, hash, full).await?)
|
||||
}
|
||||
|
||||
async fn block_by_number(
|
||||
&self,
|
||||
_number: BlockNumberOrTag,
|
||||
_full: bool,
|
||||
number: BlockNumberOrTag,
|
||||
full: bool,
|
||||
) -> Result<Option<RichBlock>> {
|
||||
Err(internal_rpc_err("unimplemented"))
|
||||
Ok(EthApi::block(self, number, full).await?)
|
||||
}
|
||||
|
||||
async fn block_transaction_count_by_hash(&self, _hash: H256) -> Result<Option<U256>> {
|
||||
|
||||
Reference in New Issue
Block a user