From e7688f4a5bcc629af19185a84cf7ce8f40117eee Mon Sep 17 00:00:00 2001 From: clabby Date: Sat, 13 Apr 2024 16:14:15 -0400 Subject: [PATCH 1/2] feat: Export inner encoding / decoding functions from `Tx*` types --- crates/consensus/src/transaction/eip1559.rs | 10 ++++----- crates/consensus/src/transaction/eip2930.rs | 6 +++--- crates/consensus/src/transaction/eip4844.rs | 23 ++++++++++++++------- crates/consensus/src/transaction/legacy.rs | 4 ++-- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/crates/consensus/src/transaction/eip1559.rs b/crates/consensus/src/transaction/eip1559.rs index 8ac0321b3a7..18a5b86897f 100644 --- a/crates/consensus/src/transaction/eip1559.rs +++ b/crates/consensus/src/transaction/eip1559.rs @@ -117,8 +117,8 @@ impl TxEip1559 { }) } - /// Encodes only the transaction's fields into the desired buffer, without a RLP header. - pub(crate) fn fields_len(&self) -> usize { + /// Outputs the length of the transaction's fields, without a RLP header. + pub fn fields_len(&self) -> usize { let mut len = 0; len += self.chain_id.length(); len += self.nonce.length(); @@ -175,8 +175,8 @@ impl TxEip1559 { } /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating - /// hash that for eip2718 does not require a rlp header - pub(crate) fn encode_with_signature( + /// hash that for eip2718 does not require a rlp header. + pub fn encode_with_signature( &self, signature: &Signature, out: &mut dyn BufMut, @@ -200,7 +200,7 @@ impl TxEip1559 { /// header. /// /// This __does__ expect the bytes to start with a list header and include a signature. - pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { let header = Header::decode(buf)?; if !header.list { return Err(alloy_rlp::Error::UnexpectedString); diff --git a/crates/consensus/src/transaction/eip2930.rs b/crates/consensus/src/transaction/eip2930.rs index 7dec4cc33c2..92d11e03c7e 100644 --- a/crates/consensus/src/transaction/eip2930.rs +++ b/crates/consensus/src/transaction/eip2930.rs @@ -98,7 +98,7 @@ impl TxEip2930 { } /// Outputs the length of the transaction's fields, without a RLP header. - pub(crate) fn fields_len(&self) -> usize { + pub fn fields_len(&self) -> usize { let mut len = 0; len += self.chain_id.length(); len += self.nonce.length(); @@ -154,7 +154,7 @@ impl TxEip2930 { /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating /// hash that for eip2718 does not require a rlp header - pub(crate) fn encode_with_signature( + pub fn encode_with_signature( &self, signature: &Signature, out: &mut dyn BufMut, @@ -190,7 +190,7 @@ impl TxEip2930 { /// header. /// /// This __does__ expect the bytes to start with a list header and include a signature. - pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { let header = Header::decode(buf)?; if !header.list { return Err(alloy_rlp::Error::UnexpectedString); diff --git a/crates/consensus/src/transaction/eip4844.rs b/crates/consensus/src/transaction/eip4844.rs index 9a0744780c7..77b14f06e27 100644 --- a/crates/consensus/src/transaction/eip4844.rs +++ b/crates/consensus/src/transaction/eip4844.rs @@ -147,7 +147,8 @@ impl TxEip4844Variant { } } - pub(crate) fn fields_len(&self) -> usize { + /// Outputs the length of the transaction's fields, without a RLP header. + pub fn fields_len(&self) -> usize { match self { TxEip4844Variant::TxEip4844(tx) => tx.fields_len(), TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx().fields_len(), @@ -160,7 +161,7 @@ impl TxEip4844Variant { /// /// If `with_header` is `true`, the following will be encoded: /// `rlp(tx_type (0x03) || rlp([transaction_payload_body, blobs, commitments, proofs]))` - pub(crate) fn encode_with_signature( + pub fn encode_with_signature( &self, signature: &Signature, out: &mut dyn BufMut, @@ -196,7 +197,13 @@ impl TxEip4844Variant { } } - pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + /// Decodes the transaction from RLP bytes, including the signature. + /// + /// This __does not__ expect the bytes to start with a transaction type byte or string + /// header. + /// + /// This __does__ expect the bytes to start with a list header and include a signature. + pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { let mut current_buf = *buf; let _header = Header::decode(&mut current_buf)?; @@ -510,7 +517,7 @@ impl TxEip4844 { } /// Outputs the length of the transaction's fields, without a RLP header. - pub(crate) fn fields_len(&self) -> usize { + pub fn fields_len(&self) -> usize { let mut len = 0; len += self.chain_id.length(); len += self.nonce.length(); @@ -588,7 +595,7 @@ impl TxEip4844 { /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating /// hash that for eip2718 does not require a rlp header - pub(crate) fn encode_with_signature( + pub fn encode_with_signature( &self, signature: &Signature, out: &mut dyn BufMut, @@ -624,7 +631,7 @@ impl TxEip4844 { /// header. /// /// This __does__ expect the bytes to start with a list header and include a signature. - pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { let header = Header::decode(buf)?; if !header.list { return Err(alloy_rlp::Error::UnexpectedString); @@ -832,7 +839,7 @@ impl TxEip4844WithSidecar { /// /// where `tx_payload` is the RLP encoding of the [TxEip4844] transaction fields: /// `rlp([chain_id, nonce, max_priority_fee_per_gas, ..., v, r, s])` - pub(crate) fn encode_with_signature_fields(&self, signature: &Signature, out: &mut dyn BufMut) { + pub fn encode_with_signature_fields(&self, signature: &Signature, out: &mut dyn BufMut) { let inner_payload_length = self.tx.fields_len() + signature.rlp_vrs_len(); let inner_header = Header { list: true, payload_length: inner_payload_length }; @@ -858,7 +865,7 @@ impl TxEip4844WithSidecar { /// This __does__ expect the bytes to start with a list header and include a signature. /// /// This is the inverse of [TxEip4844WithSidecar::encode_with_signature_fields]. - pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { let header = Header::decode(buf)?; if !header.list { return Err(alloy_rlp::Error::UnexpectedString); diff --git a/crates/consensus/src/transaction/legacy.rs b/crates/consensus/src/transaction/legacy.rs index 602b87b0d3c..3900f6f2546 100644 --- a/crates/consensus/src/transaction/legacy.rs +++ b/crates/consensus/src/transaction/legacy.rs @@ -75,7 +75,7 @@ impl TxLegacy { /// Outputs the length of the transaction's fields, without a RLP header or length of the /// eip155 fields. - pub(crate) fn fields_len(&self) -> usize { + pub fn fields_len(&self) -> usize { let mut len = 0; len += self.nonce.length(); len += self.gas_price.length(); @@ -153,7 +153,7 @@ impl TxLegacy { /// header. /// /// This __does__ expect the bytes to start with a list header and include a signature. - pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { + pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { let header = Header::decode(buf)?; if !header.list { return Err(alloy_rlp::Error::UnexpectedString); From b9f93b8ac2a1da7c1de755e483c72e1c0093eb82 Mon Sep 17 00:00:00 2001 From: clabby Date: Mon, 15 Apr 2024 22:09:37 -0400 Subject: [PATCH 2/2] Add `#[doc(hidden)]` to exposed low-level encoding functions --- crates/consensus/src/transaction/eip1559.rs | 3 +++ crates/consensus/src/transaction/eip2930.rs | 3 +++ crates/consensus/src/transaction/eip4844.rs | 7 +++++++ crates/consensus/src/transaction/legacy.rs | 2 ++ 4 files changed, 15 insertions(+) diff --git a/crates/consensus/src/transaction/eip1559.rs b/crates/consensus/src/transaction/eip1559.rs index 18a5b86897f..871a6169e4f 100644 --- a/crates/consensus/src/transaction/eip1559.rs +++ b/crates/consensus/src/transaction/eip1559.rs @@ -118,6 +118,7 @@ impl TxEip1559 { } /// Outputs the length of the transaction's fields, without a RLP header. + #[doc(hidden)] pub fn fields_len(&self) -> usize { let mut len = 0; len += self.chain_id.length(); @@ -176,6 +177,7 @@ impl TxEip1559 { /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating /// hash that for eip2718 does not require a rlp header. + #[doc(hidden)] pub fn encode_with_signature( &self, signature: &Signature, @@ -200,6 +202,7 @@ impl TxEip1559 { /// header. /// /// This __does__ expect the bytes to start with a list header and include a signature. + #[doc(hidden)] pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { let header = Header::decode(buf)?; if !header.list { diff --git a/crates/consensus/src/transaction/eip2930.rs b/crates/consensus/src/transaction/eip2930.rs index 92d11e03c7e..e4f09d0c3e3 100644 --- a/crates/consensus/src/transaction/eip2930.rs +++ b/crates/consensus/src/transaction/eip2930.rs @@ -98,6 +98,7 @@ impl TxEip2930 { } /// Outputs the length of the transaction's fields, without a RLP header. + #[doc(hidden)] pub fn fields_len(&self) -> usize { let mut len = 0; len += self.chain_id.length(); @@ -154,6 +155,7 @@ impl TxEip2930 { /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating /// hash that for eip2718 does not require a rlp header + #[doc(hidden)] pub fn encode_with_signature( &self, signature: &Signature, @@ -190,6 +192,7 @@ impl TxEip2930 { /// header. /// /// This __does__ expect the bytes to start with a list header and include a signature. + #[doc(hidden)] pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { let header = Header::decode(buf)?; if !header.list { diff --git a/crates/consensus/src/transaction/eip4844.rs b/crates/consensus/src/transaction/eip4844.rs index 77b14f06e27..6286bbfe379 100644 --- a/crates/consensus/src/transaction/eip4844.rs +++ b/crates/consensus/src/transaction/eip4844.rs @@ -148,6 +148,7 @@ impl TxEip4844Variant { } /// Outputs the length of the transaction's fields, without a RLP header. + #[doc(hidden)] pub fn fields_len(&self) -> usize { match self { TxEip4844Variant::TxEip4844(tx) => tx.fields_len(), @@ -161,6 +162,7 @@ impl TxEip4844Variant { /// /// If `with_header` is `true`, the following will be encoded: /// `rlp(tx_type (0x03) || rlp([transaction_payload_body, blobs, commitments, proofs]))` + #[doc(hidden)] pub fn encode_with_signature( &self, signature: &Signature, @@ -203,6 +205,7 @@ impl TxEip4844Variant { /// header. /// /// This __does__ expect the bytes to start with a list header and include a signature. + #[doc(hidden)] pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { let mut current_buf = *buf; let _header = Header::decode(&mut current_buf)?; @@ -517,6 +520,7 @@ impl TxEip4844 { } /// Outputs the length of the transaction's fields, without a RLP header. + #[doc(hidden)] pub fn fields_len(&self) -> usize { let mut len = 0; len += self.chain_id.length(); @@ -595,6 +599,7 @@ impl TxEip4844 { /// Inner encoding function that is used for both rlp [`Encodable`] trait and for calculating /// hash that for eip2718 does not require a rlp header + #[doc(hidden)] pub fn encode_with_signature( &self, signature: &Signature, @@ -631,6 +636,7 @@ impl TxEip4844 { /// header. /// /// This __does__ expect the bytes to start with a list header and include a signature. + #[doc(hidden)] pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { let header = Header::decode(buf)?; if !header.list { @@ -865,6 +871,7 @@ impl TxEip4844WithSidecar { /// This __does__ expect the bytes to start with a list header and include a signature. /// /// This is the inverse of [TxEip4844WithSidecar::encode_with_signature_fields]. + #[doc(hidden)] pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { let header = Header::decode(buf)?; if !header.list { diff --git a/crates/consensus/src/transaction/legacy.rs b/crates/consensus/src/transaction/legacy.rs index 3900f6f2546..424b5d8927e 100644 --- a/crates/consensus/src/transaction/legacy.rs +++ b/crates/consensus/src/transaction/legacy.rs @@ -75,6 +75,7 @@ impl TxLegacy { /// Outputs the length of the transaction's fields, without a RLP header or length of the /// eip155 fields. + #[doc(hidden)] pub fn fields_len(&self) -> usize { let mut len = 0; len += self.nonce.length(); @@ -153,6 +154,7 @@ impl TxLegacy { /// header. /// /// This __does__ expect the bytes to start with a list header and include a signature. + #[doc(hidden)] pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result> { let header = Header::decode(buf)?; if !header.list {