tempo_primitives/reth_compat/transaction/
envelope.rs1use crate::transaction::envelope::TempoTxEnvelope;
2
3impl reth_primitives_traits::InMemorySize for TempoTxEnvelope {
4 fn size(&self) -> usize {
5 match self {
6 Self::Legacy(tx) => tx.size(),
7 Self::Eip2930(tx) => tx.size(),
8 Self::Eip1559(tx) => tx.size(),
9 Self::Eip7702(tx) => tx.size(),
10 Self::AA(tx) => tx.size(),
11 }
12 }
13}
14
15#[cfg(feature = "reth-codec")]
16mod codec {
17 use crate::{
18 TempoSignature, TempoTransaction,
19 transaction::{
20 envelope::{TEMPO_SYSTEM_TX_SIGNATURE, TempoTxEnvelope, TempoTxType},
21 tt_signed::AASigned,
22 },
23 };
24
25 use alloy_consensus::{TxEip1559, TxEip2930, TxEip7702, TxLegacy};
26 use alloy_eips::eip2718::EIP7702_TX_TYPE_ID;
27 use alloy_primitives::{
28 Bytes, Signature,
29 bytes::{self, BufMut},
30 };
31 use reth_codecs::{
32 Compact, DecompressError,
33 alloy::transaction::{CompactEnvelope, Envelope},
34 txtype::{
35 COMPACT_EXTENDED_IDENTIFIER_FLAG, COMPACT_IDENTIFIER_EIP1559,
36 COMPACT_IDENTIFIER_EIP2930, COMPACT_IDENTIFIER_LEGACY,
37 },
38 };
39
40 impl reth_codecs::alloy::transaction::FromTxCompact for TempoTxEnvelope {
41 type TxType = TempoTxType;
42
43 fn from_tx_compact(
44 buf: &[u8],
45 tx_type: Self::TxType,
46 signature: Signature,
47 ) -> (Self, &[u8]) {
48 use alloy_consensus::Signed;
49 use reth_codecs::Compact;
50
51 match tx_type {
52 TempoTxType::Legacy => {
53 let (tx, buf) = TxLegacy::from_compact(buf, buf.len());
54 let tx = Signed::new_unhashed(tx, signature);
55 (Self::Legacy(tx), buf)
56 }
57 TempoTxType::Eip2930 => {
58 let (tx, buf) = TxEip2930::from_compact(buf, buf.len());
59 let tx = Signed::new_unhashed(tx, signature);
60 (Self::Eip2930(tx), buf)
61 }
62 TempoTxType::Eip1559 => {
63 let (tx, buf) = TxEip1559::from_compact(buf, buf.len());
64 let tx = Signed::new_unhashed(tx, signature);
65 (Self::Eip1559(tx), buf)
66 }
67 TempoTxType::Eip7702 => {
68 let (tx, buf) = TxEip7702::from_compact(buf, buf.len());
69 let tx = Signed::new_unhashed(tx, signature);
70 (Self::Eip7702(tx), buf)
71 }
72 TempoTxType::AA => {
73 let (tx, buf) = TempoTransaction::from_compact(buf, buf.len());
74 let (sig_bytes, buf) = Bytes::from_compact(buf, buf.len());
78 let aa_sig = TempoSignature::from_bytes(&sig_bytes)
79 .map_err(|e| panic!("Failed to decode AA signature: {e}"))
80 .unwrap();
81 let tx = AASigned::new_unhashed(tx, aa_sig);
82 (Self::AA(tx), buf)
83 }
84 }
85 }
86 }
87
88 impl reth_codecs::alloy::transaction::ToTxCompact for TempoTxEnvelope {
89 fn to_tx_compact(&self, buf: &mut (impl BufMut + AsMut<[u8]>)) {
90 match self {
91 Self::Legacy(tx) => tx.tx().to_compact(buf),
92 Self::Eip2930(tx) => tx.tx().to_compact(buf),
93 Self::Eip1559(tx) => tx.tx().to_compact(buf),
94 Self::Eip7702(tx) => tx.tx().to_compact(buf),
95 Self::AA(tx) => {
96 let mut len = tx.tx().to_compact(buf);
97 len += tx.signature().to_bytes().to_compact(buf);
98 len
99 }
100 };
101 }
102 }
103
104 impl Envelope for TempoTxEnvelope {
105 fn signature(&self) -> &Signature {
106 match self {
107 Self::Legacy(tx) => tx.signature(),
108 Self::Eip2930(tx) => tx.signature(),
109 Self::Eip1559(tx) => tx.signature(),
110 Self::Eip7702(tx) => tx.signature(),
111 Self::AA(_tx) => {
112 &TEMPO_SYSTEM_TX_SIGNATURE
120 }
121 }
122 }
123
124 fn tx_type(&self) -> Self::TxType {
125 Self::tx_type(self)
126 }
127 }
128
129 impl Compact for TempoTxType {
130 fn to_compact<B>(&self, buf: &mut B) -> usize
131 where
132 B: BufMut + AsMut<[u8]>,
133 {
134 match self {
135 Self::Legacy => COMPACT_IDENTIFIER_LEGACY,
136 Self::Eip2930 => COMPACT_IDENTIFIER_EIP2930,
137 Self::Eip1559 => COMPACT_IDENTIFIER_EIP1559,
138 Self::Eip7702 => {
139 buf.put_u8(EIP7702_TX_TYPE_ID);
140 COMPACT_EXTENDED_IDENTIFIER_FLAG
141 }
142 Self::AA => {
143 buf.put_u8(crate::transaction::TEMPO_TX_TYPE_ID);
144 COMPACT_EXTENDED_IDENTIFIER_FLAG
145 }
146 }
147 }
148
149 fn from_compact(mut buf: &[u8], identifier: usize) -> (Self, &[u8]) {
150 use bytes::Buf;
151 (
152 match identifier {
153 COMPACT_IDENTIFIER_LEGACY => Self::Legacy,
154 COMPACT_IDENTIFIER_EIP2930 => Self::Eip2930,
155 COMPACT_IDENTIFIER_EIP1559 => Self::Eip1559,
156 COMPACT_EXTENDED_IDENTIFIER_FLAG => {
157 let extended_identifier = buf.get_u8();
158 match extended_identifier {
159 EIP7702_TX_TYPE_ID => Self::Eip7702,
160 crate::transaction::TEMPO_TX_TYPE_ID => Self::AA,
161 _ => panic!("Unsupported TxType identifier: {extended_identifier}"),
162 }
163 }
164 _ => panic!("Unknown identifier for TxType: {identifier}"),
165 },
166 buf,
167 )
168 }
169 }
170
171 impl Compact for TempoTxEnvelope {
172 fn to_compact<B>(&self, buf: &mut B) -> usize
173 where
174 B: BufMut + AsMut<[u8]>,
175 {
176 CompactEnvelope::to_compact(self, buf)
177 }
178
179 fn from_compact(buf: &[u8], len: usize) -> (Self, &[u8]) {
180 CompactEnvelope::from_compact(buf, len)
181 }
182 }
183
184 impl reth_db_api::table::Compress for TempoTxEnvelope {
185 type Compressed = alloc::vec::Vec<u8>;
186
187 fn compress_to_buf<B: alloy_primitives::bytes::BufMut + AsMut<[u8]>>(&self, buf: &mut B) {
188 let _ = Compact::to_compact(self, buf);
189 }
190 }
191
192 impl reth_db_api::table::Decompress for TempoTxEnvelope {
193 fn decompress(value: &[u8]) -> Result<Self, DecompressError> {
194 let (obj, _) = Compact::from_compact(value, value.len());
195 Ok(obj)
196 }
197 }
198}