@@ -1430,6 +1430,74 @@ func importWatchOnlyAccounts(wallet *wallet.Wallet,
1430
1430
return nil
1431
1431
}
1432
1432
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
+
1433
1501
// initNeutrinoBackend inits a new instance of the neutrino light client
1434
1502
// backend given a target chain directory to store the chain state.
1435
1503
func initNeutrinoBackend (ctx context.Context , cfg * Config , chainDir string ,
@@ -1471,8 +1539,26 @@ func initNeutrinoBackend(ctx context.Context, cfg *Config, chainDir string,
1471
1539
lncfg .SqliteNeutrinoDBName , lncfg .NSNeutrinoDB ,
1472
1540
)
1473
1541
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
+
1474
1560
default :
1475
- dbName := filepath .Join (dbPath , "neutrino.db" )
1561
+ dbName := filepath .Join (dbPath , lncfg . NeutrinoDBName )
1476
1562
db , err = walletdb .Create (
1477
1563
kvdb .BoltBackendName , dbName , ! cfg .SyncFreelist ,
1478
1564
cfg .DB .Bolt .DBTimeout , false ,
0 commit comments