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 SubscribeCommitteeSubnets VC action #13702

Merged
merged 6 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,700 changes: 850 additions & 850 deletions proto/prysm/v1alpha1/beacon_block.pb.go

Large diffs are not rendered by default.

1,239 changes: 625 additions & 614 deletions proto/prysm/v1alpha1/validator.pb.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions proto/prysm/v1alpha1/validator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ message DutiesResponse {

// Whether the validator belongs in the sync committee and has to perform sync committee duty.
bool is_sync_committee = 8;

// The number of committees in the duty's slot.
uint64 committees_at_slot = 9;
Copy link
Contributor

Choose a reason for hiding this comment

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

All other values in this file have a comment.

}
}

Expand Down
1 change: 0 additions & 1 deletion testing/validator-mock/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/v5/testing/validator-mock",
visibility = ["//visibility:public"],
deps = [
"//consensus-types/primitives:go_default_library",
"//consensus-types/validator:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//validator/client/iface:go_default_library",
Expand Down
3 changes: 1 addition & 2 deletions testing/validator-mock/validator_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions validator/client/beacon-api/beacon_api_validator_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/validator/client/iface"
Expand Down Expand Up @@ -174,9 +173,9 @@ func (c *beaconApiValidatorClient) SubmitValidatorRegistrations(ctx context.Cont
})
}

