Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rpc): make trace filter req field hex or decimal #3772

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions crates/primitives/src/serde_helper/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,33 @@ pub mod u64_hex_or_decimal {
U64HexOrNumber::from(*value).serialize(s)
}
}

/// serde functions for handling primitive optional `u64` as [U64](crate::U64)
pub mod u64_hex_or_decimal_opt {
use crate::serde_helper::num::U64HexOrNumber;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Deserializes an `u64` accepting a hex quantity string with optional 0x prefix or
/// a number
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<u64>, D::Error>
where
D: Deserializer<'de>,
{
match Option::<U64HexOrNumber>::deserialize(deserializer)? {
Some(val) => Ok(Some(val.into())),
None => Ok(None),
}
}

/// Serializes u64 as hex string
pub fn serialize<S: Serializer>(value: &Option<u64>, s: S) -> Result<S::Ok, S::Error> {
match value {
Some(val) => U64HexOrNumber::from(*val).serialize(s),
None => s.serialize_none(),
}
}
}

/// Deserializes the input into an `Option<U256>`, using [`from_int_or_hex`] to deserialize the
/// inner value.
pub fn from_int_or_hex_opt<'de, D>(deserializer: D) -> Result<Option<U256>, D::Error>
Expand Down
25 changes: 20 additions & 5 deletions crates/rpc/rpc-types/src/eth/trace/filter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! `trace_filter` types and support
use reth_primitives::{Address, BlockNumber};
use reth_primitives::{serde_helper::num::u64_hex_or_decimal_opt, Address};
use serde::{Deserialize, Serialize};

/// Trace filter.
Expand All @@ -8,15 +8,30 @@ use serde::{Deserialize, Serialize};
#[serde(rename_all = "camelCase")]
pub struct TraceFilter {
/// From block
pub from_block: Option<BlockNumber>,
#[serde(with = "u64_hex_or_decimal_opt")]
pub from_block: Option<u64>,
/// To block
pub to_block: Option<BlockNumber>,
#[serde(with = "u64_hex_or_decimal_opt")]
pub to_block: Option<u64>,
/// From address
pub from_address: Option<Vec<Address>>,
/// To address
pub to_address: Option<Vec<Address>>,
/// Output offset
pub after: Option<usize>,
pub after: Option<u64>,
/// Output amount
pub count: Option<usize>,
pub count: Option<u64>,
}

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

#[test]
fn test_parse_filter() {
let s = r#"{"fromBlock": "0x3","toBlock": "0x5"}"#;
let filter: TraceFilter = serde_json::from_str(s).unwrap();
assert_eq!(filter.from_block, Some(3));
assert_eq!(filter.to_block, Some(5));
}
}