2020-07-18 18:05:04 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
2020-07-18 18:05:04 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_migrateArchivedIndex(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
setup func(t *testing.T, db *bbolt.DB)
|
|
|
|
eval func(t *testing.T, db *bbolt.DB)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "only runs once",
|
|
|
|
setup: func(t *testing.T, db *bbolt.DB) {
|
2020-08-08 18:39:01 +00:00
|
|
|
err := db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
_, err := tx.CreateBucketIfNotExists(archivedRootBucket)
|
|
|
|
assert.NoError(t, err)
|
2020-07-18 18:05:04 +00:00
|
|
|
if err := tx.Bucket(archivedRootBucket).Put(bytesutil.Uint64ToBytesLittleEndian(2048), []byte("foo")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Bucket(migrationsBucket).Put(migrationArchivedIndex0Key, migrationCompleted)
|
2020-08-08 18:39:01 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2020-07-18 18:05:04 +00:00
|
|
|
},
|
|
|
|
eval: func(t *testing.T, db *bbolt.DB) {
|
2020-08-08 18:39:01 +00:00
|
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
2020-07-18 18:05:04 +00:00
|
|
|
v := tx.Bucket(archivedRootBucket).Get(bytesutil.Uint64ToBytesLittleEndian(2048))
|
2021-01-22 15:15:40 +00:00
|
|
|
assert.DeepEqual(t, []byte("foo"), v, "Did not receive correct data for key 2048")
|
2020-07-18 18:05:04 +00:00
|
|
|
return nil
|
2020-08-08 18:39:01 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2020-07-18 18:05:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "migrates and deletes entries",
|
|
|
|
setup: func(t *testing.T, db *bbolt.DB) {
|
2020-08-08 18:39:01 +00:00
|
|
|
err := db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
_, err := tx.CreateBucketIfNotExists(archivedRootBucket)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = tx.CreateBucketIfNotExists(slotsHasObjectBucket)
|
|
|
|
assert.NoError(t, err)
|
2020-07-18 18:05:04 +00:00
|
|
|
if err := tx.Bucket(archivedRootBucket).Put(bytesutil.Uint64ToBytesLittleEndian(2048), []byte("foo")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-23 18:53:46 +00:00
|
|
|
sb := util.NewBeaconBlock()
|
2020-08-27 18:13:32 +00:00
|
|
|
sb.Block.Slot = 2048
|
|
|
|
b, err := encode(context.Background(), sb)
|
2020-07-18 18:05:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Bucket(blocksBucket).Put([]byte("foo"), b)
|
2020-08-08 18:39:01 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2020-07-18 18:05:04 +00:00
|
|
|
},
|
|
|
|
eval: func(t *testing.T, db *bbolt.DB) {
|
2020-08-08 18:39:01 +00:00
|
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
2020-07-18 18:05:04 +00:00
|
|
|
k := uint64(2048)
|
2021-01-22 15:15:40 +00:00
|
|
|
v := tx.Bucket(stateSlotIndicesBucket).Get(bytesutil.Uint64ToBytesBigEndian(k))
|
|
|
|
assert.DeepEqual(t, []byte("foo"), v, "Did not receive correct data for key %d", k)
|
2020-07-18 18:05:04 +00:00
|
|
|
return nil
|
2020-08-08 18:39:01 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2020-07-18 18:05:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "deletes old buckets",
|
|
|
|
setup: func(t *testing.T, db *bbolt.DB) {
|
2020-08-08 18:39:01 +00:00
|
|
|
err := db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
_, err := tx.CreateBucketIfNotExists(archivedRootBucket)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = tx.CreateBucketIfNotExists(slotsHasObjectBucket)
|
|
|
|
assert.NoError(t, err)
|
2020-07-18 18:05:04 +00:00
|
|
|
return tx.Bucket(slotsHasObjectBucket).Put(savedStateSlotsKey, []byte("foo"))
|
2020-08-08 18:39:01 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2020-07-18 18:05:04 +00:00
|
|
|
},
|
|
|
|
eval: func(t *testing.T, db *bbolt.DB) {
|
2020-08-08 18:39:01 +00:00
|
|
|
err := db.View(func(tx *bbolt.Tx) error {
|
|
|
|
assert.Equal(t, (*bbolt.Bucket)(nil), tx.Bucket(slotsHasObjectBucket), "Expected %v to be deleted", savedStateSlotsKey)
|
|
|
|
assert.Equal(t, (*bbolt.Bucket)(nil), tx.Bucket(archivedRootBucket), "Expected %v to be deleted", savedStateSlotsKey)
|
2020-07-18 18:05:04 +00:00
|
|
|
return nil
|
2020-08-08 18:39:01 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2020-07-18 18:05:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
db := setupDB(t).db
|
|
|
|
tt.setup(t, db)
|
2021-07-30 13:38:54 +00:00
|
|
|
assert.NoError(t, migrateArchivedIndex(context.Background(), db), "migrateArchivedIndex(tx) error")
|
2020-07-18 18:05:04 +00:00
|
|
|
tt.eval(t, db)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|