From bebc2b433ff7200422014e68bf008ea4b5ade32b Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 12 Jul 2023 20:54:50 +0200 Subject: [PATCH] fix: serialize selfdestruct as suicide (#3736) --- crates/rpc/rpc-types/src/eth/trace/parity.rs | 64 ++++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/crates/rpc/rpc-types/src/eth/trace/parity.rs b/crates/rpc/rpc-types/src/eth/trace/parity.rs index 3c4b0bc57c..1f9e73f5fe 100644 --- a/crates/rpc/rpc-types/src/eth/trace/parity.rs +++ b/crates/rpc/rpc-types/src/eth/trace/parity.rs @@ -99,11 +99,38 @@ impl DerefMut for StateDiff { pub enum Action { Call(CallAction), Create(CreateAction), + /// Parity style traces never renamed suicide to selfdestruct: + /// + /// For compatibility reasons, this is serialized as `suicide`: + #[serde(rename = "suicide", alias = "selfdestruct")] Selfdestruct(SelfdestructAction), Reward(RewardAction), } +impl Action { + /// Returns true if this is a call action + pub fn is_call(&self) -> bool { + matches!(self, Action::Call(_)) + } + + /// Returns true if this is a create action + pub fn is_create(&self) -> bool { + matches!(self, Action::Call(_)) + } + + /// Returns true if this is a selfdestruct action + pub fn is_selfdestruct(&self) -> bool { + matches!(self, Action::Selfdestruct(_)) + } + /// Returns true if this is a reward action + pub fn is_reward(&self) -> bool { + matches!(self, Action::Reward(_)) + } +} + /// An external action type. +/// +/// Used as enum identifier for [Action] #[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] #[serde(rename_all = "lowercase")] pub enum ActionType { @@ -112,6 +139,7 @@ pub enum ActionType { /// Contract creation. Create, /// Contract suicide/selfdestruct. + #[serde(rename = "suicide", alias = "selfdestruct")] Selfdestruct, /// A block reward. Reward, @@ -318,8 +346,6 @@ mod tests { "to": "0x160f5f00288e9e1cc8655b327e081566e580a71d", "value": "0x244b" }, - "blockHash": "0xbca9ee244882bd00a19737a66f24002a4562a949c4d5ebd03c32e04111cff536", - "blockNumber": 17600209, "error": "Reverted", "result": { "gasUsed": "0x9daf", @@ -327,11 +353,41 @@ mod tests { }, "subtraces": 3, "traceAddress": [], - "transactionHash": "0x0e48a8d4419efaa2d3a9b8f625a1c559a4179fd19ddd10c02842965f3a7e7b63", - "transactionPosition": 0, "type": "call" }"#; let val = serde_json::from_str::(s).unwrap(); serde_json::to_value(val).unwrap(); } + + #[test] + fn test_selfdestruct_suicide() { + let input = r#"{ + "action": { + "address": "0x66e29f0b6b1b07071f2fde4345d512386cb66f5f", + "refundAddress": "0x66e29f0b6b1b07071f2fde4345d512386cb66f5f", + "balance": "0x244b" + }, + "error": "Reverted", + "result": { + "gasUsed": "0x9daf", + "output": "0x000000000000000000000000000000000000000000000000011c37937e080000" + }, + "subtraces": 3, + "traceAddress": [], + "type": "suicide" + }"#; + let val = serde_json::from_str::(input).unwrap(); + assert!(val.action.is_selfdestruct()); + + let json = serde_json::to_value(val.clone()).unwrap(); + let expect = serde_json::from_str::(input).unwrap(); + similar_asserts::assert_eq!(json, expect); + let s = serde_json::to_string(&val).unwrap(); + let json = serde_json::from_str::(&s).unwrap(); + similar_asserts::assert_eq!(json, expect); + + let input = input.replace("suicide", "selfdestruct"); + let val = serde_json::from_str::(&input).unwrap(); + assert!(val.action.is_selfdestruct()); + } }