1
1
use crate :: sync:: block_lookups:: parent_lookup:: PARENT_FAIL_TOLERANCE ;
2
2
use crate :: sync:: block_lookups:: single_block_lookup:: {
3
- LookupRequestError , LookupVerifyError , SingleBlockLookup , SingleLookupRequestState , State ,
3
+ LookupRequestError , SingleBlockLookup , SingleLookupRequestState ,
4
4
} ;
5
5
use crate :: sync:: block_lookups:: {
6
6
BlobRequestState , BlockLookups , BlockRequestState , PeerId , SINGLE_BLOCK_LOOKUP_MAX_ATTEMPTS ,
7
7
} ;
8
8
use crate :: sync:: manager:: { BlockProcessType , Id , SingleLookupReqId } ;
9
- use crate :: sync:: network_context:: SyncNetworkContext ;
9
+ use crate :: sync:: network_context:: {
10
+ BlobsByRootSingleBlockRequest , BlocksByRootSingleRequest , SyncNetworkContext ,
11
+ } ;
10
12
use beacon_chain:: block_verification_types:: RpcBlock ;
11
13
use beacon_chain:: data_availability_checker:: ChildComponents ;
12
- use beacon_chain:: { get_block_root, BeaconChainTypes } ;
13
- use lighthouse_network:: rpc:: methods:: BlobsByRootRequest ;
14
- use lighthouse_network:: rpc:: BlocksByRootRequest ;
15
- use std:: ops:: IndexMut ;
14
+ use beacon_chain:: BeaconChainTypes ;
16
15
use std:: sync:: Arc ;
17
16
use std:: time:: Duration ;
18
- use types:: blob_sidecar:: { BlobIdentifier , FixedBlobSidecarList } ;
19
- use types:: { BlobSidecar , ChainSpec , Hash256 , SignedBeaconBlock } ;
17
+ use types:: blob_sidecar:: FixedBlobSidecarList ;
18
+ use types:: { Hash256 , SignedBeaconBlock } ;
20
19
21
20
#[ derive( Debug , Copy , Clone ) ]
22
21
pub enum ResponseType {
@@ -73,9 +72,6 @@ pub trait RequestState<L: Lookup, T: BeaconChainTypes> {
73
72
/// The type of the request .
74
73
type RequestType ;
75
74
76
- /// A block or blob response.
77
- type ResponseType ;
78
-
79
75
/// The type created after validation.
80
76
type VerifiedResponseType : Clone ;
81
77
@@ -85,30 +81,27 @@ pub trait RequestState<L: Lookup, T: BeaconChainTypes> {
85
81
/* Request building methods */
86
82
87
83
/// Construct a new request.
88
- fn build_request (
89
- & mut self ,
90
- spec : & ChainSpec ,
91
- ) -> Result < ( PeerId , Self :: RequestType ) , LookupRequestError > {
84
+ fn build_request ( & mut self ) -> Result < ( PeerId , Self :: RequestType ) , LookupRequestError > {
92
85
// Verify and construct request.
93
86
self . too_many_attempts ( ) ?;
94
87
let peer = self . get_peer ( ) ?;
95
- let request = self . new_request ( spec ) ;
88
+ let request = self . new_request ( ) ;
96
89
Ok ( ( peer, request) )
97
90
}
98
91
99
92
/// Construct a new request and send it.
100
93
fn build_request_and_send (
101
94
& mut self ,
102
95
id : Id ,
103
- cx : & SyncNetworkContext < T > ,
96
+ cx : & mut SyncNetworkContext < T > ,
104
97
) -> Result < ( ) , LookupRequestError > {
105
98
// Check if request is necessary.
106
99
if !self . get_state ( ) . is_awaiting_download ( ) {
107
100
return Ok ( ( ) ) ;
108
101
}
109
102
110
103
// Construct request.
111
- let ( peer_id, request) = self . build_request ( & cx . chain . spec ) ?;
104
+ let ( peer_id, request) = self . build_request ( ) ?;
112
105
113
106
// Update request state.
114
107
let req_counter = self . get_state_mut ( ) . on_download_start ( peer_id) ;
@@ -144,61 +137,18 @@ pub trait RequestState<L: Lookup, T: BeaconChainTypes> {
144
137
}
145
138
146
139
/// Initialize `Self::RequestType`.
147
- fn new_request ( & self , spec : & ChainSpec ) -> Self :: RequestType ;
140
+ fn new_request ( & self ) -> Self :: RequestType ;
148
141
149
142
/// Send the request to the network service.
150
143
fn make_request (
151
144
id : SingleLookupReqId ,
152
145
peer_id : PeerId ,
153
146
request : Self :: RequestType ,
154
- cx : & SyncNetworkContext < T > ,
147
+ cx : & mut SyncNetworkContext < T > ,
155
148
) -> Result < ( ) , LookupRequestError > ;
156
149
157
150
/* Response handling methods */
158
151
159
- /// Verify the response is valid based on what we requested.
160
- fn verify_response (
161
- & mut self ,
162
- expected_block_root : Hash256 ,
163
- peer_id : PeerId ,
164
- response : Option < Self :: ResponseType > ,
165
- ) -> Result < Option < Self :: VerifiedResponseType > , LookupVerifyError > {
166
- let result = match * self . get_state ( ) . get_state ( ) {
167
- State :: AwaitingDownload => Err ( LookupVerifyError :: ExtraBlocksReturned ) ,
168
- State :: Downloading { peer_id : _ } => {
169
- // TODO: We requested a download from Downloading { peer_id }, but the network
170
- // injects a response from a different peer_id. What should we do? The peer_id to
171
- // track for scoring is the one that actually sent the response, not the state's
172
- self . verify_response_inner ( expected_block_root, response)
173
- }
174
- State :: Processing { .. } | State :: Processed { .. } => match response {
175
- // We sent the block for processing and received an extra block.
176
- Some ( _) => Err ( LookupVerifyError :: ExtraBlocksReturned ) ,
177
- // This is simply the stream termination and we are already processing the block
178
- None => Ok ( None ) ,
179
- } ,
180
- } ;
181
-
182
- match result {
183
- Ok ( Some ( response) ) => {
184
- self . get_state_mut ( ) . on_download_success ( peer_id) ;
185
- Ok ( Some ( response) )
186
- }
187
- Ok ( None ) => Ok ( None ) ,
188
- Err ( e) => {
189
- self . get_state_mut ( ) . on_download_failure ( ) ;
190
- Err ( e)
191
- }
192
- }
193
- }
194
-
195
- /// The response verification unique to block or blobs.
196
- fn verify_response_inner (
197
- & mut self ,
198
- expected_block_root : Hash256 ,
199
- response : Option < Self :: ResponseType > ,
200
- ) -> Result < Option < Self :: VerifiedResponseType > , LookupVerifyError > ;
201
-
202
152
/// A getter for the parent root of the response. Returns an `Option` because we won't know
203
153
/// the blob parent if we don't end up getting any blobs in the response.
204
154
fn get_parent_root ( verified_response : & Self :: VerifiedResponseType ) -> Option < Hash256 > ;
@@ -247,49 +197,24 @@ pub trait RequestState<L: Lookup, T: BeaconChainTypes> {
247
197
}
248
198
249
199
impl < L : Lookup , T : BeaconChainTypes > RequestState < L , T > for BlockRequestState < L > {
250
- type RequestType = BlocksByRootRequest ;
251
- type ResponseType = Arc < SignedBeaconBlock < T :: EthSpec > > ;
200
+ type RequestType = BlocksByRootSingleRequest ;
252
201
type VerifiedResponseType = Arc < SignedBeaconBlock < T :: EthSpec > > ;
253
202
type ReconstructedResponseType = RpcBlock < T :: EthSpec > ;
254
203
255
- fn new_request ( & self , spec : & ChainSpec ) -> BlocksByRootRequest {
256
- BlocksByRootRequest :: new ( vec ! [ self . requested_block_root] , spec )
204
+ fn new_request ( & self ) -> Self :: RequestType {
205
+ BlocksByRootSingleRequest ( self . requested_block_root )
257
206
}
258
207
259
208
fn make_request (
260
209
id : SingleLookupReqId ,
261
210
peer_id : PeerId ,
262
211
request : Self :: RequestType ,
263
- cx : & SyncNetworkContext < T > ,
212
+ cx : & mut SyncNetworkContext < T > ,
264
213
) -> Result < ( ) , LookupRequestError > {
265
214
cx. block_lookup_request ( id, peer_id, request)
266
215
. map_err ( LookupRequestError :: SendFailed )
267
216
}
268
217
269
- fn verify_response_inner (
270
- & mut self ,
271
- expected_block_root : Hash256 ,
272
- response : Option < Self :: ResponseType > ,
273
- ) -> Result < Option < Arc < SignedBeaconBlock < T :: EthSpec > > > , LookupVerifyError > {
274
- match response {
275
- Some ( block) => {
276
- // Compute the block root using this specific function so that we can get timing
277
- // metrics.
278
- let block_root = get_block_root ( & block) ;
279
- if block_root != expected_block_root {
280
- // return an error and drop the block
281
- // NOTE: we take this is as a download failure to prevent counting the
282
- // attempt as a chain failure, but simply a peer failure.
283
- Err ( LookupVerifyError :: RootMismatch )
284
- } else {
285
- // Return the block for processing.
286
- Ok ( Some ( block) )
287
- }
288
- }
289
- None => Err ( LookupVerifyError :: NoBlockReturned ) ,
290
- }
291
- }
292
-
293
218
fn get_parent_root ( verified_response : & Arc < SignedBeaconBlock < T :: EthSpec > > ) -> Option < Hash256 > {
294
219
Some ( verified_response. parent_root ( ) )
295
220
}
@@ -340,60 +265,27 @@ impl<L: Lookup, T: BeaconChainTypes> RequestState<L, T> for BlockRequestState<L>
340
265
}
341
266
342
267
impl < L : Lookup , T : BeaconChainTypes > RequestState < L , T > for BlobRequestState < L , T :: EthSpec > {
343
- type RequestType = BlobsByRootRequest ;
344
- type ResponseType = Arc < BlobSidecar < T :: EthSpec > > ;
268
+ type RequestType = BlobsByRootSingleBlockRequest ;
345
269
type VerifiedResponseType = FixedBlobSidecarList < T :: EthSpec > ;
346
270
type ReconstructedResponseType = FixedBlobSidecarList < T :: EthSpec > ;
347
271
348
- fn new_request ( & self , spec : & ChainSpec ) -> BlobsByRootRequest {
349
- let blob_id_vec: Vec < BlobIdentifier > = self . requested_ids . clone ( ) . into ( ) ;
350
- BlobsByRootRequest :: new ( blob_id_vec, spec)
272
+ fn new_request ( & self ) -> Self :: RequestType {
273
+ BlobsByRootSingleBlockRequest {
274
+ block_root : self . block_root ,
275
+ indices : self . requested_ids . indices ( ) ,
276
+ }
351
277
}
352
278
353
279
fn make_request (
354
280
id : SingleLookupReqId ,
355
281
peer_id : PeerId ,
356
282
request : Self :: RequestType ,
357
- cx : & SyncNetworkContext < T > ,
283
+ cx : & mut SyncNetworkContext < T > ,
358
284
) -> Result < ( ) , LookupRequestError > {
359
285
cx. blob_lookup_request ( id, peer_id, request)
360
286
. map_err ( LookupRequestError :: SendFailed )
361
287
}
362
288
363
- fn verify_response_inner (
364
- & mut self ,
365
- expected_block_root : Hash256 ,
366
- blob : Option < Self :: ResponseType > ,
367
- ) -> Result < Option < FixedBlobSidecarList < T :: EthSpec > > , LookupVerifyError > {
368
- match blob {
369
- Some ( blob) => {
370
- let received_id = blob. id ( ) ;
371
-
372
- if !self . requested_ids . contains ( & received_id) {
373
- return Err ( LookupVerifyError :: UnrequestedBlobId ( received_id) ) ;
374
- }
375
- if !blob. verify_blob_sidecar_inclusion_proof ( ) . unwrap_or ( false ) {
376
- return Err ( LookupVerifyError :: InvalidInclusionProof ) ;
377
- }
378
- if blob. block_root ( ) != expected_block_root {
379
- return Err ( LookupVerifyError :: UnrequestedHeader ) ;
380
- }
381
-
382
- // State should remain downloading until we receive the stream terminator.
383
- self . requested_ids . remove ( & received_id) ;
384
-
385
- // The inclusion proof check above ensures `blob.index` is < MAX_BLOBS_PER_BLOCK
386
- let blob_index = blob. index ;
387
- * self . blob_download_queue . index_mut ( blob_index as usize ) = Some ( blob) ;
388
- Ok ( None )
389
- }
390
- None => {
391
- let blobs = std:: mem:: take ( & mut self . blob_download_queue ) ;
392
- Ok ( Some ( blobs) )
393
- }
394
- }
395
- }
396
-
397
289
fn get_parent_root ( verified_response : & FixedBlobSidecarList < T :: EthSpec > ) -> Option < Hash256 > {
398
290
verified_response
399
291
. into_iter ( )
0 commit comments