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