feat(rpc): add basic state access functions (#1136)

This commit is contained in:
Matthias Seitz
2023-02-02 23:21:11 +01:00
committed by GitHub
parent e048718ea2
commit e09b3e098a
3 changed files with 97 additions and 1 deletions

View File

@ -0,0 +1,19 @@
//! Contains RPC handler implementations specific to blocks.
use crate::{eth::error::EthResult, EthApi};
use reth_primitives::H256;
use reth_provider::{BlockProvider, StateProviderFactory};
use reth_rpc_types::RichBlock;
impl<Pool, Client, Network> EthApi<Pool, Client, Network>
where
Client: BlockProvider + StateProviderFactory + 'static,
{
pub(crate) async fn block_by_hash(
&self,
_hash: H256,
_full: bool,
) -> EthResult<Option<RichBlock>> {
todo!()
}
}

View File

@ -7,13 +7,18 @@ use crate::eth::signer::EthSigner;
use async_trait::async_trait; use async_trait::async_trait;
use reth_interfaces::Result; use reth_interfaces::Result;
use reth_network_api::NetworkInfo; use reth_network_api::NetworkInfo;
use reth_primitives::{Address, ChainInfo, U64}; use reth_primitives::{
rpc::{BlockId, BlockNumber},
Address, ChainInfo, H256, U64,
};
use reth_provider::{BlockProvider, StateProviderFactory}; use reth_provider::{BlockProvider, StateProviderFactory};
use reth_rpc_types::Transaction; use reth_rpc_types::Transaction;
use reth_transaction_pool::TransactionPool; use reth_transaction_pool::TransactionPool;
use std::sync::Arc; use std::sync::Arc;
mod block;
mod server; mod server;
mod state;
mod transactions; mod transactions;
/// `Eth` API trait. /// `Eth` API trait.
@ -72,6 +77,58 @@ impl<Pool, Client, Network> EthApi<Pool, Client, Network> {
} }
} }
// === State access helpers ===
impl<Pool, Client, Network> EthApi<Pool, Client, Network>
where
Client: BlockProvider + StateProviderFactory + 'static,
{
fn convert_block_number(&self, num: BlockNumber) -> Result<Option<u64>> {
self.client().convert_block_number(num)
}
/// Returns the state at the given [BlockId] enum.
pub(crate) fn state_at_block_id(
&self,
block_id: BlockId,
) -> Result<Option<<Client as StateProviderFactory>::HistorySP<'_>>> {
match block_id {
BlockId::Hash(hash) => self.state_at_hash(H256(hash.0)).map(Some),
BlockId::Number(num) => self.state_at_block_number(num),
}
}
/// Returns the state at the given [BlockNumber] enum
///
/// Returns `None` if no state available.
pub(crate) fn state_at_block_number(
&self,
num: BlockNumber,
) -> Result<Option<<Client as StateProviderFactory>::HistorySP<'_>>> {
if let Some(number) = self.convert_block_number(num)? {
self.state_at_number(number).map(Some)
} else {
Ok(None)
}
}
/// Returns the state at the given block number
pub(crate) fn state_at_hash(
&self,
block_hash: H256,
) -> Result<<Client as StateProviderFactory>::HistorySP<'_>> {
self.client().history_by_block_hash(block_hash)
}
/// Returns the state at the given block number
pub(crate) fn state_at_number(
&self,
block_number: u64,
) -> Result<<Client as StateProviderFactory>::HistorySP<'_>> {
self.client().history_by_block_number(block_number)
}
}
#[async_trait] #[async_trait]
impl<Pool, Client, Network> EthApiSpec for EthApi<Pool, Client, Network> impl<Pool, Client, Network> EthApiSpec for EthApi<Pool, Client, Network>
where where

View File

@ -0,0 +1,20 @@
//! Contains RPC handler implementations specific to state.
use crate::EthApi;
use reth_interfaces::Result;
use reth_primitives::{rpc::BlockId, Address, H256, U256};
use reth_provider::{BlockProvider, StateProviderFactory};
impl<Pool, Client, Network> EthApi<Pool, Client, Network>
where
Client: BlockProvider + StateProviderFactory + 'static,
{
async fn storage_at(
&self,
_address: Address,
_index: U256,
_block_number: Option<BlockId>,
) -> Result<H256> {
todo!()
}
}