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

Beat [4/4]: implement Consumer in chainWatcher #9277

Merged

Conversation

yyforyongyu
Copy link
Member

@yyforyongyu yyforyongyu commented Nov 18, 2024

This PR implements the Consumer interface in chainWatcher with necessary refactors.

TODOs

  • add a release note

Depends on

NOTE: itest is fixed in the final PR


This change is Reviewable

Copy link
Contributor

coderabbitai bot commented Nov 18, 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. (Experiment)
  • @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.

@yyforyongyu yyforyongyu force-pushed the yy-blockbeat-finalize branch from ac8ce82 to a16b2d4 Compare November 18, 2024 11:34
@yyforyongyu yyforyongyu force-pushed the yy-blockbeat-end branch 2 times, most recently from 0ddd693 to 07631c0 Compare November 18, 2024 11:50
@saubyk saubyk added this to the v0.19.0 milestone Nov 18, 2024
@yyforyongyu yyforyongyu force-pushed the yy-blockbeat-finalize branch 3 times, most recently from c5f77e3 to 3456f2e Compare November 20, 2024 06:06
@yyforyongyu yyforyongyu force-pushed the yy-blockbeat-finalize branch from 3456f2e to 3d08b74 Compare November 20, 2024 06:21
@yyforyongyu yyforyongyu force-pushed the yy-blockbeat-finalize branch from 3d08b74 to e69cd41 Compare November 21, 2024 13:16
@yyforyongyu yyforyongyu force-pushed the yy-blockbeat-finalize branch from e69cd41 to cdd805f Compare November 21, 2024 14:45
contractcourt/chain_watcher.go Outdated Show resolved Hide resolved
contractcourt/chain_watcher.go Show resolved Hide resolved
contractcourt/chain_watcher.go Show resolved Hide resolved
contractcourt/chain_arbitrator.go Outdated Show resolved Hide resolved
contractcourt/channel_arbitrator.go Outdated Show resolved Hide resolved
@yyforyongyu yyforyongyu force-pushed the yy-blockbeat-finalize branch 2 times, most recently from 1e58126 to abbee3b Compare November 22, 2024 11:58
@yyforyongyu yyforyongyu force-pushed the yy-blockbeat-finalize branch from abbee3b to aeafa63 Compare November 25, 2024 05:49
@yyforyongyu yyforyongyu force-pushed the yy-blockbeat-end branch 2 times, most recently from 0032fd7 to 5c9c6d0 Compare November 25, 2024 06:21
@ellemouton ellemouton removed their request for review November 27, 2024 08:54
@yyforyongyu yyforyongyu force-pushed the yy-blockbeat-finalize branch from 29521a1 to 7b19d5c Compare November 27, 2024 12:24

// If this is a taproot channel, before we proceed, we want to ensure
// that the expected funding output has confirmed on chain.
if c.cfg.chanState.IsPending && c.cfg.chanState.ChanType.IsTaproot() {
Copy link
Member

Choose a reason for hiding this comment

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

Posted the context in the other PR, not a bad idea to add more of that rationale here for future reviewers.

}

// We have broadcast our commitment, and it is now confirmed onchain.
case closeInfo := <-c.cfg.ChainEvents.LocalUnilateralClosure:
Copy link
Member

Choose a reason for hiding this comment

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

Review note to make sure we remove the handling of these cases in the channelAttendant goroutine.

Copy link
Member

Choose a reason for hiding this comment

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

So this is still here: we handle all these events in two locations now. IIUC, we only want to handle them when received by the block beat.

Copy link
Member Author

Choose a reason for hiding this comment

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

