Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion htlcswitch/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,9 @@ func (l *channelLink) Start() error {
}()
}

l.updateFeeTimer = time.NewTimer(l.randomFeeUpdateTimeout())
// this timer will fire in one minute after the channel is ready
// and it will reset to a random interval after that
l.updateFeeTimer = time.NewTimer(time.Minute)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Linking back to the issue,

Open the app, do any payments, close it in under 10 minutes

Though less likely, if the following happened we'd still have this issue or?

Open the app, do any payments, close it in under 1 minutes

Comment on lines +536 to +538
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The PR description states the goal is to trigger the fee update 'right after the channel becomes active', but a one-minute delay is used here. While this is an improvement over the previous 10-60 minute random delay, a shorter duration like 10 seconds would better match the stated intent.

Additionally, the comment explains what the code does, but not why. According to the style guide, comments should explain the 'why' behind the code. I've suggested an improved comment that clarifies the intent.

Suggested change
// this timer will fire in one minute after the channel is ready
// and it will reset to a random interval after that
l.updateFeeTimer = time.NewTimer(time.Minute)
// Trigger an initial fee update shortly after channel activation to get a
// recent fee estimate. Subsequent updates will use a random interval.
l.updateFeeTimer = time.NewTimer(10 * time.Second)
References
  1. Comments should explain the 'why' behind a certain block of code, not just what the code does. (link)


l.wg.Add(1)
go l.htlcManager()
Expand Down
4 changes: 4 additions & 0 deletions htlcswitch/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,10 @@ func TestChannelLinkCancelFullCommitment(t *testing.T) {
n := newTwoHopNetwork(
t, channels.aliceToBob, channels.bobToAlice, testStartingHeight,
)
// This test takes a long time to finish and the link breaks if the fee
// update times out during it, blocking the test indefinitely
n.aliceChannelLink.updateFeeTimer.Reset(time.Minute * 10)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This mitigation works, though it may introduce flakiness to the unit tests since we are now relying on them to be finished in under 1min. We should still give the "send out a fee update immediately on startup" a shot, and I'll help figure out how to best handle the tests.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Many tests break if we immediately send an update. If you're up to the task then sure, this change is trivial enough. But I still think it's better to set the timer to 0 instead (achieving the same effect without forcing it in all cases), reset it to 10 minutes for the tests + write a separate test for this instant update. I'll take a look in a day or two!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@yyforyongyu Hi, maybe you could take this over? I really don't have time now it seems...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

sure, thanks for all the input so far!

n.bobChannelLink.updateFeeTimer.Reset(time.Minute * 10)
Comment on lines +859 to +862
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Instead of resetting the timer to a longer duration (10 minutes), it would be cleaner and make the test more deterministic to stop the timer altogether. This prevents any fee updates from occurring during this long-running test, avoiding potential interference with the test's logic.

Note that time.Timer.Stop() returns false if the timer has already fired. To prevent a race condition where the timer fires just before it's stopped, it's best practice to also drain the timer's channel, like this:

if !timer.Stop() {
    select {
    case <-timer.C:
    default:
    }
}
Suggested change
// This test takes a long time to finish and the link breaks if the fee
// update times out during it, blocking the test indefinitely
n.aliceChannelLink.updateFeeTimer.Reset(time.Minute * 10)
n.bobChannelLink.updateFeeTimer.Reset(time.Minute * 10)
// This test takes a long time to finish and the link breaks if the fee
// update times out during it, blocking the test indefinitely
n.aliceChannelLink.updateFeeTimer.Stop()
n.bobChannelLink.updateFeeTimer.Stop()


// Fill up the commitment from Alice's side with 20 sat payments.
count := (input.MaxHTLCNumber / 2)
Expand Down