github.com/prysmaticlabs/prysm@v1.4.4/validator/slashing-protection/external.go (about)

     1  package slashingprotection
     2  
     3  import (
     4  	"context"
     5  
     6  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     7  )
     8  
     9  // CheckBlockSafety this function is part of slashing protection for block proposals it performs
    10  // validation without db update. To be used before the block is signed.
    11  func (s *Service) CheckBlockSafety(ctx context.Context, blockHeader *ethpb.BeaconBlockHeader) bool {
    12  	slashable, err := s.slasherClient.IsSlashableBlockNoUpdate(ctx, blockHeader)
    13  	if err != nil {
    14  		log.Errorf("External slashing block protection returned an error: %v", err)
    15  		return false
    16  	}
    17  	if slashable != nil && slashable.Slashable {
    18  		log.Warn("External slashing proposal protection found the block to be slashable")
    19  	}
    20  	return !slashable.Slashable
    21  }
    22  
    23  // CommitBlock this function is part of slashing protection for block proposals it performs
    24  // validation and db update. To be used after the block is proposed.
    25  func (s *Service) CommitBlock(ctx context.Context, blockHeader *ethpb.SignedBeaconBlockHeader) (bool, error) {
    26  	ps, err := s.slasherClient.IsSlashableBlock(ctx, blockHeader)
    27  	if err != nil {
    28  		log.Errorf("External slashing block protection returned an error: %v", err)
    29  		return false, err
    30  	}
    31  	if ps != nil && ps.ProposerSlashing != nil {
    32  		log.Warn("External slashing proposal protection found the block to be slashable")
    33  		return false, nil
    34  	}
    35  	return true, nil
    36  }
    37  
    38  // CheckAttestationSafety implements the slashing protection for attestations without db update.
    39  // To be used before signing.
    40  func (s *Service) CheckAttestationSafety(ctx context.Context, attestation *ethpb.IndexedAttestation) bool {
    41  	slashable, err := s.slasherClient.IsSlashableAttestationNoUpdate(ctx, attestation)
    42  	if err != nil {
    43  		log.Errorf("External slashing attestation protection returned an error: %v", err)
    44  		return false
    45  	}
    46  	if slashable.Slashable {
    47  		log.Warn("External slashing attestation protection found the attestation to be slashable")
    48  	}
    49  	return !slashable.Slashable
    50  }
    51  
    52  // CommitAttestation implements the slashing protection for attestations it performs
    53  // validation and db update. To be used after the attestation is proposed.
    54  func (s *Service) CommitAttestation(ctx context.Context, attestation *ethpb.IndexedAttestation) bool {
    55  	as, err := s.slasherClient.IsSlashableAttestation(ctx, attestation)
    56  	if err != nil {
    57  		log.Errorf("External slashing attestation protection returned an error: %v", err)
    58  		return false
    59  	}
    60  	if as != nil && as.AttesterSlashing != nil {
    61  		log.Warnf("External slashing attestation protection found the attestation to be slashable: %v", as)
    62  		return false
    63  	}
    64  	return true
    65  }