Skip to content

Commit

Permalink
feat: Export inner encoding / decoding functions from Tx* types
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Apr 13, 2024
1 parent 8cb0307 commit 842bf91
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
8 changes: 4 additions & 4 deletions crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl TxEip1559 {
}

/// Encodes only the transaction's fields into the desired buffer, 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();
Expand Down Expand Up @@ -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,
Expand All @@ -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<Signed<Self>> {
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: 3 additions & 3 deletions crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<Signed<Self>> {
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
23 changes: 15 additions & 8 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
Expand Down Expand Up @@ -196,7 +197,13 @@ 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.
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 +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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<Signed<Self>> {
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 +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 };

Expand All @@ -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<Signed<Self>> {
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
4 changes: 2 additions & 2 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<Signed<Self>> {
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 842bf91

Please sign in to comment.