Skip to content

Expose confirmation count for pending 'channel open' transactions #9677

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

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

Conversation

NishantBansal2003
Copy link
Contributor

@NishantBansal2003 NishantBansal2003 commented Apr 4, 2025

Change Description

Fixes: #9452
This PR enhances the PendingChannelsResponse_PendingChannel structure to include the ConfirmationsUntilActive field. This new field provides users with the exact number of confirmations required for a pending channel's transition to an active state.

Changes:

  • Database Update:
    The channel's shortchannelID is now persisted in the database once its funding transaction receives 1 confirmation. This ensures we have a reference point to track progress toward the required confirmation depth.

  • API Enhancement:
    Introduced the ConfirmationsUntilActive field in the PendingChannelsResponse_PendingChannel response. This field indicates the remaining confirmations needed for the channel to become active.​

    • If the funding transaction is unconfirmed, ConfirmationsUntilActive defaults to the total required confirmations (NumConfsRequired).

Steps to Test

  • itest: itest have been added to validate this new functionality.

  • Manual Verification: To manually test:​

    • Establish a channel between two peers without confirming the funding transaction.​

    • Execute lncli pendingchannels to observe the confirmations_until_active field, which should display the remaining confirmations needed for the pending channel to transition to an active state.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

Copy link
Contributor

coderabbitai bot commented Apr 4, 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.
    • 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.
  • @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.

@NishantBansal2003 NishantBansal2003 changed the title Conf count Expose confirmation count for pending 'channel open' transactions Apr 4, 2025
@saubyk saubyk added this to the v0.20.0 milestone Apr 4, 2025
@saubyk saubyk added this to lnd v0.20 Apr 4, 2025
@saubyk saubyk moved this to In progress in lnd v0.20 Apr 4, 2025
@saubyk saubyk removed this from lnd v0.20 Apr 4, 2025
@saubyk saubyk self-requested a review April 4, 2025 18:36
Copy link
Contributor

@MPins MPins left a comment

Choose a reason for hiding this comment

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

Good job 👍

Comment on lines 981 to 984
// TestPendingChannelConfirmation verifies that the confirmed state is updated
// upon calling MarkConfirmedScid and that Refresh updates the in-memory state
// of another OpenChannel to reflect a preceding call to MarkConfirmedScid on
// a different OpenChannel.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Two sentences improve the readability.

// TestOpeningChannelTxConfirmation verifies that calling MarkConfirmedScid
// correctly updates the confirmed state. It also ensures that calling Refresh
// on a different OpenChannel updates its in-memory state to reflect the prior
// MarkConfirmedScid call.

