Skip to content

Commit

Permalink
feat(rpc-types-trace): always serialize result if no error (#1258)
Browse files Browse the repository at this point in the history
* feat(rpc-types-trace): error or result

Signed-off-by: jsvisa <delweng@gmail.com>

* add a test case

Signed-off-by: jsvisa <delweng@gmail.com>

* feat: error + result

Signed-off-by: jsvisa <delweng@gmail.com>

* touchup

---------

Signed-off-by: jsvisa <delweng@gmail.com>
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
jsvisa and mattsse authored Sep 9, 2024
1 parent 4492c25 commit 7b487e4
Showing 1 changed file with 45 additions and 8 deletions.
53 changes: 45 additions & 8 deletions crates/rpc-types-trace/src/parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ pub struct TransactionTrace {
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
/// Output of the trace, can be CALL or CREATE
#[serde(default)]
pub result: Option<TraceOutput>,
/// How many subtraces this trace has.
pub subtraces: usize,
Expand Down Expand Up @@ -498,15 +499,10 @@ impl Serialize for LocalizedTransactionTrace {
if let Some(error) = error {
s.serialize_field("error", error)?;
}

match result {
Some(TraceOutput::Call(call)) => {
s.serialize_field("result", call)?;
}
Some(TraceOutput::Create(create)) => {
s.serialize_field("result", create)?;
}
None => {}
Some(TraceOutput::Call(call)) => s.serialize_field("result", call)?,
Some(TraceOutput::Create(create)) => s.serialize_field("result", create)?,
None => s.serialize_field("result", &None::<()>)?,
}

s.serialize_field("subtraces", &subtraces)?;
Expand Down Expand Up @@ -822,6 +818,47 @@ mod tests {
let serialized = serde_json::to_string_pretty(&trace).unwrap();
similar_asserts::assert_eq!(serialized, reference_data);
}

#[test]
fn test_transaction_trace_null_result() {
let trace = TransactionTrace {
action: Action::Call(CallAction {
from: Address::from_str("0x1234567890123456789012345678901234567890").unwrap(),
call_type: CallType::Call,
gas: 100000,
input: Bytes::from_str("0x1234").unwrap(),
to: Address::from_str("0x0987654321098765432109876543210987654321").unwrap(),
value: U256::from(0),
}),
..Default::default()
};

let serialized = serde_json::to_string(&trace).unwrap();
let deserialized: serde_json::Value = serde_json::from_str(&serialized).unwrap();

assert_eq!(deserialized["result"], serde_json::Value::Null);
assert!(deserialized.as_object().unwrap().contains_key("result"));
assert!(!deserialized.as_object().unwrap().contains_key("error"));

let deserialized_trace: TransactionTrace = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized_trace.result, None);
}

#[test]
fn test_transaction_trace_error_result() {
let trace = TransactionTrace { error: Some("Reverted".to_string()), ..Default::default() };

let serialized = serde_json::to_string(&trace).unwrap();
let deserialized: serde_json::Value = serde_json::from_str(&serialized).unwrap();

assert_eq!(deserialized["result"], serde_json::Value::Null);
assert!(deserialized.as_object().unwrap().contains_key("result"));
assert!(deserialized.as_object().unwrap().contains_key("error"));

let deserialized_trace: TransactionTrace = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized_trace.result, None);
}

#[test]
fn test_nethermind_trace_result_null_output_value() {
let reference_data = r#"{
Expand Down

0 comments on commit 7b487e4

Please sign in to comment.