func (c *beaconApiValidatorClient) SubscribeCommitteeSubnets(ctx context.Context, in *ethpb.CommitteeSubnetsSubscribeRequest, validatorIndices []primitives.ValidatorIndex) (*empty.Empty, error) {
func (c *beaconApiValidatorClient) SubscribeCommitteeSubnets(ctx context.Context, in *ethpb.CommitteeSubnetsSubscribeRequest, duties []*ethpb.DutiesResponse_Duty) (*empty.Empty, error) {
return wrapInMetrics[*empty.Empty]("SubscribeCommitteeSubnets", func() (*empty.Empty, error) {
return new(empty.Empty), c.subscribeCommitteeSubnets(ctx, in, validatorIndices)
return new(empty.Empty), c.subscribeCommitteeSubnets(ctx, in, duties)
})
}

Expand Down
25 changes: 17 additions & 8 deletions validator/client/beacon-api/duties.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ func (c beaconApiValidatorClient) getDutiesForEpoch(
if err != nil {
return nil, errors.Wrapf(err, "failed to get committees for epoch `%d`", epoch)
}
slotCommittees := make(map[string]uint64)
for _, c := range committees {
n, ok := slotCommittees[c.Slot]
if !ok {
n = 0
}
slotCommittees[c.Slot] = n + 1
}

// Mapping from a validator index to its attesting committee's index and slot
attesterDutiesMapping := make(map[primitives.ValidatorIndex]committeeIndexSlotPair)
Expand Down Expand Up @@ -195,14 +203,15 @@ func (c beaconApiValidatorClient) getDutiesForEpoch(
}

duties[index] = &ethpb.DutiesResponse_Duty{
Committee: committeeValidatorIndices,
CommitteeIndex: committeeIndex,
AttesterSlot: attesterSlot,
ProposerSlots: proposerDutySlots[validatorIndex],
PublicKey: pubkey,
Status: validatorStatus.Status,
ValidatorIndex: validatorIndex,
IsSyncCommittee: syncDutiesMapping[validatorIndex],
Committee: committeeValidatorIndices,
CommitteeIndex: committeeIndex,
AttesterSlot: attesterSlot,
ProposerSlots: proposerDutySlots[validatorIndex],
PublicKey: pubkey,
Status: validatorStatus.Status,
ValidatorIndex: validatorIndex,
IsSyncCommittee: syncDutiesMapping[validatorIndex],
CommitteesAtSlot: slotCommittees[strconv.FormatUint(uint64(attesterSlot), 10)],
rkapka marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
72 changes: 39 additions & 33 deletions validator/client/beacon-api/duties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,69 +880,75 @@ func TestGetDutiesForEpoch_Valid(t *testing.T) {
validatorIndices[0],
validatorIndices[1],
},
CommitteeIndex: committeeIndices[0],
AttesterSlot: committeeSlots[0],
PublicKey: pubkeys[0],
Status: statuses[0],
ValidatorIndex: validatorIndices[0],
CommitteeIndex: committeeIndices[0],
AttesterSlot: committeeSlots[0],
PublicKey: pubkeys[0],
Status: statuses[0],
ValidatorIndex: validatorIndices[0],
CommitteesAtSlot: 1,
},
{
Committee: []primitives.ValidatorIndex{
validatorIndices[0],
validatorIndices[1],
},
CommitteeIndex: committeeIndices[0],
AttesterSlot: committeeSlots[0],
PublicKey: pubkeys[1],
Status: statuses[1],
ValidatorIndex: validatorIndices[1],
CommitteeIndex: committeeIndices[0],
AttesterSlot: committeeSlots[0],
PublicKey: pubkeys[1],
Status: statuses[1],
ValidatorIndex: validatorIndices[1],
CommitteesAtSlot: 1,
},
{
Committee: []primitives.ValidatorIndex{
validatorIndices[2],
validatorIndices[3],
},
CommitteeIndex: committeeIndices[1],
AttesterSlot: committeeSlots[1],
PublicKey: pubkeys[2],
Status: statuses[2],
ValidatorIndex: validatorIndices[2],
CommitteeIndex: committeeIndices[1],
AttesterSlot: committeeSlots[1],
PublicKey: pubkeys[2],
Status: statuses[2],
ValidatorIndex: validatorIndices[2],
CommitteesAtSlot: 1,
},
{
Committee: []primitives.ValidatorIndex{
validatorIndices[2],
validatorIndices[3],
},
CommitteeIndex: committeeIndices[1],
AttesterSlot: committeeSlots[1],
PublicKey: pubkeys[3],
Status: statuses[3],
ValidatorIndex: validatorIndices[3],
CommitteeIndex: committeeIndices[1],
AttesterSlot: committeeSlots[1],
PublicKey: pubkeys[3],
Status: statuses[3],
ValidatorIndex: validatorIndices[3],
CommitteesAtSlot: 1,
},
{
Committee: []primitives.ValidatorIndex{
validatorIndices[4],
validatorIndices[5],
},
CommitteeIndex: committeeIndices[2],
AttesterSlot: committeeSlots[2],
PublicKey: pubkeys[4],
Status: statuses[4],
ValidatorIndex: validatorIndices[4],
ProposerSlots: expectedProposerSlots1,
CommitteeIndex: committeeIndices[2],
AttesterSlot: committeeSlots[2],
PublicKey: pubkeys[4],
Status: statuses[4],
ValidatorIndex: validatorIndices[4],
ProposerSlots: expectedProposerSlots1,
CommitteesAtSlot: 1,
},
{
Committee: []primitives.ValidatorIndex{
validatorIndices[4],
validatorIndices[5],
},
CommitteeIndex: committeeIndices[2],
AttesterSlot: committeeSlots[2],
PublicKey: pubkeys[5],
Status: statuses[5],
ValidatorIndex: validatorIndices[5],
ProposerSlots: expectedProposerSlots2,
IsSyncCommittee: testCase.fetchSyncDuties,
CommitteeIndex: committeeIndices[2],
AttesterSlot: committeeSlots[2],
PublicKey: pubkeys[5],
Status: statuses[5],
ValidatorIndex: validatorIndices[5],
ProposerSlots: expectedProposerSlots2,
IsSyncCommittee: testCase.fetchSyncDuties,
CommitteesAtSlot: 1,
},
{
PublicKey: pubkeys[6],
Expand Down
53 changes: 8 additions & 45 deletions validator/client/beacon-api/subscribe_committee_subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,63 +8,26 @@ import (

"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/api/server/structs"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/time/slots"
)

func (c beaconApiValidatorClient) subscribeCommitteeSubnets(ctx context.Context, in *ethpb.CommitteeSubnetsSubscribeRequest, validatorIndices []primitives.ValidatorIndex) error {
func (c beaconApiValidatorClient) subscribeCommitteeSubnets(ctx context.Context, in *ethpb.CommitteeSubnetsSubscribeRequest, duties []*ethpb.DutiesResponse_Duty) error {
if in == nil {
return errors.New("committee subnets subscribe request is nil")
}

if len(in.CommitteeIds) != len(in.Slots) || len(in.CommitteeIds) != len(in.IsAggregator) || len(in.CommitteeIds) != len(validatorIndices) {
return errors.New("arrays `in.CommitteeIds`, `in.Slots`, `in.IsAggregator` and `validatorIndices` don't have the same length")
if len(in.CommitteeIds) != len(in.Slots) || len(in.CommitteeIds) != len(in.IsAggregator) || len(in.CommitteeIds) != len(duties) {
return errors.New("arrays `in.CommitteeIds`, `in.Slots`, `in.IsAggregator` and `duties` don't have the same length")
}

slotToCommitteesAtSlotMap := make(map[primitives.Slot]uint64)
jsonCommitteeSubscriptions := make([]*structs.BeaconCommitteeSubscription, len(in.CommitteeIds))
for index := range in.CommitteeIds {
subscribeSlot := in.Slots[index]
subscribeCommitteeId := in.CommitteeIds[index]
subscribeIsAggregator := in.IsAggregator[index]
subscribeValidatorIndex := validatorIndices[index]

committeesAtSlot, foundSlot := slotToCommitteesAtSlotMap[subscribeSlot]
if !foundSlot {
// Lazily fetch the committeesAtSlot from the beacon node if they are not already in the map
epoch := slots.ToEpoch(subscribeSlot)
duties, err := c.dutiesProvider.GetAttesterDuties(ctx, epoch, validatorIndices)
if err != nil {
return errors.Wrapf(err, "failed to get duties for epoch `%d`", epoch)
}

for _, duty := range duties {
dutySlot, err := strconv.ParseUint(duty.Slot, 10, 64)
if err != nil {
return errors.Wrapf(err, "failed to parse slot `%s`", duty.Slot)
}

committees, err := strconv.ParseUint(duty.CommitteesAtSlot, 10, 64)
if err != nil {
return errors.Wrapf(err, "failed to parse CommitteesAtSlot `%s`", duty.CommitteesAtSlot)
}

slotToCommitteesAtSlotMap[primitives.Slot(dutySlot)] = committees
}

// If the slot still isn't in the map, we either received bad data from the beacon node or the caller of this function gave us bad data
if committeesAtSlot, foundSlot = slotToCommitteesAtSlotMap[subscribeSlot]; !foundSlot {
return errors.Errorf("failed to get committees for slot `%d`", subscribeSlot)
}
}

jsonCommitteeSubscriptions[index] = &structs.BeaconCommitteeSubscription{
CommitteeIndex: strconv.FormatUint(uint64(subscribeCommitteeId), 10),
CommitteesAtSlot: strconv.FormatUint(committeesAtSlot, 10),
Slot: strconv.FormatUint(uint64(subscribeSlot), 10),
IsAggregator: subscribeIsAggregator,
ValidatorIndex: strconv.FormatUint(uint64(subscribeValidatorIndex), 10),
CommitteeIndex: strconv.FormatUint(uint64(in.CommitteeIds[index]), 10),
CommitteesAtSlot: strconv.FormatUint(duties[index].CommitteesAtSlot, 10),
Slot: strconv.FormatUint(uint64(in.Slots[index]), 10),
IsAggregator: in.IsAggregator[index],
ValidatorIndex: strconv.FormatUint(uint64(duties[index].ValidatorIndex), 10),
}
}

Expand Down
Loading
Loading