2019-11-13 05:32:42 +00:00
|
|
|
package beacon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2019-11-13 05:32:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ListBeaconCommittees for a given epoch.
|
|
|
|
//
|
|
|
|
// If no filter criteria is specified, the response returns
|
|
|
|
// all beacon committees for the current epoch. The results are paginated by default.
|
|
|
|
func (bs *Server) ListBeaconCommittees(
|
|
|
|
ctx context.Context,
|
|
|
|
req *ethpb.ListCommitteesRequest,
|
|
|
|
) (*ethpb.BeaconCommittees, error) {
|
2019-11-19 16:12:50 +00:00
|
|
|
|
2019-11-13 05:32:42 +00:00
|
|
|
var requestingGenesis bool
|
|
|
|
var startSlot uint64
|
2019-12-20 16:02:12 +00:00
|
|
|
headSlot := bs.HeadFetcher.HeadSlot()
|
2019-11-13 05:32:42 +00:00
|
|
|
switch q := req.QueryFilter.(type) {
|
|
|
|
case *ethpb.ListCommitteesRequest_Epoch:
|
|
|
|
startSlot = helpers.StartSlot(q.Epoch)
|
|
|
|
case *ethpb.ListCommitteesRequest_Genesis:
|
|
|
|
requestingGenesis = q.Genesis
|
|
|
|
default:
|
2019-12-20 16:02:12 +00:00
|
|
|
startSlot = headSlot
|
2019-11-13 05:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var attesterSeed [32]byte
|
|
|
|
var activeIndices []uint64
|
2019-12-20 16:02:12 +00:00
|
|
|
var err error
|
2019-11-13 05:32:42 +00:00
|
|
|
// This is the archival condition, if the requested epoch is < current epoch or if we are
|
|
|
|
// requesting data from the genesis epoch.
|
2019-12-20 16:02:12 +00:00
|
|
|
if requestingGenesis || helpers.SlotToEpoch(startSlot) < helpers.SlotToEpoch(headSlot) {
|
|
|
|
activeIndices, err = bs.HeadFetcher.HeadValidatorsIndices(helpers.SlotToEpoch(startSlot))
|
2019-11-13 05:32:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.Internal,
|
2019-11-13 21:03:12 +00:00
|
|
|
"Could not retrieve active indices for epoch %d: %v",
|
2019-11-13 05:32:42 +00:00
|
|
|
helpers.SlotToEpoch(startSlot),
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
archivedCommitteeInfo, err := bs.BeaconDB.ArchivedCommitteeInfo(ctx, helpers.SlotToEpoch(startSlot))
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.Internal,
|
2019-11-13 21:03:12 +00:00
|
|
|
"Could not request archival data for epoch %d: %v",
|
2019-11-13 05:32:42 +00:00
|
|
|
helpers.SlotToEpoch(startSlot),
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if archivedCommitteeInfo == nil {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.NotFound,
|
2019-11-13 21:03:12 +00:00
|
|
|
"Could not retrieve data for epoch %d, perhaps --archive in the running beacon node is disabled",
|
2019-11-13 05:32:42 +00:00
|
|
|
helpers.SlotToEpoch(startSlot),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
attesterSeed = bytesutil.ToBytes32(archivedCommitteeInfo.AttesterSeed)
|
2019-12-20 16:02:12 +00:00
|
|
|
} else if !requestingGenesis && helpers.SlotToEpoch(startSlot) == helpers.SlotToEpoch(headSlot) {
|
2019-11-13 05:32:42 +00:00
|
|
|
// Otherwise, we use data from the current epoch.
|
2019-12-20 16:02:12 +00:00
|
|
|
currentEpoch := helpers.SlotToEpoch(headSlot)
|
|
|
|
activeIndices, err = bs.HeadFetcher.HeadValidatorsIndices(currentEpoch)
|
2019-11-13 05:32:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.Internal,
|
2019-11-13 21:03:12 +00:00
|
|
|
"Could not retrieve active indices for current epoch %d: %v",
|
2019-11-13 05:32:42 +00:00
|
|
|
currentEpoch,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
2019-12-20 16:02:12 +00:00
|
|
|
attesterSeed, err = bs.HeadFetcher.HeadSeed(currentEpoch)
|
2019-11-13 05:32:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.Internal,
|
2019-11-13 21:03:12 +00:00
|
|
|
"Could not retrieve attester seed for current epoch %d: %v",
|
2019-11-13 05:32:42 +00:00
|
|
|
currentEpoch,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, we are requesting data from the future and we return an error.
|
|
|
|
return nil, status.Errorf(
|
2019-11-13 21:03:12 +00:00
|
|
|
codes.InvalidArgument,
|
|
|
|
"Cannot retrieve information about an epoch in the future, current epoch %d, requesting %d",
|
2019-12-20 16:02:12 +00:00
|
|
|
helpers.SlotToEpoch(headSlot),
|
2019-11-18 23:24:33 +00:00
|
|
|
helpers.SlotToEpoch(startSlot),
|
2019-11-13 05:32:42 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-12-03 23:44:58 +00:00
|
|
|
committeesList := make(map[uint64]*ethpb.BeaconCommittees_CommitteesList)
|
2019-11-13 05:32:42 +00:00
|
|
|
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 {
|
|
|
|
countAtSlot = params.BeaconConfig().MaxCommitteesPerSlot
|
|
|
|
}
|
|
|
|
if countAtSlot == 0 {
|
|
|
|
countAtSlot = 1
|
|
|
|
}
|
2019-12-03 23:44:58 +00:00
|
|
|
committeeItems := make([]*ethpb.BeaconCommittees_CommitteeItem, countAtSlot)
|
2020-01-09 16:28:11 +00:00
|
|
|
for committeeIndex := uint64(0); committeeIndex < countAtSlot; committeeIndex++ {
|
|
|
|
committee, err := helpers.BeaconCommittee(activeIndices, attesterSeed, slot, committeeIndex)
|
2019-11-13 05:32:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.Internal,
|
2019-11-13 21:03:12 +00:00
|
|
|
"Could not compute committee for slot %d: %v",
|
2019-11-13 05:32:42 +00:00
|
|
|
slot,
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
2020-01-09 16:28:11 +00:00
|
|
|
committeeItems[committeeIndex] = ðpb.BeaconCommittees_CommitteeItem{
|
2019-12-03 23:44:58 +00:00
|
|
|
ValidatorIndices: committee,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
committeesList[slot] = ðpb.BeaconCommittees_CommitteesList{
|
|
|
|
Committees: committeeItems,
|
2019-11-13 05:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ðpb.BeaconCommittees{
|
|
|
|
Epoch: helpers.SlotToEpoch(startSlot),
|
2019-12-03 23:44:58 +00:00
|
|
|
Committees: committeesList,
|
2019-11-13 05:32:42 +00:00
|
|
|
ActiveValidatorCount: uint64(len(activeIndices)),
|
|
|
|
}, nil
|
|
|
|
}
|