2019-08-23 19:46:04 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-10-01 20:05:17 +00:00
|
|
|
"fmt"
|
2019-08-23 19:46:04 +00:00
|
|
|
|
2019-12-20 03:18:08 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2019-10-01 15:13:04 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2019-08-23 19:46:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2019-10-31 22:32:22 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/traceutil"
|
2019-11-07 19:17:00 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-10-31 22:32:22 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-08-23 19:46:04 +00:00
|
|
|
)
|
|
|
|
|
2019-10-31 22:32:22 +00:00
|
|
|
var errPointsToBlockNotInDatabase = errors.New("attestation points to a block which is not in the database")
|
|
|
|
|
2019-08-23 19:46:04 +00:00
|
|
|
// validateBeaconAttestation validates that the block being voted for passes validation before forwarding to the
|
|
|
|
// network.
|
2019-12-20 03:18:08 +00:00
|
|
|
func (r *Service) validateBeaconAttestation(ctx context.Context, pid peer.ID, msg *pubsub.Message) bool {
|
|
|
|
// Validation runs on publish (not just subscriptions), so we should approve any message from
|
|
|
|
// ourselves.
|
|
|
|
if pid == r.p2p.PeerID() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-09-20 17:54:32 +00:00
|
|
|
// Attestation processing requires the target block to be present in the database, so we'll skip
|
|
|
|
// validating or processing attestations until fully synced.
|
|
|
|
if r.initialSync.Syncing() {
|
2019-12-20 03:18:08 +00:00
|
|
|
return false
|
2019-09-20 17:54:32 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 22:32:22 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "sync.validateBeaconAttestation")
|
|
|
|
defer span.End()
|
|
|
|
|
2019-09-20 17:54:32 +00:00
|
|
|
// TODO(1332): Add blocks.VerifyAttestation before processing further.
|
|
|
|
// Discussion: https://github.com/ethereum/eth2.0-specs/issues/1332
|
|
|
|
|
2019-12-20 03:18:08 +00:00
|
|
|
m, err := r.decodePubsubMessage(msg)
|
2019-08-23 19:46:04 +00:00
|
|
|
if err != nil {
|
2019-12-20 03:18:08 +00:00
|
|
|
log.WithError(err).Error("Failed to decode message")
|
|
|
|
traceutil.AnnotateError(span, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
att, ok := m.(*ethpb.Attestation)
|
|
|
|
if !ok {
|
|
|
|
traceutil.AnnotateError(span, errors.New("wrong proto message type"))
|
|
|
|
log.Error("Wrong proto message type")
|
|
|
|
return false
|
2019-08-23 19:46:04 +00:00
|
|
|
}
|
|
|
|
|
2019-10-31 22:32:22 +00:00
|
|
|
span.AddAttributes(
|
|
|
|
trace.StringAttribute("blockRoot", fmt.Sprintf("%#x", att.Data.BeaconBlockRoot)),
|
|
|
|
)
|
|
|
|
|
2019-08-23 19:46:04 +00:00
|
|
|
// Only valid blocks are saved in the database.
|
|
|
|
if !r.db.HasBlock(ctx, bytesutil.ToBytes32(att.Data.BeaconBlockRoot)) {
|
2019-10-01 20:05:17 +00:00
|
|
|
log.WithField(
|
|
|
|
"blockRoot",
|
|
|
|
fmt.Sprintf("%#x", att.Data.BeaconBlockRoot),
|
2019-10-31 22:32:22 +00:00
|
|
|
).WithError(errPointsToBlockNotInDatabase).Debug("Ignored incoming attestation that points to a block which is not in the database")
|
|
|
|
traceutil.AnnotateError(span, errPointsToBlockNotInDatabase)
|
2019-12-20 03:18:08 +00:00
|
|
|
return false
|
2019-09-04 00:22:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 19:17:00 +00:00
|
|
|
finalizedEpoch := r.chain.FinalizedCheckpt().Epoch
|
2019-11-08 16:09:07 +00:00
|
|
|
attestationDataEpochOld := finalizedEpoch >= att.Data.Source.Epoch || finalizedEpoch >= att.Data.Target.Epoch
|
|
|
|
if finalizedEpoch != 0 && attestationDataEpochOld {
|
2019-12-20 03:18:08 +00:00
|
|
|
traceutil.AnnotateError(span, errors.New("wrong proto message type"))
|
2019-11-07 19:17:00 +00:00
|
|
|
log.WithFields(logrus.Fields{
|
2019-12-20 03:18:08 +00:00
|
|
|
"TargetEpoch": att.Data.Target.Epoch,
|
|
|
|
"SourceEpoch": att.Data.Source.Epoch,
|
2019-11-07 19:17:00 +00:00
|
|
|
}).Debug("Rejecting old attestation")
|
2019-12-20 03:18:08 +00:00
|
|
|
return false
|
2019-11-07 19:17:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 03:18:08 +00:00
|
|
|
msg.ValidatorData = att // Used in downstream subscriber
|
|
|
|
|
|
|
|
return true
|
2019-08-23 19:46:04 +00:00
|
|
|
}
|