1
1
use beacon_chain:: {
2
2
block_verification_types:: RpcBlock , data_column_verification:: CustodyDataColumn , get_block_root,
3
3
} ;
4
- use lighthouse_network:: PeerId ;
5
4
use ssz_types:: VariableList ;
6
5
use std:: {
7
6
collections:: { HashMap , VecDeque } ,
@@ -29,16 +28,13 @@ pub struct RangeBlockComponentsRequest<E: EthSpec> {
29
28
/// Used to determine if the number of data columns stream termination this accumulator should
30
29
/// wait for. This may be less than the number of `expects_custody_columns` due to request batching.
31
30
num_custody_column_requests : Option < usize > ,
32
- /// The peers the request was made to.
33
- pub ( crate ) peer_ids : Vec < PeerId > ,
34
31
}
35
32
36
33
impl < E : EthSpec > RangeBlockComponentsRequest < E > {
37
34
pub fn new (
38
35
expects_blobs : bool ,
39
36
expects_custody_columns : Option < Vec < ColumnIndex > > ,
40
37
num_custody_column_requests : Option < usize > ,
41
- peer_ids : Vec < PeerId > ,
42
38
) -> Self {
43
39
Self {
44
40
blocks : <_ >:: default ( ) ,
@@ -50,38 +46,31 @@ impl<E: EthSpec> RangeBlockComponentsRequest<E> {
50
46
expects_blobs,
51
47
expects_custody_columns,
52
48
num_custody_column_requests,
53
- peer_ids,
54
49
}
55
50
}
56
51
57
- // TODO: This function should be deprecated when simplying the retry mechanism of this range
58
- // requests.
59
- pub fn get_requirements ( & self ) -> ( bool , Option < Vec < ColumnIndex > > ) {
60
- ( self . expects_blobs , self . expects_custody_columns . clone ( ) )
61
- }
62
-
63
- pub fn add_block_response ( & mut self , block_opt : Option < Arc < SignedBeaconBlock < E > > > ) {
64
- match block_opt {
65
- Some ( block) => self . blocks . push_back ( block) ,
66
- None => self . is_blocks_stream_terminated = true ,
52
+ pub fn add_blocks ( & mut self , blocks : Vec < Arc < SignedBeaconBlock < E > > > ) {
53
+ for block in blocks {
54
+ self . blocks . push_back ( block) ;
67
55
}
56
+ self . is_blocks_stream_terminated = true ;
68
57
}
69
58
70
- pub fn add_sidecar_response ( & mut self , sidecar_opt : Option < Arc < BlobSidecar < E > > > ) {
71
- match sidecar_opt {
72
- Some ( sidecar) => self . blobs . push_back ( sidecar) ,
73
- None => self . is_sidecars_stream_terminated = true ,
59
+ pub fn add_blobs ( & mut self , blobs : Vec < Arc < BlobSidecar < E > > > ) {
60
+ for blob in blobs {
61
+ self . blobs . push_back ( blob) ;
74
62
}
63
+ self . is_sidecars_stream_terminated = true ;
75
64
}
76
65
77
- pub fn add_data_column ( & mut self , column_opt : Option < Arc < DataColumnSidecar < E > > > ) {
78
- match column_opt {
79
- Some ( column) => self . data_columns . push_back ( column) ,
80
- // TODO(das): this mechanism is dangerous, if somehow there are two requests for the
81
- // same column index it can terminate early. This struct should track that all requests
82
- // for all custody columns terminate.
83
- None => self . custody_columns_streams_terminated += 1 ,
66
+ pub fn add_custody_columns ( & mut self , columns : Vec < Arc < DataColumnSidecar < E > > > ) {
67
+ for column in columns {
68
+ self . data_columns . push_back ( column) ;
84
69
}
70
+ // TODO(das): this mechanism is dangerous, if somehow there are two requests for the
71
+ // same column index it can terminate early. This struct should track that all requests
72
+ // for all custody columns terminate.
73
+ self . custody_columns_streams_terminated += 1 ;
85
74
}
86
75
87
76
pub fn into_responses ( self , spec : & ChainSpec ) -> Result < Vec < RpcBlock < E > > , String > {
@@ -249,14 +238,15 @@ mod tests {
249
238
let mut info = RangeBlockComponentsRequest :: < E > :: new ( false , None , None , vec ! [ peer_id] ) ;
250
239
let mut rng = XorShiftRng :: from_seed ( [ 42 ; 16 ] ) ;
251
240
let blocks = ( 0 ..4 )
252
- . map ( |_| generate_rand_block_and_blobs :: < E > ( ForkName :: Base , NumBlobs :: None , & mut rng) . 0 )
241
+ . map ( |_| {
242
+ generate_rand_block_and_blobs :: < E > ( ForkName :: Base , NumBlobs :: None , & mut rng)
243
+ . 0
244
+ . into ( )
245
+ } )
253
246
. collect :: < Vec < _ > > ( ) ;
254
247
255
248
// Send blocks and complete terminate response
256
- for block in blocks {
257
- info. add_block_response ( Some ( block. into ( ) ) ) ;
258
- }
259
- info. add_block_response ( None ) ;
249
+ info. add_blocks ( blocks) ;
260
250
261
251
// Assert response is finished and RpcBlocks can be constructed
262
252
assert ! ( info. is_finished( ) ) ;
@@ -271,17 +261,16 @@ mod tests {
271
261
let blocks = ( 0 ..4 )
272
262
. map ( |_| {
273
263
// Always generate some blobs.
274
- generate_rand_block_and_blobs :: < E > ( ForkName :: Deneb , NumBlobs :: Number ( 3 ) , & mut rng) . 0
264
+ generate_rand_block_and_blobs :: < E > ( ForkName :: Deneb , NumBlobs :: Number ( 3 ) , & mut rng)
265
+ . 0
266
+ . into ( )
275
267
} )
276
268
. collect :: < Vec < _ > > ( ) ;
277
269
278
270
// Send blocks and complete terminate response
279
- for block in blocks {
280
- info. add_block_response ( Some ( block. into ( ) ) ) ;
281
- }
282
- info. add_block_response ( None ) ;
271
+ info. add_blocks ( blocks) ;
283
272
// Expect no blobs returned
284
- info. add_sidecar_response ( None ) ;
273
+ info. add_blobs ( vec ! [ ] ) ;
285
274
286
275
// Assert response is finished and RpcBlocks can be constructed, even if blobs weren't returned.
287
276
// This makes sure we don't expect blobs here when they have expired. Checking this logic should
@@ -294,10 +283,11 @@ mod tests {
294
283
fn rpc_block_with_custody_columns ( ) {
295
284
let spec = test_spec :: < E > ( ) ;
296
285
let expects_custody_columns = vec ! [ 1 , 2 , 3 , 4 ] ;
286
+ let custody_column_request_ids = vec ! [ 0 , 1 , 2 , 3 ] ;
297
287
let mut info = RangeBlockComponentsRequest :: < E > :: new (
298
288
false ,
299
289
Some ( expects_custody_columns. clone ( ) ) ,
300
- Some ( expects_custody_columns . len ( ) ) ,
290
+ Some ( custody_column_request_ids . len ( ) ) ,
301
291
vec ! [ PeerId :: random( ) ] ,
302
292
) ;
303
293
let mut rng = XorShiftRng :: from_seed ( [ 42 ; 16 ] ) ;
@@ -313,25 +303,18 @@ mod tests {
313
303
. collect :: < Vec < _ > > ( ) ;
314
304
315
305
// Send blocks and complete terminate response
316
- for block in & blocks {
317
- info. add_block_response ( Some ( block. 0 . clone ( ) . into ( ) ) ) ;
318
- }
319
- info. add_block_response ( None ) ;
306
+ info. add_blocks ( blocks. iter ( ) . map ( |b| b. 0 . clone ( ) . into ( ) ) . collect ( ) ) ;
320
307
// Assert response is not finished
321
308
assert ! ( !info. is_finished( ) ) ;
322
309
323
- // Send data columns interleaved
324
- for block in & blocks {
325
- for column in & block. 1 {
326
- if expects_custody_columns. contains ( & column. index ) {
327
- info. add_data_column ( Some ( column. clone ( ) ) ) ;
328
- }
329
- }
330
- }
331
-
332
- // Terminate the requests
333
- for ( i, _column_index) in expects_custody_columns. iter ( ) . enumerate ( ) {
334
- info. add_data_column ( None ) ;
310
+ // Send data columns
311
+ for ( i, & column_index) in expects_custody_columns. iter ( ) . enumerate ( ) {
312
+ info. add_custody_columns (
313
+ blocks
314
+ . iter ( )
315
+ . flat_map ( |b| b. 1 . iter ( ) . filter ( |d| d. index == column_index) . cloned ( ) )
316
+ . collect ( ) ,
317
+ ) ;
335
318
336
319
if i < expects_custody_columns. len ( ) - 1 {
337
320
assert ! (
@@ -353,12 +336,19 @@ mod tests {
353
336
#[ test]
354
337
fn rpc_block_with_custody_columns_batched ( ) {
355
338
let spec = test_spec :: < E > ( ) ;
356
- let expects_custody_columns = vec ! [ 1 , 2 , 3 , 4 ] ;
357
- let num_of_data_column_requests = 2 ;
339
+ let batched_column_requests = vec ! [ vec![ 1_u64 , 2 ] , vec![ 3 , 4 ] ] ;
340
+ let expects_custody_columns = batched_column_requests
341
+ . iter ( )
342
+ . cloned ( )
343
+ . flatten ( )
344
+ . collect :: < Vec < _ > > ( ) ;
345
+ let custody_column_request_ids =
346
+ ( 0 ..batched_column_requests. len ( ) as u32 ) . collect :: < Vec < _ > > ( ) ;
347
+ let num_of_data_column_requests = custody_column_request_ids. len ( ) ;
358
348
let mut info = RangeBlockComponentsRequest :: < E > :: new (
359
349
false ,
360
350
Some ( expects_custody_columns. clone ( ) ) ,
361
- Some ( num_of_data_column_requests ) ,
351
+ Some ( custody_column_request_ids . len ( ) ) ,
362
352
vec ! [ PeerId :: random( ) ] ,
363
353
) ;
364
354
let mut rng = XorShiftRng :: from_seed ( [ 42 ; 16 ] ) ;
@@ -374,25 +364,23 @@ mod tests {
374
364
. collect :: < Vec < _ > > ( ) ;
375
365
376
366
// Send blocks and complete terminate response
377
- for block in & blocks {
378
- info. add_block_response ( Some ( block. 0 . clone ( ) . into ( ) ) ) ;
379
- }
380
- info. add_block_response ( None ) ;
367
+ info. add_blocks ( blocks. iter ( ) . map ( |b| b. 0 . clone ( ) . into ( ) ) . collect ( ) ) ;
381
368
// Assert response is not finished
382
369
assert ! ( !info. is_finished( ) ) ;
383
370
384
- // Send data columns interleaved
385
- for block in & blocks {
386
- for column in & block. 1 {
387
- if expects_custody_columns. contains ( & column. index ) {
388
- info. add_data_column ( Some ( column. clone ( ) ) ) ;
389
- }
390
- }
391
- }
371
+ for ( i, column_indices) in batched_column_requests. iter ( ) . enumerate ( ) {
372
+ // Send the set of columns in the same batch request
373
+ info. add_custody_columns (
374
+ blocks
375
+ . iter ( )
376
+ . flat_map ( |b| {
377
+ b. 1 . iter ( )
378
+ . filter ( |d| column_indices. contains ( & d. index ) )
379
+ . cloned ( )
380
+ } )
381
+ . collect :: < Vec < _ > > ( ) ,
382
+ ) ;
392
383
393
- // Terminate the requests
394
- for i in 0 ..num_of_data_column_requests {
395
- info. add_data_column ( None ) ;
396
384
if i < num_of_data_column_requests - 1 {
397
385
assert ! (
398
386
!info. is_finished( ) ,
0 commit comments