Skip to content

Commit

Permalink
feat(rpc): add block_by_number (#1467)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Feb 20, 2023
1 parent 058349e commit 8d1dc58
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
14 changes: 13 additions & 1 deletion crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,28 @@ 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),
/// A block number
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()
Expand Down
4 changes: 1 addition & 3 deletions crates/rpc/rpc-builder/tests/it/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
));
Expand Down
37 changes: 15 additions & 22 deletions crates/rpc/rpc/src/eth/api/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,35 @@ 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};

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)
}
}
}
8 changes: 4 additions & 4 deletions crates/rpc/rpc/src/eth/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down

0 comments on commit 8d1dc58

Please sign in to comment.