Skip to content

Commit

Permalink
feat: Export inner encoding / decoding functions from Tx* types (#529)
Browse files Browse the repository at this point in the history
* feat: Export inner encoding / decoding functions from `Tx*` types

* Add `#[doc(hidden)]` to exposed low-level encoding functions
  • Loading branch information
clabby committed Apr 16, 2024
1 parent 00c6449 commit 1727751
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
13 changes: 8 additions & 5 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ 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.
#[doc(hidden)]
pub fn fields_len(&self) -> usize {
let mut len = 0;
len += self.chain_id.length();
len += self.nonce.length();
Expand Down Expand Up @@ -175,8 +176,9 @@ 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.
#[doc(hidden)]
pub fn encode_with_signature(
&self,
signature: &Signature,
out: &mut dyn BufMut,
Expand All @@ -200,7 +202,8 @@ 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<Signed<Self>> {
#[doc(hidden)]
pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let header = Header::decode(buf)?;
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
Expand Down
9 changes: 6 additions & 3 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ impl TxEip2930 {
}

/// Outputs the length of the transaction's fields, without a RLP header.
pub(crate) fn fields_len(&self) -> usize {
#[doc(hidden)]
pub fn fields_len(&self) -> usize {
let mut len = 0;
len += self.chain_id.length();
len += self.nonce.length();
Expand Down Expand Up @@ -154,7 +155,8 @@ 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(
#[doc(hidden)]
pub fn encode_with_signature(
&self,
signature: &Signature,
out: &mut dyn BufMut,
Expand Down Expand Up @@ -190,7 +192,8 @@ 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<Signed<Self>> {
#[doc(hidden)]
pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let header = Header::decode(buf)?;
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
Expand Down
30 changes: 22 additions & 8 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ impl TxEip4844Variant {
}
}

pub(crate) fn fields_len(&self) -> usize {
/// 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(),
TxEip4844Variant::TxEip4844WithSidecar(tx) => tx.tx().fields_len(),
Expand All @@ -160,7 +162,8 @@ 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(
#[doc(hidden)]
pub fn encode_with_signature(
&self,
signature: &Signature,
out: &mut dyn BufMut,
Expand Down Expand Up @@ -196,7 +199,14 @@ impl TxEip4844Variant {
}
}

pub(crate) fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
/// 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.
#[doc(hidden)]
pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let mut current_buf = *buf;
let _header = Header::decode(&mut current_buf)?;

Expand Down Expand Up @@ -510,7 +520,8 @@ impl TxEip4844 {
}

/// Outputs the length of the transaction's fields, without a RLP header.
pub(crate) fn fields_len(&self) -> usize {
#[doc(hidden)]
pub fn fields_len(&self) -> usize {
let mut len = 0;
len += self.chain_id.length();
len += self.nonce.length();
Expand Down Expand Up @@ -588,7 +599,8 @@ 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(
#[doc(hidden)]
pub fn encode_with_signature(
&self,
signature: &Signature,
out: &mut dyn BufMut,
Expand Down Expand Up @@ -624,7 +636,8 @@ 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<Signed<Self>> {
#[doc(hidden)]
pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let header = Header::decode(buf)?;
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
Expand Down Expand Up @@ -832,7 +845,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 };

Expand All @@ -858,7 +871,8 @@ 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<Signed<Self>> {
#[doc(hidden)]
pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let header = Header::decode(buf)?;
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
Expand Down
6 changes: 4 additions & 2 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ 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 {
#[doc(hidden)]
pub fn fields_len(&self) -> usize {
let mut len = 0;
len += self.nonce.length();
len += self.gas_price.length();
Expand Down Expand Up @@ -153,7 +154,8 @@ 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<Signed<Self>> {
#[doc(hidden)]
pub fn decode_signed_fields(buf: &mut &[u8]) -> alloy_rlp::Result<Signed<Self>> {
let header = Header::decode(buf)?;
if !header.list {
return Err(alloy_rlp::Error::UnexpectedString);
Expand Down

0 comments on commit 1727751

Please sign in to comment.