From e09b3e098a0a1fe5a687b92102ac6b11a6e381ea Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 2 Feb 2023 23:21:11 +0100 Subject: [PATCH] feat(rpc): add basic state access functions (#1136) --- crates/rpc/rpc/src/eth/api/block.rs | 19 ++++++++++ crates/rpc/rpc/src/eth/api/mod.rs | 59 ++++++++++++++++++++++++++++- crates/rpc/rpc/src/eth/api/state.rs | 20 ++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 crates/rpc/rpc/src/eth/api/block.rs create mode 100644 crates/rpc/rpc/src/eth/api/state.rs diff --git a/crates/rpc/rpc/src/eth/api/block.rs b/crates/rpc/rpc/src/eth/api/block.rs new file mode 100644 index 000000000..47e121991 --- /dev/null +++ b/crates/rpc/rpc/src/eth/api/block.rs @@ -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 EthApi +where + Client: BlockProvider + StateProviderFactory + 'static, +{ + pub(crate) async fn block_by_hash( + &self, + _hash: H256, + _full: bool, + ) -> EthResult> { + todo!() + } +} diff --git a/crates/rpc/rpc/src/eth/api/mod.rs b/crates/rpc/rpc/src/eth/api/mod.rs index 2dd681e5c..da200b611 100644 --- a/crates/rpc/rpc/src/eth/api/mod.rs +++ b/crates/rpc/rpc/src/eth/api/mod.rs @@ -7,13 +7,18 @@ use crate::eth::signer::EthSigner; use async_trait::async_trait; use reth_interfaces::Result; 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_rpc_types::Transaction; use reth_transaction_pool::TransactionPool; use std::sync::Arc; +mod block; mod server; +mod state; mod transactions; /// `Eth` API trait. @@ -72,6 +77,58 @@ impl EthApi { } } +// === State access helpers === + +impl EthApi +where + Client: BlockProvider + StateProviderFactory + 'static, +{ + fn convert_block_number(&self, num: BlockNumber) -> Result> { + 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::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::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<::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<::HistorySP<'_>> { + self.client().history_by_block_number(block_number) + } +} + #[async_trait] impl EthApiSpec for EthApi where diff --git a/crates/rpc/rpc/src/eth/api/state.rs b/crates/rpc/rpc/src/eth/api/state.rs new file mode 100644 index 000000000..9eb2d4bb1 --- /dev/null +++ b/crates/rpc/rpc/src/eth/api/state.rs @@ -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 EthApi +where + Client: BlockProvider + StateProviderFactory + 'static, +{ + async fn storage_at( + &self, + _address: Address, + _index: U256, + _block_number: Option, + ) -> Result { + todo!() + } +}