From 04290e6ce016b9828f9fc0f6693dde6922da4a91 Mon Sep 17 00:00:00 2001 From: Rakan Alhneiti Date: Thu, 4 Apr 2024 05:32:11 +0300 Subject: [PATCH] Implement deserializer --- crates/sequencer-client/src/lib.rs | 3 +- .../rollup-interface/src/node/rpc/mod.rs | 57 +++++++++++++++++-- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/crates/sequencer-client/src/lib.rs b/crates/sequencer-client/src/lib.rs index 00dba52a6e..526837f891 100644 --- a/crates/sequencer-client/src/lib.rs +++ b/crates/sequencer-client/src/lib.rs @@ -5,6 +5,7 @@ use jsonrpsee::http_client::{HttpClient, HttpClientBuilder}; use jsonrpsee::rpc_params; use reth_primitives::B256; use serde::Deserialize; +use sov_rollup_interface::rpc::utils::tx_hex; use sov_rollup_interface::soft_confirmation::SignedSoftConfirmationBatch; /// Configuration for SequencerClient. @@ -76,7 +77,7 @@ pub struct GetSoftBatchResponse { pub da_slot_hash: [u8; 32], #[serde(with = "hex::serde")] pub da_slot_txs_commitment: [u8; 32], - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none", with = "tx_hex")] pub txs: Option>>, #[serde(with = "hex::serde")] pub pre_state_root: Vec, diff --git a/crates/sovereign-sdk/rollup-interface/src/node/rpc/mod.rs b/crates/sovereign-sdk/rollup-interface/src/node/rpc/mod.rs index 580f0d1993..774b4af4b6 100644 --- a/crates/sovereign-sdk/rollup-interface/src/node/rpc/mod.rs +++ b/crates/sovereign-sdk/rollup-interface/src/node/rpc/mod.rs @@ -178,10 +178,7 @@ pub struct SoftBatchResponse { #[serde(with = "hex::serde")] pub hash: [u8; 32], /// The transactions in this batch. - #[serde( - skip_serializing_if = "Option::is_none", - serialize_with = "utils::tx_hex::serialize" - )] + #[serde(skip_serializing_if = "Option::is_none", with = "utils::tx_hex")] pub txs: Option>>, /// Pre-state root of the soft batch. #[serde(with = "hex::serde")] @@ -486,9 +483,13 @@ pub mod utils { /// the actual vector of bytes by returning the hexidecimal representation of those bytes /// for each item. pub mod tx_hex { - use hex::ToHex; + use core::fmt; + use core::marker::PhantomData; + + use hex::{FromHex, ToHex}; + use serde::de::{self, Error, SeqAccess, Visitor}; use serde::ser::SerializeSeq; - use serde::Serializer; + use serde::{Deserialize, Deserializer, Serializer}; use crate::maybestd::format; use crate::maybestd::string::String; @@ -511,6 +512,50 @@ pub mod utils { } serializer.serialize_none() } + + /// Deserializes a vector of hex string into raw bytes. + pub fn deserialize<'de, D>(deserializer: D) -> Result>>, D::Error> + where + D: Deserializer<'de>, + { + struct HexStrVisitor(PhantomData>>>); + + impl<'de> Visitor<'de> for HexStrVisitor { + type Value = Option>>; + + fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "a hex encoded string") + } + + fn visit_str(self, data: &str) -> Result + where + E: Error, + { + let data = data.trim_start_matches("0x"); + FromHex::from_hex(data) + .map_err(Error::custom) + .map(|res| Some(vec![res])) + } + + fn visit_seq(self, mut seq: A) -> Result + where + A: SeqAccess<'de>, + { + let mut result = vec![]; + while let Some(item) = seq.next_element::()? { + let item = item.trim_start_matches("0x"); + result.push( + FromHex::from_hex(&item) + .map_err(|_| de::Error::custom("Could not convert from hex"))?, + ); + } + + Ok(Some(result)) + } + } + + deserializer.deserialize_seq(HexStrVisitor(PhantomData)) + } } }