channeldb: require minimum db upgrade version

This commit is contained in:
Joost Jager 2019-09-10 11:22:01 +02:00
parent ffb8c0cfc3
commit f1942a4c33
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
2 changed files with 28 additions and 1 deletions

@ -1111,8 +1111,10 @@ func (d *DB) syncVersions(versions []version) error {
}
latestVersion := getLatestDBVersion(versions)
minUpgradeVersion := getMinUpgradeVersion(versions)
log.Infof("Checking for schema update: latest_version=%v, "+
"db_version=%v", latestVersion, meta.DbVersionNumber)
"min_upgrade_version=%v, db_version=%v", latestVersion,
minUpgradeVersion, meta.DbVersionNumber)
switch {
@ -1125,6 +1127,12 @@ func (d *DB) syncVersions(versions []version) error {
latestVersion)
return ErrDBReversion
case meta.DbVersionNumber < minUpgradeVersion:
log.Errorf("Refusing to upgrade from db_version=%d to "+
"latest_version=%d. Upgrade via intermediate major "+
"release(s).", meta.DbVersionNumber, latestVersion)
return ErrDBVersionTooLow
// If the current database version matches the latest version number,
// then we don't need to perform any migrations.
case meta.DbVersionNumber == latestVersion:
@ -1168,6 +1176,21 @@ func getLatestDBVersion(versions []version) uint32 {
return versions[len(versions)-1].number
}
// getMinUpgradeVersion returns the minimum version required to upgrade the
// database.
func getMinUpgradeVersion(versions []version) uint32 {
firstMigrationVersion := versions[0].number
// If we can upgrade from the base version with this version of lnd,
// return the base version as the minimum required version.
if firstMigrationVersion == 0 {
return 0
}
// Otherwise require the version that the first migration upgrades from.
return firstMigrationVersion - 1
}
// getMigrationsToApply retrieves the migration function that should be
// applied to the database.
func getMigrationsToApply(versions []version, version uint32) ([]migration, []uint32) {

@ -14,6 +14,10 @@ var (
// prior database version.
ErrDBReversion = fmt.Errorf("channel db cannot revert to prior version")
// ErrDBVersionTooLow is returned when detecting an attempt to upgrade a
// version for which migration is no longer supported.
ErrDBVersionTooLow = fmt.Errorf("channel db version too old to upgrade")
// ErrLinkNodesNotFound is returned when node info bucket hasn't been
// created.
ErrLinkNodesNotFound = fmt.Errorf("no link nodes exist")