fix: serialize index as hex string (#1687)

This commit is contained in:
Matthias Seitz
2023-03-09 16:25:34 +01:00
committed by GitHub
parent 1e4ab0e1ac
commit e913a536f0
3 changed files with 20 additions and 1 deletions

View File

@@ -27,4 +27,5 @@ jsonrpsee-types = { version = "0.16" }
lru = "0.9"
[dev-dependencies]
rand = "0.8"
reth-interfaces = { path = "../../interfaces", features = ["test-utils"] }

View File

@@ -19,7 +19,7 @@ impl Serialize for Index {
where
S: Serializer,
{
serializer.serialize_str(&format!("{:x}", self.0))
serializer.serialize_str(&format!("0x{:x}", self.0))
}
}
@@ -71,3 +71,20 @@ impl<'a> Deserialize<'a> for Index {
deserializer.deserialize_any(IndexVisitor)
}
}
#[cfg(test)]
mod tests {
use super::*;
use rand::{thread_rng, Rng};
#[test]
fn test_serde_index_rand() {
let mut rng = thread_rng();
for _ in 0..100 {
let index = Index(rng.gen());
let val = serde_json::to_string(&index).unwrap();
let de: Index = serde_json::from_str(&val).unwrap();
assert_eq!(index, de);
}
}
}