github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/operations/attestations/prune_expired.go (about)

     1  package attestations
     2  
     3  import (
     4  	"time"
     5  
     6  	types "github.com/prysmaticlabs/eth2-types"
     7  	"github.com/prysmaticlabs/prysm/shared/params"
     8  	"github.com/prysmaticlabs/prysm/shared/timeutils"
     9  )
    10  
    11  // pruneAttsPool prunes attestations pool on every slot interval.
    12  func (s *Service) pruneAttsPool() {
    13  	ticker := time.NewTicker(s.cfg.pruneInterval)
    14  	defer ticker.Stop()
    15  	for {
    16  		select {
    17  		case <-ticker.C:
    18  			s.pruneExpiredAtts()
    19  			s.updateMetrics()
    20  		case <-s.ctx.Done():
    21  			log.Debug("Context closed, exiting routine")
    22  			return
    23  		}
    24  	}
    25  }
    26  
    27  // This prunes expired attestations from the pool.
    28  func (s *Service) pruneExpiredAtts() {
    29  	aggregatedAtts := s.cfg.Pool.AggregatedAttestations()
    30  	for _, att := range aggregatedAtts {
    31  		if s.expired(att.Data.Slot) {
    32  			if err := s.cfg.Pool.DeleteAggregatedAttestation(att); err != nil {
    33  				log.WithError(err).Error("Could not delete expired aggregated attestation")
    34  			}
    35  			expiredAggregatedAtts.Inc()
    36  		}
    37  	}
    38  
    39  	if _, err := s.cfg.Pool.DeleteSeenUnaggregatedAttestations(); err != nil {
    40  		log.WithError(err).Error("Cannot delete seen attestations")
    41  	}
    42  	unAggregatedAtts, err := s.cfg.Pool.UnaggregatedAttestations()
    43  	if err != nil {
    44  		log.WithError(err).Error("Could not get unaggregated attestations")
    45  		return
    46  	}
    47  	for _, att := range unAggregatedAtts {
    48  		if s.expired(att.Data.Slot) {
    49  			if err := s.cfg.Pool.DeleteUnaggregatedAttestation(att); err != nil {
    50  				log.WithError(err).Error("Could not delete expired unaggregated attestation")
    51  			}
    52  			expiredUnaggregatedAtts.Inc()
    53  		}
    54  	}
    55  
    56  	blockAtts := s.cfg.Pool.BlockAttestations()
    57  	for _, att := range blockAtts {
    58  		if s.expired(att.Data.Slot) {
    59  			if err := s.cfg.Pool.DeleteBlockAttestation(att); err != nil {
    60  				log.WithError(err).Error("Could not delete expired block attestation")
    61  			}
    62  		}
    63  		expiredBlockAtts.Inc()
    64  	}
    65  }
    66  
    67  // Return true if the input slot has been expired.
    68  // Expired is defined as one epoch behind than current time.
    69  func (s *Service) expired(slot types.Slot) bool {
    70  	expirationSlot := slot + params.BeaconConfig().SlotsPerEpoch
    71  	expirationTime := s.genesisTime + uint64(expirationSlot.Mul(params.BeaconConfig().SecondsPerSlot))
    72  	currentTime := uint64(timeutils.Now().Unix())
    73  	return currentTime >= expirationTime
    74  }