github.com/prysmaticlabs/prysm@v1.4.4/slasher/beaconclient/submit.go (about)

     1  package beaconclient
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
     8  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     9  	"github.com/prysmaticlabs/prysm/shared/sliceutil"
    10  	"github.com/sirupsen/logrus"
    11  	"go.opencensus.io/trace"
    12  )
    13  
    14  // subscribeDetectedProposerSlashings subscribes to an event feed for
    15  // slashing objects from the slasher runtime. Upon receiving
    16  // a proposer slashing from the feed, we submit the object to the
    17  // connected beacon node via a client RPC.
    18  func (s *Service) subscribeDetectedProposerSlashings(ctx context.Context, ch chan *ethpb.ProposerSlashing) {
    19  	ctx, span := trace.StartSpan(ctx, "beaconclient.submitProposerSlashing")
    20  	defer span.End()
    21  	sub := s.cfg.ProposerSlashingsFeed.Subscribe(ch)
    22  	defer sub.Unsubscribe()
    23  	for {
    24  		select {
    25  		case slashing := <-ch:
    26  			if _, err := s.cfg.BeaconClient.SubmitProposerSlashing(ctx, slashing); err != nil {
    27  				log.Error(err)
    28  			}
    29  		case <-sub.Err():
    30  			log.Error("Subscriber closed, exiting goroutine")
    31  			return
    32  		case <-ctx.Done():
    33  			log.Error("Context canceled")
    34  			return
    35  		}
    36  	}
    37  }
    38  
    39  // subscribeDetectedAttesterSlashings subscribes to an event feed for
    40  // slashing objects from the slasher runtime. Upon receiving an
    41  // attester slashing from the feed, we submit the object to the
    42  // connected beacon node via a client RPC.
    43  func (s *Service) subscribeDetectedAttesterSlashings(ctx context.Context, ch chan *ethpb.AttesterSlashing) {
    44  	ctx, span := trace.StartSpan(ctx, "beaconclient.submitAttesterSlashing")
    45  	defer span.End()
    46  	sub := s.cfg.AttesterSlashingsFeed.Subscribe(ch)
    47  	defer sub.Unsubscribe()
    48  	for {
    49  		select {
    50  		case slashing := <-ch:
    51  			if slashing != nil && slashing.Attestation_1 != nil && slashing.Attestation_2 != nil {
    52  				slashableIndices := sliceutil.IntersectionUint64(slashing.Attestation_1.AttestingIndices, slashing.Attestation_2.AttestingIndices)
    53  				_, err := s.cfg.BeaconClient.SubmitAttesterSlashing(ctx, slashing)
    54  				if err == nil {
    55  					log.WithFields(logrus.Fields{
    56  						"sourceEpoch": slashing.Attestation_1.Data.Source.Epoch,
    57  						"targetEpoch": slashing.Attestation_1.Data.Target.Epoch,
    58  						"indices":     slashableIndices,
    59  					}).Info("Found a valid attester slashing! Submitting to beacon node")
    60  				} else if strings.Contains(err.Error(), helpers.ErrSigFailedToVerify.Error()) {
    61  					log.WithError(err).Errorf("Could not submit attester slashing with indices %v", slashableIndices)
    62  				} else if !strings.Contains(err.Error(), "could not slash") {
    63  					log.WithError(err).Errorf("Could not slash validators with indices %v", slashableIndices)
    64  				}
    65  			}
    66  		case <-sub.Err():
    67  			log.Error("Subscriber closed, exiting goroutine")
    68  			return
    69  		case <-ctx.Done():
    70  			log.Error("Context canceled")
    71  			return
    72  		}
    73  	}
    74  }