Skip to content

Commit

Permalink
feat: Include missing block id in error responses
Browse files Browse the repository at this point in the history
Fixes #7368.
  • Loading branch information
ryanschneider committed Apr 1, 2024
1 parent e087e7e commit e8a3335
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
15 changes: 5 additions & 10 deletions crates/rpc/rpc/src/eth/api/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,11 @@ where
pub async fn call(
&self,
request: TransactionRequest,
block_number: Option<BlockId>,
block_id: Option<BlockId>,
overrides: EvmOverrides,
) -> EthResult<Bytes> {
let (res, _env) = self
.transact_call_at(
request,
block_number.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest)),
overrides,
)
.await?;
let block_id = block_id.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest));
let (res, _env) = self.transact_call_at(request, block_id, overrides).await?;

ensure_success(res.result)
}
Expand Down Expand Up @@ -376,10 +371,10 @@ where
pub(crate) async fn create_access_list_at(
&self,
request: TransactionRequest,
block_number: Option<BlockId>,
block_id: Option<BlockId>,
) -> EthResult<AccessListWithGasUsed> {
self.on_blocking_task(|this| async move {
this.create_access_list_with(request, block_number).await
this.create_access_list_with(request, block_id).await
})
.await
}
Expand Down
9 changes: 7 additions & 2 deletions crates/rpc/rpc/src/eth/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use reth_primitives::{
U256, U64,
};
use reth_provider::{
BlockReaderIdExt, ChainSpecProvider, EvmEnvProvider, StateProviderBox, StateProviderFactory,
BlockReaderIdExt, ChainSpecProvider, EvmEnvProvider, ProviderError, StateProviderBox,
StateProviderFactory,
};
use reth_rpc_types::{SyncInfo, SyncStatus};
use reth_tasks::{pool::BlockingTaskPool, TaskSpawner, TokioTaskExecutor};
Expand Down Expand Up @@ -231,7 +232,11 @@ where
///
/// Note: if not [BlockNumberOrTag::Pending] then this will only return canonical state. See also <https://github.com/paradigmxyz/reth/issues/4515>
pub fn state_at_block_id(&self, at: BlockId) -> EthResult<StateProviderBox> {
Ok(self.provider().state_by_block_id(at)?)
self.provider().state_by_block_id(at).map_err(|e| match e {
ProviderError::HeaderNotFound(_) => EthApiError::UnknownBlockId(at),
ProviderError::BlockHashNotFound(_) => EthApiError::UnknownBlockId(at),
_ => e.into(),
})
}

/// Returns the state at the given [BlockId] enum or the latest.
Expand Down
12 changes: 6 additions & 6 deletions crates/rpc/rpc/src/eth/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,10 @@ where
async fn create_access_list(
&self,
request: TransactionRequest,
block_number: Option<BlockId>,
block_id: Option<BlockId>,
) -> Result<AccessListWithGasUsed> {
trace!(target: "rpc::eth", ?request, ?block_number, "Serving eth_createAccessList");
let access_list_with_gas_used = self.create_access_list_at(request, block_number).await?;
trace!(target: "rpc::eth", ?request, ?block_id, "Serving eth_createAccessList");
let access_list_with_gas_used = self.create_access_list_at(request, block_id).await?;

Ok(access_list_with_gas_used)
}
Expand Down Expand Up @@ -416,10 +416,10 @@ where
&self,
address: Address,
keys: Vec<JsonStorageKey>,
block_number: Option<BlockId>,
block_id: Option<BlockId>,
) -> Result<EIP1186AccountProofResponse> {
trace!(target: "rpc::eth", ?address, ?keys, ?block_number, "Serving eth_getProof");
let res = EthApi::get_proof(self, address, keys, block_number).await;
trace!(target: "rpc::eth", ?address, ?keys, ?block_id, "Serving eth_getProof");
let res = EthApi::get_proof(self, address, keys, block_id).await;

Ok(res.map_err(|e| match e {
EthApiError::InvalidBlockRange => {
Expand Down
10 changes: 7 additions & 3 deletions crates/rpc/rpc/src/eth/api/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use reth_primitives::{
TransactionMeta, TransactionSigned, TransactionSignedEcRecovered, B256, U128, U256, U64,
};
use reth_provider::{
BlockReaderIdExt, ChainSpecProvider, EvmEnvProvider, StateProviderBox, StateProviderFactory,
BlockReaderIdExt, ChainSpecProvider, EvmEnvProvider, ProviderError, StateProviderBox,
StateProviderFactory,
};
use reth_revm::{
database::StateProviderDatabase,
Expand Down Expand Up @@ -535,8 +536,11 @@ where
let block_hash = self
.provider()
.block_hash_for_id(at)?
.ok_or_else(|| EthApiError::UnknownBlockNumber)?;
let (cfg, env) = self.cache().get_evm_env(block_hash).await?;
.ok_or_else(|| EthApiError::UnknownBlockId(at))?;
let (cfg, env) = self.cache().get_evm_env(block_hash).await.map_err(|e| match e {
ProviderError::HeaderNotFound(_) => EthApiError::UnknownBlockId(at),
_ => e.into(),
})?;
Ok((cfg, env, block_hash.into()))
}
}
Expand Down
21 changes: 20 additions & 1 deletion crates/rpc/rpc/src/eth/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use jsonrpsee::{
use reth_interfaces::RethError;
use reth_primitives::{revm_primitives::InvalidHeader, Address, Bytes, U256};
use reth_revm::tracing::{js::JsInspectorError, MuxError};
use reth_rpc_types::{error::EthRpcErrorCode, request::TransactionInputError, BlockError};
use reth_rpc_types::{error::EthRpcErrorCode, request::TransactionInputError, BlockError, BlockId};
use reth_transaction_pool::error::{
Eip4844PoolTransactionError, InvalidPoolTransactionError, PoolError, PoolErrorKind,
PoolTransactionError,
Expand Down Expand Up @@ -44,6 +44,9 @@ pub enum EthApiError {
/// When an unknown block number is encountered
#[error("unknown block number")]
UnknownBlockNumber,
/// When an unknown block is encountered
#[error("unknown block id")]
UnknownBlockId(BlockId),
/// Thrown when querying for `finalized` or `safe` block before the merge transition is
/// finalized, <https://github.com/ethereum/execution-apis/blob/6d17705a875e52c26826124c2a8a15ed542aeca2/src/schemas/block.yaml#L109>
#[error("unknown block")]
Expand Down Expand Up @@ -156,6 +159,22 @@ impl From<EthApiError> for ErrorObject<'static> {
EthApiError::UnknownBlockNumber | EthApiError::UnknownBlockOrTxIndex => {
rpc_error_with_code(EthRpcErrorCode::ResourceNotFound.code(), error.to_string())
}
EthApiError::UnknownBlockId(id) => {
let arg = match id {
BlockId::Hash(h) => {
if h.require_canonical == Some(true) {
format!("canonical hash {}", h.block_hash)
} else {
format!("hash {}", h.block_hash)
}
}
BlockId::Number(n) => format!("number {}", n),
};
rpc_error_with_code(
EthRpcErrorCode::ResourceNotFound.code(),
format!("{}: {}", error, arg),
)
}
EthApiError::UnknownSafeOrFinalizedBlock => {
rpc_error_with_code(EthRpcErrorCode::UnknownBlock.code(), error.to_string())
}
Expand Down

0 comments on commit e8a3335

Please sign in to comment.