Skip to content

Commit 8fd5e1f

Browse files
committed
Move validation to main.rs
1 parent ffd56b4 commit 8fd5e1f

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/candid_rpc/mod.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use candid::Nat;
1313
use canhttp::http::json::JsonRpcRequest;
1414
use canhttp::multi::{ReductionError, Timestamp};
1515
use ethers_core::{types::Transaction, utils::rlp};
16-
use evm_rpc_types::{Hex, Hex32, MultiRpcResult, Nat256, RpcError, RpcResult, ValidationError};
16+
use evm_rpc_types::{
17+
BlockTag, GetLogsArgs, Hex, Hex32, MultiRpcResult, Nat256, RpcError, RpcResult, ValidationError,
18+
};
1719

1820
fn process_result<T>(
1921
method: impl Into<MetricRpcMethod> + Clone,
@@ -71,23 +73,6 @@ impl CandidRpcClient {
7173
max_block_range: u32,
7274
) -> MultiRpcResult<Vec<evm_rpc_types::LogEntry>> {
7375
use crate::candid_rpc::cketh_conversion::{from_log_entries, into_get_logs_param};
74-
75-
if let (
76-
Some(evm_rpc_types::BlockTag::Number(from)),
77-
Some(evm_rpc_types::BlockTag::Number(to)),
78-
) = (&args.from_block, &args.to_block)
79-
{
80-
let from = Nat::from(from.clone());
81-
let to = Nat::from(to.clone());
82-
let block_count = if to > from { to - from } else { from - to };
83-
if block_count > max_block_range {
84-
return MultiRpcResult::Consistent(Err(ValidationError::Custom(format!(
85-
"Requested {} blocks; limited to {} when specifying a start and end block",
86-
block_count, max_block_range
87-
))
88-
.into()));
89-
}
90-
}
9176
process_result(
9277
RpcMethod::EthGetLogs,
9378
self.client.eth_get_logs(into_get_logs_param(args)).await,
@@ -97,7 +82,7 @@ impl CandidRpcClient {
9782

9883
pub async fn eth_get_block_by_number(
9984
&self,
100-
block: evm_rpc_types::BlockTag,
85+
block: BlockTag,
10186
) -> MultiRpcResult<evm_rpc_types::Block> {
10287
use crate::candid_rpc::cketh_conversion::{from_block, into_block_spec};
10388
process_result(
@@ -208,3 +193,21 @@ fn get_transaction_hash(raw_signed_transaction_hex: &Hex) -> Option<Hex32> {
208193
let transaction: Transaction = rlp::decode(raw_signed_transaction_hex.as_ref()).ok()?;
209194
Some(Hex32::from(transaction.hash.0))
210195
}
196+
197+
pub fn validate_get_logs_block_range(args: &GetLogsArgs, max_block_range: u32) -> RpcResult<()> {
198+
if let (Some(BlockTag::Number(from)), Some(BlockTag::Number(to))) =
199+
(&args.from_block, &args.to_block)
200+
{
201+
let from = Nat::from(from.clone());
202+
let to = Nat::from(to.clone());
203+
let block_count = if to > from { to - from } else { from - to };
204+
if block_count > max_block_range {
205+
return Err(ValidationError::Custom(format!(
206+
"Requested {} blocks; limited to {} when specifying a start and end block",
207+
block_count, max_block_range
208+
))
209+
.into());
210+
}
211+
}
212+
Ok(())
213+
}

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use canhttp::{cycles::CyclesChargingPolicy, multi::Timestamp};
22
use canlog::{log, Log, Sort};
33
use evm_rpc::{
4-
candid_rpc::CandidRpcClient,
4+
candid_rpc::{validate_get_logs_block_range, CandidRpcClient},
55
http::{
66
charging_policy_with_collateral, json_rpc_request, json_rpc_request_arg,
77
service_request_builder, transform_http_request,
@@ -43,6 +43,9 @@ pub async fn eth_get_logs(
4343
) -> MultiRpcResult<Vec<evm_rpc_types::LogEntry>> {
4444
let config = config.unwrap_or_default();
4545
let max_block_range = config.max_block_range_or_default();
46+
if let Err(err) = validate_get_logs_block_range(&args, max_block_range) {
47+
return MultiRpcResult::Consistent(Err(err));
48+
}
4649
match CandidRpcClient::new(source, Some(RpcConfig::from(config)), now()) {
4750
Ok(source) => source.eth_get_logs(args, max_block_range).await,
4851
Err(err) => Err(err).into(),

0 commit comments

Comments
 (0)