What changed, and why it matters
This commit adds a startup safety check in LND (a Bitcoin Lightning Network node implementation) that verifies a native SQL database was created for the same Bitcoin network (mainnet, testnet, etc.) the node is currently configured to use. Without this check, a user could accidentally point LND at the wrong database—such as reusing a mainnet database while running on testnet—which could silently corrupt payment channel data. The change makes LND refuse to start rather than risk that corruption.
Treat as a hardening/data-integrity fix. Operators using native SQL backends (Postgres, SQLite, etc.) should ensure their databases were initialized for the network they intend to run; after this patch, mismatches will cause startup failures rather than silent corruption. No immediate emergency response is warranted because the issue is primarily self-inflicted misconfiguration, not a remote attack vector.
Security signals we found
Prevents silent cross-network database reuse that could corrupt channel state
Adds explicit startup abort on network mismatch for native SQL backends
Targets data-integrity/confused-deputy style operational risk rather than remote exploitation
Evidence from the diff
In config_builder.go, the DefaultDatabaseBuilder.BuildDatabase method now constructs a chainparams.Store from the native SQL base DB and calls ValidateNetwork against d.cfg.ActiveNetParams.Params before creating the invoice, graph, and payments SQL stores. This validates the chain parameters recorded in the SQL backend match the active network. If validation fails, the database is cleaned up and startup aborts with an error. The commit is purely a defensive validation and does not change how data is stored or processed once validated.
Changed components
lnd/config_builder.goDefaultDatabaseBuilder.BuildDatabasenative SQL store initializationchainparams.Store validationInspect captured patch +24 / −1
diff --git a/config_builder.go b/config_builder.go
index 0f563d6..a448235 100644
--- a/config_builder.go
+++ b/config_builder.go
@@ -30,6 +30,7 @@ import (
"github.com/lightninglabs/neutrino/pushtx"
"github.com/lightningnetwork/lnd/blockcache"
"github.com/lightningnetwork/lnd/chainntnfs"
+ "github.com/lightningnetwork/lnd/chainparams"
"github.com/lightningnetwork/lnd/chainreg"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/clock"
@@ -1237,8 +1238,28 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
// With the DB ready and migrations applied, we can now create
// the base DB and transaction executor for the native SQL
- // invoice store.
+ // stores.
baseDB := dbs.NativeSQLStore.GetBaseDB()
+
+ // Validate that the database was initialised for the same
+ // network as the currently active network. This catches cases
+ // where a user accidentally reuses a database (e.g. via a
+ // postgres DSN or by copying a file) across different networks
+ // (e.g. mainnet → testnet), which would otherwise lead to
+ // silent data corruption. This check applies to all native SQL
+ // backends.
+ chainParamsStore := chainparams.NewStore(baseDB)
+ err = chainParamsStore.ValidateNetwork(
+ ctx, d.cfg.ActiveNetParams.Params,
+ )
+ if err != nil {
+ cleanUp()
+ d.logger.Error(err)
+
+ return nil, nil, err
+ }
+
+ // Create the invoice store.
invoiceExecutor := sqldb.NewTransactionExecutor(
baseDB, func(tx *sql.Tx) invoices.SQLInvoiceQueries {
return baseDB.WithTx(tx)
@@ -1251,6 +1272,7 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
dbs.InvoiceDB = sqlInvoiceDB
+ // Create the graph store.
graphExecutor := sqldb.NewTransactionExecutor(
baseDB, func(tx *sql.Tx) graphdb.SQLQueries {
return baseDB.WithTx(tx)
@@ -1272,6 +1294,7 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
return nil, nil, err
}
+ // Create the payments store.
paymentsExecutor := sqldb.NewTransactionExecutor(
baseDB, func(tx *sql.Tx) paymentsdb.SQLQueries {
return baseDB.WithTx(tx)
Why this scored 58/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.