Skip to main content

alloy_eips/eip7594/
rlp.rs

1use alloc::vec::Vec;
2use alloy_rlp::BufMut;
3
4/// Selects the blob payload representation used when encoding a blob transaction sidecar.
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
6pub enum BlobSidecarEncoding {
7    /// Encode the complete sidecar, including its blob payloads.
8    #[default]
9    WithBlobs,
10    /// Encode an empty blob list while retaining commitments and proofs.
11    ///
12    /// This is the sidecar representation used by eth/72 `PooledTransactions` responses as
13    /// specified by [EIP-8070].
14    ///
15    /// [EIP-8070]: https://eips.ethereum.org/EIPS/eip-8070
16    WithoutBlobs,
17}
18
19/// A helper trait for encoding [EIP-7594](https://eips.ethereum.org/EIPS/eip-7594) sidecars.
20pub trait Encodable7594 {
21    /// The length of the 7594 encoded envelope. This is the length of the wrapper
22    /// version + the length of the inner encoding.
23    fn encode_7594_len(&self) -> usize;
24
25    /// Encode the sidecar according to [EIP-7594] rules. First a 1-byte
26    /// wrapper version (if any), then the body of the sidecar.
27    ///
28    /// [EIP-7594] inner encodings are unspecified, and produce an opaque
29    /// bytestring.
30    ///
31    /// [EIP-7594]: https://eips.ethereum.org/EIPS/eip-7594
32    fn encode_7594(&self, out: &mut dyn BufMut);
33
34    /// Returns the length of the EIP-7594 encoding for the selected blob representation.
35    ///
36    /// The default delegates to [`Self::encode_7594_len`] because encodings without blob payloads
37    /// only differ for sidecar types that contain blobs.
38    fn encode_7594_len_with(&self, _encoding: BlobSidecarEncoding) -> usize {
39        self.encode_7594_len()
40    }
41
42    /// Encodes the sidecar using the selected blob representation.
43    ///
44    /// The default delegates to [`Self::encode_7594`] because encodings without blob payloads only
45    /// differ for sidecar types that contain blobs.
46    fn encode_7594_with(&self, _encoding: BlobSidecarEncoding, out: &mut dyn BufMut) {
47        self.encode_7594(out);
48    }
49
50    /// Encode the sidecar according to [EIP-7594] rules. First a 1-byte
51    /// wrapper version (if any), then the body of the sidecar.
52    ///
53    /// This is a convenience method for encoding into a vec, and returning the
54    /// vec.
55    fn encoded_7594(&self) -> Vec<u8> {
56        let mut out = Vec::with_capacity(self.encode_7594_len());
57        self.encode_7594(&mut out);
58        out
59    }
60}
61
62/// A helper trait for decoding [EIP-7594](https://eips.ethereum.org/EIPS/eip-7594) sidecars.
63pub trait Decodable7594: Sized {
64    /// Decode the sidecar according to [EIP-7594] rules. First a 1-byte
65    /// wrapper version (if any), then the body of the sidecar.
66    ///
67    /// [EIP-7594] inner encodings are unspecified, and produce an opaque
68    /// bytestring.
69    ///
70    /// [EIP-7594]: https://eips.ethereum.org/EIPS/eip-7594
71    fn decode_7594(buf: &mut &[u8]) -> alloy_rlp::Result<Self>;
72}