Skip to main content

reth_ethereum_primitives/
receipt.rs

1use alloy_consensus::TxType;
2pub use alloy_consensus::{EthereumReceipt, TxTy};
3
4/// Raw ethereum receipt.
5pub type Receipt<T = TxType> = EthereumReceipt<T>;
6
7#[cfg(feature = "rpc")]
8/// Receipt representation for RPC.
9pub type RpcReceipt<T = TxType> = EthereumReceipt<T, alloy_rpc_types_eth::Log>;
10
11#[cfg(test)]
12mod tests {
13    use super::*;
14    use crate::TransactionSigned;
15    use alloy_consensus::{ReceiptWithBloom, TxReceipt, TxType};
16    use alloy_eips::eip2718::Encodable2718;
17    use alloy_primitives::{
18        address, b256, bloom, bytes, hex_literal::hex, Address, Bloom, Bytes, Log, LogData,
19    };
20    use alloy_rlp::{Decodable, Encodable};
21    use reth_codecs::Compact;
22    use reth_primitives_traits::proofs::{
23        calculate_receipt_root, calculate_transaction_root, calculate_withdrawals_root,
24    };
25
26    /// Ethereum full block.
27    ///
28    /// Withdrawals can be optionally included at the end of the RLP encoded message.
29    pub(crate) type Block<T = TransactionSigned> = alloy_consensus::Block<T>;
30
31    #[test]
32    #[cfg(feature = "reth-codec")]
33    fn test_decode_receipt() {
34        reth_codecs::test_utils::test_decode::<Receipt<TxType>>(&hex!(
35            "c428b52ffd23fc42696156b10200f034792b6a94c3850215c2fef7aea361a0c31b79d9a32652eefc0d4e2e730036061cff7344b6fc6132b50cda0ed810a991ae58ef013150c12b2522533cb3b3a8b19b7786a8b5ff1d3cdc84225e22b02def168c8858df"
36        ));
37    }
38
39    // Test vector from: https://eips.ethereum.org/EIPS/eip-2481
40    #[test]
41    fn encode_legacy_receipt() {
42        let expected = hex!(
43            "f901668001b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f85ff85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff"
44        );
45
46        let mut data = Vec::with_capacity(expected.length());
47        let receipt = ReceiptWithBloom {
48            receipt: Receipt {
49                tx_type: TxType::Legacy,
50                cumulative_gas_used: 0x1u64,
51                logs: vec![Log::new_unchecked(
52                    address!("0x0000000000000000000000000000000000000011"),
53                    vec![
54                        b256!("0x000000000000000000000000000000000000000000000000000000000000dead"),
55                        b256!("0x000000000000000000000000000000000000000000000000000000000000beef"),
56                    ],
57                    bytes!("0100ff"),
58                )],
59                success: false,
60            },
61            logs_bloom: [0; 256].into(),
62        };
63
64        receipt.encode(&mut data);
65
66        // check that the rlp length equals the length of the expected rlp
67        assert_eq!(receipt.length(), expected.len());
68        assert_eq!(data, expected);
69    }
70
71    // Test vector from: https://eips.ethereum.org/EIPS/eip-2481
72    #[test]
73    fn decode_legacy_receipt() {
74        let data = hex!(
75            "f901668001b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f85ff85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff"
76        );
77
78        // EIP658Receipt
79        let expected = ReceiptWithBloom {
80            receipt: Receipt {
81                tx_type: TxType::Legacy,
82                cumulative_gas_used: 0x1u64,
83                logs: vec![Log::new_unchecked(
84                    address!("0x0000000000000000000000000000000000000011"),
85                    vec![
86                        b256!("0x000000000000000000000000000000000000000000000000000000000000dead"),
87                        b256!("0x000000000000000000000000000000000000000000000000000000000000beef"),
88                    ],
89                    bytes!("0100ff"),
90                )],
91                success: false,
92            },
93            logs_bloom: [0; 256].into(),
94        };
95
96        let receipt = ReceiptWithBloom::decode(&mut &data[..]).unwrap();
97        assert_eq!(receipt, expected);
98    }
99
100    #[test]
101    fn gigantic_receipt() {
102        let receipt = Receipt {
103            cumulative_gas_used: 16747627,
104            success: true,
105            tx_type: TxType::Legacy,
106            logs: vec![
107                Log::new_unchecked(
108                    address!("0x4bf56695415f725e43c3e04354b604bcfb6dfb6e"),
109                    vec![b256!(
110                        "0xc69dc3d7ebff79e41f525be431d5cd3cc08f80eaf0f7819054a726eeb7086eb9"
111                    )],
112                    Bytes::from(vec![1; 0xffffff]),
113                ),
114                Log::new_unchecked(
115                    address!("0xfaca325c86bf9c2d5b413cd7b90b209be92229c2"),
116                    vec![b256!(
117                        "0x8cca58667b1e9ffa004720ac99a3d61a138181963b294d270d91c53d36402ae2"
118                    )],
119                    Bytes::from(vec![1; 0xffffff]),
120                ),
121            ],
122        };
123
124        let mut data = vec![];
125        receipt.to_compact(&mut data);
126        let (decoded, _) = Receipt::<TxType>::from_compact(&data[..], data.len());
127        assert_eq!(decoded, receipt);
128    }
129
130    #[test]
131    fn test_encode_2718_length() {
132        let receipt = ReceiptWithBloom {
133            receipt: Receipt {
134                tx_type: TxType::Eip1559,
135                success: true,
136                cumulative_gas_used: 21000,
137                logs: vec![],
138            },
139            logs_bloom: Bloom::default(),
140        };
141
142        let encoded = receipt.encoded_2718();
143        assert_eq!(
144            encoded.len(),
145            receipt.encode_2718_len(),
146            "Encoded length should match the actual encoded data length"
147        );
148
149        // Test for legacy receipt as well
150        let legacy_receipt = ReceiptWithBloom {
151            receipt: Receipt {
152                tx_type: TxType::Legacy,
153                success: true,
154                cumulative_gas_used: 21000,
155                logs: vec![],
156            },
157            logs_bloom: Bloom::default(),
158        };
159
160        let legacy_encoded = legacy_receipt.encoded_2718();
161        assert_eq!(
162            legacy_encoded.len(),
163            legacy_receipt.encode_2718_len(),
164            "Encoded length for legacy receipt should match the actual encoded data length"
165        );
166    }
167
168    #[test]
169    fn check_transaction_root() {
170        let data = &hex!(
171            "f90262f901f9a092230ce5476ae868e98c7979cfc165a93f8b6ad1922acf2df62e340916efd49da01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa02307107a867056ca33b5087e77c4174f47625e48fb49f1c70ced34890ddd88f3a08151d548273f6683169524b66ca9fe338b9ce42bc3540046c828fd939ae23bcba0c598f69a5674cae9337261b669970e24abc0b46e6d284372a239ec8ccbf20b0ab901000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000083020000018502540be40082a8618203e800a00000000000000000000000000000000000000000000000000000000000000000880000000000000000f863f861800a8405f5e10094100000000000000000000000000000000000000080801ba07e09e26678ed4fac08a249ebe8ed680bf9051a5e14ad223e4b2b9d26e0208f37a05f6e3f188e3e6eab7d7d3b6568f5eac7d687b08d307d3154ccd8c87b4630509bc0"
172        );
173        let block_rlp = &mut data.as_slice();
174        let block: Block = Block::decode(block_rlp).unwrap();
175
176        let tx_root = calculate_transaction_root(&block.body.transactions);
177        assert_eq!(block.transactions_root, tx_root, "Must be the same");
178    }
179
180    #[test]
181    fn check_withdrawals_root() {
182        // Single withdrawal, amount 0
183        // https://github.com/ethereum/tests/blob/9760400e667eba241265016b02644ef62ab55de2/BlockchainTests/EIPTests/bc4895-withdrawals/amountIs0.json
184        let data = &hex!(
185            "f90238f90219a0151934ad9b654c50197f37018ee5ee9bb922dec0a1b5e24a6d679cb111cdb107a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa0046119afb1ab36aaa8f66088677ed96cd62762f6d3e65642898e189fbe702d51a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008001887fffffffffffffff8082079e42a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b42188000000000000000009a048a703da164234812273ea083e4ec3d09d028300cd325b46a6a75402e5a7ab95c0c0d9d8808094c94f5374fce5edbc8e2a8697c15331677e6ebf0b80"
186        );
187        let block: Block = Block::decode(&mut data.as_slice()).unwrap();
188        assert!(block.body.withdrawals.is_some());
189        let withdrawals = block.body.withdrawals.as_ref().unwrap();
190        assert_eq!(withdrawals.len(), 1);
191        let withdrawals_root = calculate_withdrawals_root(withdrawals);
192        assert_eq!(block.withdrawals_root, Some(withdrawals_root));
193
194        // 4 withdrawals, identical indices
195        // https://github.com/ethereum/tests/blob/9760400e667eba241265016b02644ef62ab55de2/BlockchainTests/EIPTests/bc4895-withdrawals/twoIdenticalIndex.json
196        let data = &hex!(
197            "f9028cf90219a0151934ad9b654c50197f37018ee5ee9bb922dec0a1b5e24a6d679cb111cdb107a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347942adc25665018aa1fe0e6bc666dac8fc2697ff9baa0ccf7b62d616c2ad7af862d67b9dcd2119a90cebbff8c3cd1e5d7fc99f8755774a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008001887fffffffffffffff8082079e42a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b42188000000000000000009a0a95b9a7b58a6b3cb4001eb0be67951c5517141cb0183a255b5cae027a7b10b36c0c0f86cda808094c94f5374fce5edbc8e2a8697c15331677e6ebf0b822710da028094c94f5374fce5edbc8e2a8697c15331677e6ebf0b822710da018094c94f5374fce5edbc8e2a8697c15331677e6ebf0b822710da028094c94f5374fce5edbc8e2a8697c15331677e6ebf0b822710"
198        );
199        let block: Block = Block::decode(&mut data.as_slice()).unwrap();
200        assert!(block.body.withdrawals.is_some());
201        let withdrawals = block.body.withdrawals.as_ref().unwrap();
202        assert_eq!(withdrawals.len(), 4);
203        let withdrawals_root = calculate_withdrawals_root(withdrawals);
204        assert_eq!(block.withdrawals_root, Some(withdrawals_root));
205    }
206    #[test]
207    fn check_receipt_root_optimism() {
208        use alloy_consensus::ReceiptWithBloom;
209
210        let logs = vec![Log {
211            address: Address::ZERO,
212            data: LogData::new_unchecked(vec![], Default::default()),
213        }];
214        let bloom = bloom!(
215            "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"
216        );
217        let receipt = ReceiptWithBloom {
218            receipt: Receipt {
219                tx_type: TxType::Eip2930,
220                success: true,
221                cumulative_gas_used: 102068,
222                logs,
223            },
224            logs_bloom: bloom,
225        };
226        let receipt = vec![receipt];
227        let root = calculate_receipt_root(&receipt);
228        assert_eq!(
229            root,
230            b256!("0xfe70ae4a136d98944951b2123859698d59ad251a381abc9960fa81cae3d0d4a0")
231        );
232    }
233
234    // Ensures that reth and alloy receipts encode to the same JSON
235    #[test]
236    #[cfg(feature = "rpc")]
237    fn test_receipt_serde() {
238        use alloy_consensus::ReceiptEnvelope;
239
240        let input = r#"{"status":"0x1","cumulativeGasUsed":"0x175cc0e","logs":[{"address":"0xa18b9ca2a78660d44ab38ae72e72b18792ffe413","topics":["0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925","0x000000000000000000000000e7e7d8006cbff47bc6ac2dabf592c98e97502708","0x0000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d"],"data":"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","blockHash":"0xbf9e6a368a399f996a0f0b27cab4191c028c3c99f5f76ea08a5b70b961475fcb","blockNumber":"0x164b59f","blockTimestamp":"0x68c9a713","transactionHash":"0x533aa9e57865675bb94f41aa2895c0ac81eee69686c77af16149c301e19805f1","transactionIndex":"0x14d","logIndex":"0x238","removed":false}],"logsBloom":"0x00000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000400000040000000000000004000000000000000000000000000000000000000000000020000000000000000000000000080000000000000000000000000200000020000000000000000000000000000000000000000000000000000000000000020000010000000000000000000000000000000000000000000000000000000000000","type":"0x2","transactionHash":"0x533aa9e57865675bb94f41aa2895c0ac81eee69686c77af16149c301e19805f1","transactionIndex":"0x14d","blockHash":"0xbf9e6a368a399f996a0f0b27cab4191c028c3c99f5f76ea08a5b70b961475fcb","blockNumber":"0x164b59f","gasUsed":"0xb607","effectiveGasPrice":"0x4a3ee768","from":"0xe7e7d8006cbff47bc6ac2dabf592c98e97502708","to":"0xa18b9ca2a78660d44ab38ae72e72b18792ffe413","contractAddress":null}"#;
241        let receipt: RpcReceipt = serde_json::from_str(input).unwrap();
242        let envelope: ReceiptEnvelope<alloy_rpc_types_eth::Log> =
243            serde_json::from_str(input).unwrap();
244
245        assert_eq!(envelope, receipt.clone().into());
246
247        let json_envelope = serde_json::to_value(&envelope).unwrap();
248        let json_receipt = serde_json::to_value(receipt.into_with_bloom()).unwrap();
249        assert_eq!(json_envelope, json_receipt);
250    }
251}