prysm-pulse/slasher/detection/attestations/mock_spanner.go
Ivan Martinez cc5fc0af1a
Plug-in double voting detection into detection service (#4960)
* Add double vote detection to spanner
* Add documentation
* Update slasher/detection/attestations/spanner.go
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into slasher-spanner-double
* Merge branch 'slasher-spanner-double' of https://github.com/0xKiwi/Prysm into slasher-spanner-double
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into slasher-spanner-double
* Gazelle
* Add double vote detection func
* Implement double voting detection
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into slasher-implement-double
* Merge branch 'master' into slasher-implement-double
* Merge branch 'slasher-implement-double' of https://github.com/0xKiwi/Prysm into slasher-implement-double
* Fix typo
* Remove filter, replace with slot + committee index
* Change bloom filter to 2 sig bytes
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into slasher-change-filter
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into slasher-implement-double
* Merge branch 'slasher-change-filter' of https://github.com/0xKiwi/Prysm into slasher-implement-double
* Change detection to use prefix
* Fix runtime
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into slasher-implement-double
* Fix bug and comments
* Merge branch 'master' of https://github.com/prysmaticlabs/Prysm into slasher-implement-double
* Fix flaky test
* Merge branch 'master' into slasher-implement-double
* Improve logs
* Merge branch 'slasher-implement-double' of https://github.com/0xKiwi/Prysm into slasher-implement-double
* Add ok check
* Fix test
* Merge branch 'master' into slasher-implement-double
2020-03-03 18:08:21 +00:00

81 lines
3.0 KiB
Go

package attestations
import (
"context"
"sync"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/slasher/detection/attestations/iface"
"github.com/prysmaticlabs/prysm/slasher/detection/attestations/types"
)
var _ = iface.SpanDetector(&MockSpanDetector{})
// MockSpanDetector defines a struct which can detect slashable
// attestation offenses by tracking validator min-max
// spans from validators.
type MockSpanDetector struct {
// Slice of epochs for valindex => min-max span.
spans []map[uint64]types.Span
lock sync.RWMutex
}
// DetectSlashingForValidator mocks a detected slashing, if the sent attestation data
// has a source epoch of 0, nothing will be detected. If the sent attestation data has a target
// epoch equal to or greater than 6, it will "detect" a surrounded vote for the target epoch + 1.
// If the target epoch is greater than 12, it will "detect" a surrounding vote for target epoch - 1.
// Lastly, if it has a target epoch less than 6, it will "detect" a double vote for the target epoch.
func (s *MockSpanDetector) DetectSlashingForValidator(
ctx context.Context,
validatorIdx uint64,
attData *ethpb.AttestationData,
) (*types.DetectionResult, error) {
switch {
// If the source epoch is 0, don't find a slashing.
case attData.Source.Epoch == 0:
return nil, nil
// If the target epoch is > 12, it will "detect" a surrounded saved attestation.
case attData.Target.Epoch > 12:
return &types.DetectionResult{
Kind: types.SurroundVote,
SlashableEpoch: attData.Target.Epoch - 1,
SigBytes: [2]byte{1, 2},
}, nil
// If the target epoch is >= 6 < 12, it will "detect" a surrounding saved attestation.
case attData.Target.Epoch >= 6:
return &types.DetectionResult{
Kind: types.SurroundVote,
SlashableEpoch: attData.Target.Epoch + 1,
SigBytes: [2]byte{1, 2},
}, nil
// If the target epoch is less than 6, it will "detect" a double vote.
default:
return &types.DetectionResult{
Kind: types.DoubleVote,
SlashableEpoch: attData.Target.Epoch,
SigBytes: [2]byte{1, 2},
}, nil
}
}
// SpanForEpochByValidator returns the specific min-max span for a
// validator index in a given epoch.
func (s *MockSpanDetector) SpanForEpochByValidator(ctx context.Context, valIdx uint64, epoch uint64) (types.Span, error) {
return types.Span{MinSpan: 0, MaxSpan: 0, SigBytes: [2]byte{}, HasAttested: false}, nil
}
// ValidatorSpansByEpoch returns a list of all validator spans in a given epoch.
func (s *MockSpanDetector) ValidatorSpansByEpoch(ctx context.Context, epoch uint64) map[uint64]types.Span {
return make(map[uint64]types.Span, 0)
}
// DeleteValidatorSpansByEpoch mocks the delete spans by epoch function.
func (s *MockSpanDetector) DeleteValidatorSpansByEpoch(ctx context.Context, validatorIdx uint64, epoch uint64) error {
return nil
}
// UpdateSpans is a mock for updating the spans for a given attestation..
func (s *MockSpanDetector) UpdateSpans(ctx context.Context, att *ethpb.IndexedAttestation) error {
return nil
}