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: serde for consensus tx types #361

Merged
merged 14 commits into from
Mar 26, 2024

Conversation

klkvr
Copy link
Member

@klkvr klkvr commented Mar 21, 2024

Motivation

Adds Serialize and Deserialize impls for TypedTransaction, TxEnvelope, etc.

Bumps core

Solution

I don't really like the way we manage type on tagged tx enums, current version is from ethers, perhaps there is a way we can do better here?

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

all of these make sense to make,

I like the tx type tag and prefer this to untagged

crates/consensus/src/transaction/eip1559.rs Show resolved Hide resolved
@@ -62,12 +62,17 @@ impl TryFrom<u8> for TxType {
///
/// [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
Copy link
Member

Choose a reason for hiding this comment

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

not sure if we want the type as tag here,
I think for readability this makes sense, and untagged could be a mess
wdyt @prestwich

Copy link
Member

Choose a reason for hiding this comment

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

I think that type may be omitted for legacy txns, and any objects generated in the past and stored somewhere will definitely have type omitted. So this would prevent us from handling old receipts at the very least

Copy link
Member Author

Choose a reason for hiding this comment

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

@prestwich so those should be untagged instead?

Copy link
Member

Choose a reason for hiding this comment

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

ideally we would write custom impls here I think? but I don't feel super strongly about it. can you check ethers-rs and its issues for how we used to do this? as long as we're not worse than ethers, we should be fine to move forward

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't find any issues related to this type tag, likely because ethers users couldn't really have issues with deserializing txses serialized by ethers itself. And deserialization of external JSON-encoded transactions was likely done through Transaction which is more like an RPC type without much restrictions.

imo ideal impl here would be the one which is able to deserialize any valid eth_getTransactionByHash response into TxEnvelope and per execution spec all RPC responses must include "type"

Copy link
Member

Choose a reason for hiding this comment

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

I thin i'm comfortable leaving as typed, as supporting outdated nodes or long-term stored responses is not a compelling usecase.

imo ideal impl here would be the one which is able to deserialize any valid eth_getTransactionByHash response into TxEnvelope and per execution spec all RPC responses must include "type"

agree, tho as stated elsewhere, i think the "right" way to achieve this is by embedding TxEnvelope in the transaction rpc response type

Copy link
Member Author

Choose a reason for hiding this comment

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

agree, tho as stated elsewhere, i think the "right" way to achieve this is by embedding TxEnvelope in the transaction rpc response type

perhaps this is the next step once network abstraction is generic enough to allow us such strict constraints on RPC responses without breaking stuff for projects using Ethereum network for everything?

Copy link
Member

Choose a reason for hiding this comment

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

the way to enforce it as a bound on network (if we even want to do that) would be to have type TransactionResponse: RpcObject + AsRef<Self::TxEnvelope>, basically. That would ensure that any response object contained an envelope

I think that the longer we go without embedding consensus objects in the RPC types, the worse of a time we'll have making the change

My primary target list is

  • Log
  • TxEnvelope
  • Receipt

crates/eips/src/eip2930.rs Outdated Show resolved Hide resolved
crates/serde/src/num.rs Outdated Show resolved Hide resolved
crates/consensus/src/transaction/eip4844.rs Show resolved Hide resolved
crates/consensus/src/transaction/eip4844.rs Show resolved Hide resolved
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

lgtm, pending @prestwich

crates/consensus/src/transaction/eip4844.rs Show resolved Hide resolved
crates/consensus/Cargo.toml Show resolved Hide resolved
@@ -62,12 +62,17 @@ impl TryFrom<u8> for TxType {
///
/// [EIP-2718]: https://eips.ethereum.org/EIPS/eip-2718
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "type"))]
Copy link
Member

Choose a reason for hiding this comment

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

I think that type may be omitted for legacy txns, and any objects generated in the past and stored somewhere will definitely have type omitted. So this would prevent us from handling old receipts at the very least

crates/consensus/src/transaction/typed.rs Show resolved Hide resolved
crates/serde/src/num.rs Outdated Show resolved Hide resolved
@klkvr klkvr marked this pull request as ready for review March 23, 2024 17:12
@@ -3,8 +3,11 @@ use alloy_primitives::{Signature, B256};

/// A transaction with a signature and hash seal.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Copy link
Member

Choose a reason for hiding this comment

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

is there followup work to dedupe the rpc_types signature here?

@@ -914,6 +954,7 @@ impl Transaction for TxEip4844WithSidecar {
/// This represents a set of blobs, and its corresponding commitments and proofs.
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
#[repr(C)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Copy link
Member

@prestwich prestwich Mar 26, 2024

Choose a reason for hiding this comment

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

Can we proactively dedupe this as part of this PR?

current code in RPC types is as follows

/// This represents a set of blobs, and its corresponding commitments and proofs.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[repr(C)]
pub struct BlobTransactionSidecar {
    /// The blob data.
    pub blobs: Vec<Blob>,
    /// The blob commitments.
    pub commitments: Vec<Bytes48>,
    /// The blob proofs.
    pub proofs: Vec<Bytes48>,
}

Copy link
Member Author

@klkvr klkvr Mar 26, 2024

Choose a reason for hiding this comment

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

@prestwich rpc-engine-types relies on Blob and Bytes48 having ssz support and c-kzg types does not have it, what's the desired way to handle this?

Copy link
Member

Choose a reason for hiding this comment

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

manually impl ssz trait with a helper struct? If it's getting that complicated, it should go in a separate PR though

@prestwich prestwich merged commit 7e39c85 into alloy-rs:main Mar 26, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants