Skip to content

Commit

Permalink
feat(rpc): remove SerializationForVersion blanket implementation f…
Browse files Browse the repository at this point in the history
…or serde
  • Loading branch information
t00ts committed Jan 7, 2025
1 parent f40f3a2 commit dbca79f
Show file tree
Hide file tree
Showing 45 changed files with 1,221 additions and 468 deletions.
8 changes: 8 additions & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ macros::i64_backed_u64::serdes!(TransactionIndex);
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize, Default, Dummy)]
pub struct GasPrice(pub u128);

/// A hex representation of a [GasPrice].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize, Default, Dummy)]
pub struct GasPriceHex(pub GasPrice);

/// Starknet resource bound: amount.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize, Default, Dummy)]
pub struct ResourceAmount(pub u64);
Expand All @@ -178,6 +182,10 @@ pub struct ResourceAmount(pub u64);
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize, Default, Dummy)]
pub struct Tip(pub u64);

// A hex representation of a [Tip].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default, Dummy)]
pub struct TipHex(pub Tip);

/// Starknet resource bound: price per unit.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize, Default, Dummy)]
pub struct ResourcePricePerUnit(pub u128);
Expand Down
1 change: 1 addition & 0 deletions crates/common/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ macro_rules! felt {
($hex:expr) => {{
// This forces const evaluation of the macro call. Without this the invocation
// will only be evaluated at runtime.
use ::pathfinder_crypto;
const CONST_FELT: pathfinder_crypto::Felt =
match pathfinder_crypto::Felt::from_hex_str($hex) {
Ok(f) => f,
Expand Down
4 changes: 2 additions & 2 deletions crates/executor/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use pathfinder_crypto::Felt;

use super::felt::IntoFelt;

#[derive(Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FeeEstimate {
pub l1_gas_consumed: primitive_types::U256,
pub l1_gas_price: primitive_types::U256,
Expand Down Expand Up @@ -85,7 +85,7 @@ impl FeeEstimate {
}
}

#[derive(Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PriceUnit {
Wei,
Fri,
Expand Down
55 changes: 48 additions & 7 deletions crates/rpc/src/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ pub trait SerializeForVersion {
fn serialize(&self, serializer: Serializer) -> Result<Ok, Error>;
}

// This blanket implementation should be removed once all existing DTOs have
// been migrated.
impl<T> SerializeForVersion for T
where
T: serde::Serialize,
{
impl SerializeForVersion for serde_json::Value {
fn serialize(&self, _serializer: Serializer) -> Result<Ok, Error> {
self.serialize(BaseSerializer {})
Ok(self.clone())
}
}

impl SerializeForVersion for &serde_json::Value {
fn serialize(&self, _serializer: Serializer) -> Result<Ok, Error> {
Ok((*self).clone())
}
}

Expand All @@ -63,16 +64,41 @@ impl Serializer {
value.serialize(self)
}

pub fn serialize_unit(self) -> Result<Ok, Error> {
use serde::Serializer;
BaseSerializer {}.serialize_unit()
}

pub fn serialize_str(self, value: &str) -> Result<Ok, Error> {
use serde::Serializer;
BaseSerializer {}.serialize_str(value)
}

pub fn serialize_i32(self, value: i32) -> Result<Ok, Error> {
use serde::Serializer;
BaseSerializer {}.serialize_i32(value)
}

pub fn serialize_i64(self, value: i64) -> Result<Ok, Error> {
use serde::Serializer;
BaseSerializer {}.serialize_i64(value)
}

pub fn serialize_u32(self, value: u32) -> Result<Ok, Error> {
use serde::Serializer;
BaseSerializer {}.serialize_u32(value)
}

pub fn serialize_u64(self, value: u64) -> Result<Ok, Error> {
use serde::Serializer;
BaseSerializer {}.serialize_u64(value)
}

pub fn serialize_u128(self, value: u128) -> Result<Ok, Error> {
use serde::Serializer;
BaseSerializer {}.serialize_u128(value)
}

pub fn serialize_bool(self, value: bool) -> Result<Ok, Error> {
use serde::Serializer;
BaseSerializer {}.serialize_bool(value)
Expand Down Expand Up @@ -136,6 +162,21 @@ impl SerializeStruct {
Ok(())
}

/// Serializes optional value as null if its [`None`].
pub fn serialize_optional_with_null(
&mut self,
key: &'static str,
value: Option<impl SerializeForVersion>,
) -> Result<(), Error> {
if let Some(value) = value {
self.serialize_field(key, &value)?;
} else {
self.serialize_field(key, &serde_json::Value::Null)?;
}

Ok(())
}

pub fn flatten(&mut self, value: &dyn SerializeForVersion) -> Result<(), Error> {
let value = value.serialize(Serializer::new(self.version))?;

Expand Down
157 changes: 79 additions & 78 deletions crates/rpc/src/dto/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,11 @@ impl SerializeForVersion for types::CairoContractClass {
&self,
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
impl SerializeForVersion for types::ContractEntryPoints {
fn serialize(
&self,
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
let mut serializer = serializer.serialize_struct()?;

serializer.serialize_iter(
"CONSTRUCTOR",
self.constructor.len(),
&mut self.constructor.iter(),
)?;

serializer.serialize_iter(
"EXTERNAL",
self.external.len(),
&mut self.external.iter(),
)?;

serializer.serialize_iter(
"L1_HANDLER",
self.l1_handler.len(),
&mut self.l1_handler.iter(),
)?;

serializer.end()
}
}

let mut serializer = serializer.serialize_struct()?;

serializer.serialize_field("program", &self.program)?;
serializer.serialize_field("entry_points_by_type", &self.entry_points_by_type)?;
serializer.serialize_optional("abi", self.abi)?;
serializer.serialize_optional_with_null("abi", self.abi.clone())?;

serializer.end()
}
Expand All @@ -53,35 +24,6 @@ impl SerializeForVersion for types::SierraContractClass {
&self,
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
impl SerializeForVersion for types::SierraEntryPoints {
fn serialize(
&self,
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
let mut serializer = serializer.serialize_struct()?;

serializer.serialize_iter(
"CONSTRUCTOR",
self.constructor.len(),
&mut self.constructor.iter(),
)?;

serializer.serialize_iter(
"EXTERNAL",
self.external.len(),
&mut self.external.iter(),
)?;

serializer.serialize_iter(
"L1_HANDLER",
self.l1_handler.len(),
&mut self.l1_handler.iter(),
)?;

serializer.end()
}
}

let mut serializer = serializer.serialize_struct()?;

serializer.serialize_iter(
Expand All @@ -95,13 +37,63 @@ impl SerializeForVersion for types::SierraContractClass {

// ABI is optional, so skip if its empty.
let abi = (!self.abi.is_empty()).then_some(&self.abi);
serializer.serialize_optional("abi", abi)?;
serializer.serialize_optional_with_null("abi", abi)?;

serializer.end()
}
}

impl SerializeForVersion for types::SierraEntryPoints {
fn serialize(
&self,
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
let mut serializer = serializer.serialize_struct()?;

serializer.serialize_iter(
"CONSTRUCTOR",
self.constructor.len(),
&mut self.constructor.iter(),
)?;

serializer.serialize_iter("EXTERNAL", self.external.len(), &mut self.external.iter())?;

serializer.serialize_iter(
"L1_HANDLER",
self.l1_handler.len(),
&mut self.l1_handler.iter(),
)?;

serializer.end()
}
}

impl SerializeForVersion for types::ContractEntryPoints {
fn serialize(
&self,
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
let mut serializer = serializer.serialize_struct()?;

serializer.serialize_iter(
"CONSTRUCTOR",
self.constructor.len(),
&mut self.constructor.iter(),
)?;

serializer.serialize_iter("EXTERNAL", self.external.len(), &mut self.external.iter())?;

serializer.serialize_iter(
"L1_HANDLER",
self.l1_handler.len(),
&mut self.l1_handler.iter(),
)?;

serializer.end()
}
}

impl SerializeForVersion for types::ContractEntryPoint {
impl SerializeForVersion for &types::ContractEntryPoint {
fn serialize(
&self,
serializer: crate::dto::Serializer,
Expand All @@ -115,7 +107,7 @@ impl SerializeForVersion for types::ContractEntryPoint {
}
}

impl SerializeForVersion for types::SierraEntryPoint {
impl SerializeForVersion for &types::SierraEntryPoint {
fn serialize(
&self,
serializer: crate::dto::Serializer,
Expand All @@ -138,7 +130,16 @@ impl SerializeForVersion for [types::ContractAbiEntry] {
}
}

impl SerializeForVersion for types::ContractAbiEntry {
impl SerializeForVersion for Vec<types::ContractAbiEntry> {
fn serialize(
&self,
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
serializer.serialize_iter(self.len(), &mut self.iter())
}
}

impl SerializeForVersion for &types::ContractAbiEntry {
fn serialize(
&self,
serializer: crate::dto::Serializer,
Expand All @@ -151,7 +152,7 @@ impl SerializeForVersion for types::ContractAbiEntry {
}
}

impl SerializeForVersion for types::FunctionAbiEntry {
impl SerializeForVersion for &types::FunctionAbiEntry {
fn serialize(
&self,
serializer: crate::dto::Serializer,
Expand All @@ -178,7 +179,7 @@ impl SerializeForVersion for types::FunctionAbiEntry {
}
}

impl SerializeForVersion for types::EventAbiEntry {
impl SerializeForVersion for &types::EventAbiEntry {
fn serialize(
&self,
serializer: crate::dto::Serializer,
Expand All @@ -198,7 +199,7 @@ impl SerializeForVersion for types::EventAbiEntry {
}
}

impl SerializeForVersion for types::StructAbiEntry {
impl SerializeForVersion for &types::StructAbiEntry {
fn serialize(
&self,
serializer: crate::dto::Serializer,
Expand Down Expand Up @@ -263,35 +264,35 @@ impl SerializeForVersion for FunctionStateMutability {
}
}

impl SerializeForVersion for types::TypedParameter {
impl SerializeForVersion for &types::StructMember {
fn serialize(
&self,
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
let mut serializer = serializer.serialize_struct()?;

serializer.serialize_field("name", &self.name)?;
serializer.serialize_field("type", &self.r#type)?;
// FIXME: these clones could be removed if the types::* definitions were
// smarter.
let parameter = &types::TypedParameter {
name: self.typed_parameter_name.clone(),
r#type: self.typed_parameter_type.clone(),
};
serializer.flatten(&parameter)?;
serializer.serialize_field("offset", &self.offset)?;

serializer.end()
}
}

impl SerializeForVersion for types::StructMember {
impl SerializeForVersion for &types::TypedParameter {
fn serialize(
&self,
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
let mut serializer = serializer.serialize_struct()?;

// FIXME: these clones could be removed if the types::* definitions were
// smarter.
let parameter = types::TypedParameter {
name: self.typed_parameter_name.clone(),
r#type: self.typed_parameter_type.clone(),
};
serializer.flatten(&parameter)?;
serializer.serialize_field("offset", &self.offset)?;
serializer.serialize_field("name", &self.name)?;
serializer.serialize_field("type", &self.r#type)?;

serializer.end()
}
Expand Down
Loading

0 comments on commit dbca79f

Please sign in to comment.