Skip to content
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

Channel phase refactor, wrap channel phase, store wrapped phase in map #3418

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

optout21
Copy link
Contributor

Rationale:

  • Better encapsulate channel phase, shield from ChannelManager
  • In channel map, store wrapped channel phase, not phase
  • Can change the channel phase without changing the map
  • Preparation for easier introduction of new channel phases, for splicing

Changes in this PR:

  • New ChannelWrapper struct to wrap a ChannelPhase
  • ChannelManager.channel_by_id map holds ChannelWrapper structs, as opposed to ChannelPhase enums
  • Transition from pending to funded channel can be done without removing the enrtry from the map and adding again, chaning only inside the wrapper struct around the phase.

Next steps:

  • Rename Channel -> FundedChannel and ChannelWrapper -> Channel
  • Create more handling method on Channel (channel wrapper), that dispatch into different phases.

@optout21
Copy link
Contributor Author

Preferably we should get this in before further substantial dual funding or splicing changes

@optout21 optout21 changed the title Channel phase refactor, wrap channel phase, store wrapped phase in mapChannel phase Channel phase refactor, wrap channel phase, store wrapped phase in map Nov 21, 2024
@dunxen
Copy link
Contributor

dunxen commented Nov 21, 2024

Thanks!

Discussed this @jkczyz in some calls, makes sense and agree with moving the enum inward effectively. Happy to prioritise this as it will reduce matching hell in ChannelManager where this phase stuff never really belonged, but just need to review implementation first. At first glance it seems to create even more indirection. That's fine if it's temporary and cleans up ChannelManager now already. I'll do some more in-depth review.

@optout21
Copy link
Contributor Author

At first glance it seems to create even more indirection. That's fine if it's temporary and cleans up ChannelManager now already. I'll do some more in-depth review.

Please note that this is not complete, but only the basic change to the map, further steps are possible (as noted in description). Various handling logic in ChannelManager (of type match channel.phase()) can be moved into Channel, where it can inspect its own phase.
Yes, more encapsulation sometimes goes with more indirection.

@dunxen
Copy link
Contributor

dunxen commented Nov 21, 2024

Please note that this is not complete, but only the basic change to the map, further steps are possible (as noted in description). Various handling logic in ChannelManager (of type match channel.phase()) can be moved into Channel, where it can inspect its own phase. Yes, more encapsulation sometimes goes with more indirection.

Sounds good! :) I wasn't too worried about the extra wrapper, just wanted to get the bigger picture of its specific use.

Copy link

codecov bot commented Nov 21, 2024

Codecov Report

Attention: Patch coverage is 71.03275% with 115 lines in your changes missing coverage. Please review.

Project coverage is 89.21%. Comparing base (0c31021) to head (a8f83dc).

Files with missing lines Patch % Lines
lightning/src/ln/channelmanager.rs 68.22% 91 Missing and 18 partials ⚠️
lightning/src/ln/channel.rs 86.36% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3418      +/-   ##
==========================================
- Coverage   89.22%   89.21%   -0.01%     
==========================================
  Files         130      130              
  Lines      106965   107038      +73     
  Branches   106965   107038      +73     
==========================================
+ Hits        95438    95496      +58     
- Misses       8734     8748      +14     
- Partials     2793     2794       +1     

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


🚨 Try these New Features:

@optout21 optout21 marked this pull request as ready for review November 21, 2024 11:34
Copy link
Contributor

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

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

Next steps:

  • Rename Channel -> FundedChannel and ChannelWrapper -> Channel
  • Create more handling method on Channel (channel wrapper), that dispatch into different phases.

We may as well do this now. At very least the rename.

Comment on lines 1193 to 1199
pub fn context(&'a self) -> &'a ChannelContext<SP> {
self.phase.context()
}

pub fn context_mut(&'a mut self) -> &'a mut ChannelContext<SP> {
self.phase.context_mut()
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Lifetimes shouldn't be need as they would be inferred.

self.phase
}

pub fn get_funded_channel(&self) -> Option<&Channel<SP>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

We aren't consistent about this, but no need to use get_ prefix for accessor methods.

return close_chan!(chan_err, api_err, chan);
},
}
Some(channel) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you pattern match on the struct to reduce the diff size here and elsewhere?

Comment on lines 1181 to 1183
pub fn phase_take(self) -> ChannelPhase<SP> {
self.phase
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like we could move context to ChannelWrapper to avoid needing to deconstruct it. Otherwise, we still need to re-insert into the map. So, IIUC, this change doesn't really help us any.

Comment on lines +1167 to +1170
/// The inner channel phase
/// Option is used to facilitate in-place replacement (see e.g. move_v2_to_funded),
/// but it is never None, ensured in new() and set_phase()
phase: Option<ChannelPhase<SP>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

See other comment about moving ChannelContext here. Seems we shouldn't need to use an Option.

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.

3 participants