This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
forked from paradigmxyz/reth
-
Notifications
You must be signed in to change notification settings - Fork 3
/
receipts.rs
170 lines (158 loc) · 9.01 KB
/
receipts.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Implements the `GetReceipts` and `Receipts` message types.
use reth_codecs::derive_arbitrary;
use reth_primitives::{ReceiptWithBloom, H256};
use reth_rlp::{RlpDecodableWrapper, RlpEncodableWrapper};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
/// A request for transaction receipts from the given block hashes.
#[derive_arbitrary(rlp)]
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct GetReceipts(
/// The block hashes to request receipts for.
pub Vec<H256>,
);
/// The response to [`GetReceipts`], containing receipt lists that correspond to each block
/// requested.
#[derive_arbitrary(rlp)]
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodableWrapper, RlpDecodableWrapper, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Receipts(
/// Each receipt hash should correspond to a block hash in the request.
#[cfg_attr(
any(test, feature = "arbitrary"),
proptest(
strategy = "proptest::collection::vec(proptest::collection::vec(proptest::arbitrary::any::<ReceiptWithBloom>(), 0..=50), 0..=5)"
)
)]
pub Vec<Vec<ReceiptWithBloom>>,
);
#[cfg(test)]
mod test {
use crate::{
types::{message::RequestPair, GetReceipts},
Receipts,
};
use hex_literal::hex;
use reth_primitives::{Log, Receipt, ReceiptWithBloom, TxType};
use reth_rlp::{Decodable, Encodable};
#[test]
fn roundtrip_eip1559() {
let receipts = Receipts(vec![vec![ReceiptWithBloom {
receipt: Receipt {
tx_type: TxType::EIP1559,
success: false,
cumulative_gas_used: 0,
logs: vec![],
#[cfg(feature = "optimism")]
deposit_nonce: None,
},
bloom: Default::default(),
}]]);
let mut out = vec![];
receipts.encode(&mut out);
let mut out = out.as_slice();
let decoded = Receipts::decode(&mut out).unwrap();
assert!(receipts == decoded);
}
#[test]
// Test vector from: https://eips.ethereum.org/EIPS/eip-2481
fn encode_get_receipts() {
let expected = hex!("f847820457f842a000000000000000000000000000000000000000000000000000000000deadc0dea000000000000000000000000000000000000000000000000000000000feedbeef");
let mut data = vec![];
let request = RequestPair::<GetReceipts> {
request_id: 1111,
message: GetReceipts(vec![
hex!("00000000000000000000000000000000000000000000000000000000deadc0de").into(),
hex!("00000000000000000000000000000000000000000000000000000000feedbeef").into(),
]),
};
request.encode(&mut data);
assert_eq!(data, expected);
}
#[test]
// Test vector from: https://eips.ethereum.org/EIPS/eip-2481
fn decode_get_receipts() {
let data = hex!("f847820457f842a000000000000000000000000000000000000000000000000000000000deadc0dea000000000000000000000000000000000000000000000000000000000feedbeef");
let request = RequestPair::<GetReceipts>::decode(&mut &data[..]).unwrap();
assert_eq!(
request,
RequestPair::<GetReceipts> {
request_id: 1111,
message: GetReceipts(vec![
hex!("00000000000000000000000000000000000000000000000000000000deadc0de").into(),
hex!("00000000000000000000000000000000000000000000000000000000feedbeef").into(),
])
}
);
}
#[test]
// Test vector from: https://eips.ethereum.org/EIPS/eip-2481
fn encode_receipts() {
let expected = hex!("f90172820457f9016cf90169f901668001b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f85ff85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff");
let mut data = vec![];
let request = RequestPair::<Receipts> {
request_id: 1111,
message: Receipts(vec![vec![
ReceiptWithBloom {
receipt: Receipt {
tx_type: TxType::Legacy,
cumulative_gas_used: 0x1u64,
logs: vec![
Log {
address: hex!("0000000000000000000000000000000000000011").into(),
topics: vec![
hex!("000000000000000000000000000000000000000000000000000000000000dead").into(),
hex!("000000000000000000000000000000000000000000000000000000000000beef").into(),
],
data: hex!("0100ff")[..].into(),
},
],
success: false,
#[cfg(feature = "optimism")]
deposit_nonce: None,
},
bloom: hex!("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").into(),
},
]]),
};
request.encode(&mut data);
assert_eq!(data, expected);
}
#[test]
// Test vector from: https://eips.ethereum.org/EIPS/eip-2481
fn decode_receipts() {
let data = hex!("f90172820457f9016cf90169f901668001b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f85ff85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff");
let request = RequestPair::<Receipts>::decode(&mut &data[..]).unwrap();
assert_eq!(
request,
RequestPair::<Receipts> {
request_id: 1111,
message: Receipts(vec![
vec![
ReceiptWithBloom {
receipt: Receipt {
tx_type: TxType::Legacy,
cumulative_gas_used: 0x1u64,
logs: vec![
Log {
address: hex!("0000000000000000000000000000000000000011").into(),
topics: vec![
hex!("000000000000000000000000000000000000000000000000000000000000dead").into(),
hex!("000000000000000000000000000000000000000000000000000000000000beef").into(),
],
data: hex!("0100ff")[..].into(),
},
],
success: false,
#[cfg(feature = "optimism")]
deposit_nonce: None,
},
bloom: hex!("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").into(),
},
],
]),
}
);
}
}