Verify historical slots per archived point (#6159)

* Add check for slots point
* Test
* Gaz
This commit is contained in:
terence tsao
2020-06-06 14:47:10 -07:00
committed by GitHub
parent 0b70c3ea85
commit 113a49b61e
3 changed files with 63 additions and 0 deletions

View File

@@ -66,6 +66,7 @@ go_test(
"attestations_test.go",
"backup_test.go",
"blocks_test.go",
"check_historical_test_test.go",
"checkpoint_test.go",
"deposit_contract_test.go",
"encoding_test.go",

View File

@@ -2,14 +2,18 @@ package kv
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/params"
log "github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
)
var historicalStateDeletedKey = []byte("historical-states-deleted")
var archivedSlotsPerPointKey = []byte("slots-per-archived-point")
// HistoricalStatesDeleted verifies historical states exist in DB.
func (kv *Store) HistoricalStatesDeleted(ctx context.Context) error {
@@ -20,6 +24,10 @@ func (kv *Store) HistoricalStatesDeleted(ctx context.Context) error {
})
}
if err := kv.verifySlotsPerArchivePoint(); err != nil {
return err
}
var historicalStateDeleted bool
if err := kv.db.View(func(tx *bolt.Tx) error {
bkt := tx.Bucket(newStateServiceCompatibleBucket)
@@ -42,3 +50,24 @@ func (kv *Store) HistoricalStatesDeleted(ctx context.Context) error {
return bkt.Put(historicalStateDeletedKey, []byte{0x00})
})
}
// This verifies the slots per archived point has not been altered since it's used.
// The node does not allow slots per archived point to alter once it's in operation.
func (kv *Store) verifySlotsPerArchivePoint() error {
return kv.db.Update(func(tx *bolt.Tx) error {
bkt := tx.Bucket(newStateServiceCompatibleBucket)
v := bkt.Get(archivedSlotsPerPointKey)
if v == nil {
if err := bkt.Put(archivedSlotsPerPointKey, bytesutil.Bytes8(params.BeaconConfig().SlotsPerArchivedPoint)); err != nil {
return err
}
} else {
slotsPerPoint := bytesutil.FromBytes8(v)
if slotsPerPoint != params.BeaconConfig().SlotsPerArchivedPoint {
return fmt.Errorf("could not update --slots-per-archive-point after it has been set. Please continue to use %d, or resync from genesis using %d",
slotsPerPoint, params.BeaconConfig().SlotsPerArchivedPoint)
}
}
return nil
})
}

View File

@@ -0,0 +1,33 @@
package kv
import (
"strings"
"testing"
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestVerifySlotsPerArchivePoint(t *testing.T) {
db := setupDB(t)
// This should set default to 2048.
if err := db.verifySlotsPerArchivePoint(); err != nil {
t.Fatal(err)
}
// This should not fail with default 2048.
if err := db.verifySlotsPerArchivePoint(); err != nil {
t.Fatal(err)
}
params.SetupTestConfigCleanup(t)
config := params.BeaconConfig()
config.SlotsPerArchivedPoint = 256
params.OverrideBeaconConfig(config)
// This should fail.
msg := "could not update --slots-per-archive-point after it has been set"
if err := db.verifySlotsPerArchivePoint(); err == nil || !strings.Contains(err.Error(), msg) {
t.Error("Did not get wanted error")
}
}