Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use 16-bit random value in validator filter #8916

Merged
merged 12 commits into from
Dec 17, 2024

Conversation

jtraglia
Copy link
Contributor

@jtraglia jtraglia commented Dec 12, 2024

PR Description

This PR implements the changes defined here:

Notably, this PR will:

  • Override computeProposerIndex and getNextSyncCommitteeIndices.
  • In computeProposerIndex, rename hash to randomBytes to match the spec.
  • Add cached randomBytes to getNextSyncCommitteeIndices.
  • Define new MAX_RANDOM_VALUE equal to 2**16-1.

See the original implementations here:

protected int computeProposerIndex(
final BeaconState state,
final IntList indices,
final Bytes32 seed,
final UInt64 maxEffectiveBalance) {
checkArgument(!indices.isEmpty(), "compute_proposer_index indices must not be empty");
final Sha256 sha256 = getSha256Instance();
int i = 0;
final int total = indices.size();
byte[] hash = null;
while (true) {
final int candidateIndex = indices.getInt(computeShuffledIndex(i % total, total, seed));
if (i % 32 == 0) {
hash = sha256.digest(seed, uint64ToBytes(Math.floorDiv(i, 32L)));
}
final int randomByte = UnsignedBytes.toInt(hash[i % 32]);
final UInt64 validatorEffectiveBalance =
state.getValidators().get(candidateIndex).getEffectiveBalance();
if (validatorEffectiveBalance
.times(MAX_RANDOM_BYTE)
.isGreaterThanOrEqualTo(maxEffectiveBalance.times(randomByte))) {
return candidateIndex;
}
i++;
}
}

protected IntList getNextSyncCommitteeIndices(
final BeaconState state, final UInt64 maxEffectiveBalance) {
final UInt64 epoch = getCurrentEpoch(state).plus(1);
final IntList activeValidatorIndices = getActiveValidatorIndices(state, epoch);
final int activeValidatorCount = activeValidatorIndices.size();
checkArgument(activeValidatorCount > 0, "Provided state has no active validators");
final Bytes32 seed = getSeed(state, epoch, Domain.SYNC_COMMITTEE);
int i = 0;
final SszList<Validator> validators = state.getValidators();
final IntList syncCommitteeIndices = new IntArrayList();
final int syncCommitteeSize = altairConfig.getSyncCommitteeSize();
final Sha256 sha256 = getSha256Instance();
while (syncCommitteeIndices.size() < syncCommitteeSize) {
final int shuffledIndex =
miscHelpers.computeShuffledIndex(i % activeValidatorCount, activeValidatorCount, seed);
final int candidateIndex = activeValidatorIndices.getInt(shuffledIndex);
final int randomByte =
ByteUtil.toUnsignedInt(sha256.digest(seed, uint64ToBytes(i / 32))[i % 32]);
final UInt64 effectiveBalance = validators.get(candidateIndex).getEffectiveBalance();
if (effectiveBalance
.times(MAX_RANDOM_BYTE)
.isGreaterThanOrEqualTo(maxEffectiveBalance.times(randomByte))) {
syncCommitteeIndices.add(candidateIndex);
}
i++;
}

Documentation

  • I thought about documentation and added the doc-change-required label to this PR if updates are required.

Changelog

  • I thought about adding a changelog entry, and added one if I deemed necessary.

@tbenr
Copy link
Contributor

tbenr commented Dec 16, 2024

in this PR we should re-enable2 fizzing tests disabled here: #8923

@jtraglia
Copy link
Contributor Author

Hmm I'm not sure why these are still failing. The only difference is the body_root.

image

@rolfyone
Copy link
Contributor

Hmm I'm not sure why these are still failing. The only difference is the body_root.

I've got a body root diff in mine too...

@tbenr
Copy link
Contributor

tbenr commented Dec 16, 2024

this is weird. I'll spend some more time on it tomorrow, if in the meantime you don't solve the mystery

@tbenr
Copy link
Contributor

tbenr commented Dec 17, 2024

lol, my bad. Seems like I copied the wrong spec files. I also fixed a uint test that seems to be affected by the change

@jtraglia
Copy link
Contributor Author

Haha no worries. Thank you for fixing this. Comparing prettySerialize outputs is a good idea too!

Copy link
Contributor

@tbenr tbenr left a comment

Choose a reason for hiding this comment

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

LGTM

@tbenr tbenr merged commit 4292c45 into Consensys:master Dec 17, 2024
16 checks passed
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.

4 participants