Skip to content

Do not lock while looping htlcs_to_fail #1880

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

Merged

Conversation

tcharding
Copy link
Contributor

@tcharding tcharding commented Nov 29, 2022

Currently we loop over htlcs_to_fail locking channel_state for each element only to call get_htlc_inbound_temp_fail_err_and_data with the same inputs on each iteration. This is unnecessary, we can refactor and call get_htlc_inbound_temp_fail_err_and_data outside of the loop.

The first two patches prepare the way for patch 3 which moves the locking.

Fix #1727

Note to reviewers

This is my first PR to rust-lightning, please prod me if anything is done incorrectly for the project. I was struggling with the indentation (tabs vs spaces) of function arguments when spread over multiple lines so I went around the problem and added local variables.

Patch 1 adds constructors to an enum, this is slightly unusual and not done anywhere else in the codebase that I saw.

@codecov-commenter
Copy link

codecov-commenter commented Nov 29, 2022

Codecov Report

Base: 90.72% // Head: 90.70% // Decreases project coverage by -0.02% ⚠️

Coverage data is based on head (e0cc997) compared to base (64b9e83).
Patch coverage: 92.50% of modified lines in pull request are covered.

❗ Current head e0cc997 differs from pull request most recent head aaa2820. Consider uploading reports for the commit aaa2820 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1880      +/-   ##
==========================================
- Coverage   90.72%   90.70%   -0.03%     
==========================================
  Files          91       91              
  Lines       47999    48011      +12     
  Branches    47999    48011      +12     
==========================================
+ Hits        43546    43547       +1     
- Misses       4453     4464      +11     
Impacted Files Coverage Δ
lightning/src/ln/channelmanager.rs 86.36% <92.50%> (+0.01%) ⬆️
lightning/src/chain/onchaintx.rs 94.87% <0.00%> (-0.43%) ⬇️
lightning/src/util/events.rs 25.36% <0.00%> (-0.25%) ⬇️
lightning/src/chain/channelmonitor.rs 90.69% <0.00%> (-0.11%) ⬇️
lightning/src/ln/functional_tests.rs 96.99% <0.00%> (-0.09%) ⬇️

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
TheBlueMatt previously approved these changes Nov 29, 2022
failure_code, data,
}, HTLCDestination::NextHopChannel { node_id: Some(channel.get_counterparty_node_id()), channel_id: channel.channel_id() }));
timed_out_htlcs.push((source, payment_hash, HTLCFailReason::reason(failure_code, data),
HTLCDestination::NextHopChannel { node_id: Some(channel.get_counterparty_node_id()), channel_id: channel.channel_id() }));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: unnecessary indentation here (and below)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, is one tab in from the start of the function call the place you usually put args like this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Generally yea, mostly because otherwise the lines get looongggg especially after we've indented five times cause we have two matches ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And is the choice of line length arbitrary (when to put the next arg on a newline)? I'm not trolling, just trying to fit in with the code.

Copy link
Contributor

Choose a reason for hiding this comment

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

The intended limit is 100 chars.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, thanks man.

@tcharding
Copy link
Contributor Author

Changes in force push:

  • fixed indentation
  • rebased on master (since I was force pushing anyways)

@ViktorTigerstrom ViktorTigerstrom self-requested a review November 30, 2022 10:33
@TheBlueMatt
Copy link
Collaborator

Oops, sorry, needs rebase (and there's some new uses of HTLCFailReason you'll want to migrate to your constructor).

@tcharding tcharding force-pushed the 11-29-move-lock-outside-loop branch from aaa2820 to 4f14a9c Compare December 1, 2022 02:18
We create `HTLCFailReason` inline in function calls in a bunch of places
in the `channelmanager` module, we can make the code more terse with no
loss of clarity by implementing a couple of constructor methods.
Currently `fail_htlc_backwards_internal` takes ownership of its source
and reason parameters however they are not consumed so we can borrow them.

Includes refactoring to use local variables before the function call.
Currently we loop over `htlcs_to_fail` locking `channel_state` for each
element only to call `get_htlc_inbound_temp_fail_err_and_data` with the
same inputs on each iteration. This is unnecessary, we can refactor and
call `get_htlc_inbound_temp_fail_err_and_data` outside of the loop.
@tcharding tcharding force-pushed the 11-29-move-lock-outside-loop branch from 4f14a9c to 1dd3184 Compare December 1, 2022 02:32
@TheBlueMatt TheBlueMatt added this to the 0.0.113 milestone Dec 1, 2022
@TheBlueMatt
Copy link
Collaborator

Tagged 113 as #1879 will probably want to use the changes here.

@TheBlueMatt TheBlueMatt merged commit 4dafa43 into lightningdevkit:main Dec 1, 2022
@tcharding tcharding deleted the 11-29-move-lock-outside-loop branch November 1, 2023 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inline fail_holding_cell_htlcs to avoid taking channel_state lock per-iteration to calculate the same thing
6 participants