Skip to content

Commit bec84e1

Browse files
authored
Merge pull request lightningnetwork#9674 from ziggie1984/small-neutrino-fix
Move neutrino db also to postgres when using postgres as a backend
2 parents 905cf65 + 417d2de commit bec84e1

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

config_builder.go

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,74 @@ func importWatchOnlyAccounts(wallet *wallet.Wallet,
14301430
return nil
14311431
}
14321432

1433+
// handleNeutrinoPostgresDBMigration handles the migration of the neutrino db
1434+
// to postgres. Initially we kept the neutrino db in the bolt db when running
1435+
// with kvdb postgres backend. Now now move it to postgres as well. However we
1436+
// need to make a distinction whether the user migrated the neutrino db to
1437+
// postgres via lndinit or not. Currently if the db is not migrated we start
1438+
// with a fresh db in postgres.
1439+
//
1440+
// TODO(ziggie): Also migrate the db to postgres in case it is still not
1441+
// migrated ?
1442+
func handleNeutrinoPostgresDBMigration(dbName, dbPath string,
1443+
cfg *Config) error {
1444+
1445+
if !lnrpc.FileExists(dbName) {
1446+
return nil
1447+
}
1448+
1449+
// Open bolt db to check if it is tombstoned. If it is we assume that
1450+
// the neutrino db was successfully migrated to postgres. We open it
1451+
// in read-only mode to avoid long db open times.
1452+
boltDB, err := kvdb.Open(
1453+
kvdb.BoltBackendName, dbName, true,
1454+
cfg.DB.Bolt.DBTimeout, true,
1455+
)
1456+
if err != nil {
1457+
return fmt.Errorf("failed to open bolt db: %w", err)
1458+
}
1459+
defer boltDB.Close()
1460+
1461+
isTombstoned := false
1462+
err = boltDB.View(func(tx kvdb.RTx) error {
1463+
_, err = channeldb.CheckMarkerPresent(
1464+
tx, channeldb.TombstoneKey,
1465+
)
1466+
1467+
return err
1468+
}, func() {})
1469+
if err == nil {
1470+
isTombstoned = true
1471+
}
1472+
1473+
if isTombstoned {
1474+
ltndLog.Infof("Neutrino Bolt DB is tombstoned, assuming " +
1475+
"database was successfully migrated to postgres")
1476+
1477+
return nil
1478+
}
1479+
1480+
// If the db is not tombstoned, we remove the files and start fresh with
1481+
// postgres. This is the case when a user was running lnd with the
1482+
// postgres backend from the beginning without migrating from bolt.
1483+
ltndLog.Infof("Neutrino Bolt DB found but NOT tombstoned, removing " +
1484+
"it and starting fresh with postgres")
1485+
1486+
filesToRemove := []string{
1487+
filepath.Join(dbPath, "block_headers.bin"),
1488+
filepath.Join(dbPath, "reg_filter_headers.bin"),
1489+
dbName,
1490+
}
1491+
1492+
for _, file := range filesToRemove {
1493+
if err := os.Remove(file); err != nil {
1494+
ltndLog.Warnf("Could not remove %s: %v", file, err)
1495+
}
1496+
}
1497+
1498+
return nil
1499+
}
1500+
14331501
// initNeutrinoBackend inits a new instance of the neutrino light client
14341502
// backend given a target chain directory to store the chain state.
14351503
func initNeutrinoBackend(ctx context.Context, cfg *Config, chainDir string,
@@ -1471,8 +1539,26 @@ func initNeutrinoBackend(ctx context.Context, cfg *Config, chainDir string,
14711539
lncfg.SqliteNeutrinoDBName, lncfg.NSNeutrinoDB,
14721540
)
14731541

1542+
case cfg.DB.Backend == kvdb.PostgresBackendName:
1543+
dbName := filepath.Join(dbPath, lncfg.NeutrinoDBName)
1544+
1545+
// This code needs to be in place because we did not start
1546+
// the postgres backend for neutrino at the beginning. Now we
1547+
// are also moving it into the postgres backend so we can phase
1548+
// out the bolt backend.
1549+
err = handleNeutrinoPostgresDBMigration(dbName, dbPath, cfg)
1550+
if err != nil {
1551+
return nil, nil, err
1552+
}
1553+
1554+
postgresConfig := lncfg.GetPostgresConfigKVDB(cfg.DB.Postgres)
1555+
db, err = kvdb.Open(
1556+
kvdb.PostgresBackendName, ctx, postgresConfig,
1557+
lncfg.NSNeutrinoDB,
1558+
)
1559+
14741560
default:
1475-
dbName := filepath.Join(dbPath, "neutrino.db")
1561+
dbName := filepath.Join(dbPath, lncfg.NeutrinoDBName)
14761562
db, err = walletdb.Create(
14771563
kvdb.BoltBackendName, dbName, !cfg.SyncFreelist,
14781564
cfg.DB.Bolt.DBTimeout, false,

docs/release-notes/release-notes-0.19.0.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ close transaction.
150150
initial historical sync may be blocked due to a race condition in handling the
151151
syncer's internal state.
152152

153-
154-
155153
* [The max fee rate](https://github.com/lightningnetwork/lnd/pull/9491) is now
156154
respected when a coop close is initiated. Before the max fee rate would only
157155
be effective for the remote party in the negotiation.
@@ -466,6 +464,9 @@ the on going rate we'll permit.
466464
* [Establish a base DB version even if it is not yet
467465
tracked](https://github.com/lightningnetwork/lnd/pull/9647).
468466

467+
* [When running with neutrino as a backend with the kv-db backend `postgres`
468+
selected use postgres for the neutrino.db store](https://github.com/lightningnetwork/lnd/pull/9674).
469+
469470
## Code Health
470471

471472
* A code refactor that [moves all the graph related DB code out of the

lncfg/db.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
TowerClientDBName = "wtclient.db"
2626
TowerServerDBName = "watchtower.db"
2727
WalletDBName = "wallet.db"
28+
NeutrinoDBName = "neutrino.db"
2829

2930
SqliteChannelDBName = "channel.sqlite"
3031
SqliteChainDBName = "chain.sqlite"

0 commit comments

Comments
 (0)