Skip to content

sqldb: add network-mismatch safeguard for native-SQL backends#10684

Open
ziggie1984 wants to merge 6 commits intolightningnetwork:masterfrom
ziggie1984:postgres-network-separation
Open

sqldb: add network-mismatch safeguard for native-SQL backends#10684
ziggie1984 wants to merge 6 commits intolightningnetwork:masterfrom
ziggie1984:postgres-network-separation

Conversation

@ziggie1984
Copy link
Copy Markdown
Collaborator

@ziggie1984 ziggie1984 commented Mar 27, 2026

This PR prevents silent data corruption that can occur when a user accidentally points lnd at a native-SQL database that was initialised for a different Bitcoin network.

Changes

A new metadata table (migration 000014) stores a single network key on first startup. On every subsequent restart, lnd reads that value and refuses to start if it doesn't match the configured network, printing a clear error message with remediation steps.

The check is implemented on BaseDB and applies to both the PostgreSQL and SQLite backends. The itest exercises the full startup-refusal path for PostgreSQL. Unit tests covering the mismatch and idempotent-match cases run against both backends via the test_db_postgres || test_db_sqlite build tag.

Fixes: #10354

@ziggie1984 ziggie1984 force-pushed the postgres-network-separation branch from d58fddd to 84fa870 Compare March 27, 2026 21:32
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a safety mechanism to ensure that a database initialized for one Bitcoin network cannot be accidentally reused for another. By persisting the network identifier in a new metadata table during the first startup, the system can now detect and block incompatible configurations, providing clear error messages to the user instead of allowing silent data corruption.

Highlights

  • Network Mismatch Safeguard: Introduced a new metadata table to persist the Bitcoin network upon first database initialization, preventing silent data corruption from cross-network database reuse.
  • Validation Logic: Implemented a mandatory network validation check in the database startup sequence for both PostgreSQL and SQLite backends.
  • Testing: Added comprehensive unit tests for network validation and a new integration test specifically exercising the startup-refusal path for PostgreSQL.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism to prevent silent data corruption by ensuring that the database is not reused across different Bitcoin networks. It adds a metadata table to store the network name on first startup and validates it on subsequent restarts for both PostgreSQL and SQLite backends. Feedback was provided regarding the accuracy of a comment in config_builder.go and a suggestion to improve code organization by moving the BaseDB.ValidateNetwork implementation to interfaces.go.

@ziggie1984 ziggie1984 force-pushed the postgres-network-separation branch from 84fa870 to 752ad80 Compare March 27, 2026 21:44
@saubyk saubyk added this to v0.21 Mar 27, 2026
@saubyk saubyk moved this to In progress in v0.21 Mar 27, 2026
@ziggie1984 ziggie1984 force-pushed the postgres-network-separation branch 2 times, most recently from 309ee14 to 3e33be4 Compare March 28, 2026 10:06
-- The metadata table stores key/value pairs that describe properties of the
-- database. It is used to persist and validate invariants across restarts,
-- such as which Bitcoin network the database was initialised for.
CREATE TABLE IF NOT EXISTS metadata (
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not something more specific here?

Given that we do already have the opauqe KV layer from the initial version of the db.

@ziggie1984 ziggie1984 force-pushed the postgres-network-separation branch 4 times, most recently from 3041b52 to 02ec43f Compare March 31, 2026 09:45
@ziggie1984 ziggie1984 requested a review from Roasbeef March 31, 2026 09:46
@gijswijs gijswijs self-requested a review March 31, 2026 09:50
@ziggie1984 ziggie1984 force-pushed the postgres-network-separation branch from 02ec43f to 65d3faf Compare March 31, 2026 16:36
@ziggie1984 ziggie1984 force-pushed the postgres-network-separation branch from 65d3faf to b927793 Compare March 31, 2026 23:05
This is in particular important when running with a postgres
backend.

This only works if you run LND with the native sql flag but
people should run it with this flag from 21 on anyways.
@ziggie1984 ziggie1984 force-pushed the postgres-network-separation branch from b927793 to 5b917c4 Compare April 1, 2026 06:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

[feature]: Network Separation Issue for PostgreSQL Backend

3 participants