Skip to content

Commit 7ac9148

Browse files
committed
itest: cover postgres network separation
1 parent a6f72e8 commit 7ac9148

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

itest/list_on_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,10 @@ var allTestCases = []*lntest.TestCase{
731731
Name: "payment migration",
732732
TestFunc: testPaymentMigration,
733733
},
734+
{
735+
Name: "postgres network separation",
736+
TestFunc: testPostgresNetworkSeparation,
737+
},
734738
{
735739
Name: "payment address mismatch",
736740
TestFunc: testWrongPaymentAddr,
@@ -787,6 +791,10 @@ var allTestCases = []*lntest.TestCase{
787791
Name: "estimate on chain fee auto selected inputs",
788792
TestFunc: testEstimateOnChainFeeAutoSelectedInputs,
789793
},
794+
{
795+
Name: "postgres network separation",
796+
TestFunc: testPostgresNetworkSeparation,
797+
},
790798
}
791799

792800
// appendPrefixed is used to add a prefix to each test name in the subtests
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package itest
2+
3+
import (
4+
"github.com/btcsuite/btcd/chaincfg"
5+
"github.com/lightningnetwork/lnd/lntest"
6+
"github.com/lightningnetwork/lnd/lntest/node"
7+
"github.com/lightningnetwork/lnd/sqldb"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
const networkMismatchErr = "database network mismatch"
12+
13+
// testPostgresNetworkSeparation verifies that lnd refuses to start when the
14+
// active Bitcoin network does not match the network stored in the postgres
15+
// metadata table. This prevents silent data corruption that would occur if a
16+
// user accidentally reused the same postgres DSN across different networks.
17+
//
18+
// The test flow is:
19+
// 1. Start lnd on regtest with native SQL enabled → first startup writes
20+
// "regtest" into the metadata table.
21+
// 2. Stop the node.
22+
// 3. Restart lnd with the same postgres DSN but switch to simnet → lnd must
23+
// detect the network mismatch and exit with an error.
24+
func testPostgresNetworkSeparation(ht *lntest.HarnessTest) {
25+
// This test is only relevant for the postgres backend with native SQL.
26+
alice := ht.NewNodeWithCoins("Alice", []string{"--db.use-native-sql"})
27+
28+
if alice.Cfg.DBBackend != node.BackendPostgres {
29+
ht.Skip("node not running with postgres backend")
30+
}
31+
32+
// Confirm the node started successfully on the initial network.
33+
require.NoError(ht, alice.Stop())
34+
35+
// Assert the mismatch condition directly at the store layer so this
36+
// test can't pass due to an unrelated startup failure.
37+
store, err := sqldb.NewPostgresStore(&sqldb.PostgresConfig{
38+
Dsn: alice.Cfg.PostgresDsn,
39+
Timeout: defaultTimeout,
40+
})
41+
require.NoError(ht, err)
42+
defer store.Close()
43+
44+
err = store.ValidateNetwork(ht.Context(), chaincfg.SimNetParams.Name)
45+
require.ErrorContains(ht, err, networkMismatchErr)
46+
47+
// Now restart alice but override the network to simnet. The DSN still
48+
// points at the same postgres database, so lnd should detect the
49+
// mismatch and refuse to start.
50+
//
51+
// We append --bitcoin.simnet and remove --bitcoin.regtest by
52+
// overriding via ExtraArgs (ExtraArgs are appended last and take
53+
// precedence over the generated args).
54+
alice.Cfg.NetParams = &chaincfg.SimNetParams
55+
alice.SetExtraArgs([]string{
56+
"--db.use-native-sql",
57+
"--bitcoin.simnet",
58+
"--bitcoin.node=neutrino",
59+
})
60+
61+
// StartLndCmd launches the process without waiting for it to become
62+
// ready, which is what we want since we expect it to exit early.
63+
require.NoError(ht, alice.StartLndCmd(ht.Context()))
64+
65+
// The process should exit with a non-zero status due to the network
66+
// mismatch error returned by ValidateNetwork.
67+
require.ErrorContains(ht, alice.WaitForProcessExit(), "exit status 1")
68+
}

0 commit comments

Comments
 (0)