Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce dedicated types for Any type aliases #2046

Merged
merged 9 commits into from
Mar 4, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
deserde, use wrapper
  • Loading branch information
stevencartavia committed Feb 12, 2025
commit dc60da0ea5cee78dfe609bf5d740507434abe9cc
53 changes: 26 additions & 27 deletions crates/network/src/any/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ pub use unknowns::{AnyTxType, UnknownTxEnvelope, UnknownTypedTransaction};
pub use alloy_consensus_any::{AnyHeader, AnyReceiptEnvelope};

use crate::Network;
use alloy_network_primitives::BlockResponse;
pub use alloy_rpc_types_any::{AnyRpcHeader, AnyTransactionReceipt};
use alloy_rpc_types_eth::{Block, Transaction, TransactionRequest};
use alloy_rpc_types_eth::{Block, BlockTransactions, Transaction, TransactionRequest};
use alloy_serde::WithOtherFields;
use serde::{Deserialize, Serialize};
use std::ops::{Deref, DerefMut};
Expand Down Expand Up @@ -67,12 +68,11 @@ impl Network for AnyNetwork {

type HeaderResponse = AnyRpcHeader;

type BlockResponse =
WithOtherFields<Block<WithOtherFields<Transaction<AnyTxEnvelope>>, AnyRpcHeader>>;
type BlockResponse = AnyRpcBlock;
}

/// A wrapper for [`AnyRpcBlock`] that allows for handling unknown block types.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct AnyRpcBlock(
WithOtherFields<Block<WithOtherFields<Transaction<AnyTxEnvelope>>, AnyRpcHeader>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also need from impls for these, you can use derive_more::From for all of these

);
Expand All @@ -86,6 +86,27 @@ impl AnyRpcBlock {
}
}

impl BlockResponse for AnyRpcBlock {
type Header = AnyRpcHeader;
type Transaction = WithOtherFields<Transaction<AnyTxEnvelope>>;

fn header(&self) -> &Self::Header {
&self.0.inner.header
}

fn transactions(&self) -> &BlockTransactions<Self::Transaction> {
&self.0.inner.transactions
}

fn transactions_mut(&mut self) -> &mut BlockTransactions<Self::Transaction> {
&mut self.0.inner.transactions
}

fn other_fields(&self) -> Option<&alloy_serde::OtherFields> {
self.0.other_fields()
}
}

impl AsRef<WithOtherFields<Block<WithOtherFields<Transaction<AnyTxEnvelope>>, AnyRpcHeader>>>
for AnyRpcBlock
{
Expand All @@ -110,20 +131,8 @@ impl DerefMut for AnyRpcBlock {
}
}

impl<'de> Deserialize<'de> for AnyRpcBlock {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let inner = WithOtherFields::<
Block<WithOtherFields<Transaction<AnyTxEnvelope>>, AnyRpcHeader>,
>::deserialize(deserializer)?;
Ok(Self(inner))
}
}

/// A wrapper for [`AnyRpcTransaction`] that allows for handling unknown transaction types.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct AnyRpcTransaction(WithOtherFields<Transaction<AnyTxEnvelope>>);

impl AnyRpcTransaction {
Expand Down Expand Up @@ -152,13 +161,3 @@ impl DerefMut for AnyRpcTransaction {
&mut self.0
}
}

impl<'de> Deserialize<'de> for AnyRpcTransaction {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let inner = WithOtherFields::<Transaction<AnyTxEnvelope>>::deserialize(deserializer)?;
Ok(Self(inner))
}
}