prysm-pulse/validator/db/kv/prune_attester_protection_test.go
Raul Jordan 286444a2ec
Shorter Pruning Period for Slashing Protection (#8590)
* begin a simpler pruning process

* shorter pruning

* pass all pruning tests

* passing tests

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2021-03-11 15:51:16 +00:00

156 lines
4.9 KiB
Go

package kv
import (
"context"
"fmt"
"testing"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
bolt "go.etcd.io/bbolt"
)
func TestPruneAttestations_NoPruning(t *testing.T) {
pubKey := [48]byte{1}
validatorDB := setupDB(t, [][48]byte{pubKey})
// Write attesting history for every single epoch
// since genesis to a specified number of epochs.
numEpochs := params.BeaconConfig().SlashingProtectionPruningEpochs - 1
err := setupAttestationsForEveryEpoch(t, validatorDB, pubKey, numEpochs)
require.NoError(t, err)
// Next, attempt to prune and realize that we still have all epochs intact
err = validatorDB.PruneAttestations(context.Background())
require.NoError(t, err)
startEpoch := types.Epoch(0)
err = checkAttestingHistoryAfterPruning(
t,
validatorDB,
pubKey,
startEpoch,
numEpochs,
false, /* should be pruned */
)
require.NoError(t, err)
}
func TestPruneAttestations_OK(t *testing.T) {
pubKey := [48]byte{1}
validatorDB := setupDB(t, [][48]byte{pubKey})
// Write attesting history for every single epoch
// since genesis to SLASHING_PROTECTION_PRUNING_EPOCHS * 2.
numEpochs := params.BeaconConfig().SlashingProtectionPruningEpochs * 2
err := setupAttestationsForEveryEpoch(t, validatorDB, pubKey, numEpochs)
require.NoError(t, err)
err = validatorDB.PruneAttestations(context.Background())
require.NoError(t, err)
// Next, verify that we pruned every epoch
// from genesis to SLASHING_PROTECTION_PRUNING_EPOCHS - 1.
startEpoch := types.Epoch(0)
err = checkAttestingHistoryAfterPruning(
t,
validatorDB,
pubKey,
startEpoch,
params.BeaconConfig().SlashingProtectionPruningEpochs-1,
true, /* should be pruned */
)
require.NoError(t, err)
// Next, verify that we pruned every epoch
// from N = SLASHING_PROTECTION_PRUNING_EPOCHS to N * 2.
startEpoch = params.BeaconConfig().SlashingProtectionPruningEpochs
endEpoch := startEpoch * 2
err = checkAttestingHistoryAfterPruning(
t,
validatorDB,
pubKey,
startEpoch,
endEpoch,
false, /* should not be pruned */
)
require.NoError(t, err)
}
// Saves attesting history for every (source, target = source + 1) pairs since genesis
// up to a given number of epochs for a validator public key.
func setupAttestationsForEveryEpoch(t testing.TB, validatorDB *Store, pubKey [48]byte, numEpochs types.Epoch) error {
return validatorDB.update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(pubKeysBucket)
pkBucket, err := bucket.CreateBucketIfNotExists(pubKey[:])
if err != nil {
return err
}
signingRootsBucket, err := pkBucket.CreateBucketIfNotExists(attestationSigningRootsBucket)
if err != nil {
return err
}
sourceEpochsBucket, err := pkBucket.CreateBucketIfNotExists(attestationSourceEpochsBucket)
if err != nil {
return err
}
for sourceEpoch := types.Epoch(0); sourceEpoch < numEpochs; sourceEpoch++ {
targetEpoch := sourceEpoch + 1
targetEpochBytes := bytesutil.EpochToBytesBigEndian(targetEpoch)
sourceEpochBytes := bytesutil.EpochToBytesBigEndian(sourceEpoch)
// Save (source epoch, target epoch) pairs.
if err := sourceEpochsBucket.Put(sourceEpochBytes, targetEpochBytes); err != nil {
return err
}
// Save signing root for target epoch.
var signingRoot [32]byte
copy(signingRoot[:], fmt.Sprintf("%d", targetEpochBytes))
if err := signingRootsBucket.Put(targetEpochBytes, signingRoot[:]); err != nil {
return err
}
}
return nil
})
}
// Verifies, based on a boolean input argument, whether or not we should have
// pruned all attesting history since genesis up to a specified number of epochs.
func checkAttestingHistoryAfterPruning(
t testing.TB,
validatorDB *Store,
pubKey [48]byte,
startEpoch types.Epoch,
numEpochs types.Epoch,
shouldBePruned bool,
) error {
return validatorDB.view(func(tx *bolt.Tx) error {
bucket := tx.Bucket(pubKeysBucket)
pkBkt := bucket.Bucket(pubKey[:])
signingRootsBkt := pkBkt.Bucket(attestationSigningRootsBucket)
sourceEpochsBkt := pkBkt.Bucket(attestationSourceEpochsBucket)
for sourceEpoch := startEpoch; sourceEpoch < numEpochs; sourceEpoch++ {
targetEpoch := sourceEpoch + 1
targetEpochBytes := bytesutil.EpochToBytesBigEndian(targetEpoch)
sourceEpochBytes := bytesutil.EpochToBytesBigEndian(sourceEpoch)
storedTargetEpoch := sourceEpochsBkt.Get(sourceEpochBytes)
signingRoot := signingRootsBkt.Get(targetEpochBytes)
if shouldBePruned {
// Expect to have no data if we have pruned.
require.Equal(t, true, signingRoot == nil)
require.Equal(t, true, storedTargetEpoch == nil)
} else {
// Expect the correct signing root.
var expectedSigningRoot [32]byte
copy(expectedSigningRoot[:], fmt.Sprintf("%d", targetEpochBytes))
require.DeepEqual(t, expectedSigningRoot[:], signingRoot)
// Expect the correct target epoch.
require.DeepEqual(t, targetEpochBytes, storedTargetEpoch)
}
}
return nil
})
}