mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-29 00:58:11 -05:00
feat(rpc): impl uncle count handlers (#1511)
This commit is contained in:
@@ -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")]
|
||||
|
||||
@@ -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()
|
||||
));
|
||||
|
||||
@@ -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>,
|
||||
|
||||
@@ -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`
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user