Skip to content

feat(consensus): implement get_extended_sample_count from Fulu spec (#1105) - #1575

Open
alok-108 wants to merge 2 commits into
ReamLabs:masterfrom
alok-108:feat/get-extended-sample-count
Open

alok-108 wants to merge 2 commits into
ReamLabs:masterfrom
alok-108:feat/get-extended-sample-count

Conversation

@alok-108

@alok-108 alok-108 commented Sep 12, 2026 •

Copy link
Copy Markdown

What was wrong?

Issue #1105 requested implementing get_extended_sample_count from the Ethereum Fulu specification draft (peer-sampling.md). The codebase lacked the hypergeometric cumulative distribution functions and sample-count calculation routines for peer sampling.

How was it fixed?

  • Implemented math_comb (binomial coefficient combinations n choose k) matching Python's math.comb.
  • Implemented hypergeom_cdf computing hypergeometric distribution CDF matching consensus-specs reference.
  • Implemented get_extended_sample_count matching the Fulu peer sampling specification.
  • Added comprehensive unit tests covering:
    • Exact spec test vectors across failure ranges 0..=64
    • Monotonicity checks
    • Edge cases and panic bounds checks

Note: As noted in discussions, upstream Ethereum consensus-specs PR #4393 removed peer sampling in favor of custody sampling.

Closes #1105

To-Do

…eamLabs#1105)

- Implement get_extended_sample_count adhering to Ethereum Fulu peer-sampling spec
- Implement math_comb and hypergeom_cdf helpers
- Add unit tests covering spec test vectors, monotonicity, zero-failures, and panic on invalid bounds

Resolves ReamLabs#1105
@perfogic

Copy link
Copy Markdown
Collaborator

Same for this one, peer-sampling is removed from consensus-specs since ethereum/consensus-specs#4393

@alok-108

Copy link
Copy Markdown
Author

Thanks for pointing this out @perfogic!

I verified that upstream consensus-specs#4393 indeed dropped specs/fulu/peer-sampling.md from the Fulu scope in favor of custody sampling (as also tracked in #1478 and #1552).

The implementation here (math_comb, hypergeom_cdf, and get_extended_sample_count) was built against the reference in #1105 and passes all original spec test vectors and property tests.

Given that peer sampling is no longer part of the official Fulu consensus specification:

  • If Ream is strictly adhering to post-#4393 Fulu specifications (custody sampling), we can close this PR and Issue Implement get_extended_sample_count from the Fulu Spec #1105 as superseded by upstream spec changes.
  • Alternatively, if any of the underlying hypergeometric / combinatorial math utilities (math_comb, hypergeom_cdf) are useful to retain in a general math/stats helper module for custody scoring or analysis, I can gladly refactor and move them.

@shariqnaiyer @perfogic please let me know your preferred direction and I can close or adapt accordingly!

@alok-108 alok-108 changed the title feat(consensus): implement get_extended_sample_count from Fulu spec (… feat(consensus): implement get_extended_sample_count from Fulu spec (#1105) Sep 17, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be using shorthands, can you make the code more readable and reduce the comments

@alok-108 alok-108 Sep 27, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback @KolbyML! I've addressed this in commit 2f714f5:

  • Eliminated shorthands: Renamed math_comb to combinations and hypergeom_cdf to hypergeometric_cdf. Replaced single-letter and abbreviated variable names with self-documenting ones (total_items, chosen_items, result, index, population_size, sample_size, success_states, denominator, numerator, cumulative_probability, etc.).
  • Aligned parameter ordering: Reordered hypergeometric_cdf parameters to match the canonical Python consensus-specs order: (allowed_successes, success_states, population_size, sample_size).
  • Reduced comment density: Lowered comment ratio, removing redundant inline comments in tests while preserving essential specification references and doc comments.
  • All 6 unit tests, cargo fmt, and cargo clippy pass cleanly.

…ments

- Rename math_comb to combinations and hypergeom_cdf to hypergeometric_cdf
- Replace shorthand variable names with self-documenting identifiers
- Reorder hypergeometric_cdf parameters to match Python consensus-specs reference
- Reduce comment density to under 8% while preserving core spec references
- Tighten hypergeometric_cdf loop bound
@alok-108
alok-108 requested a review from KolbyML September 27, 2026 01:36
@alok-108 alok-108 closed this Oct 22, 2026
@alok-108
alok-108 deleted the feat/get-extended-sample-count branch October 22, 2026 22:42
@alok-108
alok-108 restored the feat/get-extended-sample-count branch October 22, 2026 22:45
@alok-108 alok-108 reopened this Oct 22, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement get_extended_sample_count from the Fulu Spec

3 participants