Skip to content

Commit 03e0624

Browse files
committed
[DK::shortcut] Further shortcut with singular strong group
1 parent 4b2a66c commit 03e0624

1 file changed

Lines changed: 31 additions & 13 deletions

File tree

‎consensus/src/processes/dagknight/protocol.rs‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ pub struct DagknightData {
114114
pub conflict_ordered_parents: Vec<Hash>, // The rest of the parents, ordered by conflict hierarchy (parents from latest/topmost conflicts first)
115115
}
116116

117+
#[derive(Default)]
118+
struct ShortcutResult {
119+
strong_groups: Vec<Hash>,
120+
weak_groups: BlockHashSet,
121+
}
122+
117123
impl<
118124
C: DagknightStore + DagknightStoreReader,
119125
O: HeaderStoreReader + 'static,
@@ -199,23 +205,31 @@ impl<
199205
// Check shortcut to identify weak groups at k=0
200206
// This doesn't change conflict hierarchy - it just marks groups as weak
201207
// for later processes to decide what to do with such weak groups
202-
let weak_groups: BlockHashSet =
203-
if use_shortcut { self.check_weak_block_shortcut(&agreement_grouping, conflict_genesis) } else { BlockHashSet::new() };
208+
let ShortcutResult { strong_groups, weak_groups } =
209+
if use_shortcut { self.check_weak_block_shortcut(&agreement_grouping, conflict_genesis) } else { Default::default() };
204210

205211
// Pick a "winner" among these subgroups
206212
let (winning_conflict_genesis, winning_subgroup) = {
207-
let best_groups = self.rank(conflict_genesis, &agreement_grouping, &curr_subgroup, &weak_groups);
213+
if use_shortcut && strong_groups.len() == 1 {
214+
// If there is a singular strong group, win with it immediately
215+
let key = strong_groups[0];
216+
let value = agreement_grouping[&key].clone();
208217

209-
#[allow(clippy::style)]
210-
let final_winner = if best_groups.len() > 1 {
211-
self.tie_breaking(conflict_genesis, &curr_subgroup, &best_groups)
218+
(key, value)
212219
} else {
213-
let single_winner = best_groups.into_iter().next().expect("best_groups should be non-empty after filtering");
214-
(single_winner.conflict_genesis, single_winner.subgroup)
215-
};
220+
let best_groups = self.rank(conflict_genesis, &agreement_grouping, &curr_subgroup, &weak_groups);
221+
222+
#[allow(clippy::style)]
223+
let final_winner = if best_groups.len() > 1 {
224+
self.tie_breaking(conflict_genesis, &curr_subgroup, &best_groups)
225+
} else {
226+
let single_winner = best_groups.into_iter().next().expect("best_groups should be non-empty after filtering");
227+
(single_winner.conflict_genesis, single_winner.subgroup)
228+
};
216229

217-
// This will always be Some since curr_subgroup.len() > 1 and thus there is at least one subgroup
218-
final_winner
230+
// This will always be Some since curr_subgroup.len() > 1 and thus there is at least one subgroup
231+
final_winner
232+
}
219233
};
220234

221235
// Add the non-winners to the ordered parents
@@ -370,7 +384,7 @@ impl<
370384
best_groups_cell.take()
371385
}
372386

373-
fn check_weak_block_shortcut(&self, agreement_grouping: &HashMap<Hash, Arc<Vec<Hash>>>, conflict_genesis: Hash) -> BlockHashSet {
387+
fn check_weak_block_shortcut(&self, agreement_grouping: &HashMap<Hash, Arc<Vec<Hash>>>, conflict_genesis: Hash) -> ShortcutResult {
374388
// First pass: compute k=0 blue_work for each subgroup
375389
let subgroup_work: Vec<(Hash, Uint192)> = agreement_grouping
376390
.iter()
@@ -403,6 +417,8 @@ impl<
403417

404418
// Collect weak subgroups (subgroups whose blue_work < threshold)
405419
let mut weak_groups = BlockHashSet::new();
420+
let mut strong_groups = Vec::new();
421+
406422
for (cg, blue_work) in &subgroup_work {
407423
if *blue_work < threshold {
408424
debug!(
@@ -413,12 +429,14 @@ impl<
413429
threshold.as_u64()
414430
);
415431
weak_groups.insert(*cg);
432+
} else {
433+
strong_groups.push(*cg);
416434
}
417435
}
418436

419437
debug!("SHORTCUT cg: {} | {} weak groups out of {} total", conflict_genesis, weak_groups.len(), subgroup_work.len());
420438

421-
weak_groups
439+
ShortcutResult { strong_groups, weak_groups }
422440
}
423441

424442
/// Applies a coloring to the conflict zone, and determines if the

0 commit comments

Comments
 (0)