1
- use crate :: util:: enveloped;
2
- use crate :: Log ;
1
+ use crate :: { EnvelopedDecodable , EnvelopedDecoderError , EnvelopedEncodable , Log } ;
3
2
use alloc:: vec:: Vec ;
3
+ use bytes:: BytesMut ;
4
4
use ethereum_types:: { Bloom , H256 , U256 } ;
5
- use rlp:: { Decodable , DecoderError , Encodable , Rlp , RlpStream } ;
5
+ use rlp:: { Decodable , DecoderError , Rlp } ;
6
6
use rlp_derive:: { RlpDecodable , RlpEncodable } ;
7
7
8
8
#[ derive( Clone , Debug , PartialEq , Eq , RlpEncodable , RlpDecodable ) ]
@@ -37,8 +37,42 @@ pub type EIP1559ReceiptData = EIP658ReceiptData;
37
37
38
38
pub type ReceiptV0 = FrontierReceiptData ;
39
39
40
+ impl EnvelopedEncodable for ReceiptV0 {
41
+ fn type_id ( & self ) -> Option < u8 > {
42
+ None
43
+ }
44
+ fn encode_payload ( & self ) -> BytesMut {
45
+ rlp:: encode ( self )
46
+ }
47
+ }
48
+
49
+ impl EnvelopedDecodable for ReceiptV0 {
50
+ type PayloadDecoderError = DecoderError ;
51
+
52
+ fn decode ( bytes : & [ u8 ] ) -> Result < Self , EnvelopedDecoderError < Self :: PayloadDecoderError > > {
53
+ Ok ( rlp:: decode ( bytes) ?)
54
+ }
55
+ }
56
+
40
57
pub type ReceiptV1 = EIP658ReceiptData ;
41
58
59
+ impl EnvelopedEncodable for ReceiptV1 {
60
+ fn type_id ( & self ) -> Option < u8 > {
61
+ None
62
+ }
63
+ fn encode_payload ( & self ) -> BytesMut {
64
+ rlp:: encode ( self )
65
+ }
66
+ }
67
+
68
+ impl EnvelopedDecodable for ReceiptV1 {
69
+ type PayloadDecoderError = DecoderError ;
70
+
71
+ fn decode ( bytes : & [ u8 ] ) -> Result < Self , EnvelopedDecoderError < Self :: PayloadDecoderError > > {
72
+ Ok ( rlp:: decode ( bytes) ?)
73
+ }
74
+ }
75
+
42
76
#[ derive( Clone , Debug , PartialEq , Eq ) ]
43
77
#[ cfg_attr(
44
78
feature = "with-codec" ,
@@ -52,34 +86,44 @@ pub enum ReceiptV2 {
52
86
EIP2930 ( EIP2930ReceiptData ) ,
53
87
}
54
88
55
- impl Encodable for ReceiptV2 {
56
- fn rlp_append ( & self , s : & mut RlpStream ) {
89
+ impl EnvelopedEncodable for ReceiptV2 {
90
+ fn type_id ( & self ) -> Option < u8 > {
91
+ match self {
92
+ Self :: Legacy ( _) => None ,
93
+ Self :: EIP2930 ( _) => Some ( 1 ) ,
94
+ }
95
+ }
96
+
97
+ fn encode_payload ( & self ) -> BytesMut {
57
98
match self {
58
- Self :: Legacy ( r) => r . rlp_append ( s ) ,
59
- Self :: EIP2930 ( r) => enveloped ( 1 , r , s ) ,
99
+ Self :: Legacy ( r) => rlp :: encode ( r ) ,
100
+ Self :: EIP2930 ( r) => rlp :: encode ( r ) ,
60
101
}
61
102
}
62
103
}
63
104
64
- impl Decodable for ReceiptV2 {
65
- fn decode ( rlp : & Rlp ) -> Result < Self , DecoderError > {
66
- let slice = rlp. data ( ) ?;
105
+ impl EnvelopedDecodable for ReceiptV2 {
106
+ type PayloadDecoderError = DecoderError ;
107
+
108
+ fn decode ( bytes : & [ u8 ] ) -> Result < Self , EnvelopedDecoderError < Self :: PayloadDecoderError > > {
109
+ if bytes. is_empty ( ) {
110
+ return Err ( EnvelopedDecoderError :: UnknownTypeId ) ;
111
+ }
67
112
68
- let first = * slice . get ( 0 ) . ok_or ( DecoderError :: Custom ( "empty slice" ) ) ? ;
113
+ let first = bytes [ 0 ] ;
69
114
115
+ let rlp = Rlp :: new ( bytes) ;
70
116
if rlp. is_list ( ) {
71
- return Ok ( Self :: Legacy ( Decodable :: decode ( rlp) ?) ) ;
117
+ return Ok ( Self :: Legacy ( Decodable :: decode ( & rlp) ?) ) ;
72
118
}
73
119
74
- let s = slice
75
- . get ( 1 ..)
76
- . ok_or ( DecoderError :: Custom ( "no receipt body" ) ) ?;
120
+ let s = & bytes[ 1 ..] ;
77
121
78
122
if first == 0x01 {
79
- return rlp:: decode ( s) . map ( Self :: EIP2930 ) ;
123
+ return Ok ( Self :: EIP2930 ( rlp:: decode ( s) ? ) ) ;
80
124
}
81
125
82
- Err ( DecoderError :: Custom ( "invalid receipt type" ) )
126
+ Err ( DecoderError :: Custom ( "invalid receipt type" ) . into ( ) )
83
127
}
84
128
}
85
129
@@ -107,39 +151,50 @@ pub enum ReceiptV3 {
107
151
EIP1559 ( EIP1559ReceiptData ) ,
108
152
}
109
153
110
- impl Encodable for ReceiptV3 {
111
- fn rlp_append ( & self , s : & mut RlpStream ) {
154
+ impl EnvelopedEncodable for ReceiptV3 {
155
+ fn type_id ( & self ) -> Option < u8 > {
156
+ match self {
157
+ Self :: Legacy ( _) => None ,
158
+ Self :: EIP2930 ( _) => Some ( 1 ) ,
159
+ Self :: EIP1559 ( _) => Some ( 2 ) ,
160
+ }
161
+ }
162
+
163
+ fn encode_payload ( & self ) -> BytesMut {
112
164
match self {
113
- Self :: Legacy ( r) => r . rlp_append ( s ) ,
114
- Self :: EIP2930 ( r) => enveloped ( 1 , r , s ) ,
115
- Self :: EIP1559 ( r) => enveloped ( 2 , r , s ) ,
165
+ Self :: Legacy ( r) => rlp :: encode ( r ) ,
166
+ Self :: EIP2930 ( r) => rlp :: encode ( r ) ,
167
+ Self :: EIP1559 ( r) => rlp :: encode ( r ) ,
116
168
}
117
169
}
118
170
}
119
171
120
- impl Decodable for ReceiptV3 {
121
- fn decode ( rlp : & Rlp ) -> Result < Self , DecoderError > {
122
- let slice = rlp. data ( ) ?;
172
+ impl EnvelopedDecodable for ReceiptV3 {
173
+ type PayloadDecoderError = DecoderError ;
123
174
124
- let first = * slice. get ( 0 ) . ok_or ( DecoderError :: Custom ( "empty slice" ) ) ?;
175
+ fn decode ( bytes : & [ u8 ] ) -> Result < Self , EnvelopedDecoderError < Self :: PayloadDecoderError > > {
176
+ if bytes. is_empty ( ) {
177
+ return Err ( EnvelopedDecoderError :: UnknownTypeId ) ;
178
+ }
179
+
180
+ let first = bytes[ 0 ] ;
125
181
182
+ let rlp = Rlp :: new ( bytes) ;
126
183
if rlp. is_list ( ) {
127
- return Ok ( Self :: Legacy ( Decodable :: decode ( rlp) ?) ) ;
184
+ return Ok ( Self :: Legacy ( Decodable :: decode ( & rlp) ?) ) ;
128
185
}
129
186
130
- let s = slice
131
- . get ( 1 ..)
132
- . ok_or ( DecoderError :: Custom ( "no receipt body" ) ) ?;
187
+ let s = & bytes[ 1 ..] ;
133
188
134
189
if first == 0x01 {
135
- return rlp:: decode ( s) . map ( Self :: EIP2930 ) ;
190
+ return Ok ( Self :: EIP2930 ( rlp:: decode ( s) ? ) ) ;
136
191
}
137
192
138
193
if first == 0x02 {
139
- return rlp:: decode ( s) . map ( Self :: EIP1559 ) ;
194
+ return Ok ( Self :: EIP1559 ( rlp:: decode ( s) ? ) ) ;
140
195
}
141
196
142
- Err ( DecoderError :: Custom ( "invalid receipt type" ) )
197
+ Err ( DecoderError :: Custom ( "invalid receipt type" ) . into ( ) )
143
198
}
144
199
}
145
200
@@ -170,48 +225,60 @@ pub enum ReceiptAny {
170
225
EIP1559 ( EIP1559ReceiptData ) ,
171
226
}
172
227
173
- impl Encodable for ReceiptAny {
174
- fn rlp_append ( & self , s : & mut RlpStream ) {
228
+ impl EnvelopedEncodable for ReceiptAny {
229
+ fn type_id ( & self ) -> Option < u8 > {
230
+ match self {
231
+ Self :: Frontier ( _) => None ,
232
+ Self :: EIP658 ( _) => None ,
233
+ Self :: EIP2930 ( _) => Some ( 1 ) ,
234
+ Self :: EIP1559 ( _) => Some ( 2 ) ,
235
+ }
236
+ }
237
+
238
+ fn encode_payload ( & self ) -> BytesMut {
175
239
match self {
176
- Self :: Frontier ( r) => r . rlp_append ( s ) ,
177
- Self :: EIP658 ( r) => r . rlp_append ( s ) ,
178
- Self :: EIP2930 ( r) => enveloped ( 1 , r , s ) ,
179
- Self :: EIP1559 ( r) => enveloped ( 2 , r , s ) ,
240
+ Self :: Frontier ( r) => rlp :: encode ( r ) ,
241
+ Self :: EIP658 ( r) => rlp :: encode ( r ) ,
242
+ Self :: EIP2930 ( r) => rlp :: encode ( r ) ,
243
+ Self :: EIP1559 ( r) => rlp :: encode ( r ) ,
180
244
}
181
245
}
182
246
}
183
247
184
- impl Decodable for ReceiptAny {
185
- fn decode ( rlp : & Rlp ) -> Result < Self , DecoderError > {
186
- let slice = rlp. data ( ) ?;
248
+ impl EnvelopedDecodable for ReceiptAny {
249
+ type PayloadDecoderError = DecoderError ;
250
+
251
+ fn decode ( bytes : & [ u8 ] ) -> Result < Self , EnvelopedDecoderError < Self :: PayloadDecoderError > > {
252
+ if bytes. is_empty ( ) {
253
+ return Err ( EnvelopedDecoderError :: UnknownTypeId ) ;
254
+ }
187
255
188
- let first = * slice . get ( 0 ) . ok_or ( DecoderError :: Custom ( "empty slice" ) ) ? ;
256
+ let first = bytes [ 0 ] ;
189
257
258
+ let rlp = Rlp :: new ( bytes) ;
190
259
if rlp. is_list ( ) {
191
260
if rlp. item_count ( ) ? == 4 {
192
261
let first = rlp. at ( 0 ) ?;
193
262
if first. is_data ( ) && first. data ( ) ?. len ( ) <= 1 {
194
- return Ok ( Self :: Frontier ( Decodable :: decode ( rlp) ?) ) ;
263
+ return Ok ( Self :: Frontier ( Decodable :: decode ( & rlp) ?) ) ;
195
264
} else {
196
- return Ok ( Self :: EIP658 ( Decodable :: decode ( rlp) ?) ) ;
265
+ return Ok ( Self :: EIP658 ( Decodable :: decode ( & rlp) ?) ) ;
197
266
}
198
267
}
199
268
200
- return Err ( DecoderError :: RlpIncorrectListLen ) ;
269
+ return Err ( DecoderError :: RlpIncorrectListLen . into ( ) ) ;
201
270
}
202
271
203
- let s = slice
204
- . get ( 1 ..)
205
- . ok_or ( DecoderError :: Custom ( "no receipt body" ) ) ?;
272
+ let s = & bytes[ 1 ..] ;
206
273
207
274
if first == 0x01 {
208
- return rlp:: decode ( s) . map ( Self :: EIP2930 ) ;
275
+ return Ok ( Self :: EIP2930 ( rlp:: decode ( s) ? ) ) ;
209
276
}
210
277
211
278
if first == 0x02 {
212
- return rlp:: decode ( s) . map ( Self :: EIP1559 ) ;
279
+ return Ok ( Self :: EIP1559 ( rlp:: decode ( s) ? ) ) ;
213
280
}
214
281
215
- Err ( DecoderError :: Custom ( "invalid receipt type" ) )
282
+ Err ( DecoderError :: Custom ( "invalid receipt type" ) . into ( ) )
216
283
}
217
284
}
0 commit comments