2020-07-18 18:05:04 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-07-30 13:38:54 +00:00
|
|
|
"context"
|
2020-07-18 18:05:04 +00:00
|
|
|
"strconv"
|
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
|
2020-07-18 18:05:04 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
var migrationBlockSlotIndex0Key = []byte("block_slot_index_0")
|
|
|
|
|
2021-07-30 13:38:54 +00:00
|
|
|
func migrateBlockSlotIndex(ctx context.Context, db *bolt.DB) error {
|
|
|
|
if updateErr := db.Update(func(tx *bolt.Tx) error {
|
|
|
|
mb := tx.Bucket(migrationsBucket)
|
|
|
|
if b := mb.Get(migrationBlockSlotIndex0Key); 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(blockSlotIndicesBucket)
|
2020-07-18 18:05:04 +00:00
|
|
|
|
2021-07-30 13:38:54 +00:00
|
|
|
// Convert indices from strings to big endian integers.
|
|
|
|
if err := bkt.ForEach(func(k, v []byte) error {
|
|
|
|
key, err := strconv.ParseUint(string(k), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = bkt.Delete(k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = bkt.Put(bytesutil.Uint64ToBytesBigEndian(key), v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// check if context is cancelled in between
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2020-07-18 18:05:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-30 13:38:54 +00:00
|
|
|
return mb.Put(migrationBlockSlotIndex0Key, migrationCompleted)
|
|
|
|
}); updateErr != nil {
|
|
|
|
log.WithError(updateErr).Errorf("could not migrate bucket: %s", blockSlotIndicesBucket)
|
|
|
|
return updateErr
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-18 18:05:04 +00:00
|
|
|
}
|