Skip to content

Commit 86b04d6

Browse files
polinikitaVorobyevIlya
authored andcommitted
add new function to take useful block_refs
1 parent 44a8543 commit 86b04d6

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

crates/starfish/core/src/dag_state.rs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use bytes::Bytes;
1616
use itertools::Itertools as _;
1717
use starfish_config::AuthorityIndex;
1818
use tokio::time::Instant;
19-
use tracing::{debug, error, info};
19+
use tracing::{debug, error, info, warn};
2020

2121
use crate::{
2222
block_header::{
@@ -1294,6 +1294,59 @@ impl DagState {
12941294
block_refs
12951295
}
12961296

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+
12971350
/// Retrieves up to `MAX_HEADERS_PER_BUNDLE` previously unknown block
12981351
/// headers for the given authority, restricted to rounds strictly below
12991352
/// `round_upper_bound_exclusive`.
@@ -1330,11 +1383,11 @@ impl DagState {
13301383
round_upper_bound_exclusive: Round,
13311384
useful_authorities: BTreeSet<AuthorityIndex>,
13321385
) -> 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+
);
13381391

13391392
self.get_block_headers(&block_refs)
13401393
.into_iter()

0 commit comments

Comments
 (0)