From 0d872ba81a9ab47c2866ddde173d3fafa76c5a78 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 14 Nov 2024 20:16:04 +0100 Subject: [PATCH] feat: add missing OpTxType trait impls (#258) adds encodeable+decodable+partialeq --- crates/consensus/src/transaction/envelope.rs | 32 +++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/consensus/src/transaction/envelope.rs b/crates/consensus/src/transaction/envelope.rs index 302425560..f47b675b8 100644 --- a/crates/consensus/src/transaction/envelope.rs +++ b/crates/consensus/src/transaction/envelope.rs @@ -8,7 +8,7 @@ use alloy_eips::{ eip7702::SignedAuthorization, }; use alloy_primitives::{Address, Bytes, TxKind, B256, U256}; -use alloy_rlp::{Decodable, Encodable}; +use alloy_rlp::{BufMut, Decodable, Encodable}; use derive_more::Display; use crate::TxDeposit; @@ -79,6 +79,36 @@ impl TryFrom for OpTxType { } } +impl PartialEq for OpTxType { + fn eq(&self, other: &u8) -> bool { + (*self as u8) == *other + } +} + +impl PartialEq for u8 { + fn eq(&self, other: &OpTxType) -> bool { + *self == *other as Self + } +} + +impl Encodable for OpTxType { + fn encode(&self, out: &mut dyn BufMut) { + (*self as u8).encode(out); + } + + fn length(&self) -> usize { + 1 + } +} + +impl Decodable for OpTxType { + 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, modified for OP Stack chains. /// /// # Note: