2020-02-27 20:23:35 +00:00
|
|
|
package validator
|
|
|
|
|
|
|
|
import (
|
2020-09-07 19:53:30 +00:00
|
|
|
"bytes"
|
2020-02-27 20:23:35 +00:00
|
|
|
"context"
|
|
|
|
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2020-04-11 19:47:22 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-05-11 16:21:34 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-02-27 20:23:35 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"go.opencensus.io/trace"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
// SubmitAggregateSelectionProof is called by a validator when its assigned to be an aggregator.
|
|
|
|
// The aggregator submits the selection proof to obtain the aggregated attestation
|
|
|
|
// object to sign over.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (vs *Server) SubmitAggregateSelectionProof(ctx context.Context, req *ethpb.AggregateSelectionRequest) (*ethpb.AggregateSelectionResponse, error) {
|
2020-04-14 20:27:03 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "AggregatorServer.SubmitAggregateSelectionProof")
|
2020-02-27 20:23:35 +00:00
|
|
|
defer span.End()
|
|
|
|
span.AddAttributes(trace.Int64Attribute("slot", int64(req.Slot)))
|
|
|
|
|
2020-10-12 08:11:05 +00:00
|
|
|
if vs.SyncChecker.Syncing() {
|
2020-02-27 20:23:35 +00:00
|
|
|
return nil, status.Errorf(codes.Unavailable, "Syncing to latest head, not ready to respond")
|
|
|
|
}
|
|
|
|
|
2020-10-12 08:11:05 +00:00
|
|
|
st, err := vs.HeadFetcher.HeadState(ctx)
|
2020-02-27 20:23:35 +00:00
|
|
|
if err != nil {
|
2020-04-11 19:47:22 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "Could not determine head state: %v", err)
|
2020-02-27 20:23:35 +00:00
|
|
|
}
|
2020-04-11 19:47:22 +00:00
|
|
|
|
|
|
|
validatorIndex, exists := st.ValidatorIndexByPubkey(bytesutil.ToBytes48(req.PublicKey))
|
2020-02-27 20:23:35 +00:00
|
|
|
if !exists {
|
|
|
|
return nil, status.Error(codes.Internal, "Could not locate validator index in DB")
|
|
|
|
}
|
|
|
|
|
|
|
|
epoch := helpers.SlotToEpoch(req.Slot)
|
2020-05-11 16:21:34 +00:00
|
|
|
activeValidatorIndices, err := helpers.ActiveValidatorIndices(st, epoch)
|
2020-02-27 20:23:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not get validators: %v", err)
|
|
|
|
}
|
2020-05-11 16:21:34 +00:00
|
|
|
seed, err := helpers.Seed(st, epoch, params.BeaconConfig().DomainBeaconAttester)
|
2020-02-27 20:23:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not get seed: %v", err)
|
|
|
|
}
|
|
|
|
committee, err := helpers.BeaconCommittee(activeValidatorIndices, seed, req.Slot, req.CommitteeIndex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the validator is an aggregator
|
|
|
|
isAggregator, err := helpers.IsAggregator(uint64(len(committee)), req.SlotSignature)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not get aggregator status: %v", err)
|
|
|
|
}
|
|
|
|
if !isAggregator {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "Validator is not an aggregator")
|
|
|
|
}
|
|
|
|
|
2020-10-12 08:11:05 +00:00
|
|
|
if err := vs.AttPool.AggregateUnaggregatedAttestationsBySlotIndex(req.Slot, req.CommitteeIndex); err != nil {
|
2020-04-14 20:27:03 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "Could not aggregate unaggregated attestations")
|
|
|
|
}
|
2020-10-12 08:11:05 +00:00
|
|
|
aggregatedAtts := vs.AttPool.AggregatedAttestationsBySlotIndex(req.Slot, req.CommitteeIndex)
|
2020-02-27 20:23:35 +00:00
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
// Filter out the best aggregated attestation (ie. the one with the most aggregated bits).
|
|
|
|
if len(aggregatedAtts) == 0 {
|
2020-10-12 08:11:05 +00:00
|
|
|
aggregatedAtts = vs.AttPool.UnaggregatedAttestationsBySlotIndex(req.Slot, req.CommitteeIndex)
|
2020-06-05 00:06:45 +00:00
|
|
|
if len(aggregatedAtts) == 0 {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not find attestation for slot and committee in pool")
|
|
|
|
}
|
2020-04-14 20:27:03 +00:00
|
|
|
}
|
2020-07-13 22:02:01 +00:00
|
|
|
|
|
|
|
var indexInCommittee uint64
|
|
|
|
for i, idx := range committee {
|
|
|
|
if idx == validatorIndex {
|
|
|
|
indexInCommittee = uint64(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
best := aggregatedAtts[0]
|
|
|
|
for _, aggregatedAtt := range aggregatedAtts[1:] {
|
2020-07-13 22:02:01 +00:00
|
|
|
// The aggregator should prefer an attestation that they have signed. We check this by
|
|
|
|
// looking at the attestation's committee index against the validator's committee index
|
|
|
|
// and check the aggregate bits to ensure the validator's index is set.
|
|
|
|
if aggregatedAtt.Data.CommitteeIndex == req.CommitteeIndex &&
|
|
|
|
aggregatedAtt.AggregationBits.BitAt(indexInCommittee) &&
|
|
|
|
(!best.AggregationBits.BitAt(indexInCommittee) ||
|
|
|
|
aggregatedAtt.AggregationBits.Count() > best.AggregationBits.Count()) {
|
2020-04-14 20:27:03 +00:00
|
|
|
best = aggregatedAtt
|
2020-02-27 20:23:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 22:02:01 +00:00
|
|
|
// If the "best" still doesn't contain the validator's index, check the aggregation bits to
|
|
|
|
// choose the attestation with the most bits set.
|
|
|
|
if !best.AggregationBits.BitAt(indexInCommittee) &&
|
|
|
|
aggregatedAtt.AggregationBits.Count() > best.AggregationBits.Count() {
|
|
|
|
best = aggregatedAtt
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 20:27:03 +00:00
|
|
|
a := ðpb.AggregateAttestationAndProof{
|
|
|
|
Aggregate: best,
|
|
|
|
SelectionProof: req.SlotSignature,
|
|
|
|
AggregatorIndex: validatorIndex,
|
|
|
|
}
|
|
|
|
return ðpb.AggregateSelectionResponse{AggregateAndProof: a}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubmitSignedAggregateSelectionProof is called by a validator to broadcast a signed
|
|
|
|
// aggregated and proof object.
|
2020-10-12 08:11:05 +00:00
|
|
|
func (vs *Server) SubmitSignedAggregateSelectionProof(ctx context.Context, req *ethpb.SignedAggregateSubmitRequest) (*ethpb.SignedAggregateSubmitResponse, error) {
|
2020-04-15 03:38:24 +00:00
|
|
|
if req.SignedAggregateAndProof == nil || req.SignedAggregateAndProof.Message == nil ||
|
|
|
|
req.SignedAggregateAndProof.Message.Aggregate == nil || req.SignedAggregateAndProof.Message.Aggregate.Data == nil {
|
2020-04-14 20:27:03 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "Signed aggregate request can't be nil")
|
2020-02-27 20:23:35 +00:00
|
|
|
}
|
2020-09-07 19:53:30 +00:00
|
|
|
emptySig := make([]byte, params.BeaconConfig().BLSSignatureLength)
|
|
|
|
if bytes.Equal(req.SignedAggregateAndProof.Signature, emptySig) ||
|
|
|
|
bytes.Equal(req.SignedAggregateAndProof.Message.SelectionProof, emptySig) {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "Signed signatures can't be zero hashes")
|
|
|
|
}
|
2020-02-27 20:23:35 +00:00
|
|
|
|
2020-09-10 20:16:35 +00:00
|
|
|
// As a preventive measure, a beacon node shouldn't broadcast an attestation whose slot is out of range.
|
2020-10-12 08:11:05 +00:00
|
|
|
if err := helpers.ValidateAttestationTime(req.SignedAggregateAndProof.Message.Aggregate.Data.Slot, vs.GenesisTimeFetcher.GenesisTime()); err != nil {
|
2020-09-10 20:16:35 +00:00
|
|
|
return nil, status.Error(codes.InvalidArgument, "Attestation slot is no longer valid from current time")
|
|
|
|
}
|
|
|
|
|
2020-10-12 08:11:05 +00:00
|
|
|
if err := vs.P2P.Broadcast(ctx, req.SignedAggregateAndProof); err != nil {
|
2020-04-14 20:27:03 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "Could not broadcast signed aggregated attestation: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"slot": req.SignedAggregateAndProof.Message.Aggregate.Data.Slot,
|
|
|
|
"committeeIndex": req.SignedAggregateAndProof.Message.Aggregate.Data.CommitteeIndex,
|
|
|
|
"validatorIndex": req.SignedAggregateAndProof.Message.AggregatorIndex,
|
|
|
|
"aggregatedCount": req.SignedAggregateAndProof.Message.Aggregate.AggregationBits.Count(),
|
|
|
|
}).Debug("Broadcasting aggregated attestation and proof")
|
|
|
|
|
|
|
|
return ðpb.SignedAggregateSubmitResponse{}, nil
|
2020-02-27 20:23:35 +00:00
|
|
|
}
|