2021-01-07 21:40:37 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-02-22 23:20:57 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-01-07 21:40:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2021-02-03 19:29:20 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
"go.opencensus.io/trace"
|
2021-01-07 21:40:37 +00:00
|
|
|
)
|
|
|
|
|
2021-03-11 15:51:16 +00:00
|
|
|
// PruneAttestations loops through every public key in the public keys bucket
|
|
|
|
// and prunes all attestation data that has target epochs older the highest
|
|
|
|
// target epoch minus some constant of how many epochs we keep track of for slashing
|
|
|
|
// protection. This routine is meant to run on startup.
|
|
|
|
func (s *Store) PruneAttestations(ctx context.Context) error {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "Validator.PruneAttestations")
|
2021-02-03 19:29:20 +00:00
|
|
|
defer span.End()
|
2021-01-20 14:39:07 +00:00
|
|
|
return s.update(func(tx *bolt.Tx) error {
|
2021-01-07 21:40:37 +00:00
|
|
|
bucket := tx.Bucket(pubKeysBucket)
|
|
|
|
return bucket.ForEach(func(pubKey []byte, _ []byte) error {
|
|
|
|
pkBucket := bucket.Bucket(pubKey)
|
|
|
|
if pkBucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := pruneSourceEpochsBucket(pkBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-17 19:23:59 +00:00
|
|
|
if err := pruneTargetEpochsBucket(pkBucket); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-07 21:40:37 +00:00
|
|
|
return pruneSigningRootsBucket(pkBucket)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func pruneSourceEpochsBucket(bucket *bolt.Bucket) error {
|
|
|
|
sourceEpochsBucket := bucket.Bucket(attestationSourceEpochsBucket)
|
|
|
|
if sourceEpochsBucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// We obtain the highest source epoch from the source epochs bucket.
|
|
|
|
// Then, we obtain the corresponding target epoch for that source epoch.
|
|
|
|
highestSourceEpochBytes, _ := sourceEpochsBucket.Cursor().Last()
|
|
|
|
highestTargetEpochBytes := sourceEpochsBucket.Get(highestSourceEpochBytes)
|
2021-02-09 10:05:22 +00:00
|
|
|
highestTargetEpoch := bytesutil.BytesToEpochBigEndian(highestTargetEpochBytes)
|
2021-01-07 21:40:37 +00:00
|
|
|
|
|
|
|
return sourceEpochsBucket.ForEach(func(k []byte, v []byte) error {
|
2021-02-09 10:05:22 +00:00
|
|
|
targetEpoch := bytesutil.BytesToEpochBigEndian(v)
|
2021-03-11 15:51:16 +00:00
|
|
|
if targetEpoch < pruningEpochCutoff(highestTargetEpoch) {
|
2021-01-07 21:40:37 +00:00
|
|
|
return sourceEpochsBucket.Delete(k)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-17 19:23:59 +00:00
|
|
|
func pruneTargetEpochsBucket(bucket *bolt.Bucket) error {
|
|
|
|
targetEpochsBucket := bucket.Bucket(attestationTargetEpochsBucket)
|
|
|
|
if targetEpochsBucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// We obtain the highest target epoch from the bucket.
|
|
|
|
highestTargetEpochBytes, _ := targetEpochsBucket.Cursor().Last()
|
|
|
|
highestTargetEpoch := bytesutil.BytesToEpochBigEndian(highestTargetEpochBytes)
|
|
|
|
|
2021-03-11 15:51:16 +00:00
|
|
|
return targetEpochsBucket.ForEach(func(k []byte, v []byte) error {
|
2021-02-17 19:23:59 +00:00
|
|
|
targetEpoch := bytesutil.BytesToEpochBigEndian(k)
|
2021-03-11 15:51:16 +00:00
|
|
|
if targetEpoch < pruningEpochCutoff(highestTargetEpoch) {
|
2021-02-17 19:23:59 +00:00
|
|
|
return targetEpochsBucket.Delete(k)
|
|
|
|
}
|
2021-03-11 15:51:16 +00:00
|
|
|
return nil
|
|
|
|
})
|
2021-02-17 19:23:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 21:40:37 +00:00
|
|
|
func pruneSigningRootsBucket(bucket *bolt.Bucket) error {
|
|
|
|
signingRootsBucket := bucket.Bucket(attestationSigningRootsBucket)
|
|
|
|
if signingRootsBucket == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We obtain the highest target epoch from the signing roots bucket.
|
|
|
|
highestTargetEpochBytes, _ := signingRootsBucket.Cursor().Last()
|
2021-02-09 10:05:22 +00:00
|
|
|
highestTargetEpoch := bytesutil.BytesToEpochBigEndian(highestTargetEpochBytes)
|
2021-01-07 21:40:37 +00:00
|
|
|
|
|
|
|
return signingRootsBucket.ForEach(func(k []byte, v []byte) error {
|
2021-02-09 10:05:22 +00:00
|
|
|
targetEpoch := bytesutil.BytesToEpochBigEndian(k)
|
2021-03-11 15:51:16 +00:00
|
|
|
if targetEpoch < pruningEpochCutoff(highestTargetEpoch) {
|
2021-01-07 21:40:37 +00:00
|
|
|
return signingRootsBucket.Delete(k)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:51:16 +00:00
|
|
|
// This helper function determines the cutoff epoch where, for all epochs before it, we should prune
|
|
|
|
// the slashing protection database. This is computed by taking in an epoch and subtracting
|
|
|
|
// SLASHING_PROTECTION_PRUNING_EPOCHS from the value. For example, if we are keeping track of 512 epochs
|
|
|
|
// in the database, if we pass in epoch 612, then we want to prune all epochs before epoch 100.
|
|
|
|
func pruningEpochCutoff(epoch types.Epoch) types.Epoch {
|
|
|
|
minEpoch := types.Epoch(0)
|
|
|
|
if epoch > params.BeaconConfig().SlashingProtectionPruningEpochs {
|
|
|
|
minEpoch = epoch - params.BeaconConfig().SlashingProtectionPruningEpochs
|
|
|
|
}
|
|
|
|
return minEpoch
|
2021-01-07 21:40:37 +00:00
|
|
|
}
|