mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat(rpc): add basic state access functions (#1136)
This commit is contained in:
19
crates/rpc/rpc/src/eth/api/block.rs
Normal file
19
crates/rpc/rpc/src/eth/api/block.rs
Normal 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!()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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
|
||||||
|
|||||||
20
crates/rpc/rpc/src/eth/api/state.rs
Normal file
20
crates/rpc/rpc/src/eth/api/state.rs
Normal 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!()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user