From e23fc40d638446557af8a9091cb0ed5354867817 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Thu, 2 Aug 2018 16:46:17 -0700 Subject: [PATCH] channeldb/db: check for db reversions on startup --- channeldb/db.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/channeldb/db.go b/channeldb/db.go index f3b6ede1d..3f3ced0b9 100644 --- a/channeldb/db.go +++ b/channeldb/db.go @@ -663,12 +663,24 @@ func (d *DB) syncVersions(versions []version) error { } } - // If the current database version matches the latest version number, - // then we don't need to perform any migrations. latestVersion := getLatestDBVersion(versions) log.Infof("Checking for schema update: latest_version=%v, "+ "db_version=%v", latestVersion, meta.DbVersionNumber) - if meta.DbVersionNumber == latestVersion { + + switch { + + // If the database reports a higher version that we are aware of, the + // user is probably trying to revert to a prior version of lnd. We fail + // here to prevent reversions and unintended corruption. + case meta.DbVersionNumber > latestVersion: + log.Errorf("Refusing to revert from db_version=%d to "+ + "lower version=%d", meta.DbVersionNumber, + latestVersion) + return ErrDBReversion + + // If the current database version matches the latest version number, + // then we don't need to perform any migrations. + case meta.DbVersionNumber == latestVersion: return nil }