2020-02-27 17:22:39 +00:00
|
|
|
package detection
|
|
|
|
|
|
|
|
import (
|
2020-04-14 20:27:03 +00:00
|
|
|
"bytes"
|
2020-02-27 17:22:39 +00:00
|
|
|
"context"
|
|
|
|
|
2020-03-03 18:08:21 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2020-02-27 17:22:39 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2020-03-13 18:04:22 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2020-02-27 17:22:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/sliceutil"
|
2020-03-12 16:38:58 +00:00
|
|
|
status "github.com/prysmaticlabs/prysm/slasher/db/types"
|
2020-02-27 17:22:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/slasher/detection/attestations/types"
|
2020-03-08 17:56:43 +00:00
|
|
|
"go.opencensus.io/trace"
|
2020-02-27 17:22:39 +00:00
|
|
|
)
|
|
|
|
|
2020-03-24 18:30:21 +00:00
|
|
|
// DetectAttesterSlashings detects double, surround and surrounding attestation offences given an attestation.
|
|
|
|
func (ds *Service) DetectAttesterSlashings(
|
2020-02-27 17:22:39 +00:00
|
|
|
ctx context.Context,
|
|
|
|
att *ethpb.IndexedAttestation,
|
|
|
|
) ([]*ethpb.AttesterSlashing, error) {
|
2020-03-24 18:30:21 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "detection.DetectAttesterSlashings")
|
2020-03-08 17:56:43 +00:00
|
|
|
defer span.End()
|
2020-03-09 18:14:19 +00:00
|
|
|
results, err := ds.minMaxSpanDetector.DetectSlashingsForAttestation(ctx, att)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If the response is nil, there was no slashing detected.
|
|
|
|
if len(results) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-03-03 18:08:21 +00:00
|
|
|
|
2020-03-09 21:21:39 +00:00
|
|
|
var slashings []*ethpb.AttesterSlashing
|
2020-03-09 18:14:19 +00:00
|
|
|
for _, result := range results {
|
2020-03-03 18:08:21 +00:00
|
|
|
var slashing *ethpb.AttesterSlashing
|
|
|
|
switch result.Kind {
|
|
|
|
case types.DoubleVote:
|
|
|
|
slashing, err = ds.detectDoubleVote(ctx, att, result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not detect double votes on attestation")
|
|
|
|
}
|
|
|
|
case types.SurroundVote:
|
|
|
|
slashing, err = ds.detectSurroundVotes(ctx, att, result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not detect surround votes on attestation")
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 21:21:39 +00:00
|
|
|
if slashing != nil {
|
|
|
|
slashings = append(slashings, slashing)
|
|
|
|
}
|
2020-03-03 18:08:21 +00:00
|
|
|
}
|
2020-03-13 18:04:22 +00:00
|
|
|
|
|
|
|
// Clear out any duplicate results.
|
|
|
|
keys := make(map[[32]byte]bool)
|
|
|
|
var slashingList []*ethpb.AttesterSlashing
|
|
|
|
for _, ss := range slashings {
|
|
|
|
hash, err := hashutil.HashProto(ss)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not hash slashing")
|
|
|
|
}
|
|
|
|
if _, value := keys[hash]; !value {
|
|
|
|
keys[hash] = true
|
|
|
|
slashingList = append(slashingList, ss)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 16:38:58 +00:00
|
|
|
if err = ds.slasherDB.SaveAttesterSlashings(ctx, status.Active, slashings); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-13 18:04:22 +00:00
|
|
|
return slashingList, nil
|
2020-02-27 17:22:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 18:31:20 +00:00
|
|
|
// UpdateSpans passthrough function that updates span maps given an indexed attestation.
|
|
|
|
func (ds *Service) UpdateSpans(ctx context.Context, att *ethpb.IndexedAttestation) error {
|
|
|
|
return ds.minMaxSpanDetector.UpdateSpans(ctx, att)
|
|
|
|
}
|
|
|
|
|
2020-03-03 18:08:21 +00:00
|
|
|
// detectDoubleVote cross references the passed in attestation with the bloom filter maintained
|
|
|
|
// for every epoch for the validator in order to determine if it is a double vote.
|
|
|
|
func (ds *Service) detectDoubleVote(
|
2020-02-27 17:22:39 +00:00
|
|
|
ctx context.Context,
|
2020-03-03 18:08:21 +00:00
|
|
|
incomingAtt *ethpb.IndexedAttestation,
|
|
|
|
detectionResult *types.DetectionResult,
|
|
|
|
) (*ethpb.AttesterSlashing, error) {
|
2020-03-08 17:56:43 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "detection.detectDoubleVote")
|
|
|
|
defer span.End()
|
2020-03-03 18:08:21 +00:00
|
|
|
if detectionResult == nil || detectionResult.Kind != types.DoubleVote {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
otherAtts, err := ds.slasherDB.IndexedAttestationsWithPrefix(ctx, detectionResult.SlashableEpoch, detectionResult.SigBytes[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, att := range otherAtts {
|
|
|
|
if att.Data == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-13 18:04:22 +00:00
|
|
|
|
2020-03-03 18:08:21 +00:00
|
|
|
// If there are no shared indices, there is no validator to slash.
|
2020-03-13 18:04:22 +00:00
|
|
|
if len(sliceutil.IntersectionUint64(att.AttestingIndices, []uint64{detectionResult.ValidatorIndex})) == 0 {
|
2020-03-03 18:08:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if isDoubleVote(incomingAtt, att) {
|
2020-03-10 19:41:55 +00:00
|
|
|
doubleVotesDetected.Inc()
|
2020-03-03 18:08:21 +00:00
|
|
|
return ðpb.AttesterSlashing{
|
|
|
|
Attestation_1: incomingAtt,
|
|
|
|
Attestation_2: att,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-03-13 18:04:22 +00:00
|
|
|
|
2020-03-03 18:08:21 +00:00
|
|
|
}
|
2020-03-08 17:56:43 +00:00
|
|
|
return nil, nil
|
2020-02-27 17:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// detectSurroundVotes cross references the passed in attestation with the requested validator's
|
|
|
|
// voting history in order to detect any possible surround votes.
|
|
|
|
func (ds *Service) detectSurroundVotes(
|
|
|
|
ctx context.Context,
|
|
|
|
incomingAtt *ethpb.IndexedAttestation,
|
2020-03-03 18:08:21 +00:00
|
|
|
detectionResult *types.DetectionResult,
|
|
|
|
) (*ethpb.AttesterSlashing, error) {
|
2020-03-08 17:56:43 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "detection.detectSurroundVotes")
|
|
|
|
defer span.End()
|
2020-03-03 18:08:21 +00:00
|
|
|
if detectionResult == nil || detectionResult.Kind != types.SurroundVote {
|
2020-02-27 17:22:39 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-03-03 18:08:21 +00:00
|
|
|
otherAtts, err := ds.slasherDB.IndexedAttestationsWithPrefix(ctx, detectionResult.SlashableEpoch, detectionResult.SigBytes[:])
|
2020-02-27 17:22:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, att := range otherAtts {
|
|
|
|
if att.Data == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// If there are no shared indices, there is no validator to slash.
|
2020-03-13 18:04:22 +00:00
|
|
|
if len(sliceutil.IntersectionUint64(att.AttestingIndices, []uint64{detectionResult.ValidatorIndex})) == 0 {
|
2020-02-27 17:22:39 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-03 18:08:21 +00:00
|
|
|
// Slashings must be submitted as the incoming attestation surrounding the saved attestation.
|
|
|
|
// So we swap the order if needed.
|
|
|
|
if isSurrounding(incomingAtt, att) {
|
2020-03-10 19:41:55 +00:00
|
|
|
surroundingVotesDetected.Inc()
|
2020-03-03 18:08:21 +00:00
|
|
|
return ðpb.AttesterSlashing{
|
|
|
|
Attestation_1: incomingAtt,
|
|
|
|
Attestation_2: att,
|
|
|
|
}, nil
|
2020-03-13 18:04:22 +00:00
|
|
|
} else if isSurrounding(att, incomingAtt) {
|
2020-03-10 19:41:55 +00:00
|
|
|
surroundedVotesDetected.Inc()
|
2020-03-03 18:08:21 +00:00
|
|
|
return ðpb.AttesterSlashing{
|
2020-02-27 17:22:39 +00:00
|
|
|
Attestation_1: att,
|
|
|
|
Attestation_2: incomingAtt,
|
2020-03-03 18:08:21 +00:00
|
|
|
}, nil
|
2020-02-27 17:22:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 18:08:21 +00:00
|
|
|
return nil, errors.New("unexpected false positive in surround vote detection")
|
|
|
|
}
|
|
|
|
|
2020-03-24 18:30:21 +00:00
|
|
|
// DetectDoubleProposals checks if the given signed beacon block is a slashable offense and returns the slashing.
|
|
|
|
func (ds *Service) DetectDoubleProposals(ctx context.Context, incomingBlock *ethpb.SignedBeaconBlockHeader) (*ethpb.ProposerSlashing, error) {
|
|
|
|
return ds.proposalsDetector.DetectDoublePropose(ctx, incomingBlock)
|
2020-03-19 11:59:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 20:27:03 +00:00
|
|
|
func isDoublePropose(
|
|
|
|
incomingBlockHeader *ethpb.SignedBeaconBlockHeader,
|
|
|
|
prevBlockHeader *ethpb.SignedBeaconBlockHeader,
|
|
|
|
) bool {
|
|
|
|
return incomingBlockHeader.Header.ProposerIndex == prevBlockHeader.Header.ProposerIndex &&
|
|
|
|
!bytes.Equal(incomingBlockHeader.Signature, prevBlockHeader.Signature) &&
|
|
|
|
incomingBlockHeader.Header.Slot == prevBlockHeader.Header.Slot
|
|
|
|
}
|
|
|
|
|
2020-03-03 18:08:21 +00:00
|
|
|
func isDoubleVote(incomingAtt *ethpb.IndexedAttestation, prevAtt *ethpb.IndexedAttestation) bool {
|
|
|
|
return !proto.Equal(incomingAtt.Data, prevAtt.Data) && incomingAtt.Data.Target.Epoch == prevAtt.Data.Target.Epoch
|
2020-02-27 17:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isSurrounding(incomingAtt *ethpb.IndexedAttestation, prevAtt *ethpb.IndexedAttestation) bool {
|
|
|
|
return incomingAtt.Data.Source.Epoch < prevAtt.Data.Source.Epoch &&
|
|
|
|
incomingAtt.Data.Target.Epoch > prevAtt.Data.Target.Epoch
|
|
|
|
}
|