feat(rpc): impl uncle count handlers (#1511)

This commit is contained in:
Matthias Seitz
2023-02-22 23:40:58 +01:00
committed by GitHub
parent 823866ac57
commit 17dffba150
5 changed files with 21 additions and 15 deletions

View File

@@ -62,11 +62,11 @@ pub trait EthApi {
/// Returns the number of uncles in a block from a block matching the given block hash.
#[method(name = "eth_getUncleCountByBlockHash")]
async fn block_uncles_count_by_hash(&self, hash: H256) -> Result<U256>;
async fn block_uncles_count_by_hash(&self, hash: H256) -> Result<Option<U256>>;
/// Returns the number of uncles in a block with given block number.
#[method(name = "eth_getUncleCountByBlockNumber")]
async fn block_uncles_count_by_number(&self, number: BlockNumberOrTag) -> Result<U256>;
async fn block_uncles_count_by_number(&self, number: BlockNumberOrTag) -> Result<Option<U256>>;
/// Returns an uncle block of the given block and index.
#[method(name = "eth_getUncleByBlockHashAndIndex")]

View File

@@ -71,16 +71,12 @@ where
EthApiClient::block_by_number(client, block_number, false).await.unwrap();
EthApiClient::block_transaction_count_by_number(client, block_number).await.unwrap();
EthApiClient::block_transaction_count_by_hash(client, hash).await.unwrap();
EthApiClient::block_uncles_count_by_hash(client, hash).await.unwrap();
EthApiClient::block_uncles_count_by_number(client, block_number).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_uncles_count_by_hash(client, hash).await.err().unwrap()
));
assert!(is_unimplemented(
EthApiClient::block_uncles_count_by_number(client, block_number).await.err().unwrap()
));
assert!(is_unimplemented(
EthApiClient::uncle_by_block_hash_and_index(client, hash, index).await.err().unwrap()
));

View File

@@ -12,6 +12,17 @@ impl<Client, Pool, Network> EthApi<Client, Pool, Network>
where
Client: BlockProvider + StateProviderFactory + 'static,
{
/// Returns the uncle headers of the given block
///
/// Returns an empty vec if there are none.
pub(crate) fn ommers(
&self,
block_id: impl Into<BlockId>,
) -> EthResult<Option<Vec<reth_primitives::Header>>> {
let block_id = block_id.into();
Ok(self.client().ommers(block_id)?)
}
pub(crate) async fn block_transaction_count(
&self,
block_id: impl Into<BlockId>,

View File

@@ -89,13 +89,13 @@ where
}
/// Handler for: `eth_getUncleCountByBlockHash`
async fn block_uncles_count_by_hash(&self, _hash: H256) -> Result<U256> {
Err(internal_rpc_err("unimplemented"))
async fn block_uncles_count_by_hash(&self, hash: H256) -> Result<Option<U256>> {
Ok(EthApi::ommers(self, hash)?.map(|ommers| U256::from(ommers.len())))
}
/// Handler for: `eth_getUncleCountByBlockNumber`
async fn block_uncles_count_by_number(&self, _number: BlockNumberOrTag) -> Result<U256> {
Err(internal_rpc_err("unimplemented"))
async fn block_uncles_count_by_number(&self, number: BlockNumberOrTag) -> Result<Option<U256>> {
Ok(EthApi::ommers(self, number)?.map(|ommers| U256::from(ommers.len())))
}
/// Handler for: `eth_getUncleByBlockHashAndIndex`

View File

@@ -140,9 +140,8 @@ impl<DB: Database> BlockProvider for ShareableDatabase<DB> {
if let Some(number) = self.block_number_for_id(id)? {
let tx = self.db.tx()?;
// TODO: this can be optimized to return empty Vec post-merge
let ommers =
tx.get::<tables::BlockOmmers>(number)?.map(|o| o.ommers).unwrap_or_default();
return Ok(Some(ommers))
let ommers = tx.get::<tables::BlockOmmers>(number)?.map(|o| o.ommers);
return Ok(ommers)
}
Ok(None)