fix: add unknown block error (#3779)

Co-authored-by: suneal <suneal@soulwalletlabs.com>
This commit is contained in:
suneal
2023-07-14 22:23:37 +08:00
committed by GitHub
parent 715d41dbc7
commit d1610f3df9
3 changed files with 23 additions and 5 deletions

View File

@@ -13,6 +13,9 @@ pub enum EthRpcErrorCode {
/// > If the block is not found, the callee SHOULD raise a JSON-RPC error (the recommended
/// > error code is -32001: Resource not found).
ResourceNotFound,
/// 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>
UnknownBlock,
}
impl EthRpcErrorCode {
@@ -23,6 +26,7 @@ impl EthRpcErrorCode {
EthRpcErrorCode::ExecutionError => 3,
EthRpcErrorCode::InvalidInput => -32000,
EthRpcErrorCode::ResourceNotFound => -32001,
EthRpcErrorCode::UnknownBlock => -39001,
}
}
}

View File

@@ -26,6 +26,10 @@ pub enum EthApiError {
PoolError(RpcPoolError),
#[error("Unknown block number")]
UnknownBlockNumber,
/// 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")]
UnknownSafeOrFinalizedBlock,
#[error("Unknown block or tx index")]
UnknownBlockOrTxIndex,
#[error("Invalid block range")]
@@ -101,6 +105,9 @@ impl From<EthApiError> for ErrorObject<'static> {
EthApiError::UnknownBlockNumber | EthApiError::UnknownBlockOrTxIndex => {
rpc_error_with_code(EthRpcErrorCode::ResourceNotFound.code(), error.to_string())
}
EthApiError::UnknownSafeOrFinalizedBlock => {
rpc_error_with_code(EthRpcErrorCode::UnknownBlock.code(), error.to_string())
}
EthApiError::Unsupported(msg) => internal_rpc_err(msg),
EthApiError::InternalJsTracerError(msg) => internal_rpc_err(msg),
EthApiError::InvalidParams(msg) => invalid_params_rpc_err(msg),
@@ -143,11 +150,12 @@ impl From<reth_interfaces::provider::ProviderError> for EthApiError {
ProviderError::HeaderNotFound(_) |
ProviderError::BlockHashNotFound(_) |
ProviderError::BestBlockNotFound |
ProviderError::FinalizedBlockNotFound |
ProviderError::SafeBlockNotFound |
ProviderError::BlockNumberForTransactionIndexNotFound |
ProviderError::TotalDifficultyNotFound { .. } |
ProviderError::UnknownBlockHash(_) => EthApiError::UnknownBlockNumber,
ProviderError::FinalizedBlockNotFound | ProviderError::SafeBlockNotFound => {
EthApiError::UnknownSafeOrFinalizedBlock
}
err => EthApiError::Internal(err.into()),
}
}

View File

@@ -1,5 +1,5 @@
use super::BlockHashReader;
use reth_interfaces::Result;
use reth_interfaces::{provider::ProviderError, Result};
use reth_primitives::{BlockHashOrNumber, BlockId, BlockNumber, BlockNumberOrTag, ChainInfo, H256};
/// Client trait for getting important block numbers (such as the latest block number), converting
@@ -61,8 +61,14 @@ pub trait BlockIdReader: BlockNumReader + Send + Sync {
.map(|res_opt| res_opt.map(|num_hash| num_hash.number))
}
BlockNumberOrTag::Number(num) => num,
BlockNumberOrTag::Finalized => return self.finalized_block_number(),
BlockNumberOrTag::Safe => return self.safe_block_number(),
BlockNumberOrTag::Finalized => match self.finalized_block_number()? {
Some(block_number) => block_number,
None => return Err(ProviderError::FinalizedBlockNotFound.into()),
},
BlockNumberOrTag::Safe => match self.safe_block_number()? {
Some(block_number) => block_number,
None => return Err(ProviderError::SafeBlockNotFound.into()),
},
};
Ok(Some(num))
}