From 8d1dc58af5c377f307d880283ef0079a235e1e1d Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 20 Feb 2023 17:48:14 +0100 Subject: [PATCH] feat(rpc): add block_by_number (#1467) --- crates/primitives/src/block.rs | 14 +++++++++- crates/rpc/rpc-builder/tests/it/http.rs | 4 +-- crates/rpc/rpc/src/eth/api/block.rs | 37 ++++++++++--------------- crates/rpc/rpc/src/eth/api/server.rs | 8 +++--- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index b453a56bb6..13b8b9d548 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -156,9 +156,9 @@ impl Decodable for BlockHashOrNumber { } } -#[derive(Copy, Clone, Debug, PartialEq, Eq)] /// A Block Identifier /// +#[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 { + match self { + BlockId::Hash(hash) => Some(hash.block_hash), + BlockId::Number(_) => None, + } + } +} + impl From for BlockId { fn from(num: u64) -> Self { BlockNumberOrTag::Number(num).into() diff --git a/crates/rpc/rpc-builder/tests/it/http.rs b/crates/rpc/rpc-builder/tests/it/http.rs index 4d7e27b442..b2c62c598d 100644 --- a/crates/rpc/rpc-builder/tests/it/http.rs +++ b/crates/rpc/rpc-builder/tests/it/http.rs @@ -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() )); diff --git a/crates/rpc/rpc/src/eth/api/block.rs b/crates/rpc/rpc/src/eth/api/block.rs index 2710ea5a50..d9784e7f52 100644 --- a/crates/rpc/rpc/src/eth/api/block.rs +++ b/crates/rpc/rpc/src/eth/api/block.rs @@ -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 EthApi where Client: BlockProvider + StateProviderFactory + 'static, { - pub(crate) async fn block_by_hash( + pub(crate) async fn block( &self, - hash: H256, + block_id: impl Into, full: bool, ) -> EthResult> { - 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> { - 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) - } - } } diff --git a/crates/rpc/rpc/src/eth/api/server.rs b/crates/rpc/rpc/src/eth/api/server.rs index 0b35ddbe7d..f984aea42d 100644 --- a/crates/rpc/rpc/src/eth/api/server.rs +++ b/crates/rpc/rpc/src/eth/api/server.rs @@ -56,15 +56,15 @@ where } async fn block_by_hash(&self, hash: H256, full: bool) -> Result> { - 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> { - Err(internal_rpc_err("unimplemented")) + Ok(EthApi::block(self, number, full).await?) } async fn block_transaction_count_by_hash(&self, _hash: H256) -> Result> {