Skip to content

sqldb+graph/db: add node related tables and implement some node CRUD#9866

Merged
guggero merged 6 commits intolightningnetwork:masterfrom
ellemouton:graphSQL7-nodes-tables
May 28, 2025
Merged

sqldb+graph/db: add node related tables and implement some node CRUD#9866
guggero merged 6 commits intolightningnetwork:masterfrom
ellemouton:graphSQL7-nodes-tables

Conversation

@ellemouton
Copy link
Collaborator

(Re-opening #9824 - base branch was accidentally deleted)

In this PR, the following schemas are defined for storing graph nodes:

(NOTE: the block_height column is only shown as an example to show how v2 data might fit into this schema. It is not included in the initial schema definition).

nodes (4)

  • nodes: stores the main info about the node from a node_announcement. Note that we might sometimes insert a "shell node" for when we get a channel announcement that points to a node that we dont yet have a record for. In this case, we will only have the public key of the node along with the gossip version it was advertised on. So the version:pub_key pair is always unique.
  • node_addresses: normalised node addresses.
  • node_extra_types: stores the normalised TLV records of a node_announcement for any fields that we dont explicitly store in the nodes table.
  • node_features: stores the normalised features advertised in a node announcement.
  • source_nodes: we store a pointer to an entry in the nodes table to indicate which node belongs to this node. NOTE: there will be an entry here per gossip protocol that we are aware of.

The following methods are implemented on the SQLStore:

  • AddLightningNode
  • FetchLightningNode
  • HasLightningNode
  • AddrsForNode
  • DeleteLightningNode
  • FetchNodeFeatures
  • LookupAlias
  • NodeUpdatesInHorizon
  • SourceNode
  • SetSourceNode

This then lets us run a number of existing unit tests against the new SQLStore backend. Namely:

  • TestNodeInsertionAndDeletion
  • TestLightningNodePersistence
  • TestAliasLookup
  • TestNodeUpdatesInHorizon
  • TestSourceNode

Part of #9795

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 26, 2025

Important

Review skipped

Auto reviews are limited to specific labels.

🏷️ Labels to auto review (1)
  • llm-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@guggero guggero self-requested a review May 26, 2025 08:26
@ellemouton ellemouton force-pushed the graphSQL7-nodes-tables branch 3 times, most recently from ac4106d to d96416a Compare May 26, 2025 12:12
@ellemouton ellemouton self-assigned this May 26, 2025
@ellemouton ellemouton added this to the v0.20.0 milestone May 26, 2025
@saubyk saubyk added this to lnd v0.20 May 26, 2025
@saubyk saubyk moved this to In progress in lnd v0.20 May 26, 2025
@ellemouton ellemouton requested a review from bhandras May 27, 2025 05:44
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Very nice, LGTM 🎉

}

// NewReadTx creates a new read transaction option set.
func NewReadTx() TxOptions {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could return a pointer here, then we could use NewReadTx() directly in the call to ExecTx.
And we could move that to the sqldb for re-use (we define too many different types of them in tapd for example with no real reason). And perhaps shorten to ReadTx() and also add a WriteTx()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

did the pointer part here.

for the re-use point: gonna do that in a separate PR that does it for both here and invoices if that's ok

_, node, err = getNodeByPubKey(ctx, db, pubKey)

return err
}, func() {})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a sqldb.NoOpReset and use that here? Then we can more easily find and replace them once we turn this into a functional option in sqldbv2.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

gonna do this in a separate PR that addresses it both here and for invoices if that's ok

Copy link
Collaborator

@bhandras bhandras left a comment

Choose a reason for hiding this comment

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

Excellent work! Looks good, just a few nits and comments.

},
}

return s.nodeScheduler.Execute(ctx, r)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we need to be careful here, as if there're two upserts in the same batch, they might get reordered?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

should be fine right? cause it will get added in order to the slice held by the batcher and executed in that order yeah?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay discussed this offline, now with the addition that upserting a node will check the last update time we should be fine.

In this commit, the various SQL schemas required to store graph node
related data is defined. Specifically, the following tables are defined:

- nodes
- node_extra_types
- node_features
- node_addresses
@ellemouton ellemouton force-pushed the graphSQL7-nodes-tables branch from d96416a to 3abea08 Compare May 27, 2025 14:51
In this commit, we add the various sqlc queries that we need in order
to implement the following V1Store methods:

- AddLightningNode
- FetchLightningNode
- HasLightningNode
- AddrsForNode
- DeleteLightningNode
- FetchNodeFeatures

These are implemented by SQLStore which then lets us use the SQLStore
backend for the following unit tests:

- TestNodeInsertionAndDeletion
- TestLightningNodePersistence
In this commit, we let the SQLStore implement LookupAlias. This then
lets us run the TestAliasLookup unit test against the SQL backends.
In this commit we add the necessary SQL queries and then implement the
SQLStore's NodeUpdatesInHorizon method. This lets us run the
TestNodeUpdatesInHorizon unit tests against SQL backends.
In this commit, we add the `source_nodes` table. It points to entries in
the `nodes` table. This table will store one entry per protocol version
that we are announcing a node_announcement on.

With this commit, we can run the TestSourceNode unit test against our
SQL backends.
@ellemouton ellemouton force-pushed the graphSQL7-nodes-tables branch from 3abea08 to efc04b9 Compare May 27, 2025 16:37
Copy link
Collaborator

@bhandras bhandras left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

@ellemouton
Copy link
Collaborator Author

cc @guggero for override merge 🙏 (unit-race flake is an RBF test)

@guggero guggero merged commit 8e96bd0 into lightningnetwork:master May 28, 2025
35 of 38 checks passed
@github-project-automation github-project-automation bot moved this from In progress to Done in lnd v0.20 May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants