Skip to content

Commit d0794e5

Browse files
authored
fix: use enveloped encoding for typed transactions (#239)
* fix: use enveloped encoding for typed transactions remove redundant method fix: remove encode_enveloped * improve docs for `encode_signed` * fix vec with_capacity and `into_signed` for 4844 with sidecar fix more raw tx encodings, need to fix length hmm this is all network encoding? still TODO: clear up api boundaries remove encode_signed, decode_signed large refactors to encoding logic todo: analogous methods on Signed, fix tests fix tests fix more tests and lints remove incorrect doc remove unnecessary printlns remove deref, remove tagged legacy fix tests fix Encodable length and add test * fix API changes from tx builder * add test rlp data
1 parent 0c588bf commit d0794e5

10 files changed

Lines changed: 721 additions & 313 deletions

File tree

crates/consensus/src/receipt/envelope.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{Receipt, ReceiptWithBloom, TxType};
2-
use alloy_eips::eip2718::{Decodable2718, Eip2718Error, Encodable2718};
2+
use alloy_eips::eip2718::{Decodable2718, Encodable2718};
33
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable};
44

55
/// Receipt envelope, as defined in [EIP-2718].
@@ -16,8 +16,6 @@ use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable};
1616
pub enum ReceiptEnvelope {
1717
/// Receipt envelope with no type flag.
1818
Legacy(ReceiptWithBloom),
19-
/// Receipt envelope with type flag 0.
20-
TaggedLegacy(ReceiptWithBloom),
2119
/// Receipt envelope with type flag 1, containing a [EIP-2930] receipt.
2220
///
2321
/// [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
@@ -36,7 +34,7 @@ impl ReceiptEnvelope {
3634
/// Return the [`TxType`] of the inner receipt.
3735
pub const fn tx_type(&self) -> TxType {
3836
match self {
39-
Self::Legacy(_) | Self::TaggedLegacy(_) => TxType::Legacy,
37+
Self::Legacy(_) => TxType::Legacy,
4038
Self::Eip2930(_) => TxType::Eip2930,
4139
Self::Eip1559(_) => TxType::Eip1559,
4240
Self::Eip4844(_) => TxType::Eip4844,
@@ -47,23 +45,17 @@ impl ReceiptEnvelope {
4745
/// however, future receipt types may be added.
4846
pub const fn as_receipt_with_bloom(&self) -> Option<&ReceiptWithBloom> {
4947
match self {
50-
Self::Legacy(t)
51-
| Self::TaggedLegacy(t)
52-
| Self::Eip2930(t)
53-
| Self::Eip1559(t)
54-
| Self::Eip4844(t) => Some(t),
48+
Self::Legacy(t) | Self::Eip2930(t) | Self::Eip1559(t) | Self::Eip4844(t) => Some(t),
5549
}
5650
}
5751

5852
/// Return the inner receipt. Currently this is infallible, however, future
5953
/// receipt types may be added.
6054
pub const fn as_receipt(&self) -> Option<&Receipt> {
6155
match self {
62-
Self::Legacy(t)
63-
| Self::TaggedLegacy(t)
64-
| Self::Eip2930(t)
65-
| Self::Eip1559(t)
66-
| Self::Eip4844(t) => Some(&t.receipt),
56+
Self::Legacy(t) | Self::Eip2930(t) | Self::Eip1559(t) | Self::Eip4844(t) => {
57+
Some(&t.receipt)
58+
}
6759
}
6860
}
6961

@@ -100,7 +92,6 @@ impl Decodable for ReceiptEnvelope {
10092
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
10193
match Self::network_decode(buf) {
10294
Ok(t) => Ok(t),
103-
Err(Eip2718Error::RlpError(e)) => Err(e),
10495
Err(_) => Err(alloy_rlp::Error::Custom("Unexpected type")),
10596
}
10697
}
@@ -110,7 +101,6 @@ impl Encodable2718 for ReceiptEnvelope {
110101
fn type_flag(&self) -> Option<u8> {
111102
match self {
112103
Self::Legacy(_) => None,
113-
Self::TaggedLegacy(_) => Some(TxType::Legacy as u8),
114104
Self::Eip2930(_) => Some(TxType::Eip2930 as u8),
115105
Self::Eip1559(_) => Some(TxType::Eip1559 as u8),
116106
Self::Eip4844(_) => Some(TxType::Eip4844 as u8),
@@ -131,30 +121,31 @@ impl Encodable2718 for ReceiptEnvelope {
131121
}
132122

133123
impl Decodable2718 for ReceiptEnvelope {
134-
fn typed_decode(ty: u8, buf: &mut &[u8]) -> Result<Self, Eip2718Error> {
124+
fn typed_decode(ty: u8, buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
135125
let receipt = Decodable::decode(buf)?;
136-
match ty.try_into()? {
137-
TxType::Legacy => Ok(Self::TaggedLegacy(receipt)),
126+
match ty.try_into().map_err(|_| alloy_rlp::Error::Custom("Unexpected type"))? {
138127
TxType::Eip2930 => Ok(Self::Eip2930(receipt)),
139128
TxType::Eip1559 => Ok(Self::Eip1559(receipt)),
140129
TxType::Eip4844 => Ok(Self::Eip4844(receipt)),
130+
TxType::Legacy => {
131+
Err(alloy_rlp::Error::Custom("type-0 eip2718 transactions are not supported"))
132+
}
141133
}
142134
}
143135

144-
fn fallback_decode(buf: &mut &[u8]) -> Result<Self, Eip2718Error> {
136+
fn fallback_decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
145137
Ok(Self::Legacy(Decodable::decode(buf)?))
146138
}
147139
}
148140

149141
#[cfg(any(test, feature = "arbitrary"))]
150142
impl<'a> arbitrary::Arbitrary<'a> for ReceiptEnvelope {
151143
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
152-
let tx_type = u.int_in_range(-1..=2)?;
144+
let tx_type = u.int_in_range(0..=2)?;
153145
let receipt = Receipt::arbitrary(u)?.with_bloom();
154146

155147
match tx_type {
156-
-1 => Ok(Self::Legacy(receipt)),
157-
0 => Ok(Self::TaggedLegacy(receipt)),
148+
0 => Ok(Self::Legacy(receipt)),
158149
1 => Ok(Self::Eip2930(receipt)),
159150
2 => Ok(Self::Eip1559(receipt)),
160151
_ => unreachable!(),

crates/consensus/src/signed.rs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::transaction::SignableTransaction;
22
use alloy_primitives::{Signature, B256};
3-
use alloy_rlp::BufMut;
43

54
/// A transaction with a signature and hash seal.
65
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -10,14 +9,6 @@ pub struct Signed<T, Sig = Signature> {
109
hash: B256,
1110
}
1211

13-
impl<T> std::ops::Deref for Signed<T> {
14-
type Target = T;
15-
16-
fn deref(&self) -> &Self::Target {
17-
&self.tx
18-
}
19-
}
20-
2112
impl<T, Sig> Signed<T, Sig> {
2213
/// Returns a reference to the transaction.
2314
pub const fn tx(&self) -> &T {
@@ -33,6 +24,11 @@ impl<T, Sig> Signed<T, Sig> {
3324
pub const fn hash(&self) -> &B256 {
3425
&self.hash
3526
}
27+
28+
/// Splits the transaction into parts.
29+
pub fn into_parts(self) -> (T, Sig, B256) {
30+
(self.tx, self.signature, self.hash)
31+
}
3632
}
3733

3834
impl<T: SignableTransaction<Sig>, Sig> Signed<T, Sig> {
@@ -45,32 +41,6 @@ impl<T: SignableTransaction<Sig>, Sig> Signed<T, Sig> {
4541
pub fn signature_hash(&self) -> B256 {
4642
self.tx.signature_hash()
4743
}
48-
49-
/// Output the signed RLP for the transaction.
50-
pub fn encode_signed(&self, out: &mut dyn BufMut) {
51-
self.tx.encode_signed(&self.signature, out);
52-
}
53-
54-
/// Produce the RLP encoded signed transaction.
55-
pub fn rlp_signed(&self) -> Vec<u8> {
56-
let mut buf = vec![];
57-
self.encode_signed(&mut buf);
58-
buf
59-
}
60-
}
61-
62-
impl<T: SignableTransaction<Sig>, Sig> alloy_rlp::Encodable for Signed<T, Sig> {
63-
fn encode(&self, out: &mut dyn BufMut) {
64-
self.tx.encode_signed(&self.signature, out)
65-
}
66-
67-
// TODO: impl length
68-
}
69-
70-
impl<T: SignableTransaction<Sig>, Sig> alloy_rlp::Decodable for Signed<T, Sig> {
71-
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
72-
<T as SignableTransaction<Sig>>::decode_signed(buf)
73-
}
7444
}
7545

7646
#[cfg(feature = "k256")]

0 commit comments

Comments
 (0)