-
Notifications
You must be signed in to change notification settings - Fork 995
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
Add back State To ComputeProposerIndex #5674
Changes from all commits
6a30332
50e9176
7bb6cb0
46e92d2
741feb4
0da0226
b2dd0fb
4a898e8
77a9f26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -179,11 +179,63 @@ func BeaconProposerIndex(state *stateTrie.BeaconState) (uint64, error) { | |||||||||
return 0, errors.Wrap(err, "could not update committee cache") | ||||||||||
} | ||||||||||
|
||||||||||
return ComputeProposerIndex(state.Validators(), indices, seedWithSlotHash) | ||||||||||
return ComputeProposerIndex(state, indices, seedWithSlotHash) | ||||||||||
} | ||||||||||
|
||||||||||
// ComputeProposerIndex returns the index sampled by effective balance, which is used to calculate proposer. | ||||||||||
// | ||||||||||
// This method is more efficient than ComputeProposerIndexWithValidators as it uses the read only validator | ||||||||||
// abstraction to retrieve validator related data. Whereas the other method requires a whole copy of the validator | ||||||||||
// set. | ||||||||||
// | ||||||||||
// Spec pseudocode definition: | ||||||||||
// def compute_proposer_index(state: BeaconState, indices: Sequence[ValidatorIndex], seed: Hash) -> ValidatorIndex: | ||||||||||
// """ | ||||||||||
// Return from ``indices`` a random index sampled by effective balance. | ||||||||||
// """ | ||||||||||
// assert len(indices) > 0 | ||||||||||
// MAX_RANDOM_BYTE = 2**8 - 1 | ||||||||||
// i = 0 | ||||||||||
// while True: | ||||||||||
// candidate_index = indices[compute_shuffled_index(ValidatorIndex(i % len(indices)), len(indices), seed)] | ||||||||||
// random_byte = hash(seed + int_to_bytes(i // 32, length=8))[i % 32] | ||||||||||
// effective_balance = state.validators[candidate_index].effective_balance | ||||||||||
// if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE * random_byte: | ||||||||||
// return ValidatorIndex(candidate_index) | ||||||||||
// i += 1 | ||||||||||
func ComputeProposerIndex(bState *stateTrie.BeaconState, activeIndices []uint64, seed [32]byte) (uint64, error) { | ||||||||||
length := uint64(len(activeIndices)) | ||||||||||
if length == 0 { | ||||||||||
return 0, errors.New("empty active indices list") | ||||||||||
} | ||||||||||
maxRandomByte := uint64(1<<8 - 1) | ||||||||||
hashFunc := hashutil.CustomSHA256Hasher() | ||||||||||
|
||||||||||
for i := uint64(0); ; i++ { | ||||||||||
candidateIndex, err := ComputeShuffledIndex(i%length, length, seed, true /* shuffle */) | ||||||||||
if err != nil { | ||||||||||
return 0, err | ||||||||||
} | ||||||||||
candidateIndex = activeIndices[candidateIndex] | ||||||||||
if int(candidateIndex) >= bState.NumValidators() { | ||||||||||
return 0, errors.New("active index out of range") | ||||||||||
} | ||||||||||
b := append(seed[:], bytesutil.Bytes8(i/32)...) | ||||||||||
randomByte := hashFunc(b)[i%32] | ||||||||||
v, err := bState.ValidatorAtIndexReadOnly(candidateIndex) | ||||||||||
if err != nil { | ||||||||||
return 0, nil | ||||||||||
} | ||||||||||
effectiveBal := v.EffectiveBalance() | ||||||||||
|
||||||||||
if effectiveBal*maxRandomByte >= params.BeaconConfig().MaxEffectiveBalance*uint64(randomByte) { | ||||||||||
return candidateIndex, nil | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
// ComputeProposerIndexWithValidators returns the index sampled by effective balance, which is used to calculate proposer. | ||||||||||
// | ||||||||||
// Note: This method signature deviates slightly from the spec recommended definition. The full | ||||||||||
// state object is not required to compute the proposer index. | ||||||||||
// | ||||||||||
|
@@ -202,7 +254,8 @@ func BeaconProposerIndex(state *stateTrie.BeaconState) (uint64, error) { | |||||||||
// if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE * random_byte: | ||||||||||
// return ValidatorIndex(candidate_index) | ||||||||||
// i += 1 | ||||||||||
func ComputeProposerIndex(validators []*ethpb.Validator, activeIndices []uint64, seed [32]byte) (uint64, error) { | ||||||||||
// Deprecated: Prefer using the beacon state with ComputeProposerIndex to avoid an unnecessary copy of the validator set. | ||||||||||
func ComputeProposerIndexWithValidators(validators []*ethpb.Validator, activeIndices []uint64, seed [32]byte) (uint64, error) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need this? Can it be marked as deprecated?
Suggested change
If deprecated is not appropriate, please still put a message somewhere that users should prefer the other method whenever possible.Otherwise we could still see misuse with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think deprecated would be appropriate here. This is only still there because an archival method requires this, but that archival method is going to be deprecated anyway There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make sure this method is tested as well? |
||||||||||
length := uint64(len(activeIndices)) | ||||||||||
if length == 0 { | ||||||||||
return 0, errors.New("empty active indices list") | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somewhere in the comment, can you please mention:
1.) This is faster than
ComputeProposerIndexWithValidators
2.) It uses read only validator set from beacon state
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright done @terencechain