Skip to content

Commit 5348232

Browse files
committed
itest: cover postgres network separation
1 parent cee991b commit 5348232

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

itest/list_on_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,10 @@ var allTestCases = []*lntest.TestCase{
787787
Name: "estimate on chain fee auto selected inputs",
788788
TestFunc: testEstimateOnChainFeeAutoSelectedInputs,
789789
},
790+
{
791+
Name: "postgres network separation",
792+
TestFunc: testPostgresNetworkSeparation,
793+
},
790794
}
791795

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

lntest/harness.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,12 @@ func (h *HarnessTest) IsNeutrinoBackend() bool {
14381438
return h.manager.chainBackend.Name() == NeutrinoBackendName
14391439
}
14401440

1441+
// IsPostgresBackend returns true if the test harness is configured to use a
1442+
// Postgres database backend.
1443+
func (h *HarnessTest) IsPostgresBackend() bool {
1444+
return h.manager.dbBackend == node.BackendPostgres
1445+
}
1446+
14411447
// fundCoins attempts to send amt satoshis from the internal mining node to the
14421448
// targeted lightning node. The confirmed boolean indicates whether the
14431449
// transaction that pays to the target should confirm. For neutrino backend,

0 commit comments

Comments
 (0)