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

Optimize List Validator Assignments #4456

Merged
merged 12 commits into from Jan 9, 2020
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 49 additions & 38 deletions beacon-chain/rpc/beacon/assignments.go
Expand Up @@ -101,42 +101,56 @@ func (bs *Server) ListValidatorAssignments(

shouldFetchFromArchive := requestedEpoch < bs.FinalizationFetcher.FinalizedCheckpt().Epoch

// initialize all committee related data.
committeeAssignments := map[uint64]*helpers.CommitteeAssignmentContainer{}
proposerIndexToSlot := map[uint64]uint64{}
archivedInfo := &pb.ArchivedCommitteeInfo{}
archivedBalances := []uint64{}

if shouldFetchFromArchive {
archivedInfo, err = bs.BeaconDB.ArchivedCommitteeInfo(ctx, requestedEpoch)
Copy link
Member

Choose a reason for hiding this comment

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

I know this is not new code, but can you refactor everything within shouldFetchFromArchive into its function?

if shouldFetchFromArchive {
        bs.archivedCommitteeAssignment(ctx, requestedEpoch)
        ...
} else {
	committeeAssignments, proposerIndexToSlot, err = helpers.CommitteeAssignments(headState, requestedEpoch)
	if err != nil {
		return nil, status.Errorf(codes.Internal, "Could not compute committee assignments: %v", err)
	}
}

if err != nil {
return nil, status.Errorf(
codes.Internal,
"Could not retrieve archived committee info for epoch %d",
requestedEpoch,
)
}
if archivedInfo == nil {
return nil, status.Errorf(
codes.NotFound,
"Could not retrieve data for epoch %d, perhaps --archive in the running beacon node is disabled",
requestedEpoch,
)
}
archivedBalances, err = bs.BeaconDB.ArchivedBalances(ctx, requestedEpoch)
if err != nil {
return nil, status.Errorf(
codes.Internal,
"Could not retrieve archived balances for epoch %d",
requestedEpoch,
)
}
if archivedBalances == nil {
return nil, status.Errorf(
codes.NotFound,
"Could not retrieve data for epoch %d, perhaps --archive in the running beacon node is disabled",
requestedEpoch,
)
}
} else {
committeeAssignments, proposerIndexToSlot, err = helpers.CommitteeAssignments(headState, requestedEpoch)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not compute committee assignments: %v", err)
}
}

for _, index := range filteredIndices[start:end] {
if int(index) >= len(headState.Validators) {
return nil, status.Errorf(codes.OutOfRange, "Validator index %d >= validator count %d",
index, len(headState.Validators))
}
if shouldFetchFromArchive {
archivedInfo, err := bs.BeaconDB.ArchivedCommitteeInfo(ctx, requestedEpoch)
if err != nil {
return nil, status.Errorf(
codes.Internal,
"Could not retrieve archived committee info for epoch %d",
requestedEpoch,
)
}
if archivedInfo == nil {
return nil, status.Errorf(
codes.NotFound,
"Could not retrieve data for epoch %d, perhaps --archive in the running beacon node is disabled",
requestedEpoch,
)
}
archivedBalances, err := bs.BeaconDB.ArchivedBalances(ctx, requestedEpoch)
if err != nil {
return nil, status.Errorf(
codes.Internal,
"Could not retrieve archived balances for epoch %d",
requestedEpoch,
)
}
if archivedBalances == nil {
return nil, status.Errorf(
codes.NotFound,
"Could not retrieve data for epoch %d, perhaps --archive in the running beacon node is disabled",
requestedEpoch,
)
}
committee, committeeIndex, attesterSlot, proposerSlot, err := archivedValidatorCommittee(
requestedEpoch,
index,
Expand All @@ -157,15 +171,12 @@ func (bs *Server) ListValidatorAssignments(
res = append(res, assign)
continue
}
committee, committeeIndex, attesterSlot, proposerSlot, err := helpers.CommitteeAssignment(headState, requestedEpoch, index)
Copy link
Member

Choose a reason for hiding this comment

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

Yikes, this was the really slow one!

if err != nil {
return nil, status.Errorf(codes.Internal, "Could not retrieve assignment for validator %d: %v", index, err)
}
comAssingnment := committeeAssignments[index]
Copy link
Member

Choose a reason for hiding this comment

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

Typo here

assign := &ethpb.ValidatorAssignments_CommitteeAssignment{
BeaconCommittees: committee,
CommitteeIndex: committeeIndex,
AttesterSlot: attesterSlot,
ProposerSlot: proposerSlot,
BeaconCommittees: comAssingnment.Committee,
CommitteeIndex: comAssingnment.CommitteeIndex,
AttesterSlot: comAssingnment.AttesterSlot,
ProposerSlot: proposerIndexToSlot[index],
PublicKey: headState.Validators[index].PublicKey,
}
res = append(res, assign)
Expand Down