Skip to content
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

multi: make reassignment of alias channel edge atomic #8777

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

ziggie1984
Copy link
Collaborator

@ziggie1984 ziggie1984 commented May 24, 2024

When the option SCID is used we need to make sure we update the
channel data in an atomic way so that we don't miss any updates
by our peer because we rate limit ChanUpdate msg which can cause some side effects revealed in #8582

Prerequisite for: #8582

EDIT:

So the main rational behind this change is, that we need to make sure that in case a channel is an alias channel (zero-conf or non-zeroconf) that we "ReAdd" the edge info atomically. This guarantees that we delete all data which might still have the alias channel as a channel id and moreover we need to do it atomically so that we do not receive updates in the meantime and therefore rate-limit the gossip-messages.

Copy link
Contributor

coderabbitai bot commented May 24, 2024

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.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

@ziggie1984 ziggie1984 added this to the v0.18.1 milestone May 24, 2024
@ziggie1984 ziggie1984 self-assigned this May 24, 2024
@ziggie1984
Copy link
Collaborator Author

@Crypt-iQ could you take a look and evaluate if this is the right direction I am going, it relates to the atomic deletion of the option-scid channels also discussed here(#7759 (comment))

Thank you in advance 🙏

@Crypt-iQ Crypt-iQ self-requested a review May 29, 2024 18:50
@Crypt-iQ
Copy link
Collaborator

@Crypt-iQ could you take a look and evaluate if this is the right direction I am going, it relates to the atomic deletion of the option-scid channels also discussed here(#7759 (comment))

Thank you in advance 🙏

Yes this approach looks good

@saubyk saubyk modified the milestones: v0.18.1, 0.19.0 Jun 20, 2024
@ziggie1984 ziggie1984 force-pushed the removal-of-edge-atomic branch 2 times, most recently from 655f5b0 to ef2a738 Compare December 16, 2024 14:13
@ziggie1984 ziggie1984 marked this pull request as ready for review December 16, 2024 14:14
@ziggie1984 ziggie1984 requested a review from starius December 16, 2024 14:14
@ziggie1984 ziggie1984 added the size/kilo medium, proper context needed, less than 1000 lines label Dec 16, 2024
Copy link
Collaborator

@starius starius left a comment

Choose a reason for hiding this comment

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

LGTM 🎉
I left comments, mostly nits. One important question is if the announcement is still broadcasted to the channel peer as before (via addToGraph). See the question in the first commit.

funding/manager.go Outdated Show resolved Hide resolved
funding/manager.go Outdated Show resolved Hide resolved
graph/db/graph.go Outdated Show resolved Hide resolved
graph/db/graph_test.go Show resolved Hide resolved
graph/db/graph.go Outdated Show resolved Hide resolved

// Define the new channel id (under real conditions this is the new
// confirmed channel id).
newChanSCID := rand.Uint64()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's make it more deterministic and exclude (small but real) probability of newChanSCID == oldSCID:

newChanSCID := oldSCID

BTW what happens if ReAddChannelEdge is called with newChanSCID == oldSCID? IIUC, it succeeds. But this is not expected that newChanSCID == oldSCID, so maybe we should change this to fail with an error "new channel ID is equal to old channel ID" and add a test for such edge case here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no it should succeed aslo with the same SCID, in case of an option-scid channel but not zeroconf.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

But made the newChanID deterministic:

newChanSCID := oldSCID + 1

Copy link
Collaborator

Choose a reason for hiding this comment

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

no it should succeed aslo with the same SCID, in case of an option-scid channel but not zeroconf.

I propose to unit test that when the same SCID is passed, ReAddChannelEdge succeeds in case of an option-scid channel, but fails for a zeroconf channel.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hmm I think I was not clear in my description what I meant is that for zeroconf-alias channels the channelid for the reassignment is always different,
For non-zeroconf alias channels the ChannelID for the reassignment is always the same, we do this only to clear the graph data storage to remove potential ChanUpdates which where based on the alias not the channelID. Which might happen in the period between channelconfirmation (usually 3 blocks) and the 6 blocks public gossip limit.

graph/db/graph_test.go Outdated Show resolved Hide resolved
graph/db/graph_test.go Outdated Show resolved Hide resolved
lnwire/channel_ready.go Show resolved Hide resolved
docs/release-notes/release-notes-0.19.0.md Outdated Show resolved Hide resolved
@ziggie1984 ziggie1984 force-pushed the removal-of-edge-atomic branch 2 times, most recently from 0784e6b to 9ddb66e Compare December 17, 2024 14:30
@yyforyongyu yyforyongyu self-requested a review December 17, 2024 19:41
@ziggie1984 ziggie1984 changed the title multi: make deletion of edge atomic. multi: make reassignment of channel edge atomic Dec 17, 2024
@ziggie1984 ziggie1984 force-pushed the removal-of-edge-atomic branch 4 times, most recently from 71f2dc6 to fd595ba Compare December 17, 2024 20:34
@ziggie1984 ziggie1984 changed the title multi: make reassignment of channel edge atomic multi: make reassignment of alias channel edge atomic Dec 17, 2024
err = f.addToGraph(
c, &confChan.shortChanID, nil, ourPolicy,
aliasScid := c.ShortChanID()
confirmedScid := confChan.shortChanID
Copy link
Collaborator

Choose a reason for hiding this comment

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

The variables can be used above in the ReAssignSCID call and below in the sendChanUpdate call.


// Define the new channel id (under real conditions this is the new
// confirmed channel id).
newChanSCID := rand.Uint64()
Copy link
Collaborator

Choose a reason for hiding this comment

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

no it should succeed aslo with the same SCID, in case of an option-scid channel but not zeroconf.

I propose to unit test that when the same SCID is passed, ReAddChannelEdge succeeds in case of an option-scid channel, but fails for a zeroconf channel.

Copy link
Collaborator

@starius starius left a comment

Choose a reason for hiding this comment

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

LGTM! 🌴
The code looks good!
Few suggestions about tests coverage and removing copy-paste.

Comment on lines +3746 to +3747
err = f.sendChanUpdate(
completeChan, &baseScid, ourPolicy,
Copy link
Collaborator

Choose a reason for hiding this comment

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

The unit tests pass even with this call commented out. I propose to cover this case of announcement sending in unit tests as well.

Another idea. DeleteSixConfs, ReAssignSCID, and sendChanUpdate is a common sequence in both cases where they are used. Does it make sense to create a method doing DeleteSixConfs, ReAssignSCID, and sendChanUpdate and reuse it in both cases? It case there will be a third case in the future, there will be less chances to use incorrectly (e.g. to forget to call sendChanUpdate).

funding/manager.go Outdated Show resolved Hide resolved
funding/manager.go Outdated Show resolved Hide resolved
funding/manager.go Outdated Show resolved Hide resolved
funding/manager.go Outdated Show resolved Hide resolved
funding/manager.go Outdated Show resolved Hide resolved
funding/manager.go Outdated Show resolved Hide resolved
funding/manager.go Outdated Show resolved Hide resolved
funding/manager.go Outdated Show resolved Hide resolved
funding/manager.go Outdated Show resolved Hide resolved
Copy link
Member

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

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

Thanks for the fix! I think it may explain some of the flakes I've seen from the itest. Meanwhile, could you do a rebase on #9368 and change the base branch to that one? This way we can get a clean CI build and I wanna analyze the logs to understand the changes here.

funding/manager_test.go Show resolved Hide resolved
funding/manager_test.go Show resolved Hide resolved
funding/manager.go Show resolved Hide resolved
funding/manager.go Outdated Show resolved Hide resolved
// sendChanUpdate sends a ChannelUpdate to the gossiper which is as a
// consequence sent to the peer.
//
// TODO(ziggie): Refactor the gossip msges so that not always all msges have
Copy link
Member

Choose a reason for hiding this comment

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

msges -> msgs

Copy link
Member

Choose a reason for hiding this comment

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

unresolving as it's not addressed

"update: %v", err)
}
}
case <-f.quit:
Copy link
Member

Choose a reason for hiding this comment

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

nit: add a newline above

Copy link
Member

Choose a reason for hiding this comment

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

not addressed

funding/manager.go Show resolved Hide resolved
funding/manager.go Show resolved Hide resolved
lnwire/channel_ready_test.go Outdated Show resolved Hide resolved
fwdMinHTLC, fwdMaxHTLC := f.extractAnnounceParams(completeChan)

ann, err := f.newChanAnnouncement(
chanAnn, err := f.newChanAnnouncement(
Copy link
Member

Choose a reason for hiding this comment

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

I think this belongs to another PR - since the PR is aimed at fixing the atomic behavior, and this commit is touching a different aspect (gossip), and I think there's already a de-dup logic in our gossiper?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

true, I was more referring to the creation of these messages, we do not always use all of them so I decided to not create them in the first place.

Copy link
Member

Choose a reason for hiding this comment

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

Sure - in the case we should fix it in a different PR, I also wanna this change as this commit also fixes the confusing names such as newChanAnnouncement. Plus I don't see any dependencies between the atomic fix and this depup fix?

@ziggie1984 ziggie1984 force-pushed the removal-of-edge-atomic branch 2 times, most recently from 3a37dc6 to a13596d Compare December 19, 2024 14:05
@ziggie1984 ziggie1984 changed the base branch from master to yy-waiting-on-merge December 19, 2024 14:11
@ziggie1984 ziggie1984 force-pushed the removal-of-edge-atomic branch 3 times, most recently from 76fb9af to 7877c7d Compare December 19, 2024 17:21
@yyforyongyu yyforyongyu force-pushed the yy-waiting-on-merge branch 2 times, most recently from 9d62968 to d259917 Compare December 20, 2024 05:06
// DeleteAliasEdge allows the Manager to delete an alias channel edge
// from the graph. It also returns our local to-be-deleted policy.
DeleteAliasEdge func(scid lnwire.ShortChannelID) (
// ReassignSCID allows the Manager to assign a new SCID to an
Copy link
Member

Choose a reason for hiding this comment

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

ReassignSCID -> ReAssignSCID

@@ -1206,6 +1206,16 @@ func (b *Builder) processUpdate(msg interface{},
"chan_id=%v", msg.ChannelID)
}

// Look up the funding pk script so that we can register the
Copy link
Member

Choose a reason for hiding this comment

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

In case we assume valid channels or have alias channels we make
sure we also add the channel point to the block filter so that
we are notified if the channel point is spent in a block.

Did you encounter this or can you demonstrate the behavior in a test? I think we have itests for the alias channels and the UTXO filter seems to work fine?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes the zeroconf channel test is failing if we don't do this because we would never be notified if the channel closed because for alias channels we are not funneling through the channel_announcement again as before, we are just swapping it in the ReAssignmentSCID method, before we would send it again via addToGraph.

fwdMinHTLC, fwdMaxHTLC := f.extractAnnounceParams(completeChan)

ann, err := f.newChanAnnouncement(
chanAnn, err := f.newChanAnnouncement(
Copy link
Member

Choose a reason for hiding this comment

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

Sure - in the case we should fix it in a different PR, I also wanna this change as this commit also fixes the confusing names such as newChanAnnouncement. Plus I don't see any dependencies between the atomic fix and this depup fix?

"update: %v", err)
}
}
case <-f.quit:
Copy link
Member

Choose a reason for hiding this comment

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

not addressed

// reassigned with the new confirmed scid. Moreover channel
// updates with the alias scid are removed so that we do not
// relay them to the broader network.
ourPolicy, err := f.cfg.ReAssignSCID(
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this fixes the issue mentioned in the PR description re the possible race. We can still receive an channel_update from the peer while we are in the process of re-assigning the SCID. Meanwhile, in the gossiper, if we receive a channel_update that we don't understand, it's put in the prematureChannelUpdates and will be processed later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I described the problem in detail here: ef6cd7a

Looking back I think this issue could have been solved by deleting the rejectCache before adding the edge via the addToGraph function. Tho I think having the deletion and addition atomic is the better desing wdyt ?

// addToGraph. This is because the peer may have
// sent us a ChannelUpdate with an alias and we don't
// want to relay this.
ourPolicy, err := f.cfg.DeleteAliasEdge(baseScid)
Copy link
Member

Choose a reason for hiding this comment

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

I think the actually issue is we call DeleteSixConfs too early - if we only call it once the edge is recreated, we can fix the case where the peer sends a channel_update using alias?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm I don't think so, we don't want this ChanUpdate with the Alias so we need to delete the mapping earlier so we do not risk adding the Alias ChanUpdate after we readded the Edge ?

// database and adds the new edge to guarantee atomicity.
// This is important for option-scid-alias channels which may change its SCID
// over the course of its lifetime (e.g., public zero-conf channels).
func (c *ChannelGraph) ReAddChannelEdge(
Copy link
Member

Choose a reason for hiding this comment

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

A typical flow for adding an edge,

  1. ChannelAnnouncement msg received by the gossiper
  2. the gossiper validates it and sends it to the channel graph
  3. the channel graph validates and adds it to the graph db

And a similar flow is used for ChannelUpdate, but now we just bypass all the validations here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it is ok to skip the validation for the ChannelAnnouncement we created ourselves ? Why should we not trust our own ChannelAnnouncment and ChannelUpdate when its internally sourced ?

@ziggie1984 ziggie1984 changed the base branch from yy-waiting-on-merge to master December 20, 2024 11:27
This will make it simple in the next commits to test soley single
messages rather than always the combination of announcement and
channel update msg.
@ziggie1984 ziggie1984 force-pushed the removal-of-edge-atomic branch 2 times, most recently from 7b4e121 to e8e8b94 Compare December 20, 2024 11:33
When the option SCID is used we need to make sure we update the
channel data in an atomic way so that we don't miss any updates
by our peer and remove all edge info which still uses the alias
as the channel id (e.g. ChannelUpdate msgs).
Moreover add unit test for the new  ReAddEdge method.
In case we assume valid channels or have alias channels we make
sure we also add the channel point to the block filter so that
we are notified if the channel point is spent in a block.
@lightninglabs-deploy
Copy link

@Crypt-iQ: review reminder
@yyforyongyu: review reminder
@ziggie1984, remember to re-request review from reviewers when ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gossip size/kilo medium, proper context needed, less than 1000 lines
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

6 participants