Skip to content

Commit

Permalink
feat: add rlp for txtype
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Nov 14, 2024
1 parent 2ab90d3 commit 6b13cf5
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use alloy_eips::{
eip2930::AccessList,
};
use alloy_primitives::{Bytes, ChainId, PrimitiveSignature as Signature, TxKind, B256, U256};
use alloy_rlp::{Decodable, Encodable};
use alloy_rlp::{BufMut, Decodable, Encodable};
use core::fmt;

/// Ethereum `TransactionType` flags as specified in EIPs [2718], [1559], [2930],
Expand Down Expand Up @@ -61,6 +61,12 @@ impl PartialEq<u8> for TxType {
}
}

impl PartialEq<TxType> for u8 {
fn eq(&self, other: &TxType) -> bool {
*self == *other as Self
}
}

#[cfg(any(test, feature = "arbitrary"))]
impl arbitrary::Arbitrary<'_> for TxType {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
Expand All @@ -83,6 +89,23 @@ impl TryFrom<u8> for TxType {
}
}

impl Encodable for TxType {
fn encode(&self, out: &mut dyn BufMut) {
(*self as u8).encode(out);
}

fn length(&self) -> usize {
1
}
}

impl Decodable for TxType {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
let ty = u8::decode(buf)?;
Self::try_from(ty).map_err(|_| alloy_rlp::Error::Custom("invalid transaction type"))
}
}

/// The Ethereum [EIP-2718] Transaction Envelope.
///
/// # Note:
Expand Down

0 comments on commit 6b13cf5

Please sign in to comment.