From 068eeef98ed58135839d38fb9261b1659b10ddaa Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Wed, 25 Sep 2024 18:06:29 +0300 Subject: [PATCH 1/3] fix: correct for legacy transactions --- crates/consensus/src/transaction/envelope.rs | 47 +++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/crates/consensus/src/transaction/envelope.rs b/crates/consensus/src/transaction/envelope.rs index 232e88ca326..9e3f132a331 100644 --- a/crates/consensus/src/transaction/envelope.rs +++ b/crates/consensus/src/transaction/envelope.rs @@ -278,22 +278,25 @@ impl TxEnvelope { } } - /// Return the length of the inner txn, __without a type byte__. - pub fn inner_length(&self) -> usize { + /// Return the length of the inner txn, including type byte length + pub fn rlp_payload_length(&self) -> usize { match self { - Self::Legacy(t) => t.tx().fields_len() + t.signature().rlp_vrs_len(), - Self::Eip2930(t) => { + Self::Legacy(t) => { let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len(); Header { list: true, payload_length }.length() + payload_length } + Self::Eip2930(t) => { + let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len(); + Header { list: true, payload_length }.length() + payload_length + 1 + } Self::Eip1559(t) => { let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len(); - Header { list: true, payload_length }.length() + payload_length + Header { list: true, payload_length }.length() + payload_length + 1 } 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 + Header { list: true, payload_length }.length() + payload_length + 1 } TxEip4844Variant::TxEip4844WithSidecar(tx) => { let inner_payload_length = tx.tx().fields_len() + t.signature().rlp_vrs_len(); @@ -303,27 +306,15 @@ impl TxEnvelope { 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 + outer_header.length() + outer_payload_length + 1 } }, Self::Eip7702(t) => { let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len(); - Header { list: true, payload_length }.length() + payload_length + Header { list: true, payload_length }.length() + payload_length + 1 } } } - - /// 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 - } } impl Encodable for TxEnvelope { @@ -382,7 +373,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) { @@ -675,6 +666,20 @@ 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 { From 281eaec7eae946f4ea8ce7ba428b425f5de8ad32 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Wed, 25 Sep 2024 18:10:15 +0300 Subject: [PATCH 2/3] simplify rlp_payload_length --- crates/consensus/src/transaction/envelope.rs | 36 +++++--------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/crates/consensus/src/transaction/envelope.rs b/crates/consensus/src/transaction/envelope.rs index 9e3f132a331..d23d2d76d75 100644 --- a/crates/consensus/src/transaction/envelope.rs +++ b/crates/consensus/src/transaction/envelope.rs @@ -280,40 +280,22 @@ impl TxEnvelope { /// Return the length of the inner txn, including type byte length pub fn rlp_payload_length(&self) -> usize { - match self { - Self::Legacy(t) => { - let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len(); - Header { list: true, payload_length }.length() + payload_length - } - Self::Eip2930(t) => { - let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len(); - Header { list: true, payload_length }.length() + payload_length + 1 - } - Self::Eip1559(t) => { - let payload_length = t.tx().fields_len() + t.signature().rlp_vrs_len(); - Header { list: true, payload_length }.length() + payload_length + 1 - } + let payload_length = match self { + Self::Legacy(t) => t.tx().fields_len() + t.signature().rlp_vrs_len(), + 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 + 1 - } + 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 + 1 + 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 + 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 } } From 4458d5125ec3cabf0fd102138db65c5291b5f16d Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Wed, 25 Sep 2024 18:14:45 +0300 Subject: [PATCH 3/3] fmt --- crates/consensus/src/transaction/envelope.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/consensus/src/transaction/envelope.rs b/crates/consensus/src/transaction/envelope.rs index d23d2d76d75..30aa8961df5 100644 --- a/crates/consensus/src/transaction/envelope.rs +++ b/crates/consensus/src/transaction/envelope.rs @@ -659,7 +659,10 @@ mod tests { value: U256::from(7_u64), ..Default::default() }; - test_encode_decode_roundtrip(tx, Some(Signature::test_signature().with_parity(Parity::NonEip155(true)))); + test_encode_decode_roundtrip( + tx, + Some(Signature::test_signature().with_parity(Parity::NonEip155(true))), + ); } #[test]