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: RLP for TxEip4844 #1596

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Changes from all commits
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
37 changes: 16 additions & 21 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{SignableTransaction, Signed, Transaction, TxType};
use alloc::vec::Vec;
use alloy_eips::{eip2930::AccessList, eip4844::DATA_GAS_PER_BLOB, eip7702::SignedAuthorization};
use alloy_primitives::{Address, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_rlp::{Buf, BufMut, Decodable, Encodable, Header};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use core::mem;

#[doc(inline)]
Expand Down Expand Up @@ -290,22 +290,17 @@ impl RlpEcdsaTx for TxEip4844Variant {
}

fn rlp_decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
let needle = &mut &**buf;
let header = Header::decode(needle)?;
let header = Header::decode(buf)?;
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
}
let remaining_len = needle.len();
if header.payload_length > remaining_len {
return Err(alloy_rlp::Error::InputTooShort);
}
let remaining = buf.len();

let res = Self::rlp_decode_fields(buf)?;

let chunk = &mut &buf[..header.length_with_payload()];
let res = Self::rlp_decode_fields(chunk)?;
if !chunk.is_empty() {
if buf.len() + header.payload_length != remaining {
return Err(alloy_rlp::Error::UnexpectedLength);
}
buf.advance(header.length_with_payload());
Ok(res)
}

Expand Down Expand Up @@ -864,6 +859,12 @@ impl RlpEcdsaTx for TxEip4844WithSidecar {
self.sidecar.rlp_encode_fields(out);
}

fn rlp_header_signed(&self, signature: &Signature) -> Header {
let payload_length = self.tx.rlp_encoded_length_with_signature(signature)
+ self.sidecar.rlp_encoded_fields_length();
Header { list: true, payload_length }
}

fn rlp_encode_signed(&self, signature: &Signature, out: &mut dyn BufMut) {
self.rlp_header_signed(signature).encode(out);
self.tx.rlp_encode_signed(signature, out);
Expand All @@ -881,20 +882,14 @@ impl RlpEcdsaTx for TxEip4844WithSidecar {
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
}
let remaining_len = buf.len();
if header.payload_length > remaining_len {
return Err(alloy_rlp::Error::InputTooShort);
}
let remaining = buf.len();

let chunk = &mut &buf[..remaining_len];
let (tx, signature) = TxEip4844::rlp_decode_with_signature(chunk)?;
let sidecar = BlobTransactionSidecar::rlp_decode_fields(chunk)?;
let (tx, signature) = TxEip4844::rlp_decode_with_signature(buf)?;
let sidecar = BlobTransactionSidecar::rlp_decode_fields(buf)?;

// Decoding did not consume the entire payload specified by the header
if !chunk.is_empty() {
if buf.len() + header.payload_length != remaining {
return Err(alloy_rlp::Error::UnexpectedLength);
}
buf.advance(header.payload_length);

Ok((Self { tx, sidecar }, signature))
}
Expand Down