prysm-pulse/beacon-chain/db/kv/migration_archived_index_test.go
Preston Van Loon 78a25f99c3
Update fastssz (#6760)
* Update fastssz
* Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz
* fmt
* gaz
* Merge refs/heads/master into update-fssz
* goimports
* Merge refs/heads/master into update-fssz
* Merge refs/heads/master into update-fssz
* Merge refs/heads/master into update-fssz
* Merge refs/heads/master into update-fssz
* Merge refs/heads/master into update-fssz
* Fix
* fix ethereumapis
* fix again
* kafka
* fix gen file
* fix compute signing root
* gofmt
* checkpoint progress
* progress
* checkpoint
* progress
* Fix build
* checkpoint
* helpers
* Another test fixed
* gaz
* another test fix
* gofmt
* some fixes
* Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz
* fix one test
* Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz
* fill empty checkpoint roots
* more padding
* more padding
* Fix //beacon-chain/rpc/debug:go_default_test
* fix //beacon-chain/core/state:go_default_test
* fix //beacon-chain/core/state:go_default_test
* fix some htr errors
* fix //slasher/rpc:go_default_test
* Progress on //beacon-chain/core/blocks:go_default_test
* Progress on //beacon-chain/core/blocks:go_default_test
* Progress on //beacon-chain/core/blocks:go_default_test
* fix //slasher/db/kv:go_default_test
* progress
* fix //beacon-chain/sync/initial-sync:go_raceon_test
* gofmt and gaz
* fix one more test, taking a break
* Fix //beacon-chain/core/blocks:go_default_test
* Complete beacon-chain/powchain
* Do most of beacon-chain/rpc/beacon/
* Do most of beacon-chain/blockchain
* fix //beacon-chain/operations/attestations/kv:go_default_test
* Fix //beacon-chain/cache/depositcache:go_default_test
* Fix //slasher/detection:go_default_test
* Progress
* fix //beacon-chain/rpc/validator:go_default_test
* gofmt
* fix //validator/client:go_default_test
* fix
* fix //beacon-chain/blockchain:go_raceoff_test
* fix //beacon-chain/rpc/beacon:go_default_test
* fix 1 of 4 shards in //beacon-chain/sync:go_default_test
* Fix //beacon-chain/sync:go_default_test and gofmt
* prevent panic
* fix //beacon-chain/state/stategen:go_default_test
* fix
* Merge branch 'master' of github.com:prysmaticlabs/prysm into update-fssz
* fix most tests
* Self review, go mod tidy, run regen scripts
* fix slasher
* Update ethereumapis
* disable spawn strategy override
* Merge refs/heads/master into update-fssz
* Merge refs/heads/master into update-fssz
* Remove extra line in imports
* Remove extra line in imports
* Gofmt
* PR feedback from @nisdas
2020-08-24 01:46:17 +00:00

108 lines
3.4 KiB
Go

package kv
import (
"bytes"
"context"
"fmt"
"testing"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"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) {
err := db.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(archivedRootBucket)
assert.NoError(t, err)
if err := tx.Bucket(archivedRootBucket).Put(bytesutil.Uint64ToBytesLittleEndian(2048), []byte("foo")); err != nil {
return err
}
return tx.Bucket(migrationsBucket).Put(migrationArchivedIndex0Key, migrationCompleted)
})
assert.NoError(t, err)
},
eval: func(t *testing.T, db *bbolt.DB) {
err := db.View(func(tx *bbolt.Tx) error {
v := tx.Bucket(archivedRootBucket).Get(bytesutil.Uint64ToBytesLittleEndian(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
})
assert.NoError(t, err)
},
},
{
name: "migrates and deletes entries",
setup: func(t *testing.T, db *bbolt.DB) {
err := db.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(archivedRootBucket)
assert.NoError(t, err)
_, err = tx.CreateBucketIfNotExists(slotsHasObjectBucket)
assert.NoError(t, err)
if err := tx.Bucket(archivedRootBucket).Put(bytesutil.Uint64ToBytesLittleEndian(2048), []byte("foo")); err != nil {
return err
}
sb := testutil.NewBeaconBlock()
sb.Block.Slot = 2048
b, err := encode(context.Background(), sb)
if err != nil {
return err
}
return tx.Bucket(blocksBucket).Put([]byte("foo"), b)
})
assert.NoError(t, err)
},
eval: func(t *testing.T, db *bbolt.DB) {
err := db.View(func(tx *bbolt.Tx) error {
k := uint64(2048)
if v := tx.Bucket(stateSlotIndicesBucket).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
})
assert.NoError(t, err)
},
},
{
name: "deletes old buckets",
setup: func(t *testing.T, db *bbolt.DB) {
err := db.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(archivedRootBucket)
assert.NoError(t, err)
_, err = tx.CreateBucketIfNotExists(slotsHasObjectBucket)
assert.NoError(t, err)
return tx.Bucket(slotsHasObjectBucket).Put(savedStateSlotsKey, []byte("foo"))
})
assert.NoError(t, err)
},
eval: func(t *testing.T, db *bbolt.DB) {
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)
return nil
})
assert.NoError(t, err)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
db := setupDB(t).db
tt.setup(t, db)
assert.NoError(t, db.Update(migrateArchivedIndex), "migrateArchivedIndex(tx) error")
tt.eval(t, db)
})
}
}