Skip to content

Commit

Permalink
chore: move ethfiltererror (#11552)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Oct 7, 2024
1 parent c35b8be commit b67f004
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 57 deletions.
1 change: 0 additions & 1 deletion crates/rpc/rpc-eth-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub use gas_oracle::{
GasCap, GasPriceOracle, GasPriceOracleConfig, GasPriceOracleResult, RPC_DEFAULT_GAS_CAP,
};
pub use id_provider::EthSubscriptionIdProvider;
pub use logs_utils::EthFilterError;
pub use pending_block::{PendingBlock, PendingBlockEnv, PendingBlockEnvOrigin};
pub use receipt::ReceiptBuilder;
pub use transaction::TransactionSource;
56 changes: 2 additions & 54 deletions crates/rpc/rpc-eth-types/src/logs_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,12 @@
//! Log parsing for building filter.
use alloy_primitives::TxHash;
use alloy_rpc_types::{FilterId, FilteredParams, Log};
use alloy_rpc_types::{FilteredParams, Log};
use reth_chainspec::ChainInfo;
use reth_errors::ProviderError;
use reth_primitives::{BlockNumHash, Receipt};
use reth_rpc_server_types::result::rpc_error_with_code;
use reth_storage_api::BlockReader;

use crate::EthApiError;

/// Errors that can occur in the handler implementation
#[derive(Debug, thiserror::Error)]
pub enum EthFilterError {
/// Filter not found.
#[error("filter not found")]
FilterNotFound(FilterId),
/// Invalid block range.
#[error("invalid block range params")]
InvalidBlockRangeParams,
/// Query scope is too broad.
#[error("query exceeds max block range {0}")]
QueryExceedsMaxBlocks(u64),
/// Query result is too large.
#[error("query exceeds max results {0}")]
QueryExceedsMaxResults(usize),
/// Error serving request in `eth_` namespace.
#[error(transparent)]
EthAPIError(#[from] EthApiError),
/// Error thrown when a spawned task failed to deliver a response.
#[error("internal filter error")]
InternalError,
}

// convert the error
impl From<EthFilterError> for jsonrpsee_types::error::ErrorObject<'static> {
fn from(err: EthFilterError) -> Self {
match err {
EthFilterError::FilterNotFound(_) => {
rpc_error_with_code(jsonrpsee_types::error::INVALID_PARAMS_CODE, "filter not found")
}
err @ EthFilterError::InternalError => {
rpc_error_with_code(jsonrpsee_types::error::INTERNAL_ERROR_CODE, err.to_string())
}
EthFilterError::EthAPIError(err) => err.into(),
err @ (EthFilterError::InvalidBlockRangeParams |
EthFilterError::QueryExceedsMaxBlocks(_) |
EthFilterError::QueryExceedsMaxResults(_)) => {
rpc_error_with_code(jsonrpsee_types::error::INVALID_PARAMS_CODE, err.to_string())
}
}
}
}

impl From<ProviderError> for EthFilterError {
fn from(err: ProviderError) -> Self {
Self::EthAPIError(err.into())
}
}

/// Returns all matching of a block's receipts when the transaction hashes are known.
pub fn matching_block_logs_with_tx_hashes<'a, I>(
filter: &FilteredParams,
Expand Down Expand Up @@ -107,7 +55,7 @@ pub fn append_matching_block_logs(
receipts: &[Receipt],
removed: bool,
block_timestamp: u64,
) -> Result<(), EthFilterError> {
) -> Result<(), ProviderError> {
// Tracks the index of a log in the entire block.
let mut log_index: u64 = 0;

Expand Down
53 changes: 51 additions & 2 deletions crates/rpc/rpc/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use reth_provider::{BlockIdReader, BlockReader, EvmEnvProvider, ProviderError};
use reth_rpc_eth_api::{EthFilterApiServer, FullEthApiTypes, RpcTransaction, TransactionCompat};
use reth_rpc_eth_types::{
logs_utils::{self, append_matching_block_logs},
EthApiError, EthFilterConfig, EthFilterError, EthStateCache, EthSubscriptionIdProvider,
EthApiError, EthFilterConfig, EthStateCache, EthSubscriptionIdProvider,
};
use reth_rpc_server_types::ToRpcResult;
use reth_rpc_server_types::{result::rpc_error_with_code, ToRpcResult};
use reth_rpc_types_compat::transaction::from_recovered;
use reth_tasks::TaskSpawner;
use reth_transaction_pool::{NewSubpoolTransactionStream, PoolTransaction, TransactionPool};
Expand Down Expand Up @@ -695,6 +695,55 @@ impl Iterator for BlockRangeInclusiveIter {
}
}

/// Errors that can occur in the handler implementation
#[derive(Debug, thiserror::Error)]
pub enum EthFilterError {
/// Filter not found.
#[error("filter not found")]
FilterNotFound(FilterId),
/// Invalid block range.
#[error("invalid block range params")]
InvalidBlockRangeParams,
/// Query scope is too broad.
#[error("query exceeds max block range {0}")]
QueryExceedsMaxBlocks(u64),
/// Query result is too large.
#[error("query exceeds max results {0}")]
QueryExceedsMaxResults(usize),
/// Error serving request in `eth_` namespace.
#[error(transparent)]
EthAPIError(#[from] EthApiError),
/// Error thrown when a spawned task failed to deliver a response.
#[error("internal filter error")]
InternalError,
}

impl From<EthFilterError> for jsonrpsee::types::error::ErrorObject<'static> {
fn from(err: EthFilterError) -> Self {
match err {
EthFilterError::FilterNotFound(_) => rpc_error_with_code(
jsonrpsee::types::error::INVALID_PARAMS_CODE,
"filter not found",
),
err @ EthFilterError::InternalError => {
rpc_error_with_code(jsonrpsee::types::error::INTERNAL_ERROR_CODE, err.to_string())
}
EthFilterError::EthAPIError(err) => err.into(),
err @ (EthFilterError::InvalidBlockRangeParams |
EthFilterError::QueryExceedsMaxBlocks(_) |
EthFilterError::QueryExceedsMaxResults(_)) => {
rpc_error_with_code(jsonrpsee::types::error::INVALID_PARAMS_CODE, err.to_string())
}
}
}
}

impl From<ProviderError> for EthFilterError {
fn from(err: ProviderError) -> Self {
Self::EthAPIError(err.into())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit b67f004

Please sign in to comment.