2019-08-23 19:46:04 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// validateBeaconAttestation validates that the block being voted for passes validation before forwarding to the
|
|
|
|
// network.
|
2019-09-04 00:22:15 +00:00
|
|
|
func (r *RegularSync) validateBeaconAttestation(ctx context.Context, msg proto.Message, p p2p.Broadcaster, fromSelf bool) bool {
|
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() {
|
|
|
|
log.Debug("Not propagating or processing attestation during syncing")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(1332): Add blocks.VerifyAttestation before processing further.
|
|
|
|
// Discussion: https://github.com/ethereum/eth2.0-specs/issues/1332
|
|
|
|
|
2019-08-23 19:46:04 +00:00
|
|
|
att := msg.(*ethpb.Attestation)
|
|
|
|
|
|
|
|
attRoot, err := ssz.HashTreeRoot(att)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to hash attestation")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only valid blocks are saved in the database.
|
|
|
|
if !r.db.HasBlock(ctx, bytesutil.ToBytes32(att.Data.BeaconBlockRoot)) {
|
|
|
|
log.Warn("Ignored incoming attestation that points to a block that isn't in the database.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if recentlySeenRoots.Get(string(attRoot[:])) != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
recentlySeenRoots.Set(string(attRoot[:]), true /*value*/, 365*24*time.Hour /*TTL*/)
|
|
|
|
|
2019-09-04 00:22:15 +00:00
|
|
|
if fromSelf {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-08-23 19:46:04 +00:00
|
|
|
if err := p.Broadcast(ctx, msg); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to broadcast message")
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|