2020-07-18 18:05:04 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
|
2022-04-29 14:32:11 +00:00
|
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-07-21 21:34:07 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2020-07-18 18:05:04 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var migrationArchivedIndex0Key = []byte("archive_index_0")
|
|
|
|
|
2021-07-30 13:38:54 +00:00
|
|
|
func migrateArchivedIndex(ctx context.Context, db *bolt.DB) error {
|
|
|
|
if updateErr := db.Update(func(tx *bolt.Tx) error {
|
|
|
|
mb := tx.Bucket(migrationsBucket)
|
|
|
|
if b := mb.Get(migrationArchivedIndex0Key); bytes.Equal(b, migrationCompleted) {
|
|
|
|
return nil // Migration already completed.
|
2020-07-18 18:05:04 +00:00
|
|
|
}
|
2021-07-30 13:38:54 +00:00
|
|
|
|
|
|
|
bkt := tx.Bucket(archivedRootBucket)
|
|
|
|
if bkt == nil {
|
|
|
|
return nil
|
2020-07-18 18:05:04 +00:00
|
|
|
}
|
2021-07-30 13:38:54 +00:00
|
|
|
// Remove "last archived index" key before iterating over all keys.
|
|
|
|
if err := bkt.Delete(lastArchivedIndexKey); err != nil {
|
2020-07-18 18:05:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-30 13:38:54 +00:00
|
|
|
var highest types.Slot
|
|
|
|
c := bkt.Cursor()
|
|
|
|
for k, v := c.First(); k != nil; k, v = c.Next() {
|
|
|
|
// Look up actual slot from block
|
|
|
|
b := tx.Bucket(blocksBucket).Get(v)
|
|
|
|
// Skip this key if there is no block for whatever reason.
|
|
|
|
if b == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
blk := ðpb.SignedBeaconBlock{}
|
|
|
|
if err := decode(context.TODO(), b, blk); err != nil {
|
2020-07-18 18:05:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-30 13:38:54 +00:00
|
|
|
if err := tx.Bucket(stateSlotIndicesBucket).Put(bytesutil.SlotToBytesBigEndian(blk.Block.Slot), v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if blk.Block.Slot > highest {
|
|
|
|
highest = blk.Block.Slot
|
|
|
|
}
|
|
|
|
// check if context is cancelled in between
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
2020-07-18 18:05:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 13:38:54 +00:00
|
|
|
// Delete deprecated buckets.
|
|
|
|
for _, bkt := range [][]byte{slotsHasObjectBucket, archivedRootBucket} {
|
|
|
|
if tx.Bucket(bkt) != nil {
|
|
|
|
if err := tx.DeleteBucket(bkt); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark migration complete.
|
|
|
|
return mb.Put(migrationArchivedIndex0Key, migrationCompleted)
|
|
|
|
}); updateErr != nil {
|
|
|
|
log.WithError(updateErr).Errorf("could not migrate bucket: %s", archivedRootBucket)
|
|
|
|
return updateErr
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-18 18:05:04 +00:00
|
|
|
}
|