At the bottom layer, we have two notifications,

  1. RegisterBlockEpochNtfn which is the source of the block height used by blockbeat
  2. ``RegisterSpendNtfnwhich is the source of the funding spend used by thechainWatcher`

Observed from the itests, there is no gurantee of the event orders here - the chainWatcher may receive a blockbeat at height X, but only receives the spending notification at block X+1 or X-1, which means a close event may be sent from the chainWatcher to the ChannelArbitrator with one block offset, thus we need to catch the close event in two places - one in handleBlockbeat, in which we do a non-blocking read of the close event channels, and this should be most of the cases; the other one is done in channelAttendant, to catch the offset case to make sure we still process the close event.

Now, if the spending tx is notified at block X-1, we are fine as we want the close event to be there at block X. The bad case is the close event only arrives at block X+1, then we miss it at block X, which is why we added a change here 1200b75, where we always notify the spending tx before the block.

It doesn't completely solve the issue, as the above notification are sent in goroutines, and the receivers, aka subscribers, need to be aware of this possible race.

chainntnfs/txnotifier.go Show resolved Hide resolved
Copy link
Collaborator

@ziggie1984 ziggie1984 left a comment

Choose a reason for hiding this comment

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

Very close 👏

had some minor questions, PR looks dope 😺

// As a height hint, we'll try to use the opening height, but if the
// channel isn't yet open, then we'll use the height it was broadcast
// at. This may be an unconfirmed zero-conf channel.
heightHint := chanState.ShortChanID().BlockHeight
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm for zeroconf channel channels this might still be garbage data because the shortchanid is like a random alias (not super random but still).

also from the ShortChanID method:

// If IsZeroConf(), then this will the "base" (very first) ALIAS scid
	// and the confirmed SCID will be stored in ConfirmedScid.

So I think we need to first theck for IsZeroConf first otherwise the blockeheight might not be 0 but wrong ?

Copy link
Member Author

Choose a reason for hiding this comment

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

i think they are handled below?

Copy link
Collaborator

Choose a reason for hiding this comment

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

ahh gottya, kinda thought we are returning earlier. Maybe we cana structure the code so that if its zeroconf we fill out the chanID and return immediatly. and for the other case we proceed further. Otherwise we check the ShortChanID for zeroconf although it is not really needed.


// If this is a taproot channel, before we proceed, we want to ensure
// that the expected funding output has confirmed on chain.
if c.cfg.chanState.IsPending && c.cfg.chanState.ChanType.IsTaproot() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

response can be found here: #7346 (comment)

Comment on lines +1446 to +1471
// TODO(Roasbeef): need to be able to ensure this only triggers
// on confirmation, to ensure if multiple txns are broadcast, we
// act on the one that's timestamped
Copy link
Collaborator

Choose a reason for hiding this comment

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

cc @Roasbeef can you check if this TODO still needed here ?

contractcourt/chain_watcher.go Show resolved Hide resolved
contractcourt/channel_arbitrator.go Show resolved Hide resolved
contractcourt/channel_arbitrator.go Show resolved Hide resolved
contractcourt/channel_arbitrator.go Show resolved Hide resolved
@@ -2992,6 +2992,10 @@ func (c *ChannelArbitrator) handleBlockbeat(beat chainio.Blockbeat) error {
// Notify we've processed the block.
defer c.NotifyBlockProcessed(beat, nil)

// Perform a non-blocking read on the close events in case the channel
// is closed in this blockbeat.
c.receiveAndProcessCloseEvent()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we maybe check if the state is already in contractClosed so there is no need to call this again ?

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah i like it, lemme try

Copy link
Member Author

Choose a reason for hiding this comment

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

building in a side branch, will push once it passes all the tests

fundingSpendNtfn: spendNtfn,
}

// If this is a pending taproot channel, we need to register for a
// confirmation notification of the funding tx.
Copy link
Collaborator

Choose a reason for hiding this comment

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

add a shorten explanation of the reasoning behind this as a comment from: https://github.com/lightningnetwork/lnd/pull/9277/files#r1864911793

Copy link
Member Author

Choose a reason for hiding this comment

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

i think you are referencing the wrong place? this is about keysend cannot be recognized in RegisterSpend

Copy link
Collaborator

Choose a reason for hiding this comment

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

sorry correct: #7346 (comment)

contractcourt/chain_watcher.go Show resolved Hide resolved
@yyforyongyu yyforyongyu changed the base branch from yy-blockbeat-finalize to yy-feature-blockbeat December 12, 2024 16:07
This commit moves the creation of the spending notification from `Start`
to `newChainWatcher` so we subscribe the spending event before handling
the block, which is needed to properly handle the blockbeat.
To prepare for the blockbeat handler.
We now start notifying the blockbeat from the ChainArbitrator to the
chainWatcher.
This commit adds the closing height to the logging and fixes a wrong
height used in handling the breach event.
This commit adds a new method to enable us resending the blockbeat in
`ChainArbitrator`, which is needed for the channel restore as the chain
watcher and channel arbitrator are added after the start of the chain
arbitrator.
To prepare the next commit where we would handle the event upon
receiving a blockbeat.
Copy link
Collaborator

@ziggie1984 ziggie1984 left a comment

Choose a reason for hiding this comment

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

LGTM 🧘‍♂️ - pending CI run

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🧜🏼

@Roasbeef Roasbeef merged commit 542b9c6 into lightningnetwork:yy-feature-blockbeat Dec 13, 2024
18 of 21 checks passed
@yyforyongyu yyforyongyu deleted the yy-blockbeat-end branch December 15, 2024 23:12
yyforyongyu added a commit that referenced this pull request Dec 18, 2024
This is an oversight from addressing this commment:
#9277 (comment)

where we should focus on skipping the close events but not the
resolvers.
yyforyongyu added a commit to yyforyongyu/lnd that referenced this pull request Dec 19, 2024
This is an oversight from addressing this commment:
lightningnetwork#9277 (comment)

where we should focus on skipping the close events but not the
resolvers.
yyforyongyu added a commit to yyforyongyu/lnd that referenced this pull request Dec 20, 2024
This is an oversight from addressing this commment:
lightningnetwork#9277 (comment)

where we should focus on skipping the close events but not the
resolvers.
yyforyongyu added a commit that referenced this pull request Dec 20, 2024
This is an oversight from addressing this commment:
#9277 (comment)

where we should focus on skipping the close events but not the
resolvers.
yyforyongyu added a commit that referenced this pull request Dec 20, 2024
This is an oversight from addressing this commment:
#9277 (comment)

where we should focus on skipping the close events but not the
resolvers.
yyforyongyu added a commit that referenced this pull request Dec 20, 2024
This is an oversight from addressing this commment:
#9277 (comment)

where we should focus on skipping the close events but not the
resolvers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants