-
Notifications
You must be signed in to change notification settings - Fork 917
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
Optimize Archival Assignment Retrieval #4480
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
344a618
optimize further
nisdas 0a3d543
remove func
nisdas 22f56a4
Merge branch 'master' into optimizeArchival
nisdas 0643707
Merge refs/heads/master into optimizeArchival
prylabs-bulldozer[bot] e9b88d6
Merge refs/heads/master into optimizeArchival
prylabs-bulldozer[bot] 4cfe4ef
Merge refs/heads/master into optimizeArchival
prylabs-bulldozer[bot] 381e58e
Merge refs/heads/master into optimizeArchival
prylabs-bulldozer[bot] 9b25e66
Merge refs/heads/master into optimizeArchival
prylabs-bulldozer[bot] 28b546f
Merge refs/heads/master into optimizeArchival
prylabs-bulldozer[bot] 1cb52a1
raul's review
nisdas 24740d5
Merge branch 'optimizeArchival' of https://github.com/prysmaticlabs/g…
nisdas 18f8dcc
preston's review
nisdas File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ package beacon | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/pkg/errors" | ||
|
@@ -106,12 +105,22 @@ func (bs *Server) ListValidatorAssignments( | |
proposerIndexToSlot := map[uint64]uint64{} | ||
archivedInfo := &pb.ArchivedCommitteeInfo{} | ||
archivedBalances := []uint64{} | ||
archivedAssingments := make(map[uint64]*ethpb.ValidatorAssignments_CommitteeAssignment) | ||
|
||
if shouldFetchFromArchive { | ||
archivedInfo, archivedBalances, err = bs.archivedCommitteeData(ctx, requestedEpoch) | ||
if err != nil { | ||
return nil, err | ||
} | ||
archivedAssingments, err = archivedValidatorCommittee( | ||
requestedEpoch, | ||
archivedInfo, | ||
activeIndices, | ||
archivedBalances, | ||
) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not retrieve archived assignment for epoch %d: %v", requestedEpoch, err) | ||
} | ||
} else { | ||
committeeAssignments, proposerIndexToSlot, err = helpers.CommitteeAssignments(headState, requestedEpoch) | ||
if err != nil { | ||
|
@@ -125,24 +134,12 @@ func (bs *Server) ListValidatorAssignments( | |
index, len(headState.Validators)) | ||
} | ||
if shouldFetchFromArchive { | ||
committee, committeeIndex, attesterSlot, proposerSlot, err := archivedValidatorCommittee( | ||
requestedEpoch, | ||
index, | ||
archivedInfo, | ||
activeIndices, | ||
archivedBalances, | ||
) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, "Could not retrieve archived assignment for validator %d: %v", index, err) | ||
} | ||
assign := ðpb.ValidatorAssignments_CommitteeAssignment{ | ||
BeaconCommittees: committee, | ||
CommitteeIndex: committeeIndex, | ||
AttesterSlot: attesterSlot, | ||
ProposerSlot: proposerSlot, | ||
PublicKey: headState.Validators[index].PublicKey, | ||
assignment, ok := archivedAssingments[index] | ||
if !ok { | ||
return nil, status.Errorf(codes.Internal, "Could not get archived committee assignment for index %d", index) | ||
} | ||
res = append(res, assign) | ||
assignment.PublicKey = headState.Validators[index].PublicKey | ||
res = append(res, assignment) | ||
continue | ||
} | ||
comAssignment := committeeAssignments[index] | ||
|
@@ -168,25 +165,31 @@ func (bs *Server) ListValidatorAssignments( | |
// information, archived balances, and a set of active validators. | ||
func archivedValidatorCommittee( | ||
epoch uint64, | ||
validatorIndex uint64, | ||
archivedInfo *pb.ArchivedCommitteeInfo, | ||
activeIndices []uint64, | ||
archivedBalances []uint64, | ||
) ([]uint64, uint64, uint64, uint64, error) { | ||
) (map[uint64]*ethpb.ValidatorAssignments_CommitteeAssignment, error) { | ||
proposerSeed := bytesutil.ToBytes32(archivedInfo.ProposerSeed) | ||
attesterSeed := bytesutil.ToBytes32(archivedInfo.AttesterSeed) | ||
|
||
startSlot := helpers.StartSlot(epoch) | ||
proposerIndexToSlot := make(map[uint64]uint64) | ||
activeVals := make([]*ethpb.Validator, len(archivedBalances)) | ||
for i, bal := range archivedBalances { | ||
activeVals[i] = ðpb.Validator{EffectiveBalance: bal} | ||
} | ||
|
||
for slot := startSlot; slot < startSlot+params.BeaconConfig().SlotsPerEpoch; slot++ { | ||
seedWithSlot := append(proposerSeed[:], bytesutil.Bytes8(slot)...) | ||
seedWithSlotHash := hashutil.Hash(seedWithSlot) | ||
i, err := archivedProposerIndex(activeIndices, archivedBalances, seedWithSlotHash) | ||
i, err := helpers.ComputeProposerIndex(activeVals, activeIndices, seedWithSlotHash) | ||
if err != nil { | ||
return nil, 0, 0, 0, errors.Wrapf(err, "could not check proposer at slot %d", slot) | ||
return nil, errors.Wrapf(err, "could not check proposer at slot %d", slot) | ||
} | ||
proposerIndexToSlot[i] = slot | ||
} | ||
|
||
assingmentMap := make(map[uint64]*ethpb.ValidatorAssignments_CommitteeAssignment) | ||
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. typo |
||
for slot := startSlot; slot < startSlot+params.BeaconConfig().SlotsPerEpoch; slot++ { | ||
var countAtSlot = uint64(len(activeIndices)) / params.BeaconConfig().SlotsPerEpoch / params.BeaconConfig().TargetCommitteeSize | ||
if countAtSlot > params.BeaconConfig().MaxCommitteesPerSlot { | ||
|
@@ -196,21 +199,21 @@ func archivedValidatorCommittee( | |
countAtSlot = 1 | ||
} | ||
for i := uint64(0); i < countAtSlot; i++ { | ||
epochOffset := i + (slot%params.BeaconConfig().SlotsPerEpoch)*countAtSlot | ||
totalCount := countAtSlot * params.BeaconConfig().SlotsPerEpoch | ||
committee, err := helpers.ComputeCommittee(activeIndices, attesterSeed, epochOffset, totalCount) | ||
committee, err := helpers.BeaconCommittee(activeIndices, attesterSeed, slot, i) | ||
if err != nil { | ||
return nil, 0, 0, 0, errors.Wrap(err, "could not compute committee") | ||
return nil, errors.Wrap(err, "could not compute committee") | ||
} | ||
for _, index := range committee { | ||
if validatorIndex == index { | ||
proposerSlot, _ := proposerIndexToSlot[validatorIndex] | ||
return committee, i, slot, proposerSlot, nil | ||
assingmentMap[index] = ðpb.ValidatorAssignments_CommitteeAssignment{ | ||
BeaconCommittees: committee, | ||
CommitteeIndex: i, | ||
AttesterSlot: slot, | ||
ProposerSlot: proposerIndexToSlot[index], | ||
} | ||
} | ||
} | ||
} | ||
return nil, 0, 0, 0, fmt.Errorf("could not find committee for validator index %d", validatorIndex) | ||
return assingmentMap, nil | ||
} | ||
|
||
func (bs *Server) archivedCommitteeData(ctx context.Context, requestedEpoch uint64) (*pb.ArchivedCommitteeInfo, | ||
|
@@ -247,12 +250,3 @@ func (bs *Server) archivedCommitteeData(ctx context.Context, requestedEpoch uint | |
} | ||
return archivedInfo, archivedBalances, nil | ||
} | ||
|
||
// helpers.ComputeProposerIndex wrapper. | ||
func archivedProposerIndex(activeIndices []uint64, activeBalances []uint64, seed [32]byte) (uint64, error) { | ||
validators := make([]*ethpb.Validator, len(activeBalances)) | ||
for i, bal := range activeBalances { | ||
validators[i] = ðpb.Validator{EffectiveBalance: bal} | ||
} | ||
return helpers.ComputeProposerIndex(validators, activeIndices, seed) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.