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: correct encode_2718_len for legacy transactions #1360

Merged
merged 3 commits into from
Sep 25, 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
66 changes: 28 additions & 38 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,51 +278,24 @@ impl TxEnvelope {
}
}

/// Return the length of the inner txn, __without a type byte__.
pub fn inner_length(&self) -> usize {
match self {
/// Return the length of the inner txn, including type byte length
pub fn rlp_payload_length(&self) -> usize {
let payload_length = match self {
Self::Legacy(t) => t.tx().fields_len() + t.signature().rlp_vrs_len(),
Self::Eip2930(t) => {
let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len();
Header { list: true, payload_length }.length() + payload_length
}
Self::Eip1559(t) => {
let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len();
Header { list: true, payload_length }.length() + payload_length
}
Self::Eip2930(t) => t.tx().fields_len() + t.signature().rlp_vrs_len(),
Self::Eip1559(t) => t.tx().fields_len() + t.signature().rlp_vrs_len(),
Self::Eip4844(t) => match t.tx() {
TxEip4844Variant::TxEip4844(tx) => {
let payload_length = tx.fields_len() + t.signature().rlp_vrs_len();
Header { list: true, payload_length }.length() + payload_length
}
TxEip4844Variant::TxEip4844(tx) => tx.fields_len() + t.signature().rlp_vrs_len(),
TxEip4844Variant::TxEip4844WithSidecar(tx) => {
let inner_payload_length = tx.tx().fields_len() + t.signature().rlp_vrs_len();
let inner_header = Header { list: true, payload_length: inner_payload_length };

let outer_payload_length =
inner_header.length() + inner_payload_length + tx.sidecar.fields_len();
let outer_header = Header { list: true, payload_length: outer_payload_length };

outer_header.length() + outer_payload_length
inner_header.length() + inner_payload_length + tx.sidecar.fields_len()
}
},
Self::Eip7702(t) => {
let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len();
Header { list: true, payload_length }.length() + payload_length
}
}
}

/// Return the RLP payload length of the network-serialized wrapper
fn rlp_payload_length(&self) -> usize {
if let Self::Legacy(t) = self {
let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len();
return Header { list: true, payload_length }.length() + payload_length;
}
// length of inner tx body
let inner_length = self.inner_length();
// with tx type byte
inner_length + 1
Self::Eip7702(t) => t.tx().fields_len() + t.signature().rlp_vrs_len(),
};
Header { list: true, payload_length }.length() + payload_length + !self.is_legacy() as usize
}
}

Expand Down Expand Up @@ -382,7 +355,7 @@ impl Encodable2718 for TxEnvelope {
}

fn encode_2718_len(&self) -> usize {
self.inner_length() + !self.is_legacy() as usize
self.rlp_payload_length()
}

fn encode_2718(&self, out: &mut dyn alloy_rlp::BufMut) {
Expand Down Expand Up @@ -675,6 +648,23 @@ mod tests {
assert_eq!(decoded, tx_envelope);
}

#[test]
fn test_encode_decode_legacy() {
let tx = TxLegacy {
chain_id: None,
nonce: 2,
gas_limit: 1000000,
gas_price: 10000000000,
to: Address::left_padding_from(&[6]).into(),
value: U256::from(7_u64),
..Default::default()
};
test_encode_decode_roundtrip(
tx,
Some(Signature::test_signature().with_parity(Parity::NonEip155(true))),
);
}

#[test]
fn test_encode_decode_eip1559() {
let tx = TxEip1559 {
Expand Down
Loading