2020-03-13 17:35:28 +00:00
|
|
|
package attestations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2022-04-29 14:32:11 +00:00
|
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
2021-09-15 00:09:04 +00:00
|
|
|
prysmTime "github.com/prysmaticlabs/prysm/time"
|
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() {
|
2021-03-21 16:58:41 +00:00
|
|
|
ticker := time.NewTicker(s.cfg.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() {
|
2021-03-21 16:58:41 +00:00
|
|
|
aggregatedAtts := s.cfg.Pool.AggregatedAttestations()
|
2020-03-13 17:35:28 +00:00
|
|
|
for _, att := range aggregatedAtts {
|
|
|
|
if s.expired(att.Data.Slot) {
|
2021-03-21 16:58:41 +00:00
|
|
|
if err := s.cfg.Pool.DeleteAggregatedAttestation(att); err != nil {
|
2020-03-13 17:35:28 +00:00
|
|
|
log.WithError(err).Error("Could not delete expired aggregated attestation")
|
|
|
|
}
|
|
|
|
expiredAggregatedAtts.Inc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 16:58:41 +00:00
|
|
|
if _, err := s.cfg.Pool.DeleteSeenUnaggregatedAttestations(); err != nil {
|
2020-09-10 18:35:57 +00:00
|
|
|
log.WithError(err).Error("Cannot delete seen attestations")
|
|
|
|
}
|
2021-03-21 16:58:41 +00:00
|
|
|
unAggregatedAtts, err := s.cfg.Pool.UnaggregatedAttestations()
|
2020-08-21 23:27:51 +00:00
|
|
|
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) {
|
2021-03-21 16:58:41 +00:00
|
|
|
if err := s.cfg.Pool.DeleteUnaggregatedAttestation(att); err != nil {
|
2020-03-13 17:35:28 +00:00
|
|
|
log.WithError(err).Error("Could not delete expired unaggregated attestation")
|
|
|
|
}
|
|
|
|
expiredUnaggregatedAtts.Inc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 16:58:41 +00:00
|
|
|
blockAtts := s.cfg.Pool.BlockAttestations()
|
2020-03-13 17:35:28 +00:00
|
|
|
for _, att := range blockAtts {
|
|
|
|
if s.expired(att.Data.Slot) {
|
2021-03-21 16:58:41 +00:00
|
|
|
if err := s.cfg.Pool.DeleteBlockAttestation(att); err != nil {
|
2020-03-13 17:35:28 +00:00
|
|
|
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.
|
2021-02-16 07:45:34 +00:00
|
|
|
func (s *Service) expired(slot types.Slot) bool {
|
2020-03-13 17:35:28 +00:00
|
|
|
expirationSlot := slot + params.BeaconConfig().SlotsPerEpoch
|
2021-02-16 07:45:34 +00:00
|
|
|
expirationTime := s.genesisTime + uint64(expirationSlot.Mul(params.BeaconConfig().SecondsPerSlot))
|
2021-09-15 00:09:04 +00:00
|
|
|
currentTime := uint64(prysmTime.Now().Unix())
|
2020-08-10 16:16:45 +00:00
|
|
|
return currentTime >= expirationTime
|
2020-03-13 17:35:28 +00:00
|
|
|
}
|