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

chore(rpc-types): remove duplicate Index definition in rpc-types-anvil in favor of the one in rpc-types-eth #943

Merged
merged 1 commit into from
Jun 19, 2024
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
121 changes: 4 additions & 117 deletions crates/rpc-types-anvil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,78 +164,12 @@ impl Default for MineOptions {
}
}

/// A hex encoded or decimal index
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Index(usize);

impl From<Index> for usize {
fn from(idx: Index) -> Self {
idx.0
}
}

impl<'a> serde::Deserialize<'a> for Index {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'a>,
{
use std::fmt;

struct IndexVisitor;

impl<'a> serde::de::Visitor<'a> for IndexVisitor {
type Value = Index;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "hex-encoded or decimal index")
}

fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Index(value as usize))
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
value.strip_prefix("0x").map_or_else(
|| {
value.parse::<usize>().map(Index).map_err(|e| {
serde::de::Error::custom(format!("Failed to parse numeric index: {e}"))
})
},
|val| {
usize::from_str_radix(val, 16).map(Index).map_err(|e| {
serde::de::Error::custom(format!(
"Failed to parse hex encoded index value: {e}"
))
})
},
)
}

fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
self.visit_str(value.as_ref())
}
}

deserializer.deserialize_any(IndexVisitor)
}
}

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

#[test]
fn test_forking_deserialization() {
fn test_serde_forking_deserialization() {
// Test full forking object
let json_data = r#"{"forking": {"jsonRpcUrl": "https://ethereumpublicnode.com","blockNumber": "18441649"}}"#;
let forking: Forking = serde_json::from_str(json_data).unwrap();
Expand Down Expand Up @@ -266,54 +200,7 @@ mod tests {
}

#[test]
fn test_index_deserialization() {
// Test decimal index
let json_data = json!(42);
let index: Index =
serde_json::from_value(json_data).expect("Failed to deserialize decimal index");
assert_eq!(index, Index(42));

// Test hex index
let json_data = json!("0x2A");
let index: Index =
serde_json::from_value(json_data).expect("Failed to deserialize hex index");
assert_eq!(index, Index(42));

// Test invalid hex index
let json_data = json!("0xGHI");
let result: Result<Index, _> = serde_json::from_value(json_data);
assert!(result.is_err());

// Test invalid decimal index
let json_data = json!("abc");
let result: Result<Index, _> = serde_json::from_value(json_data);
assert!(result.is_err());

// Test string decimal index
let json_data = json!("123");
let index: Index =
serde_json::from_value(json_data).expect("Failed to deserialize string decimal index");
assert_eq!(index, Index(123));

// Test invalid numeric string
let json_data = json!("123abc");
let result: Result<Index, _> = serde_json::from_value(json_data);
assert!(result.is_err());

// Test negative index
let json_data = json!(-1);
let result: Result<Index, _> = serde_json::from_value(json_data);
assert!(result.is_err());

// Test large index
let json_data = json!(u64::MAX);
let index: Index =
serde_json::from_value(json_data).expect("Failed to deserialize large index");
assert_eq!(index, Index(u64::MAX as usize));
}

#[test]
fn test_deserialize_options_with_values() {
fn test_serde_deserialize_options_with_values() {
let data = r#"{"timestamp": 1620000000, "blocks": 10}"#;
let deserialized: MineOptions = serde_json::from_str(data).expect("Deserialization failed");
assert_eq!(
Expand All @@ -330,7 +217,7 @@ mod tests {
}

#[test]
fn test_deserialize_options_with_timestamp() {
fn test_serde_deserialize_options_with_timestamp() {
let data = r#"{"timestamp":"1620000000"}"#;
let deserialized: MineOptions = serde_json::from_str(data).expect("Deserialization failed");
assert_eq!(
Expand All @@ -347,7 +234,7 @@ mod tests {
}

#[test]
fn test_deserialize_timestamp() {
fn test_serde_deserialize_timestamp() {
let data = r#""1620000000""#;
let deserialized: MineOptions = serde_json::from_str(data).expect("Deserialization failed");
assert_eq!(deserialized, MineOptions::Timestamp(Some(1620000000)));
Expand Down
48 changes: 48 additions & 0 deletions crates/rpc-types-eth/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl<'a> Deserialize<'a> for Index {
mod tests {
use super::*;
use rand::{thread_rng, Rng};
use serde_json::json;

#[test]
fn test_serde_index_rand() {
Expand All @@ -103,4 +104,51 @@ mod tests {
assert_eq!(index, de);
}
}

#[test]
fn test_serde_index_deserialization() {
// Test decimal index
let json_data = json!(42);
let index: Index =
serde_json::from_value(json_data).expect("Failed to deserialize decimal index");
assert_eq!(index, Index::from(42));

// Test hex index
let json_data = json!("0x2A");
let index: Index =
serde_json::from_value(json_data).expect("Failed to deserialize hex index");
assert_eq!(index, Index::from(42));

// Test invalid hex index
let json_data = json!("0xGHI");
let result: Result<Index, _> = serde_json::from_value(json_data);
assert!(result.is_err());

// Test invalid decimal index
let json_data = json!("abc");
let result: Result<Index, _> = serde_json::from_value(json_data);
assert!(result.is_err());

// Test string decimal index
let json_data = json!("123");
let index: Index =
serde_json::from_value(json_data).expect("Failed to deserialize string decimal index");
assert_eq!(index, Index::from(123));

// Test invalid numeric string
let json_data = json!("123abc");
let result: Result<Index, _> = serde_json::from_value(json_data);
assert!(result.is_err());

// Test negative index
let json_data = json!(-1);
let result: Result<Index, _> = serde_json::from_value(json_data);
assert!(result.is_err());

// Test large index
let json_data = json!(u64::MAX);
let index: Index =
serde_json::from_value(json_data).expect("Failed to deserialize large index");
assert_eq!(index, Index::from(u64::MAX as usize));
}
}