2020-07-18 18:05:04 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2020-08-08 18:39:01 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
2020-07-18 18:05:04 +00:00
|
|
|
"go.etcd.io/bbolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_migrateBlockSlotIndex(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 {
|
2020-07-18 18:05:04 +00:00
|
|
|
if err := tx.Bucket(blockSlotIndicesBucket).Put([]byte("2048"), []byte("foo")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Bucket(migrationsBucket).Put(migrationBlockSlotIndex0Key, 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(blockSlotIndicesBucket).Get([]byte("2048"))
|
|
|
|
if !bytes.Equal(v, []byte("foo")) {
|
|
|
|
return fmt.Errorf("did not receive correct data for key 2048, wanted 'foo' got %s", v)
|
|
|
|
}
|
|
|
|
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 {
|
2020-07-18 18:05:04 +00:00
|
|
|
return tx.Bucket(blockSlotIndicesBucket).Put([]byte("2048"), []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 {
|
2020-07-18 18:05:04 +00:00
|
|
|
k := uint64(2048)
|
|
|
|
if v := tx.Bucket(blockSlotIndicesBucket).Get(bytesutil.Uint64ToBytesBigEndian(k)); !bytes.Equal(v, []byte("foo")) {
|
|
|
|
return fmt.Errorf("did not receive correct data for key %d, wanted 'foo' got %v", k, v)
|
|
|
|
}
|
|
|
|
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)
|
2020-08-08 18:39:01 +00:00
|
|
|
assert.NoError(t, db.Update(migrateBlockSlotIndex), "migrateBlockSlotIndex(tx) error")
|
2020-07-18 18:05:04 +00:00
|
|
|
tt.eval(t, db)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|