Skip to content

Commit f16ea92

Browse files
authored
refactor(blockchain): collapse the greedy selection loop guards (#592)
## 🗒️ Description / Motivation Follow-up to #590, which made the coverage tie-break in `extend_proofs_greedily` deterministic and left the loop around it with three exits for two conditions. ## What Changed | File | Change | |------|--------| | `crates/blockchain/src/block_builder.rs` | `while !remaining_indices.is_empty()` made the `None` arm of the best-candidate lookup unreachable, and that arm in turn made the `proofs.is_empty()` early return redundant. Now one `loop` with one exit — no candidate, or none that adds coverage — and `best_count` no longer needs a binding just to be compared against zero. The comment records that candidates must stay in ascending order, so the `retain` is not later "optimized" into a `swap_remove` that would reintroduce the arbitrary tie-break #590 removed | ## Correctness / Behavior Guarantees Pure refactor — same selection, same order, same stopping point. The removed guards were unreachable or redundant, not load-bearing. ## Tests Added / Run - `extend_proofs_greedily_selects_nothing_from_an_empty_pool` pins the path the early return used to shortcut. - `make fmt`, `make lint`, and the 68 `ethlambda-blockchain` lib tests — all clean on current main. ## Related Issues / PRs - Follow-up to #590 ## ✅ Verification Checklist - [x] Ran `make fmt` — clean - [x] Ran `make lint` (clippy with `-D warnings`) — clean - [x] Ran `make test` (`cargo test --workspace --profile release-fast`) — all passing
1 parent 5f04726 commit f16ea92

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

‎crates/blockchain/src/block_builder.rs‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -801,19 +801,17 @@ fn extend_proofs_greedily(
801801
selected: &mut Vec<(AggregatedAttestation, SingleMessageAggregate)>,
802802
att_data: &AttestationData,
803803
) {
804-
if proofs.is_empty() {
805-
return;
806-
}
807-
808804
let mut covered: HashSet<u64> = HashSet::new();
809805
let mut remaining_indices: Vec<usize> = (0..proofs.len()).collect();
810806

811-
while !remaining_indices.is_empty() {
807+
loop {
812808
// Pick proof covering the most uncovered validators (count only, no
813809
// allocation). Coverage ties break to the lowest index (pool insertion
814810
// order): a HashSet here would let hash-iteration order pick an
815811
// arbitrary equal-coverage winner, making the built block's
816-
// aggregation bits differ from run to run.
812+
// aggregation bits differ from run to run. The candidates must
813+
// therefore stay in ascending order, which is why the winner leaves
814+
// via `retain` below and not `swap_remove`.
817815
let best = remaining_indices
818816
.iter()
819817
.map(|&idx| {
@@ -825,12 +823,10 @@ fn extend_proofs_greedily(
825823
})
826824
.max_by_key(|&(idx, count)| (count, Reverse(idx)));
827825

828-
let Some((best_idx, best_count)) = best else {
826+
// Stops on an empty pool, and once no candidate adds coverage.
827+
let Some((best_idx, _)) = best.filter(|&(_, count)| count > 0) else {
829828
break;
830829
};
831-
if best_count == 0 {
832-
break;
833-
}
834830

835831
let proof = &proofs[best_idx];
836832

@@ -1989,4 +1985,14 @@ mod tests {
19891985
let pool_order: Vec<Vec<u64>> = (0..6).map(|g| vec![g * 2, g * 2 + 1]).collect();
19901986
assert_eq!(order, pool_order);
19911987
}
1988+
1989+
/// An empty pool selects nothing: the candidate loop finds no best proof
1990+
/// and stops on its first round, so no `proofs.is_empty()` guard is needed
1991+
/// ahead of it.
1992+
#[test]
1993+
fn extend_proofs_greedily_selects_nothing_from_an_empty_pool() {
1994+
let mut selected = Vec::new();
1995+
extend_proofs_greedily(&[], &mut selected, &make_att_data(1));
1996+
assert!(selected.is_empty());
1997+
}
19921998
}

0 commit comments

Comments
 (0)