feat(rpc): impl get code handler (#1210)

This commit is contained in:
Matthias Seitz
2023-02-07 20:16:57 +01:00
committed by GitHub
parent 10aa3d617a
commit 4c76581870
4 changed files with 40 additions and 5 deletions

View File

@@ -86,6 +86,18 @@ where
self.client().convert_block_number(num)
}
/// Returns the state at the given [BlockId] enum or the latest.
pub(crate) fn state_at_block_id_or_latest(
&self,
block_id: Option<BlockId>,
) -> Result<Option<<Client as StateProviderFactory>::HistorySP<'_>>> {
if let Some(block_id) = block_id {
self.state_at_block_id(block_id)
} else {
self.latest_state()
}
}
/// Returns the state at the given [BlockId] enum.
pub(crate) fn state_at_block_id(
&self,
@@ -126,6 +138,13 @@ where
) -> Result<<Client as StateProviderFactory>::HistorySP<'_>> {
self.client().history_by_block_number(block_number)
}
/// Returns the _latest_ state
pub(crate) fn latest_state(
&self,
) -> Result<Option<<Client as StateProviderFactory>::HistorySP<'_>>> {
self.state_at_block_number(BlockNumber::Latest)
}
}
#[async_trait]

View File

@@ -150,8 +150,8 @@ where
Err(internal_rpc_err("unimplemented"))
}
async fn get_code(&self, _address: Address, _block_number: Option<BlockId>) -> Result<Bytes> {
Err(internal_rpc_err("unimplemented"))
async fn get_code(&self, address: Address, block_number: Option<BlockId>) -> Result<Bytes> {
EthApi::get_code(self, address, block_number).to_rpc_result()
}
async fn call(&self, _request: CallRequest, _block_number: Option<BlockId>) -> Result<Bytes> {

View File

@@ -1,14 +1,24 @@
//! Contains RPC handler implementations specific to state.
use crate::EthApi;
use crate::{
eth::error::{EthApiError, EthResult},
EthApi,
};
use reth_interfaces::Result;
use reth_primitives::{rpc::BlockId, Address, H256, U256};
use reth_provider::{BlockProvider, StateProviderFactory};
use reth_primitives::{rpc::BlockId, Address, Bytes, H256, U256};
use reth_provider::{BlockProvider, StateProvider, StateProviderFactory};
impl<Client, Pool, Network> EthApi<Client, Pool, Network>
where
Client: BlockProvider + StateProviderFactory + 'static,
{
pub(crate) fn get_code(&self, address: Address, block_id: Option<BlockId>) -> EthResult<Bytes> {
let state =
self.state_at_block_id_or_latest(block_id)?.ok_or(EthApiError::UnknownBlockNumber)?;
let code = state.account_code(address)?.unwrap_or_default();
Ok(code)
}
async fn storage_at(
&self,
_address: Address,

View File

@@ -19,6 +19,12 @@ pub(crate) enum EthApiError {
InvalidTransactionSignature,
#[error(transparent)]
PoolError(GethTxPoolError),
#[error("Unknown block number")]
// TODO return -32602 here
UnknownBlockNumber,
/// Other internal error
#[error(transparent)]
Internal(#[from] reth_interfaces::Error),
}
impl_to_rpc_result!(EthApiError);