2020-03-13 17:35:28 +00:00
|
|
|
package attestations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-09-22 11:49:58 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/timeutils"
|
2020-03-13 17:35:28 +00:00
|
|
|
)
|
|
|
|
|
2020-07-03 16:43:56 +00:00
|
|
|
// pruneAttsPool prunes attestations pool on every slot interval.
|
2020-03-13 17:35:28 +00:00
|
|
|
func (s *Service) pruneAttsPool() {
|
2020-07-03 16:43:56 +00:00
|
|
|
ticker := time.NewTicker(s.pruneInterval)
|
2020-09-08 18:05:38 +00:00
|
|
|
defer ticker.Stop()
|
2020-03-13 17:35:28 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
s.pruneExpiredAtts()
|
2020-08-06 21:02:58 +00:00
|
|
|
s.updateMetrics()
|
2020-03-13 17:35:28 +00:00
|
|
|
case <-s.ctx.Done():
|
|
|
|
log.Debug("Context closed, exiting routine")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This prunes expired attestations from the pool.
|
|
|
|
func (s *Service) pruneExpiredAtts() {
|
|
|
|
aggregatedAtts := s.pool.AggregatedAttestations()
|
|
|
|
for _, att := range aggregatedAtts {
|
|
|
|
if s.expired(att.Data.Slot) {
|
|
|
|
if err := s.pool.DeleteAggregatedAttestation(att); err != nil {
|
|
|
|
log.WithError(err).Error("Could not delete expired aggregated attestation")
|
|
|
|
}
|
|
|
|
expiredAggregatedAtts.Inc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 20:21:50 +00:00
|
|
|
if _, err := s.pool.DeleteSeenUnaggregatedAttestations(); err != nil {
|
2020-09-10 18:35:57 +00:00
|
|
|
log.WithError(err).Error("Cannot delete seen attestations")
|
|
|
|
}
|
2020-08-21 23:27:51 +00:00
|
|
|
unAggregatedAtts, err := s.pool.UnaggregatedAttestations()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Could not get unaggregated attestations")
|
|
|
|
return
|
|
|
|
}
|
2020-03-13 17:35:28 +00:00
|
|
|
for _, att := range unAggregatedAtts {
|
|
|
|
if s.expired(att.Data.Slot) {
|
|
|
|
if err := s.pool.DeleteUnaggregatedAttestation(att); err != nil {
|
|
|
|
log.WithError(err).Error("Could not delete expired unaggregated attestation")
|
|
|
|
}
|
|
|
|
expiredUnaggregatedAtts.Inc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blockAtts := s.pool.BlockAttestations()
|
|
|
|
for _, att := range blockAtts {
|
|
|
|
if s.expired(att.Data.Slot) {
|
|
|
|
if err := s.pool.DeleteBlockAttestation(att); err != nil {
|
|
|
|
log.WithError(err).Error("Could not delete expired block attestation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
expiredBlockAtts.Inc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the input slot has been expired.
|
|
|
|
// Expired is defined as one epoch behind than current time.
|
|
|
|
func (s *Service) expired(slot uint64) bool {
|
|
|
|
expirationSlot := slot + params.BeaconConfig().SlotsPerEpoch
|
|
|
|
expirationTime := s.genesisTime + expirationSlot*params.BeaconConfig().SecondsPerSlot
|
2020-09-22 11:49:58 +00:00
|
|
|
currentTime := uint64(timeutils.Now().Unix())
|
2020-08-10 16:16:45 +00:00
|
|
|
return currentTime >= expirationTime
|
2020-03-13 17:35:28 +00:00
|
|
|
}
|