2016-11-22 21:50:27 +01:00
|
|
|
package channeldb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-08-03 03:17:25 +02:00
|
|
|
"io/ioutil"
|
2016-11-22 22:57:26 +01:00
|
|
|
"testing"
|
|
|
|
|
2016-11-22 21:50:27 +01:00
|
|
|
"github.com/go-errors/errors"
|
2019-12-13 03:22:19 +01:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb/kvdb"
|
2016-11-22 21:50:27 +01:00
|
|
|
)
|
|
|
|
|
2018-08-10 23:30:44 +02:00
|
|
|
// applyMigration is a helper test function that encapsulates the general steps
|
|
|
|
// which are needed to properly check the result of applying migration function.
|
|
|
|
func applyMigration(t *testing.T, beforeMigration, afterMigration func(d *DB),
|
|
|
|
migrationFunc migration, shouldFail bool) {
|
|
|
|
|
|
|
|
cdb, cleanUp, err := makeTestDB()
|
|
|
|
defer cleanUp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-05-23 20:05:28 +02:00
|
|
|
// Create a test node that will be our source node.
|
|
|
|
testNode, err := createTestVertex(cdb)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
graph := cdb.ChannelGraph()
|
|
|
|
if err := graph.SetSourceNode(testNode); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:30:44 +02:00
|
|
|
// beforeMigration usually used for populating the database
|
|
|
|
// with test data.
|
|
|
|
beforeMigration(cdb)
|
|
|
|
|
|
|
|
// Create test meta info with zero database version and put it on disk.
|
|
|
|
// Than creating the version list pretending that new version was added.
|
|
|
|
meta := &Meta{DbVersionNumber: 0}
|
|
|
|
if err := cdb.PutMeta(meta); err != nil {
|
|
|
|
t.Fatalf("unable to store meta data: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
versions := []version{
|
|
|
|
{
|
|
|
|
number: 0,
|
|
|
|
migration: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
number: 1,
|
|
|
|
migration: migrationFunc,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = errors.New(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && shouldFail {
|
|
|
|
t.Fatal("error wasn't received on migration stage")
|
|
|
|
} else if err != nil && !shouldFail {
|
2019-02-06 02:18:20 +01:00
|
|
|
t.Fatalf("error was received on migration stage: %v", err)
|
2018-08-10 23:30:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// afterMigration usually used for checking the database state and
|
|
|
|
// throwing the error if something went wrong.
|
|
|
|
afterMigration(cdb)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Sync with the latest version - applying migration function.
|
|
|
|
err = cdb.syncVersions(versions)
|
2019-09-28 08:18:14 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2018-08-10 23:30:44 +02:00
|
|
|
}
|
|
|
|
|
2016-11-22 21:50:27 +01:00
|
|
|
// TestVersionFetchPut checks the propernces of fetch/put methods
|
|
|
|
// and also initialization of meta data in case if don't have any in
|
|
|
|
// database.
|
|
|
|
func TestVersionFetchPut(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-11-22 21:50:27 +01:00
|
|
|
db, cleanUp, err := makeTestDB()
|
2016-12-22 21:04:41 +01:00
|
|
|
defer cleanUp()
|
2016-11-22 21:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
meta, err := db.FetchMeta(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:57:26 +01:00
|
|
|
if meta.DbVersionNumber != getLatestDBVersion(dbVersions) {
|
2016-11-22 21:50:27 +01:00
|
|
|
t.Fatal("initialization of meta information wasn't performed")
|
|
|
|
}
|
|
|
|
|
2017-02-23 20:56:47 +01:00
|
|
|
newVersion := getLatestDBVersion(dbVersions) + 1
|
2016-11-22 22:57:26 +01:00
|
|
|
meta.DbVersionNumber = newVersion
|
2016-11-22 21:50:27 +01:00
|
|
|
|
2016-11-28 03:44:14 +01:00
|
|
|
if err := db.PutMeta(meta); err != nil {
|
2016-11-22 21:50:27 +01:00
|
|
|
t.Fatalf("update of meta failed %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
meta, err = db.FetchMeta(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:57:26 +01:00
|
|
|
if meta.DbVersionNumber != newVersion {
|
2016-11-22 21:50:27 +01:00
|
|
|
t.Fatal("update of meta information wasn't performed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestOrderOfMigrations checks that migrations are applied in proper order.
|
|
|
|
func TestOrderOfMigrations(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-11-22 21:50:27 +01:00
|
|
|
appliedMigration := -1
|
|
|
|
versions := []version{
|
|
|
|
{0, nil},
|
|
|
|
{1, nil},
|
2019-12-13 03:22:19 +01:00
|
|
|
{2, func(tx kvdb.RwTx) error {
|
2016-11-22 21:50:27 +01:00
|
|
|
appliedMigration = 2
|
|
|
|
return nil
|
|
|
|
}},
|
2019-12-13 03:22:19 +01:00
|
|
|
{3, func(tx kvdb.RwTx) error {
|
2016-11-22 21:50:27 +01:00
|
|
|
appliedMigration = 3
|
|
|
|
return nil
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the migration that should be applied to db, as far as
|
|
|
|
// current version is 1, we skip zero and first versions.
|
2017-02-08 21:56:37 +01:00
|
|
|
migrations, _ := getMigrationsToApply(versions, 1)
|
2016-11-22 21:50:27 +01:00
|
|
|
|
|
|
|
if len(migrations) != 2 {
|
|
|
|
t.Fatal("incorrect number of migrations to apply")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply first migration.
|
|
|
|
migrations[0](nil)
|
|
|
|
|
|
|
|
// Check that first migration corresponds to the second version.
|
|
|
|
if appliedMigration != 2 {
|
|
|
|
t.Fatal("incorrect order of applying migrations")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply second migration.
|
|
|
|
migrations[1](nil)
|
|
|
|
|
|
|
|
// Check that second migration corresponds to the third version.
|
|
|
|
if appliedMigration != 3 {
|
|
|
|
t.Fatal("incorrect order of applying migrations")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestGlobalVersionList checks that there is no mistake in global version list
|
|
|
|
// in terms of version ordering.
|
|
|
|
func TestGlobalVersionList(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-11-22 22:57:26 +01:00
|
|
|
if dbVersions == nil {
|
2016-11-22 21:50:27 +01:00
|
|
|
t.Fatal("can't find versions list")
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:57:26 +01:00
|
|
|
if len(dbVersions) == 0 {
|
2016-11-22 21:50:27 +01:00
|
|
|
t.Fatal("db versions list is empty")
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:57:26 +01:00
|
|
|
prev := dbVersions[0].number
|
|
|
|
for i := 1; i < len(dbVersions); i++ {
|
|
|
|
version := dbVersions[i].number
|
2016-11-22 21:50:27 +01:00
|
|
|
|
|
|
|
if version == prev {
|
|
|
|
t.Fatal("duplicates db versions")
|
|
|
|
}
|
|
|
|
if version < prev {
|
|
|
|
t.Fatal("order of db versions is wrong")
|
|
|
|
}
|
|
|
|
|
|
|
|
prev = version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:30:44 +02:00
|
|
|
// TestMigrationWithPanic asserts that if migration logic panics, we will return
|
|
|
|
// to the original state unaltered.
|
2016-11-22 21:50:27 +01:00
|
|
|
func TestMigrationWithPanic(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-11-22 21:50:27 +01:00
|
|
|
bucketPrefix := []byte("somebucket")
|
|
|
|
keyPrefix := []byte("someprefix")
|
|
|
|
beforeMigration := []byte("beforemigration")
|
|
|
|
afterMigration := []byte("aftermigration")
|
|
|
|
|
|
|
|
beforeMigrationFunc := func(d *DB) {
|
|
|
|
// Insert data in database and in order then make sure that the
|
|
|
|
// key isn't changes in case of panic or fail.
|
2019-12-13 03:22:19 +01:00
|
|
|
err := kvdb.Update(d, func(tx kvdb.RwTx) error {
|
|
|
|
bucket, err := tx.CreateTopLevelBucket(bucketPrefix)
|
2016-11-22 21:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-13 03:22:19 +01:00
|
|
|
return bucket.Put(keyPrefix, beforeMigration)
|
2016-11-22 21:50:27 +01:00
|
|
|
})
|
2019-12-13 03:22:19 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to insert: %v", err)
|
|
|
|
}
|
2016-11-22 21:50:27 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 04:11:11 +01:00
|
|
|
// Create migration function which changes the initially created data and
|
2016-11-22 21:50:27 +01:00
|
|
|
// throw the panic, in this case we pretending that something goes.
|
2019-12-13 03:22:19 +01:00
|
|
|
migrationWithPanic := func(tx kvdb.RwTx) error {
|
|
|
|
bucket, err := tx.CreateTopLevelBucket(bucketPrefix)
|
2016-11-22 21:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket.Put(keyPrefix, afterMigration)
|
|
|
|
panic("panic!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that version of database and data wasn't changed.
|
|
|
|
afterMigrationFunc := func(d *DB) {
|
|
|
|
meta, err := d.FetchMeta(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:57:26 +01:00
|
|
|
if meta.DbVersionNumber != 0 {
|
2018-02-07 04:11:11 +01:00
|
|
|
t.Fatal("migration panicked but version is changed")
|
2016-11-22 21:50:27 +01:00
|
|
|
}
|
|
|
|
|
2019-12-13 03:22:19 +01:00
|
|
|
err = kvdb.Update(d, func(tx kvdb.RwTx) error {
|
|
|
|
bucket, err := tx.CreateTopLevelBucket(bucketPrefix)
|
2016-11-22 21:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
value := bucket.Get(keyPrefix)
|
|
|
|
if !bytes.Equal(value, beforeMigration) {
|
|
|
|
return errors.New("migration failed but data is " +
|
|
|
|
"changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
applyMigration(t,
|
|
|
|
beforeMigrationFunc,
|
|
|
|
afterMigrationFunc,
|
|
|
|
migrationWithPanic,
|
|
|
|
true)
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:30:44 +02:00
|
|
|
// TestMigrationWithFatal asserts that migrations which fail do not modify the
|
|
|
|
// database.
|
2016-11-22 21:50:27 +01:00
|
|
|
func TestMigrationWithFatal(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-11-22 21:50:27 +01:00
|
|
|
bucketPrefix := []byte("somebucket")
|
|
|
|
keyPrefix := []byte("someprefix")
|
|
|
|
beforeMigration := []byte("beforemigration")
|
|
|
|
afterMigration := []byte("aftermigration")
|
|
|
|
|
|
|
|
beforeMigrationFunc := func(d *DB) {
|
2019-12-13 03:22:19 +01:00
|
|
|
err := kvdb.Update(d, func(tx kvdb.RwTx) error {
|
|
|
|
bucket, err := tx.CreateTopLevelBucket(bucketPrefix)
|
2016-11-22 21:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-13 03:22:19 +01:00
|
|
|
return bucket.Put(keyPrefix, beforeMigration)
|
2016-11-22 21:50:27 +01:00
|
|
|
})
|
2019-12-13 03:22:19 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to insert pre migration key: %v", err)
|
|
|
|
}
|
2016-11-22 21:50:27 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 04:11:11 +01:00
|
|
|
// Create migration function which changes the initially created data and
|
|
|
|
// return the error, in this case we pretending that something goes
|
2016-11-22 21:50:27 +01:00
|
|
|
// wrong.
|
2019-12-13 03:22:19 +01:00
|
|
|
migrationWithFatal := func(tx kvdb.RwTx) error {
|
|
|
|
bucket, err := tx.CreateTopLevelBucket(bucketPrefix)
|
2016-11-22 21:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket.Put(keyPrefix, afterMigration)
|
2017-02-23 20:56:47 +01:00
|
|
|
return errors.New("some error")
|
2016-11-22 21:50:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that version of database and initial data wasn't changed.
|
|
|
|
afterMigrationFunc := func(d *DB) {
|
|
|
|
meta, err := d.FetchMeta(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:57:26 +01:00
|
|
|
if meta.DbVersionNumber != 0 {
|
2016-11-22 21:50:27 +01:00
|
|
|
t.Fatal("migration failed but version is changed")
|
|
|
|
}
|
|
|
|
|
2019-12-13 03:22:19 +01:00
|
|
|
err = kvdb.Update(d, func(tx kvdb.RwTx) error {
|
|
|
|
bucket, err := tx.CreateTopLevelBucket(bucketPrefix)
|
2016-11-22 21:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
value := bucket.Get(keyPrefix)
|
|
|
|
if !bytes.Equal(value, beforeMigration) {
|
|
|
|
return errors.New("migration failed but data is " +
|
|
|
|
"changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
applyMigration(t,
|
|
|
|
beforeMigrationFunc,
|
|
|
|
afterMigrationFunc,
|
|
|
|
migrationWithFatal,
|
|
|
|
true)
|
|
|
|
}
|
|
|
|
|
2018-08-10 23:30:44 +02:00
|
|
|
// TestMigrationWithoutErrors asserts that a successful migration has its
|
|
|
|
// changes applied to the database.
|
2016-11-22 21:50:27 +01:00
|
|
|
func TestMigrationWithoutErrors(t *testing.T) {
|
2017-06-17 00:59:20 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-11-22 21:50:27 +01:00
|
|
|
bucketPrefix := []byte("somebucket")
|
|
|
|
keyPrefix := []byte("someprefix")
|
|
|
|
beforeMigration := []byte("beforemigration")
|
|
|
|
afterMigration := []byte("aftermigration")
|
|
|
|
|
|
|
|
// Populate database with initial data.
|
|
|
|
beforeMigrationFunc := func(d *DB) {
|
2020-01-16 02:32:59 +01:00
|
|
|
err := kvdb.Update(d, func(tx kvdb.RwTx) error {
|
2019-12-13 03:22:19 +01:00
|
|
|
bucket, err := tx.CreateTopLevelBucket(bucketPrefix)
|
2016-11-22 21:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:32:59 +01:00
|
|
|
return bucket.Put(keyPrefix, beforeMigration)
|
2016-11-22 21:50:27 +01:00
|
|
|
})
|
2020-01-16 02:32:59 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to update db pre migration: %v", err)
|
|
|
|
}
|
2016-11-22 21:50:27 +01:00
|
|
|
}
|
|
|
|
|
2018-02-07 04:11:11 +01:00
|
|
|
// Create migration function which changes the initially created data.
|
2019-12-13 03:22:19 +01:00
|
|
|
migrationWithoutErrors := func(tx kvdb.RwTx) error {
|
|
|
|
bucket, err := tx.CreateTopLevelBucket(bucketPrefix)
|
2016-11-22 21:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket.Put(keyPrefix, afterMigration)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that version of database and data was properly changed.
|
|
|
|
afterMigrationFunc := func(d *DB) {
|
|
|
|
meta, err := d.FetchMeta(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-22 22:57:26 +01:00
|
|
|
if meta.DbVersionNumber != 1 {
|
2016-11-22 21:50:27 +01:00
|
|
|
t.Fatal("version number isn't changed after " +
|
2018-02-07 04:11:11 +01:00
|
|
|
"successfully applied migration")
|
2016-11-22 21:50:27 +01:00
|
|
|
}
|
|
|
|
|
2019-12-13 03:22:19 +01:00
|
|
|
err = kvdb.Update(d, func(tx kvdb.RwTx) error {
|
|
|
|
bucket, err := tx.CreateTopLevelBucket(bucketPrefix)
|
2016-11-22 21:50:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
value := bucket.Get(keyPrefix)
|
|
|
|
if !bytes.Equal(value, afterMigration) {
|
2018-02-07 04:11:11 +01:00
|
|
|
return errors.New("migration wasn't applied " +
|
2016-11-22 21:50:27 +01:00
|
|
|
"properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
applyMigration(t,
|
|
|
|
beforeMigrationFunc,
|
|
|
|
afterMigrationFunc,
|
|
|
|
migrationWithoutErrors,
|
|
|
|
false)
|
|
|
|
}
|
2018-08-03 03:17:25 +02:00
|
|
|
|
|
|
|
// TestMigrationReversion tests after performing a migration to a higher
|
|
|
|
// database version, opening the database with a lower latest db version returns
|
|
|
|
// ErrDBReversion.
|
|
|
|
func TestMigrationReversion(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tempDirName, err := ioutil.TempDir("", "channeldb")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cdb, err := Open(tempDirName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to open channeldb: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the database metadata to point to one more than the highest
|
|
|
|
// known version.
|
2019-12-13 03:22:19 +01:00
|
|
|
err = kvdb.Update(cdb, func(tx kvdb.RwTx) error {
|
2018-08-03 03:17:25 +02:00
|
|
|
newMeta := &Meta{
|
|
|
|
DbVersionNumber: getLatestDBVersion(dbVersions) + 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
return putMeta(newMeta, tx)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Close the database. Even if we succeeded, our next step is to reopen.
|
|
|
|
cdb.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to increase db version: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = Open(tempDirName)
|
|
|
|
if err != ErrDBReversion {
|
|
|
|
t.Fatalf("unexpected error when opening channeldb, "+
|
|
|
|
"want: %v, got: %v", ErrDBReversion, err)
|
|
|
|
}
|
|
|
|
}
|