2020-05-20 15:23:22 +00:00
|
|
|
package slashingprotection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2020-05-20 15:23:22 +00:00
|
|
|
)
|
|
|
|
|
2020-07-03 19:54:42 +00:00
|
|
|
// CheckBlockSafety this function is part of slashing protection for block proposals it performs
|
|
|
|
// validation without db update. To be used before the block is signed.
|
|
|
|
func (s *Service) CheckBlockSafety(ctx context.Context, blockHeader *ethpb.BeaconBlockHeader) bool {
|
|
|
|
slashable, err := s.slasherClient.IsSlashableBlockNoUpdate(ctx, blockHeader)
|
2020-05-20 15:23:22 +00:00
|
|
|
if err != nil {
|
2020-07-27 20:06:49 +00:00
|
|
|
log.Errorf("External slashing block protection returned an error: %v", err)
|
|
|
|
return false
|
2020-05-20 15:23:22 +00:00
|
|
|
}
|
2020-07-03 19:54:42 +00:00
|
|
|
if slashable != nil && slashable.Slashable {
|
2020-06-23 16:46:48 +00:00
|
|
|
log.Warn("External slashing proposal protection found the block to be slashable")
|
2020-05-20 15:23:22 +00:00
|
|
|
}
|
2020-07-03 19:54:42 +00:00
|
|
|
return !slashable.Slashable
|
2020-05-20 15:23:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:54:42 +00:00
|
|
|
// CommitBlock this function is part of slashing protection for block proposals it performs
|
|
|
|
// validation and db update. To be used after the block is proposed.
|
2020-09-11 14:53:53 +00:00
|
|
|
func (s *Service) CommitBlock(ctx context.Context, blockHeader *ethpb.SignedBeaconBlockHeader) (bool, error) {
|
2020-07-03 19:54:42 +00:00
|
|
|
ps, err := s.slasherClient.IsSlashableBlock(ctx, blockHeader)
|
2020-06-23 16:46:48 +00:00
|
|
|
if err != nil {
|
2020-07-27 20:06:49 +00:00
|
|
|
log.Errorf("External slashing block protection returned an error: %v", err)
|
2020-09-11 14:53:53 +00:00
|
|
|
return false, err
|
2020-06-23 16:46:48 +00:00
|
|
|
}
|
2021-07-23 18:01:01 +00:00
|
|
|
if ps != nil && len(ps.ProposerSlashings) != 0 {
|
2020-06-23 16:46:48 +00:00
|
|
|
log.Warn("External slashing proposal protection found the block to be slashable")
|
2020-09-11 14:53:53 +00:00
|
|
|
return false, nil
|
2020-06-23 16:46:48 +00:00
|
|
|
}
|
2020-09-11 14:53:53 +00:00
|
|
|
return true, nil
|
2020-06-23 16:46:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 19:54:42 +00:00
|
|
|
// CheckAttestationSafety implements the slashing protection for attestations without db update.
|
|
|
|
// To be used before signing.
|
|
|
|
func (s *Service) CheckAttestationSafety(ctx context.Context, attestation *ethpb.IndexedAttestation) bool {
|
2020-06-23 16:46:48 +00:00
|
|
|
slashable, err := s.slasherClient.IsSlashableAttestationNoUpdate(ctx, attestation)
|
|
|
|
if err != nil {
|
2020-07-27 20:06:49 +00:00
|
|
|
log.Errorf("External slashing attestation protection returned an error: %v", err)
|
|
|
|
return false
|
2020-06-23 16:46:48 +00:00
|
|
|
}
|
|
|
|
if slashable.Slashable {
|
|
|
|
log.Warn("External slashing attestation protection found the attestation to be slashable")
|
|
|
|
}
|
|
|
|
return !slashable.Slashable
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommitAttestation implements the slashing protection for attestations it performs
|
2020-07-03 19:54:42 +00:00
|
|
|
// validation and db update. To be used after the attestation is proposed.
|
2020-06-23 16:46:48 +00:00
|
|
|
func (s *Service) CommitAttestation(ctx context.Context, attestation *ethpb.IndexedAttestation) bool {
|
2020-05-20 15:23:22 +00:00
|
|
|
as, err := s.slasherClient.IsSlashableAttestation(ctx, attestation)
|
|
|
|
if err != nil {
|
2020-07-27 20:06:49 +00:00
|
|
|
log.Errorf("External slashing attestation protection returned an error: %v", err)
|
|
|
|
return false
|
2020-05-20 15:23:22 +00:00
|
|
|
}
|
2021-07-23 18:01:01 +00:00
|
|
|
if as != nil && len(as.AttesterSlashings) != 0 {
|
2020-05-20 15:23:22 +00:00
|
|
|
log.Warnf("External slashing attestation protection found the attestation to be slashable: %v", as)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|