Repository navigation
graph/db: unwrap dns addresses from opaque ones during migration #10162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yyforyongyu
merged 3 commits into
lightningnetwork:master
from
ellemouton:graphMigUnwrapDNSAddrs
Sep 4, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,18 +260,17 @@ func migrateNodes(ctx context.Context, cfg *sqldb.QueryConfig, | |
| "opaque data for node %x: %w", pub, err) | ||
| } | ||
|
|
||
| if err = maybeOverrideNodeAddresses(node); err != nil { | ||
| skipped++ | ||
| log.Warnf("Skipping migration of node %x with invalid "+ | ||
| "address (%v): %v", pub, node.Addresses, err) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| count++ | ||
| chunk++ | ||
|
|
||
| // TODO(elle): At this point, we should check the loaded node | ||
| // to see if we should extract any DNS addresses from its | ||
| // opaque type addresses. This is expected to be done in: | ||
| // https://github.com/lightningnetwork/lnd/pull/9455. | ||
| // This TODO is being tracked in | ||
| // https://github.com/lightningnetwork/lnd/issues/9795 as this | ||
| // must be addressed before making this code path active in | ||
| // production. | ||
|
|
||
| // Write the node to the SQL database. | ||
| id, err := insertNodeSQLMig(ctx, sqlDB, node) | ||
| if err != nil { | ||
|
|
@@ -323,12 +322,82 @@ func migrateNodes(ctx context.Context, cfg *sqldb.QueryConfig, | |
| } | ||
|
|
||
| log.Infof("Migrated %d nodes from KV to SQL in %v (skipped %d nodes "+ | ||
| "due to invalid TLV streams)", count, time.Since(totalTime), | ||
| "due to invalid TLV streams or invalid addresses)", count, | ||
| time.Since(totalTime), | ||
|
|
||
| skipped) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // maybeOverrideNodeAddresses checks if the node has any opaque addresses that | ||
| // can be parsed. If so, it replaces the node's addresses with the parsed | ||
| // addresses. If the address is unparseable, it returns an error. | ||
| func maybeOverrideNodeAddresses(node *models.LightningNode) error { | ||
| // In the majority of cases, the number of node addresses will remain | ||
| // unchanged, so we pre-allocate a slice of the same length. | ||
| addrs := make([]net.Addr, 0, len(node.Addresses)) | ||
|
|
||
| // Iterate over each address in search of any opaque addresses that we | ||
| // can inspect. | ||
| for _, addr := range node.Addresses { | ||
| opaque, ok := addr.(*lnwire.OpaqueAddrs) | ||
| if !ok { | ||
| // Any non-opaque address is left unchanged. | ||
| addrs = append(addrs, addr) | ||
| continue | ||
| } | ||
|
|
||
| // For each opaque address, we'll now attempt to parse out any | ||
| // known addresses. We'll do this in a loop, as it's possible | ||
| // that there are several addresses encoded in a single opaque | ||
| // address. | ||
| payload := opaque.Payload | ||
| for len(payload) > 0 { | ||
|
ellemouton marked this conversation as resolved.
|
||
| var ( | ||
| r = bytes.NewReader(payload) | ||
| numAddrBytes = uint16(len(payload)) | ||
| ) | ||
| byteRead, readAddr, err := lnwire.ReadAddress( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| r, numAddrBytes, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
|
bhandras marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // If we were able to read an address, we'll add it to | ||
| // our list of addresses. | ||
| if readAddr != nil { | ||
| addrs = append(addrs, readAddr) | ||
| } | ||
|
|
||
| // If the address we read was an opaque address, it | ||
| // means we've hit an unknown address type, and it has | ||
| // consumed the rest of the payload. We can break out | ||
| // of the loop. | ||
| if _, ok := readAddr.(*lnwire.OpaqueAddrs); ok { | ||
| break | ||
| } | ||
|
|
||
| // If we've read all the bytes, we can also break. | ||
| if byteRead >= numAddrBytes { | ||
| break | ||
| } | ||
|
|
||
| // Otherwise, we'll advance our payload slice and | ||
| // continue. | ||
| payload = payload[byteRead:] | ||
| } | ||
| } | ||
|
|
||
| // Override the node addresses if we have any. | ||
| if len(addrs) != 0 { | ||
| node.Addresses = addrs | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
ellemouton marked this conversation as resolved.
|
||
|
|
||
| // migrateSourceNode migrates the source node from the KV backend to the | ||
| // SQL database. | ||
| func migrateSourceNode(ctx context.Context, kvdb kvdb.Backend, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: Why are we skipping node migration when an invalid DNS address is encountered? Can't we just skip the invalid network address? In the worst-case scenario, the remaining node information is still valid and should be eligible for migration
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I read more the code perhaps invalid DNS address to propagate that violates specification probably worthwhile to skip the entire node migration because of it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah exactly :) i originally had it that way but then decided to just skip the nodes so that we are not propagating invalid nodes.
Also, this is a far edge case & in all tests i've run, i have not come across any nodes with invalid DNS addrs. So this really is just an edge case. We could really go either way and it wont matter much i think.