fix: correctly serialize TxPoolInspectSummary (#1305)

* fix: correctly serialize TxPoolInspectSummary

previously we'd use the default serde derive, now we serialize
it like geth does

* chore: add back removed Eq derive
This commit is contained in:
Georgios Konstantopoulos 2022-05-23 14:15:29 -07:00 committed by GitHub
parent f874680404
commit dc107ce892
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 1 deletions

View File

@ -7,7 +7,7 @@ use serde::{
use std::{collections::BTreeMap, fmt, str::FromStr}; use std::{collections::BTreeMap, fmt, str::FromStr};
/// Transaction summary as found in the Txpool Inspection property. /// Transaction summary as found in the Txpool Inspection property.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct TxpoolInspectSummary { pub struct TxpoolInspectSummary {
/// Recipient (None when contract creation) /// Recipient (None when contract creation)
pub to: Option<Address>, pub to: Option<Address>,
@ -83,6 +83,24 @@ impl<'de> Deserialize<'de> for TxpoolInspectSummary {
} }
} }
/// Implement the `Serialize` trait for `TxpoolInspectSummary` struct so that the
/// format matches the one from geth.
impl Serialize for TxpoolInspectSummary {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let formatted = format!(
"{:?}: {} wei + {} gas × {} wei",
self.to.unwrap_or_default(),
self.value,
self.gas,
self.gas_price
);
serializer.serialize_str(&formatted)
}
}
/// Transaction Pool Content /// Transaction Pool Content
/// ///
/// The content inspection property can be queried to list the exact details of all /// The content inspection property can be queried to list the exact details of all
@ -274,6 +292,10 @@ mod tests {
}"#; }"#;
let deserialized: TxpoolInspect = serde_json::from_str(txpool_inspect_json).unwrap(); let deserialized: TxpoolInspect = serde_json::from_str(txpool_inspect_json).unwrap();
assert_eq!(deserialized, expected_txpool_inspect()); assert_eq!(deserialized, expected_txpool_inspect());
let serialized = serde_json::to_string(&deserialized).unwrap();
let deserialized2: TxpoolInspect = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized2, deserialized);
} }
#[test] #[test]