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

fix: use enveloped encoding for typed transactions #239

Merged
merged 3 commits into from
Mar 13, 2024

Conversation

Rjected
Copy link
Contributor

@Rjected Rjected commented Feb 28, 2024

Motivation

The encoding for eth_sendRawTransaction for typed transactions was not correct, because it was encoding a RLP header for typed transactions.

Solution

This completely overhauls transaction encoding and decoding in alloy.

There is no longer encode_signed and decode_signed in Transaction. Signed<T> transactions cannot be encoded or decoded directly. Now, the main way to encode and decode transactions is through Encodable2718 and Decodable2718, specifically through TxEnvelope.

The TaggedLegacy variant is removed.
impl<T> std::ops::Deref for Signed<T> is removed.

impl From<Signed<TxEip4844WithSidecar>> for TxEnvelope is added.
impl From<Signed<TxEip4844>> for TxEnvelope is added.

The 2718 trait methods now use alloy_rlp::Result instead of Result<_, Eip2718Error>

Raw RLP of a blob transaction with sidecar is added to consensus/testdata, and is used in a roundtrip test.

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@Rjected
Copy link
Contributor Author

Rjected commented Feb 28, 2024

fixing conflicts and adding blob tx with sidecar impl...

Copy link
Contributor

@Evalir Evalir left a comment

Choose a reason for hiding this comment

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

Awesome! Agreed we need to iron these out—but I believe enveloped encoding is already handled by TxEnvelope & Decodable2718/Encodable2718? (see envelope.rs & alloy_eips::eip2718)

I do think allowing txs to be encoded alone, not in an envelope, is a bit of a footgun and we've seen this in the telegram chat a few times. Maybe we should disallow it?

@Rjected
Copy link
Contributor Author

Rjected commented Feb 29, 2024

Awesome! Agreed we need to iron these out—but I believe enveloped encoding is already handled by TxEnvelope & Decodable2718/Encodable2718? (see envelope.rs & alloy_eips::eip2718)

Ah I didn't see that at all, I'll check those out. I'll add some docs, in that case encode_signed and decode_signed should basically never be called for sendRaw

@prestwich
Copy link
Member

encode_signed and decode_signed probably should be documented as "dont call this, you want to call TxEnvelope methods instead"

@Rjected Rjected force-pushed the dan/fix-send-raw-transaction-4844 branch from 4e4a972 to 09e4eea Compare February 29, 2024 15:42
pub fn payload_len_with_signature(&self, signature: &Signature) -> usize {
/// Returns what the encoded length should be, if the transaction were RLP encoded with the
/// given signature.
pub(crate) fn encoded_len_with_signature(&self, signature: &Signature) -> usize {
Copy link
Member

Choose a reason for hiding this comment

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

side note: i consider these all tech debt that is paid down by a refactor of rlp derives

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, rlp(flatten) etc will usher in a new era of derived and correct encodings

@gakonst
Copy link
Member

gakonst commented Mar 4, 2024

@Rjected Plans here? Flagging that it seems like foundry-rs/foundry#7242 (comment) is blocked on this

@Rjected
Copy link
Contributor Author

Rjected commented Mar 4, 2024

@Rjected Plans here? Flagging that it seems like foundry-rs/foundry#7242 (comment) is blocked on this

I can get this rfr / done today to unblock

@Rjected Rjected force-pushed the dan/fix-send-raw-transaction-4844 branch 2 times, most recently from 7714411 to 40468f0 Compare March 6, 2024 18:33
@Rjected Rjected marked this pull request as ready for review March 7, 2024 17:19
@Rjected
Copy link
Contributor Author

Rjected commented Mar 7, 2024

Marked this as RFR, the code is ready to review, i plan on adding more tests today

@Rjected Rjected force-pushed the dan/fix-send-raw-transaction-4844 branch 2 times, most recently from 239737c to 7991659 Compare March 11, 2024 20:45
Rjected added 2 commits March 11, 2024 17:28
remove redundant method

fix: remove encode_enveloped

* improve docs for `encode_signed`
* fix vec with_capacity and `into_signed` for 4844 with sidecar

fix more raw tx encodings, need to fix length

hmm this is all network encoding?

still TODO: clear up api boundaries

remove encode_signed, decode_signed

large refactors to encoding logic

todo: analogous methods on Signed, fix tests

fix tests

fix more tests and lints

remove incorrect doc

remove unnecessary printlns

remove deref, remove tagged legacy

fix tests

fix Encodable length and add test
@Rjected Rjected force-pushed the dan/fix-send-raw-transaction-4844 branch from 7991659 to 6b99d2a Compare March 11, 2024 21:47
crates/consensus/src/transaction/mod.rs Show resolved Hide resolved

/// Encode with a signature. This encoding is usually RLP, but may be
/// different for future EIP-2718 transaction types.
fn encode_signed(&self, signature: &Signature, out: &mut dyn BufMut);
Copy link
Member

Choose a reason for hiding this comment

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

do we not need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

encode_signed and decode_signed are not specific enough for eth transactions, it's ambiguous whether or not you want the network or RPC RLP format for transactions. Encodable2718 provides this distinction

Copy link
Member

Choose a reason for hiding this comment

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

ok, makes sense to me. need to document this, but can be a follow up, would create an issue

crates/consensus/src/signed.rs Show resolved Hide resolved
Copy link
Member

@onbjerg onbjerg left a comment

Choose a reason for hiding this comment

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

lgtm

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.

rlp 👍

@prestwich prestwich merged commit d0794e5 into alloy-rs:main Mar 13, 2024
15 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.

6 participants