Skip to content

Commit

Permalink
feat(rpc/dto): remove dto wrappers for common types
Browse files Browse the repository at this point in the history
  • Loading branch information
t00ts committed Dec 24, 2024
1 parent bcf7fb2 commit 617bf8a
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 633 deletions.
76 changes: 29 additions & 47 deletions crates/rpc/src/dto/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ use serde::de::Error;
use crate::dto::SerializeStruct;
use crate::{Reorg, RpcVersion};

#[derive(Debug)]
pub struct BlockHeader<'a>(pub &'a pathfinder_common::BlockHeader);

#[derive(Debug)]
pub struct PendingBlockHeader<'a>(pub &'a starknet_gateway_types::reply::PendingBlock);

impl crate::dto::DeserializeForVersion for pathfinder_common::BlockId {
fn deserialize(value: super::Value) -> Result<Self, serde_json::Error> {
if value.is_string() {
Expand Down Expand Up @@ -38,48 +32,45 @@ impl crate::dto::DeserializeForVersion for pathfinder_common::BlockId {
}
}

impl crate::dto::SerializeForVersion for BlockHeader<'_> {
impl crate::dto::SerializeForVersion for pathfinder_common::BlockHeader {
fn serialize(
&self,
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
let mut serializer = serializer.serialize_struct()?;
serializer.serialize_field("block_hash", &crate::dto::Felt(&self.0.hash.0))?;
serializer.serialize_field("parent_hash", &crate::dto::Felt(&self.0.parent_hash.0))?;
serializer.serialize_field("block_number", &self.0.number.get())?;
serializer.serialize_field("new_root", &crate::dto::Felt(&self.0.state_commitment.0))?;
serializer.serialize_field("timestamp", &self.0.timestamp.get())?;
serializer.serialize_field(
"sequencer_address",
&crate::dto::Felt(&self.0.sequencer_address.0),
)?;
serializer.serialize_field("block_hash", &self.hash)?;
serializer.serialize_field("parent_hash", &self.parent_hash)?;
serializer.serialize_field("block_number", &self.number.get())?;
serializer.serialize_field("new_root", &self.state_commitment)?;
serializer.serialize_field("timestamp", &self.timestamp.get())?;
serializer.serialize_field("sequencer_address", &self.sequencer_address)?;
serializer.serialize_field(
"l1_gas_price",
&ResourcePrice {
price_in_wei: self.0.eth_l1_gas_price,
price_in_fri: self.0.strk_l1_gas_price,
price_in_wei: self.eth_l1_gas_price,
price_in_fri: self.strk_l1_gas_price,
},
)?;
serializer.serialize_field("starknet_version", &self.0.starknet_version.to_string())?;
serializer.serialize_field("starknet_version", &self.starknet_version.to_string())?;
serializer.serialize_field(
"l1_data_gas_price",
&ResourcePrice {
price_in_wei: self.0.eth_l1_data_gas_price,
price_in_fri: self.0.strk_l1_data_gas_price,
price_in_wei: self.eth_l1_data_gas_price,
price_in_fri: self.strk_l1_data_gas_price,
},
)?;
if serializer.version == RpcVersion::V08 {
serializer.serialize_field(
"l2_gas_price",
&ResourcePrice {
price_in_wei: self.0.eth_l2_gas_price,
price_in_fri: self.0.strk_l2_gas_price,
price_in_wei: self.eth_l2_gas_price,
price_in_fri: self.strk_l2_gas_price,
},
)?;
}
serializer.serialize_field(
"l1_da_mode",
&match self.0.l1_da_mode {
&match self.l1_da_mode {
L1DataAvailabilityMode::Blob => "BLOB",
L1DataAvailabilityMode::Calldata => "CALLDATA",
},
Expand All @@ -88,45 +79,42 @@ impl crate::dto::SerializeForVersion for BlockHeader<'_> {
}
}

impl crate::dto::SerializeForVersion for PendingBlockHeader<'_> {
impl crate::dto::SerializeForVersion for starknet_gateway_types::reply::PendingBlock {
fn serialize(
&self,
serializer: crate::dto::Serializer,
) -> Result<crate::dto::Ok, crate::dto::Error> {
let mut serializer = serializer.serialize_struct()?;
serializer.serialize_field("parent_hash", &crate::dto::Felt(&self.0.parent_hash.0))?;
serializer.serialize_field("timestamp", &self.0.timestamp.get())?;
serializer.serialize_field(
"sequencer_address",
&crate::dto::Felt(&self.0.sequencer_address.0),
)?;
serializer.serialize_field("parent_hash", &self.parent_hash)?;
serializer.serialize_field("timestamp", &self.timestamp.get())?;
serializer.serialize_field("sequencer_address", &self.sequencer_address)?;
serializer.serialize_field(
"l1_gas_price",
&ResourcePrice {
price_in_wei: self.0.l1_gas_price.price_in_wei,
price_in_fri: self.0.l1_gas_price.price_in_fri,
price_in_wei: self.l1_gas_price.price_in_wei,
price_in_fri: self.l1_gas_price.price_in_fri,
},
)?;
serializer.serialize_field("starknet_version", &self.0.starknet_version.to_string())?;
serializer.serialize_field("starknet_version", &self.starknet_version.to_string())?;
serializer.serialize_field(
"l1_data_gas_price",
&ResourcePrice {
price_in_wei: self.0.l1_data_gas_price.price_in_wei,
price_in_fri: self.0.l1_data_gas_price.price_in_fri,
price_in_wei: self.l1_data_gas_price.price_in_wei,
price_in_fri: self.l1_data_gas_price.price_in_fri,
},
)?;
if serializer.version == RpcVersion::V08 {
serializer.serialize_field(
"l2_gas_price",
&ResourcePrice {
price_in_wei: self.0.l2_gas_price.price_in_wei,
price_in_fri: self.0.l2_gas_price.price_in_fri,
price_in_wei: self.l2_gas_price.price_in_wei,
price_in_fri: self.l2_gas_price.price_in_fri,
},
)?;
}
serializer.serialize_field(
"l1_da_mode",
&match self.0.l1_da_mode {
&match self.l1_da_mode {
starknet_gateway_types::reply::L1DataAvailabilityMode::Blob => "BLOB",
starknet_gateway_types::reply::L1DataAvailabilityMode::Calldata => "CALLDATA",
},
Expand Down Expand Up @@ -160,15 +148,9 @@ impl crate::dto::SerializeForVersion for Reorg {
) -> Result<crate::dto::Ok, crate::dto::Error> {
let mut serializer = serializer.serialize_struct()?;
serializer.serialize_field("first_block_number", &self.first_block_number.get())?;
serializer.serialize_field(
"first_block_hash",
&crate::dto::Felt(&self.first_block_hash.0),
)?;
serializer.serialize_field("first_block_hash", &self.first_block_hash)?;
serializer.serialize_field("last_block_number", &self.last_block_number.get())?;
serializer.serialize_field(
"last_block_hash",
&crate::dto::Felt(&self.last_block_hash.0),
)?;
serializer.serialize_field("last_block_hash", &self.last_block_hash)?;
serializer.end()
}
}
Loading

0 comments on commit 617bf8a

Please sign in to comment.