From 6b13cf521f2868f7925a3cf6cc519d50e8339269 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 14 Nov 2024 18:28:32 +0100 Subject: [PATCH] feat: add rlp for txtype --- crates/consensus/src/transaction/envelope.rs | 25 +++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/consensus/src/transaction/envelope.rs b/crates/consensus/src/transaction/envelope.rs index 902356fbea9..5be7ab4e2d6 100644 --- a/crates/consensus/src/transaction/envelope.rs +++ b/crates/consensus/src/transaction/envelope.rs @@ -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], @@ -61,6 +61,12 @@ impl PartialEq for TxType { } } +impl PartialEq 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 { @@ -83,6 +89,23 @@ impl TryFrom 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 { + 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: