Skip to content

Commit

Permalink
fix: correct encode_2718_len for legacy transactions (alloy-rs#1360)
Browse files Browse the repository at this point in the history
* fix: correct  for legacy transactions

* simplify rlp_payload_length

* fmt
  • Loading branch information
klkvr authored and lwedge99 committed Oct 8, 2024
1 parent 2a3ae1d commit 373fd92
Showing 1 changed file with 28 additions and 38 deletions.
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 @@ -375,7 +348,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 @@ -668,6 +641,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

0 comments on commit 373fd92

Please sign in to comment.