From f4babdedd5c1ef9300458c3aed005cd88b48739b Mon Sep 17 00:00:00 2001 From: Diva M Date: Mon, 28 Nov 2022 11:06:12 -0500 Subject: [PATCH] more hacks --- .../src/engine_api/json_structures.rs | 64 ++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/beacon_node/execution_layer/src/engine_api/json_structures.rs b/beacon_node/execution_layer/src/engine_api/json_structures.rs index afce579ca..3a4f82430 100644 --- a/beacon_node/execution_layer/src/engine_api/json_structures.rs +++ b/beacon_node/execution_layer/src/engine_api/json_structures.rs @@ -97,7 +97,8 @@ pub struct JsonExecutionPayload { #[superstruct(only(V2))] #[serde(skip_serializing_if = "Option::is_none")] #[serde(default)] - #[serde(with = "eth2_serde_utils::u256_hex_be_opt")] + // #[serde(with = "eth2_serde_utils::u256_hex_be_opt")] + #[serde(with = "remove_this")] pub excess_data_gas: Option, pub block_hash: ExecutionBlockHash, #[serde(with = "ssz_types::serde_utils::list_of_hex_var_list")] @@ -109,6 +110,67 @@ pub struct JsonExecutionPayload { pub withdrawals: Option>, } +pub mod remove_this { + use super::Uint256 as U256; + + use serde::de::Visitor; + use serde::{de, Deserializer, Serialize, Serializer}; + use std::fmt; + use std::str::FromStr; + + pub fn serialize(num: &Option, serializer: S) -> Result + where + S: Serializer, + { + num.serialize(serializer) + } + + pub struct U256Visitor; + + impl<'de> Visitor<'de> for U256Visitor { + type Value = String; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("a well formatted hex string") + } + + fn visit_str(self, value: &str) -> Result + where + E: de::Error, + { + if !value.starts_with("0x") { + return Err(de::Error::custom("must start with 0x")); + } + let stripped = &value[2..]; + if stripped.is_empty() { + Err(de::Error::custom(format!( + "quantity cannot be {:?}", + stripped + ))) + } else if stripped == "0" { + Ok(value.to_string()) + } else if stripped.starts_with('0') { + Err(de::Error::custom("cannot have leading zero")) + } else { + Ok(value.to_string()) + } + } + } + + pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let decoded = deserializer.deserialize_string(U256Visitor)?; + + Some( + U256::from_str(&decoded) + .map_err(|e| de::Error::custom(format!("Invalid U256 string: {}", e))), + ) + .transpose() + } +} + impl JsonExecutionPayload { pub fn try_into_execution_payload( self,