rpcserver.go Outdated
@@ -3919,6 +3919,27 @@ func (r *rpcServer) fetchPendingOpenChannels() (pendingOpenChannels, error) {
pendingChan.BroadcastHeight()
fundingExpiryBlocks := int32(maxFundingHeight) - currentHeight

// ConfirmationUntilConfirmed is the number of blocks remaining
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: referring to the variable 'ConfirmationUntilConfirmed' but at this point of the code you are using 'remainingConfs'.

rpcserver.go Outdated
Comment on lines 3924 to 3926
// confirmation height. This is calculated as distance from the
// current block height to the block height where the funding
// transaction is located + required number of confirmations.
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment doesn't accurately reflect what the code does. Please review and clarify it. The code itself looks correct.

rpcserver.go Outdated
Comment on lines 3965 to 3977
CommitFee: int64(localCommitment.
CommitFee),
FeePerKw: int64(localCommitment.
FeePerKw),
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to split each one into two lines.

Comment on lines 1504 to 1505
// MarkConfirmedScid updates the channel's ShortChannelID after the channel
// has been confirmed but before it is fully opened.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it’s more accurate to say that the channel opening tx gets one confirmation, rather than calling it a channel confirmation.

@@ -978,6 +978,81 @@ func TestChannelStateTransition(t *testing.T) {
require.Empty(t, fwdPkgs, "no forwarding packages should exist")
}

// TestPendingChannelConfirmation verifies that the confirmed state is updated
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that TestOpeningChannelTxConfirmation might be more accurate for the name of the test as we are not testing the Channel confirmation, but its funding tx confirmation.

errorChan := make(chan error, 1)

// If the channel is not a zero-conf channel, we add the SCID to the
// database once the channel is confirmed but not fully opened. This
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it’s more accurate to say that the channel opening tx gets one confirmation, rather than say that channel is confirmed.

// The fundingManager is shutting down, and will resume wait on
// startup.
return nil, ErrFundingManagerShuttingDown
// If the channel confirmation is handled successfully,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it’s more accurate to say that the channel opening tx gets one confirmation, rather than calling it a channel confirmation.

@@ -3075,6 +3100,87 @@ func makeFundingScript(channel *channeldb.OpenChannel) ([]byte, error) {
return input.WitnessScriptHash(multiSigScript)
}

// handleChannelConfirmation manages the confirmation process of a channel's
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe change the name of the function to handleOpenChanTxConfirmation

Copy link
Collaborator

@saubyk saubyk left a comment

Choose a reason for hiding this comment

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

providing some initial comments. will test further.

// The number of blocks remaining before the pending channel open
// transaction is fully confirmed. A value of 0 indicates that the
// transaction has reached the required number of confirmations.
int32 confirmation_until_confirmed = 7;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This variable should be named confirmations_until_active instead, since what we are tracking is channel status change from 'pending' to 'active'

@@ -2847,6 +2847,11 @@ message PendingChannelsResponse {
// very likely canceled the funding and the channel will never become
// fully operational.
int32 funding_expiry_blocks = 3;

// The number of blocks remaining before the pending channel open
// transaction is fully confirmed. A value of 0 indicates that the
Copy link
Collaborator

Choose a reason for hiding this comment

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

its really the number of blocks remaining before the channel status is 'active', not really "full confirmed". Important to distinguish between transaction confirmations and channel status.

Copy link
Contributor

@MPins MPins left a comment

Choose a reason for hiding this comment

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

I took a deeper look at manager_test and have a few questions.

FundingBroadcastHeight, "broadcast height mismatch")

confirmedScid := lnwire.ShortChannelID{
BlockHeight: 5,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: since the opening tx was broadcast at broadcastHeight, we could confirm it at broadcastHeight + 1.
It’s not mandatory and doesn’t change the test behavior, but I think it makes the test a bit more elegant. 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done👍

sixConfChannel: make(chan *chainntnfs.TxConfirmation, 1),
sixConfChannel: make(chan *chainntnfs.TxConfirmation, 6),
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain the reasoning behind this change?

Copy link
Contributor Author

@NishantBansal2003 NishantBansal2003 Apr 12, 2025

Choose a reason for hiding this comment

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

Based on their names, I initially thought it was a typo; however, I'm not sure if it serves a specific purpose. If the naming is indeed intentional, could you explain why both oneConfChannel and sixConfChannel are the same(just for my understanding)?

Copy link
Contributor

Choose a reason for hiding this comment

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

The make(..., 6) call here is only sizing the buffer, but the test doesn't actually need a buffer of size 6. The name sixConfChannel refers to the event (i.e., sixth confirmation received), not the buffer size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, understood. I have reverted this change. PTAL

Comment on lines +1499 to +1508
// We send two notifications:
// 1. The first adds the SCID to the database, allowing calculation of
// the number of confirmations before the channel is fully opened.
// 2. The second marks the channel as open.
alice.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
Tx: fundingTx,
}
bob.mockNotifier.oneConfChannel <- &chainntnfs.TxConfirmation{
Tx: fundingTx,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It works, but this doesn't reflect the actual open channel process, right? Have you considered sending the channel confirmation after the transaction confirmation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I didn't quite understand what you meant. My understanding is that if we wait until the transaction is confirmed (after 3 or 6 blocks, etc.), then by the time we mark the channel as confirmed, it will already be open. Also, since we need the details of the SCID, we have to mark the channel as confirmed (after 1 block) even before the transaction is fully confirmed. Am I thinking in the right direction, or is there anything I should read first to better align my thoughts with your points?

Copy link
Contributor

Choose a reason for hiding this comment

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

I meaning that you are sending the confirmation tx event twice to Alice and Bob funding managers. I'm not sure if this is happening during the normal workflow when a channel is opening.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I meaning that you are sending the confirmation tx event twice to Alice and Bob funding managers. I'm not sure if this is happening during the normal workflow when a channel is opening.

Ok, so what I understand is that in a normal, real-world workflow, whenever a confirmation tx notification is registered, the notification is sent to all the goroutines that registered for it as soon as the block is mined. However, since this is a unit test, I need to explicitly send the confirmation tx event twice.

Have you considered sending the channel confirmation after the transaction confirmation?

Since we need the details of the SCID, we have to mark the channel as confirmed (after one block) even before the transaction is fully confirmed.
Also, if I send the goroutine for channel confirmation after the transaction confirmation, I still need to send the confirmation tx event twice because of the select statement:

  1. If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection.

See: https://go.dev/ref/spec#Select_statements

To avoid flaky tests, I send the confirmation tx event twice.
I hope this is the correct way of handling the process. Please suggest any better method you have in mind.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agree this testHarness could be better mocked, however both nodes need to receive the confirmation to finally set the channel to active.

Tho this changes with my proposal so I am not going to review this for now.

Copy link
Collaborator

@saubyk saubyk left a comment

Choose a reason for hiding this comment

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

Tested three scenarios:

  1. Regular channel, default confirmations required for active is 3. confirmations_until_active value in pendingchannels response tracks, until the channel is active. Test pass.
  2. Wumbo channel, default confirmations required for active is 6. confirmations_until_active value in pendingchannels response tracks, until the channel is active. Test pass.
  3. Updated the node config with bitcoin.defaultchanconfs=4. confirmations_until_active value in pendingchannels response doesn't track. It still starts off with a value of 3 (should start with 4) and channels gets active after 3 confirmations. Test fail.

Don't think that the issue with last scenario has been introduced with this change. But it would be good to investigate further why the setting in config is not having any bearing on the default behavior.

@NishantBansal2003
Copy link
Contributor Author

3. Updated the node config with bitcoin.defaultchanconfs=4. confirmations_until_active value in pendingchannels response doesn't track. It still starts off with a value of 3 (should start with 4) and channels gets active after 3 confirmations. Test fail.

I checked this locally and it works for me. I have also added an itest for this case. Not entirely sure, but it seems like you might have missed what bitcoin.defaultchanconfs means is "We'll require any incoming channel requests to wait for this many confirmations before we consider the channel active."

So:

  • Alice -> Bob (the confirmations_until_active starts off with a value of Bob's defaultchanconfs)
  • Bob -> Alice (the confirmations_until_active starts off with a value of Alice's defaultchanconfs)

@saubyk
Copy link
Collaborator

saubyk commented Apr 14, 2025

  1. Updated the node config with bitcoin.defaultchanconfs=4. confirmations_until_active value in pendingchannels response doesn't track. It still starts off with a value of 3 (should start with 4) and channels gets active after 3 confirmations. Test fail.

I checked this locally and it works for me. I have also added an itest for this case. Not entirely sure, but it seems like you might have missed what bitcoin.defaultchanconfs means is "We'll require any incoming channel requests to wait for this many confirmations before we consider the channel active."

So:

  • Alice -> Bob (the confirmations_until_active starts off with a value of Bob's defaultchanconfs)
  • Bob -> Alice (the confirmations_until_active starts off with a value of Alice's defaultchanconfs)

Good clarification. Tested again by updating the peer's setting and was able to get the correct values for confirmations_until_active.

Copy link
Collaborator

@saubyk saubyk left a comment

Choose a reason for hiding this comment

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

Great work. tAck.
Code review needs approval from two other devs.

cc: @kaloudis would you like to test this pr and provide some feedback?

This change ensures that a channel's ShortChannelID is recorded in the
database once its funding transaction receives its initial confirmation.
By doing so, we establish a reliable reference point to monitor the
channel's progress toward the required confirmation depth.
Signed-off-by: Nishant Bansal <[email protected]>
Introduce ConfirmationsUntilActive in
PendingChannelsResponse_PendingChannel. This field indicates the remaining
confirmations needed for the channel to become active. If the funding
transaction is unconfirmed, ConfirmationsUntilActive defaults to the total
required confirmations (NumConfsRequired).
Signed-off-by: Nishant Bansal <[email protected]>
Signed-off-by: Nishant Bansal <[email protected]>
@lightninglabs-deploy
Copy link

@NishantBansal2003, remember to re-request review from reviewers when ready

@saubyk saubyk moved this to Low Priority in PR Review Priority Apr 29, 2025
@saubyk saubyk requested a review from ziggie1984 April 29, 2025 16:38
@saubyk saubyk requested a review from bitromortac April 29, 2025 16:38
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.

Thanks for picking this issue up 👍.

Nice work so far, the direction is the right one, left some comments for you.


// Create a pending channel that was broadcast at height 99.
const broadcastHeight = uint32(99)
channelState := createTestChannel(t, cdb,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Formatting

require.Len(t, pendingChannels, 1, "expected one pending channel")

// Verify the broadcast height of the pending channel.
require.Equal(t, broadcastHeight, pendingChannels[0].
Copy link
Collaborator

Choose a reason for hiding this comment

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

Formatting.

Comment on lines +1009 to +1010
TxIndex: 10,
TxPosition: 15,
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 leave them empty, seems like they are ot used anyways ?

@@ -1501,6 +1501,37 @@ func (c *OpenChannel) fullSync(tx kvdb.RwTx) error {
return putOpenChannel(chanBucket, c)
}

// MarkConfirmedScid updates the channel's ShortChannelID once the channel
// opening transaction receives one confirmation.
func (c *OpenChannel) MarkConfirmedScid(scid lnwire.ShortChannelID) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What we need to do is to split what we are currently doing in

func (c *OpenChannel) MarkAsOpen(openLoc lnwire.ShortChannelID) error {

We need to split this function into two:

  1. MarkShortChannelID

  2. MarkAsOpen => removing the logic where we persit the short channel id there otherwise we make it two times for the non-zeroconf-channel.

// database once the channel opening transaction receives one
// confirmation. This enables us to calculate the number of
// confirmations before the pending channel becomes active.
if !ch.IsZeroConf() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not necessary, for can use the function waitForFundingConfirmation to update the short channel id.

FeePerKw: int64(localCommitment.
FeePerKw),
FundingExpiryBlocks: fundingExpiryBlocks,
ConfirmationsUntilActive: remainingConfs,
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should also add the confirmation height here.

// Restart both nodes to test that the appropriate state has been
// persisted and that both nodes recover gracefully.
ht.RestartNode(alice)
ht.RestartNode(bob)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also need the ensure connected directive.

Comment on lines +918 to +919
require.Equal(ht, numConfs, ht.AssertNumPendingOpenChannels(alice,
1)[0].ConfirmationsUntilActive)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Not necessary this line, I mean we are mining an empty block.


// ConfirmationsUntilActive field should decrease as each block is
// mined until the required number of confirmations is reached. Let's
// mine a few empty blocks and verify the value of
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should mine normal blocks here, because there is no tx in the mempool anyways ?

// mined until the required number of confirmations is reached. Let's
// mine a few empty blocks and verify the value of
// ConfirmationsUntilActive at each step.
for i := int32(1); i < numConfs; i++ {
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 for these cases a reverse loop fits better:

// we could also decrease the numConfs immediately when confirming the tx above which is maybe even better.

for i := numConfs-1; i > 0; i-- {
		expectedConfirmationsLeft := i

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Low Priority
Development

Successfully merging this pull request may close these issues.

[feature]: Expose negotiated confirmation count for pending 'channel open' transactions
5 participants