@@ -16,7 +16,7 @@ use bytes::Bytes;
16
16
use itertools:: Itertools as _;
17
17
use starfish_config:: AuthorityIndex ;
18
18
use tokio:: time:: Instant ;
19
- use tracing:: { debug, error, info} ;
19
+ use tracing:: { debug, error, info, warn } ;
20
20
21
21
use crate :: {
22
22
block_header:: {
@@ -1294,6 +1294,59 @@ impl DagState {
1294
1294
block_refs
1295
1295
}
1296
1296
1297
+ /// Removes and returns up to `MAX_HEADERS_PER_BUNDLE` block references that
1298
+ /// the given authority has not yet seen, limited to rounds strictly
1299
+ /// below `round_upper_bound_exclusive` and filtered by the provided authors
1300
+ /// `useful_authorities`.
1301
+ ///
1302
+ /// Side effects:
1303
+ /// - Updates `block_headers_not_known_by_authority` so these refs are no
1304
+ /// longer considered "unknown" for the authority.
1305
+ /// - Marks the given authority as knowing these blocks inside
1306
+ /// `recent_dag_cordial_knowledge`.
1307
+ ///
1308
+ /// Returns:
1309
+ /// - A vector of `BlockRef`s corresponding to the unknown blocks that were
1310
+ /// revealed to the authority.
1311
+ fn take_useful_block_refs_for_authority (
1312
+ & mut self ,
1313
+ authority_index : AuthorityIndex ,
1314
+ round_upper_bound_exclusive : Round ,
1315
+ useful_authorities : BTreeSet < AuthorityIndex > ,
1316
+ ) -> Vec < BlockRef > {
1317
+ let set = & mut self . block_headers_not_known_by_authority [ authority_index. value ( ) ] ;
1318
+
1319
+ // Collect candidate block_refs we want to take out
1320
+ let mut to_take = Vec :: new ( ) ;
1321
+
1322
+ for block_ref in set. iter ( ) {
1323
+ if to_take. len ( ) >= MAX_HEADERS_PER_BUNDLE
1324
+ || block_ref. round >= round_upper_bound_exclusive
1325
+ {
1326
+ break ;
1327
+ }
1328
+ if useful_authorities. contains ( & block_ref. author ) {
1329
+ to_take. push ( block_ref. clone ( ) ) ;
1330
+ }
1331
+ }
1332
+
1333
+ // Remove the references from the set and update cordial knowledge
1334
+ for block_ref in & to_take {
1335
+ set. remove ( block_ref) ;
1336
+
1337
+ if let Some ( ( _, who_knows_given_block) ) = self . recent_dag_cordial_knowledge
1338
+ [ block_ref. author . value ( ) ]
1339
+ . get_mut ( & ( block_ref. round , block_ref. digest ) )
1340
+ {
1341
+ who_knows_given_block. insert ( authority_index) ;
1342
+ } else {
1343
+ warn ! ( "Block_ref {block_ref} missing in recent_dag_cordial_knowledge" ) ;
1344
+ }
1345
+ }
1346
+
1347
+ to_take
1348
+ }
1349
+
1297
1350
/// Retrieves up to `MAX_HEADERS_PER_BUNDLE` previously unknown block
1298
1351
/// headers for the given authority, restricted to rounds strictly below
1299
1352
/// `round_upper_bound_exclusive`.
@@ -1330,11 +1383,11 @@ impl DagState {
1330
1383
round_upper_bound_exclusive : Round ,
1331
1384
useful_authorities : BTreeSet < AuthorityIndex > ,
1332
1385
) -> Vec < VerifiedBlockHeader > {
1333
- let block_refs = self
1334
- . take_unknown_block_refs_for_authority ( authority_index, round_upper_bound_exclusive )
1335
- . into_iter ( )
1336
- . filter ( |block_ref| useful_authorities. contains ( & block_ref . author ) )
1337
- . collect :: < Vec < _ > > ( ) ;
1386
+ let block_refs = self . take_useful_block_refs_for_authority (
1387
+ authority_index,
1388
+ round_upper_bound_exclusive ,
1389
+ useful_authorities,
1390
+ ) ;
1338
1391
1339
1392
self . get_block_headers ( & block_refs)
1340
1393
. into_iter ( )
0 commit comments