alloy_consensus/receipt/
receipts.rs

1use crate::receipt::{
2    Eip2718DecodableReceipt, Eip2718EncodableReceipt, Eip658Value, RlpDecodableReceipt,
3    RlpEncodableReceipt, TxReceipt,
4};
5use alloc::{vec, vec::Vec};
6use alloy_eips::{
7    eip2718::{Eip2718Result, Encodable2718},
8    Decodable2718, Typed2718,
9};
10use alloy_primitives::{Bloom, Log};
11use alloy_rlp::{BufMut, Decodable, Encodable, Header};
12use core::fmt;
13
14/// Receipt containing result of transaction execution.
15#[derive(Clone, Debug, Default, PartialEq, Eq)]
16#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
17#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
18#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
19#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
20#[doc(alias = "TransactionReceipt", alias = "TxReceipt")]
21pub struct Receipt<T = Log> {
22    /// If transaction is executed successfully.
23    ///
24    /// This is the `statusCode`
25    #[cfg_attr(feature = "serde", serde(flatten))]
26    pub status: Eip658Value,
27    /// Gas used
28    #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
29    pub cumulative_gas_used: u64,
30    /// Log send from contracts.
31    pub logs: Vec<T>,
32}
33
34impl<T> Receipt<T> {
35    /// Converts the receipt's log type by applying a function to each log.
36    ///
37    /// Returns the receipt with the new log type
38    pub fn map_logs<U>(self, f: impl FnMut(T) -> U) -> Receipt<U> {
39        let Self { status, cumulative_gas_used, logs } = self;
40        Receipt { status, cumulative_gas_used, logs: logs.into_iter().map(f).collect() }
41    }
42}
43
44impl<T> Receipt<T>
45where
46    T: AsRef<Log>,
47{
48    /// Calculates [`Log`]'s bloom filter. This is slow operation and
49    /// [`ReceiptWithBloom`] can be used to cache this value.
50    pub fn bloom_slow(&self) -> Bloom {
51        self.logs.iter().map(AsRef::as_ref).collect()
52    }
53
54    /// Calculates the bloom filter for the receipt and returns the
55    /// [`ReceiptWithBloom`] container type.
56    pub fn with_bloom(self) -> ReceiptWithBloom<Self> {
57        ReceiptWithBloom { logs_bloom: self.bloom_slow(), receipt: self }
58    }
59}
60
61impl<T> Receipt<T>
62where
63    T: Into<Log>,
64{
65    /// Converts a [`Receipt`] with a custom log type into a [`Receipt`] with the primitives [`Log`]
66    /// type by converting the logs.
67    ///
68    /// This is useful if log types that embed the primitives log type, e.g. the log receipt rpc
69    /// type.
70    pub fn into_primitives_receipt(self) -> Receipt<Log> {
71        self.map_logs(Into::into)
72    }
73}
74
75impl<T> TxReceipt for Receipt<T>
76where
77    T: AsRef<Log> + Clone + fmt::Debug + PartialEq + Eq + Send + Sync,
78{
79    type Log = T;
80
81    fn status_or_post_state(&self) -> Eip658Value {
82        self.status
83    }
84
85    fn status(&self) -> bool {
86        self.status.coerce_status()
87    }
88
89    fn bloom(&self) -> Bloom {
90        self.bloom_slow()
91    }
92
93    fn cumulative_gas_used(&self) -> u64 {
94        self.cumulative_gas_used
95    }
96
97    fn logs(&self) -> &[Self::Log] {
98        &self.logs
99    }
100
101    fn into_logs(self) -> Vec<Self::Log>
102    where
103        Self::Log: Clone,
104    {
105        self.logs
106    }
107}
108
109impl<T: Encodable> Receipt<T> {
110    /// Returns length of RLP-encoded receipt fields with the given [`Bloom`] without an RLP header.
111    pub fn rlp_encoded_fields_length_with_bloom(&self, bloom: &Bloom) -> usize {
112        self.status.length()
113            + self.cumulative_gas_used.length()
114            + bloom.length()
115            + self.logs.length()
116    }
117
118    /// RLP-encodes receipt fields with the given [`Bloom`] without an RLP header.
119    pub fn rlp_encode_fields_with_bloom(&self, bloom: &Bloom, out: &mut dyn BufMut) {
120        self.status.encode(out);
121        self.cumulative_gas_used.encode(out);
122        bloom.encode(out);
123        self.logs.encode(out);
124    }
125
126    /// Returns RLP header for this receipt encoding with the given [`Bloom`].
127    pub fn rlp_header_with_bloom(&self, bloom: &Bloom) -> Header {
128        Header { list: true, payload_length: self.rlp_encoded_fields_length_with_bloom(bloom) }
129    }
130}
131
132impl<T: Encodable> RlpEncodableReceipt for Receipt<T> {
133    fn rlp_encoded_length_with_bloom(&self, bloom: &Bloom) -> usize {
134        self.rlp_header_with_bloom(bloom).length_with_payload()
135    }
136
137    fn rlp_encode_with_bloom(&self, bloom: &Bloom, out: &mut dyn BufMut) {
138        self.rlp_header_with_bloom(bloom).encode(out);
139        self.rlp_encode_fields_with_bloom(bloom, out);
140    }
141}
142
143impl<T: Decodable> Receipt<T> {
144    /// RLP-decodes receipt's field with a [`Bloom`].
145    ///
146    /// Does not expect an RLP header.
147    pub fn rlp_decode_fields_with_bloom(
148        buf: &mut &[u8],
149    ) -> alloy_rlp::Result<ReceiptWithBloom<Self>> {
150        let status = Decodable::decode(buf)?;
151        let cumulative_gas_used = Decodable::decode(buf)?;
152        let logs_bloom = Decodable::decode(buf)?;
153        let logs = Decodable::decode(buf)?;
154
155        Ok(ReceiptWithBloom { receipt: Self { status, cumulative_gas_used, logs }, logs_bloom })
156    }
157}
158
159impl<T: Decodable> RlpDecodableReceipt for Receipt<T> {
160    fn rlp_decode_with_bloom(buf: &mut &[u8]) -> alloy_rlp::Result<ReceiptWithBloom<Self>> {
161        let header = Header::decode(buf)?;
162        if !header.list {
163            return Err(alloy_rlp::Error::UnexpectedString);
164        }
165
166        let remaining = buf.len();
167
168        let this = Self::rlp_decode_fields_with_bloom(buf)?;
169
170        if buf.len() + header.payload_length != remaining {
171            return Err(alloy_rlp::Error::UnexpectedLength);
172        }
173
174        Ok(this)
175    }
176}
177
178impl<T> From<ReceiptWithBloom<Self>> for Receipt<T> {
179    /// Consume the structure, returning only the receipt
180    fn from(receipt_with_bloom: ReceiptWithBloom<Self>) -> Self {
181        receipt_with_bloom.receipt
182    }
183}
184
185/// A collection of receipts organized as a two-dimensional vector.
186#[derive(
187    Clone,
188    Debug,
189    PartialEq,
190    Eq,
191    derive_more::Deref,
192    derive_more::DerefMut,
193    derive_more::From,
194    derive_more::IntoIterator,
195)]
196#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
197pub struct Receipts<T> {
198    /// A two-dimensional vector of [`Receipt`] instances.
199    pub receipt_vec: Vec<Vec<T>>,
200}
201
202impl<T> Receipts<T> {
203    /// Returns the length of the [`Receipts`] vector.
204    pub const fn len(&self) -> usize {
205        self.receipt_vec.len()
206    }
207
208    /// Returns `true` if the [`Receipts`] vector is empty.
209    pub const fn is_empty(&self) -> bool {
210        self.receipt_vec.is_empty()
211    }
212
213    /// Push a new vector of receipts into the [`Receipts`] collection.
214    pub fn push(&mut self, receipts: Vec<T>) {
215        self.receipt_vec.push(receipts);
216    }
217}
218
219impl<T> From<Vec<T>> for Receipts<T> {
220    fn from(block_receipts: Vec<T>) -> Self {
221        Self { receipt_vec: vec![block_receipts] }
222    }
223}
224
225impl<T> FromIterator<Vec<T>> for Receipts<T> {
226    fn from_iter<I: IntoIterator<Item = Vec<T>>>(iter: I) -> Self {
227        Self { receipt_vec: iter.into_iter().collect() }
228    }
229}
230
231impl<T: Encodable> Encodable for Receipts<T> {
232    fn encode(&self, out: &mut dyn BufMut) {
233        self.receipt_vec.encode(out)
234    }
235
236    fn length(&self) -> usize {
237        self.receipt_vec.length()
238    }
239}
240
241impl<T: Decodable> Decodable for Receipts<T> {
242    fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
243        Ok(Self { receipt_vec: Decodable::decode(buf)? })
244    }
245}
246
247impl<T> Default for Receipts<T> {
248    fn default() -> Self {
249        Self { receipt_vec: Default::default() }
250    }
251}
252
253/// [`Receipt`] with calculated bloom filter.
254///
255/// This convenience type allows us to lazily calculate the bloom filter for a
256/// receipt, similar to [`Sealed`].
257///
258/// [`Sealed`]: crate::Sealed
259#[derive(Clone, Debug, Default, PartialEq, Eq)]
260#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
261#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
262#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
263#[doc(alias = "TransactionReceiptWithBloom", alias = "TxReceiptWithBloom")]
264pub struct ReceiptWithBloom<T = Receipt<Log>> {
265    #[cfg_attr(feature = "serde", serde(flatten))]
266    /// The receipt.
267    pub receipt: T,
268    /// The bloom filter.
269    pub logs_bloom: Bloom,
270}
271
272impl<R> TxReceipt for ReceiptWithBloom<R>
273where
274    R: TxReceipt,
275{
276    type Log = R::Log;
277
278    fn status_or_post_state(&self) -> Eip658Value {
279        self.receipt.status_or_post_state()
280    }
281
282    fn status(&self) -> bool {
283        self.receipt.status()
284    }
285
286    fn bloom(&self) -> Bloom {
287        self.logs_bloom
288    }
289
290    fn bloom_cheap(&self) -> Option<Bloom> {
291        Some(self.logs_bloom)
292    }
293
294    fn cumulative_gas_used(&self) -> u64 {
295        self.receipt.cumulative_gas_used()
296    }
297
298    fn logs(&self) -> &[Self::Log] {
299        self.receipt.logs()
300    }
301}
302
303impl<R> From<R> for ReceiptWithBloom<R>
304where
305    R: TxReceipt,
306{
307    fn from(receipt: R) -> Self {
308        let logs_bloom = receipt.bloom();
309        Self { logs_bloom, receipt }
310    }
311}
312
313impl<R> ReceiptWithBloom<R> {
314    /// Converts the receipt type by applying the given closure to it.
315    ///
316    /// Returns the type with the new receipt type.
317    pub fn map_receipt<U>(self, f: impl FnOnce(R) -> U) -> ReceiptWithBloom<U> {
318        let Self { receipt, logs_bloom } = self;
319        ReceiptWithBloom { receipt: f(receipt), logs_bloom }
320    }
321
322    /// Create new [ReceiptWithBloom]
323    pub const fn new(receipt: R, logs_bloom: Bloom) -> Self {
324        Self { receipt, logs_bloom }
325    }
326
327    /// Consume the structure, returning the receipt and the bloom filter
328    pub fn into_components(self) -> (R, Bloom) {
329        (self.receipt, self.logs_bloom)
330    }
331
332    /// Returns a reference to the bloom.
333    pub const fn bloom_ref(&self) -> &Bloom {
334        &self.logs_bloom
335    }
336}
337
338impl<L> ReceiptWithBloom<Receipt<L>> {
339    /// Converts the receipt's log type by applying a function to each log.
340    ///
341    /// Returns the receipt with the new log type.
342    pub fn map_logs<U>(self, f: impl FnMut(L) -> U) -> ReceiptWithBloom<Receipt<U>> {
343        let Self { receipt, logs_bloom } = self;
344        ReceiptWithBloom { receipt: receipt.map_logs(f), logs_bloom }
345    }
346
347    /// Converts a [`ReceiptWithBloom`] with a custom log type into a [`ReceiptWithBloom`] with the
348    /// primitives [`Log`] type by converting the logs.
349    ///
350    /// This is useful if log types that embed the primitives log type, e.g. the log receipt rpc
351    /// type.
352    pub fn into_primitives_receipt(self) -> ReceiptWithBloom<Receipt<Log>>
353    where
354        L: Into<Log>,
355    {
356        self.map_logs(Into::into)
357    }
358}
359
360impl<R: RlpEncodableReceipt> Encodable for ReceiptWithBloom<R> {
361    fn encode(&self, out: &mut dyn BufMut) {
362        self.receipt.rlp_encode_with_bloom(&self.logs_bloom, out);
363    }
364
365    fn length(&self) -> usize {
366        self.receipt.rlp_encoded_length_with_bloom(&self.logs_bloom)
367    }
368}
369
370impl<R: RlpDecodableReceipt> Decodable for ReceiptWithBloom<R> {
371    fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
372        R::rlp_decode_with_bloom(buf)
373    }
374}
375
376impl<R: Typed2718> Typed2718 for ReceiptWithBloom<R> {
377    fn ty(&self) -> u8 {
378        self.receipt.ty()
379    }
380}
381
382impl<R> Encodable2718 for ReceiptWithBloom<R>
383where
384    R: Eip2718EncodableReceipt + Send + Sync,
385{
386    fn encode_2718_len(&self) -> usize {
387        self.receipt.eip2718_encoded_length_with_bloom(&self.logs_bloom)
388    }
389
390    fn encode_2718(&self, out: &mut dyn BufMut) {
391        self.receipt.eip2718_encode_with_bloom(&self.logs_bloom, out);
392    }
393}
394
395impl<R> Decodable2718 for ReceiptWithBloom<R>
396where
397    R: Eip2718DecodableReceipt,
398{
399    fn typed_decode(ty: u8, buf: &mut &[u8]) -> Eip2718Result<Self> {
400        R::typed_decode_with_bloom(ty, buf)
401    }
402
403    fn fallback_decode(buf: &mut &[u8]) -> Eip2718Result<Self> {
404        R::fallback_decode_with_bloom(buf)
405    }
406}
407
408#[cfg(any(test, feature = "arbitrary"))]
409impl<'a, R> arbitrary::Arbitrary<'a> for ReceiptWithBloom<R>
410where
411    R: arbitrary::Arbitrary<'a>,
412{
413    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
414        Ok(Self { receipt: R::arbitrary(u)?, logs_bloom: Bloom::arbitrary(u)? })
415    }
416}
417
418#[cfg(all(feature = "serde", feature = "serde-bincode-compat"))]
419pub(crate) mod serde_bincode_compat {
420    use alloc::borrow::Cow;
421    use serde::{Deserialize, Deserializer, Serialize, Serializer};
422    use serde_with::{DeserializeAs, SerializeAs};
423
424    /// Bincode-compatible [`super::Receipt`] serde implementation.
425    ///
426    /// Intended to use with the [`serde_with::serde_as`] macro in the following way:
427    /// ```rust
428    /// use alloy_consensus::{serde_bincode_compat, Receipt};
429    /// use serde::{de::DeserializeOwned, Deserialize, Serialize};
430    /// use serde_with::serde_as;
431    ///
432    /// #[serde_as]
433    /// #[derive(Serialize, Deserialize)]
434    /// struct Data<T: Serialize + DeserializeOwned + Clone + 'static> {
435    ///     #[serde_as(as = "serde_bincode_compat::Receipt<'_, T>")]
436    ///     receipt: Receipt<T>,
437    /// }
438    /// ```
439    #[derive(Debug, Serialize, Deserialize)]
440    pub struct Receipt<'a, T: Clone = alloy_primitives::Log> {
441        logs: Cow<'a, [T]>,
442        status: bool,
443        cumulative_gas_used: u64,
444    }
445
446    impl<'a, T: Clone> From<&'a super::Receipt<T>> for Receipt<'a, T> {
447        fn from(value: &'a super::Receipt<T>) -> Self {
448            Self {
449                logs: Cow::Borrowed(&value.logs),
450                // OP has no post state root variant
451                status: value.status.coerce_status(),
452                cumulative_gas_used: value.cumulative_gas_used,
453            }
454        }
455    }
456
457    impl<'a, T: Clone> From<Receipt<'a, T>> for super::Receipt<T> {
458        fn from(value: Receipt<'a, T>) -> Self {
459            Self {
460                status: value.status.into(),
461                cumulative_gas_used: value.cumulative_gas_used,
462                logs: value.logs.into_owned(),
463            }
464        }
465    }
466
467    impl<T: Serialize + Clone> SerializeAs<super::Receipt<T>> for Receipt<'_, T> {
468        fn serialize_as<S>(source: &super::Receipt<T>, serializer: S) -> Result<S::Ok, S::Error>
469        where
470            S: Serializer,
471        {
472            Receipt::<'_, T>::from(source).serialize(serializer)
473        }
474    }
475
476    impl<'de, T: Deserialize<'de> + Clone> DeserializeAs<'de, super::Receipt<T>> for Receipt<'de, T> {
477        fn deserialize_as<D>(deserializer: D) -> Result<super::Receipt<T>, D::Error>
478        where
479            D: Deserializer<'de>,
480        {
481            Receipt::<'_, T>::deserialize(deserializer).map(Into::into)
482        }
483    }
484
485    #[cfg(test)]
486    mod tests {
487        use super::super::{serde_bincode_compat, Receipt};
488        use alloy_primitives::Log;
489        use arbitrary::Arbitrary;
490        use bincode::config;
491        use rand::Rng;
492        use serde::{de::DeserializeOwned, Deserialize, Serialize};
493        use serde_with::serde_as;
494
495        #[test]
496        fn test_receipt_bincode_roundtrip() {
497            #[serde_as]
498            #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
499            struct Data<T: Serialize + DeserializeOwned + Clone + 'static> {
500                #[serde_as(as = "serde_bincode_compat::Receipt<'_,T>")]
501                receipt: Receipt<T>,
502            }
503
504            let mut bytes = [0u8; 1024];
505            rand::thread_rng().fill(bytes.as_mut_slice());
506            let mut data = Data {
507                receipt: Receipt::arbitrary(&mut arbitrary::Unstructured::new(&bytes)).unwrap(),
508            };
509            // ensure we don't have an invalid poststate variant
510            data.receipt.status = data.receipt.status.coerce_status().into();
511
512            let encoded = bincode::serde::encode_to_vec(&data, config::legacy()).unwrap();
513            let (decoded, _) =
514                bincode::serde::decode_from_slice::<Data<Log>, _>(&encoded, config::legacy())
515                    .unwrap();
516            assert_eq!(decoded, data);
517        }
518    }
519}
520
521#[cfg(test)]
522mod test {
523    use super::*;
524    use crate::ReceiptEnvelope;
525    use alloy_rlp::{Decodable, Encodable};
526
527    const fn assert_tx_receipt<T: TxReceipt>() {}
528
529    #[test]
530    const fn assert_receipt() {
531        assert_tx_receipt::<Receipt>();
532        assert_tx_receipt::<ReceiptWithBloom<Receipt>>();
533    }
534
535    #[cfg(feature = "serde")]
536    #[test]
537    fn root_vs_status() {
538        let receipt = super::Receipt::<()> {
539            status: super::Eip658Value::Eip658(true),
540            cumulative_gas_used: 0,
541            logs: Vec::new(),
542        };
543
544        let json = serde_json::to_string(&receipt).unwrap();
545        assert_eq!(json, r#"{"status":"0x1","cumulativeGasUsed":"0x0","logs":[]}"#);
546
547        let receipt = super::Receipt::<()> {
548            status: super::Eip658Value::PostState(Default::default()),
549            cumulative_gas_used: 0,
550            logs: Vec::new(),
551        };
552
553        let json = serde_json::to_string(&receipt).unwrap();
554        assert_eq!(
555            json,
556            r#"{"root":"0x0000000000000000000000000000000000000000000000000000000000000000","cumulativeGasUsed":"0x0","logs":[]}"#
557        );
558    }
559
560    #[cfg(feature = "serde")]
561    #[test]
562    fn deser_pre658() {
563        use alloy_primitives::b256;
564
565        let json = r#"{"root":"0x284d35bf53b82ef480ab4208527325477439c64fb90ef518450f05ee151c8e10","cumulativeGasUsed":"0x0","logs":[]}"#;
566
567        let receipt: super::Receipt<()> = serde_json::from_str(json).unwrap();
568
569        assert_eq!(
570            receipt.status,
571            super::Eip658Value::PostState(b256!(
572                "284d35bf53b82ef480ab4208527325477439c64fb90ef518450f05ee151c8e10"
573            ))
574        );
575    }
576
577    #[test]
578    fn roundtrip_encodable_eip1559() {
579        let receipts =
580            Receipts { receipt_vec: vec![vec![ReceiptEnvelope::Eip1559(Default::default())]] };
581
582        let mut out = vec![];
583        receipts.encode(&mut out);
584
585        let mut out = out.as_slice();
586        let decoded = Receipts::<ReceiptEnvelope>::decode(&mut out).unwrap();
587
588        assert_eq!(receipts, decoded);
589    }
590}