Skip to content

Lean on the holding cell when batch-forwarding/failing HTLCs #1863

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

Conversation

TheBlueMatt
Copy link
Collaborator

When we batch HTLC updates, we currently do the explicit queueing plus the commitment generation in the ChannelManager. This is a bit strange as its ultimately really a Channel responsibility to generate commitments at the correct time, with the abstraction leaking into ChannelManager with the send_htlc and get_update_fail_htlc method docs having clear comments about how send_commitment MUST be called prior to calling other Channel methods.

Luckily Channel already has an update queue - the holding cell. Thus, we can trivially rewrite the batch update logic as inserting the desired updates into the holding cell and then asking all channels to clear their holding cells.

@codecov-commenter
Copy link

codecov-commenter commented Nov 19, 2022

Codecov Report

Base: 90.69% // Head: 90.74% // Increases project coverage by +0.05% 🎉

Coverage data is based on head (65d9d0f) compared to base (52edb35).
Patch coverage: 83.05% of modified lines in pull request are covered.

❗ Current head 65d9d0f differs from pull request most recent head 5e8193f. Consider uploading reports for the commit 5e8193f to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1863      +/-   ##
==========================================
+ Coverage   90.69%   90.74%   +0.05%     
==========================================
  Files          91       91              
  Lines       48408    48326      -82     
  Branches    48408    48326      -82     
==========================================
- Hits        43902    43853      -49     
+ Misses       4506     4473      -33     
Impacted Files Coverage Δ
lightning/src/ln/channelmanager.rs 86.57% <70.96%> (+0.29%) ⬆️
lightning/src/ln/channel.rs 88.98% <96.42%> (+0.13%) ⬆️
lightning/src/chain/onchaintx.rs 94.44% <0.00%> (-0.86%) ⬇️
lightning/src/chain/channelmonitor.rs 90.79% <0.00%> (+0.05%) ⬆️
lightning/src/ln/functional_tests.rs 97.14% <0.00%> (+0.09%) ⬆️
lightning/src/util/events.rs 25.22% <0.00%> (+0.22%) ⬆️
lightning/src/ln/peer_channel_encryptor.rs 93.62% <0.00%> (+0.24%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@TheBlueMatt
Copy link
Collaborator Author

Pushed two additional commits to lean on the holding cell when doing fee updates as well...that sweet, sweet -100 total LoC.

@TheBlueMatt
Copy link
Collaborator Author

Note that the fuzzing failure here is resolved in #1859. Will leave this PR as free-standing but that will need to land first.

@TheBlueMatt
Copy link
Collaborator Author

Rebased without changes, CI should pass now.

Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

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

Nice cleanup, no major feedback. Will take another look next week

@@ -5639,41 +5641,6 @@ impl<Signer: Sign> Channel<Signer> {
Ok(Some(res))
}

/// Creates a signed commitment transaction to send to the remote peer.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I think ideally this would've been removed in the previous-previous commit

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

send_update_fee_and_commit is removed in the "updating fees" commit, and it relies on send_commitment, so we can only remove it in the last commit.

@TheBlueMatt TheBlueMatt force-pushed the 2022-11-holding-cell-batch-update branch from 2ee043c to 2409913 Compare November 25, 2022 20:30
@valentinewallace
Copy link
Contributor

(Maybe for follow-up) would it further simplify things to do the same batching in ChannelManager::send_payment_along_path?

@TheBlueMatt
Copy link
Collaborator Author

Yea, that one's substantially more complicated, but work there is coming soon (tm).

Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

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

Basically LGTM, good time for a second reviewer

@TheBlueMatt TheBlueMatt force-pushed the 2022-11-holding-cell-batch-update branch from 2409913 to d30db59 Compare November 28, 2022 23:57
@TheBlueMatt
Copy link
Collaborator Author

Rebased.

@TheBlueMatt TheBlueMatt force-pushed the 2022-11-holding-cell-batch-update branch 2 times, most recently from 74a1070 to 2233650 Compare December 2, 2022 01:10
@TheBlueMatt
Copy link
Collaborator Author

Rebased. Took this opportunity to squash since there were a lot of fixups.

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Did a quick first pass to get a high-level overview. These are predominately doc nits, main point being that I'd advocate to treat any doc comments as if they were pub, i.e., making sure to link and use backticks in order to allow the cargo doc --document-private-items CI to catch any inconsistencies.

@TheBlueMatt TheBlueMatt force-pushed the 2022-11-holding-cell-batch-update branch from 2233650 to 719bf79 Compare December 2, 2022 18:56
@TheBlueMatt
Copy link
Collaborator Author

main point being that I'd advocate to treat any doc comments as if they were pub, i.e., making sure to link and use backticks in order to allow the cargo doc --document-private-items CI to catch any inconsistencies.

Yea, most of this code predates me knowing you could even do links in docs, sooooo...

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Looks good, just a few nits/questions.

@TheBlueMatt TheBlueMatt force-pushed the 2022-11-holding-cell-batch-update branch from 65d9d0f to 5e8193f Compare December 5, 2022 18:01
Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Feel free to squash, I think. Should also alleviate the CI hangup..

Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

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

ACK after squash

When we batch HTLC updates, we currently do the explicit queueing
plus the commitment generation in the `ChannelManager`. This is a
bit strange as its ultimately really a `Channel` responsibility to
generate commitments at the correct time, with the abstraction
leaking into `ChannelManager` with the `send_htlc` and
`get_update_fail_htlc` method docs having clear comments about
how `send_commitment` MUST be called prior to calling other
`Channel` methods.

Luckily `Channel` already has an update queue - the holding cell.
Thus, we can trivially rewrite the batch update logic as inserting
the desired updates into the holding cell and then asking all
channels to clear their holding cells.
We currently free the channel holding cells in
`get_and_clear_pending_msg_events`, blocking outbound messages
while we do so. This is fine, but may block the message pipeline
longer than we need to. In the next commit we'll push
timer-originating channel fee updates out through the holding cell
pipeline, leaning more on that freeing in the future.

Thus, to avoid a regression in message time, here we clear the
holding cell after processing all timer events. This also avoids
needing to change tests in the next commit.
Like the previous commit, here we update the update_fee+commit
logic to simply push the fee update into the holding cell and then
use the standard holding-cell-freeing codepaths to actually send
the commitment update. This removes a substantial amount of code,
reducing redundant codepaths and keeping channel state machine
logic in channel.rs.
The methods return `Ok(())` always, they just happen to never
return in the case of a duplicate claim if debug assertions are
enabled.
@TheBlueMatt TheBlueMatt force-pushed the 2022-11-holding-cell-batch-update branch from 5e8193f to 1833070 Compare December 6, 2022 18:18
@TheBlueMatt
Copy link
Collaborator Author

Squashed without change.

@TheBlueMatt TheBlueMatt merged commit d9d4611 into lightningdevkit:main Dec 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants