Skip to content

Conversation

@joostjager
Copy link
Contributor

Fewer macros, less pain

@ldk-reviews-bot
Copy link

ldk-reviews-bot commented Dec 11, 2025

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@joostjager joostjager force-pushed the remove-convert-channel-err-macro branch from b34da00 to 73f182f Compare December 11, 2025 09:47
@joostjager joostjager changed the title Remove convert channel err macro Remove convert_channel_err! macro Dec 11, 2025
@codecov
Copy link

codecov bot commented Dec 11, 2025

Codecov Report

❌ Patch coverage is 90.64449% with 45 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.38%. Comparing base (fd85279) to head (d436cbf).
⚠️ Report is 20 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/ln/channelmanager.rs 90.64% 44 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main    #4275    +/-   ##
========================================
  Coverage   89.37%   89.38%            
========================================
  Files         180      180            
  Lines      139395   139623   +228     
  Branches   139395   139623   +228     
========================================
+ Hits       124591   124797   +206     
- Misses      12216    12241    +25     
+ Partials     2588     2585     -3     
Flag Coverage Δ
fuzzing 35.23% <48.23%> (+0.04%) ⬆️
tests 88.71% <90.64%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@joostjager joostjager marked this pull request as ready for review December 11, 2025 11:25
@joostjager joostjager requested a review from tnull December 11, 2025 11:25
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.

Changes look good to me, but might be good to add some more docs now that we lost the macro's match arms which made sure we did the right thing(tm) before..

/// [`ChannelManager::finish_close_channel`].
///
/// Returns a mapped error.
fn convert_channel_err_coop(
Copy link
Contributor

Choose a reason for hiding this comment

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

The replicated docs don't really explain when you're supposed to use which variant now, and the names aren't super clear either (convert_channel_err_coop/convert_channel_err_funded/convert_channel_err). Can we add some more explanations to make up for the lost functionality of the macro match arms?

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've added the channel states to the docs to make it more clear which of the variants should be used. Looked into further documentation, but I think that would lead to more code restructuring to make the docs make sense.

The goal of this PR is just to get rid of the macros without letting the docs become worse.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This method is particularly easy to mis-use, and documentation doesn't make it clear that the code is wrong at the callsite. coop is maybe enough, but it would be very nice to be much more explicit here (its not like the method name is overly long to begin with) so that its clear from the callsite whether we're doing it wrong or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It also isn't converting an error. I've now named it get_coop_close_err. Let me know if you think this is better.

@ldk-reviews-bot
Copy link

👋 The first review has been submitted!

Do you think this PR is ready for a second reviewer? If so, click here to assign a second reviewer.

@joostjager joostjager force-pushed the remove-convert-channel-err-macro branch from 73f182f to d436cbf Compare December 11, 2025 14:57
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.

LGTM

Relatively straightforward refactor, so landing this.

@tnull tnull merged commit 84a791a into lightningdevkit:main Dec 11, 2025
17 checks passed
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

Please fix the docs and maybe clean it up a bit in a followup.

})
}

fn convert_funded_channel_err_internal(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please place methods with other methods doing roughly similar things. There's already a bunch of error-handling down a few hundred lines (finish_close_channel, which even explicitly references some of the new methods). Breaking things up makes things harder to find.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved

/// [`ChannelManager::finish_close_channel`].
///
/// Returns a mapped error.
fn convert_channel_err_coop(
Copy link
Collaborator

Choose a reason for hiding this comment

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

This method is particularly easy to mis-use, and documentation doesn't make it clear that the code is wrong at the callsite. coop is maybe enough, but it would be very nice to be much more explicit here (its not like the method name is overly long to begin with) so that its clear from the callsite whether we're doing it wrong or not.

}

/// When a cooperatively closed channel is removed, two things need to happen:
/// (a) This must be called in the same `per_peer_state` lock as the channel-closing action,
Copy link
Collaborator

Choose a reason for hiding this comment

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

These docs are now all wrong - its not just "this" that must be called but one of a few methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My impression is that for a coop closed channel, this is the one method to call?

Comment on lines +10594 to +10595
&mut peer_state.closed_channel_monitor_update_ids,
&mut peer_state.in_flight_monitor_updates,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I really do not find this more readable than the macro was. Its not really a huge deal, but the macro hid some of the crap that had to be passed in because of lifetime restrictions without doing much else.

I do not buy that we should be removing macros for the sake of removing macros. There's some compilation-time wins to be had by reducing codegen steps through macro removal, and they're often hard to read in our codebase, but that doesn't mean that removing them is inherently easier to read just because they're gone, especially if they leave behind more code than we had.

Copy link
Contributor Author

@joostjager joostjager Dec 12, 2025

Choose a reason for hiding this comment

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

From previous discussions during dev sync, I got that the general consensus is that we want to get rid of macros unless absolutely necessary.

For what it is worth, I personally find macros a source of micro friction. Code navigation breaks, references lookup breaks, stack traces aren't complete, debugging gets more difficult, identifying the applicable match arm is a manual thought step, the syntax is different and rustfmt acts weird sometimes.

I agree to the lifetime argument favoring macros, but in this case I thought it was worth it.


/// When a channel is removed, two things need to happen:
/// (a) [`convert_channel_err`] must be called in the same `per_peer_state` lock as the
/// (a) [`ChannelManager::convert_channel_err`] must be called in the same `per_peer_state` lock as the
Copy link
Collaborator

Choose a reason for hiding this comment

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

No, or one of the other few methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated phrasing to the more generic

The close error must be converted to [MsgHandleErrInternal]

Comment on lines +4726 to +4727
"Can't find a peer matching the passed counterparty node_id {}",
peer_node_id
Copy link
Collaborator

Choose a reason for hiding this comment

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

As we go it would be nice to inline the format arguments where possible.

Suggested change
"Can't find a peer matching the passed counterparty node_id {}",
peer_node_id
"Can't find a peer matching the passed counterparty node_id {peer_node_id}"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed together with another one further down.

@joostjager
Copy link
Contributor Author

Follow ups: #4281

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.

5 participants