github.com/prysmaticlabs/prysm@v1.4.4/slasher/detection/attestations/mock_spanner.go (about) 1 package attestations 2 3 import ( 4 "context" 5 6 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 7 "github.com/prysmaticlabs/prysm/slasher/detection/attestations/iface" 8 "github.com/prysmaticlabs/prysm/slasher/detection/attestations/types" 9 ) 10 11 var _ iface.SpanDetector = (*MockSpanDetector)(nil) 12 13 // MockSpanDetector defines a struct which can detect slashable 14 // attestation offenses by tracking validator min-max 15 // spans from validators. 16 type MockSpanDetector struct{} 17 18 // DetectSlashingsForAttestation mocks a detected slashing, if the sent attestation data 19 // has a source epoch of 0, nothing will be detected. If the sent attestation data has a target 20 // epoch equal to or greater than 6, it will "detect" a surrounded vote for the target epoch + 1. 21 // If the target epoch is greater than 12, it will "detect" a surrounding vote for target epoch - 1. 22 // Lastly, if it has a target epoch less than 6, it will "detect" a double vote for the target epoch. 23 func (m *MockSpanDetector) DetectSlashingsForAttestation( 24 _ context.Context, 25 att *ethpb.IndexedAttestation, 26 ) ([]*types.DetectionResult, error) { 27 var detections []*types.DetectionResult 28 switch { 29 // If the source epoch is 0, don't find a slashing. 30 case att.Data.Source.Epoch == 0: 31 return nil, nil 32 // If the target epoch is > 12, it will "detect" a surrounded saved attestation. 33 case att.Data.Target.Epoch > 12: 34 detections = append(detections, &types.DetectionResult{ 35 Kind: types.SurroundVote, 36 SlashableEpoch: att.Data.Target.Epoch - 1, 37 SigBytes: [2]byte{1, 2}, 38 }) 39 return detections, nil 40 // If the target epoch is >= 6 < 12, it will "detect" a surrounding saved attestation. 41 case att.Data.Target.Epoch >= 6: 42 detections = append(detections, &types.DetectionResult{ 43 Kind: types.SurroundVote, 44 SlashableEpoch: att.Data.Target.Epoch + 1, 45 SigBytes: [2]byte{1, 2}, 46 }) 47 return detections, nil 48 // If the target epoch is less than 6, it will "detect" a double vote. 49 default: 50 detections = append(detections, &types.DetectionResult{ 51 Kind: types.DoubleVote, 52 SlashableEpoch: att.Data.Target.Epoch, 53 SigBytes: [2]byte{1, 2}, 54 }) 55 } 56 return detections, nil 57 } 58 59 // SpanForEpochByValidator returns the specific min-max span for a 60 // validator index in a given epoch. 61 func (m *MockSpanDetector) SpanForEpochByValidator(_ context.Context, _, _ uint64) (types.Span, error) { 62 return types.Span{MinSpan: 0, MaxSpan: 0, SigBytes: [2]byte{}, HasAttested: false}, nil 63 } 64 65 // ValidatorSpansByEpoch returns a list of all validator spans in a given epoch. 66 func (m *MockSpanDetector) ValidatorSpansByEpoch(_ context.Context, _ uint64) map[uint64]types.Span { 67 return make(map[uint64]types.Span) 68 } 69 70 // DeleteValidatorSpansByEpoch mocks the delete spans by epoch function. 71 func (m *MockSpanDetector) DeleteValidatorSpansByEpoch(_ context.Context, _, _ uint64) error { 72 return nil 73 } 74 75 // UpdateSpans is a mock for updating the spans for a given attestation.. 76 func (m *MockSpanDetector) UpdateSpans(_ context.Context, _ *ethpb.IndexedAttestation) error { 77 return nil 78 }