Repository navigation
Include htlc min/max for ChannelDetails and ChannelCounterparty - #1378
Conversation
2814c10 to
8caa487
Compare
| let holder_reserve = self.holder_selected_channel_reserve_satoshis; | ||
|
|
||
| cmp::min( | ||
| Some((self.channel_value_satoshis - counterparty_reserve + holder_reserve) * 1000), |
There was a problem hiding this comment.
We should subtract both reserves, not add ours.
There was a problem hiding this comment.
Oh my 🤦... Thanks! Was a bit too tired when I wrote that, as well as the inversed state check above :)
| if self.channel_state >= ChannelState::TheirInitSent as u32 { | ||
| return None; | ||
| } | ||
| let counterparty_reserve = self.counterparty_selected_channel_reserve_satoshis.unwrap_or(0); |
There was a problem hiding this comment.
If we're past TheirInitSent this is always defined, you could instead just None-check it and return based on that instead of doing a state check.
| /// True if this channel is (or will be) publicly-announced. | ||
| pub is_public: bool, | ||
| /// The smallest value HTLC (in msat) we will accept, for this channel. | ||
| pub htlc_minimum_msat: u64, |
There was a problem hiding this comment.
Maybe lets call these "inbound" or something (like "inbound_htlc_minimum_msat"), to make it clearer without needing to look at docs what they're talking about.
There was a problem hiding this comment.
Will do, thanks!
| (28, is_funding_locked, required), | ||
| (30, is_usable, required), | ||
| (32, is_public, required), | ||
| (34, htlc_minimum_msat, required), |
There was a problem hiding this comment.
We shouldn't be adding new required even values for these, I think, no? Even means "old versions will fail to read" (instead of ignore), and required means we'll fail to read if its missing (ie old versions).
There was a problem hiding this comment.
Ok thanks, I'll make this an odd value instead to ignore it for old versions.
| info.fee_base_msat.write(writer)?; | ||
| info.fee_proportional_millionths.write(writer)?; | ||
| info.cltv_expiry_delta.write(writer)?; | ||
| // Note that this field is ignored by 0.0.99+ as the TLV Optional variant is used instead. |
There was a problem hiding this comment.
I'm not really sure what's going on here anymore. We shouldn't be adding new stuff.
There was a problem hiding this comment.
Ok thanks! Would it be more correct to only add this as a TLV Optional field instead?
There was a problem hiding this comment.
Yes, we can only add new fields via TLV now.
|
Thanks for the feedback! Sorry for the delay in continuing this PR. I've been out traveling, but will pick this up again tomorrow. |
Codecov Report
@@ Coverage Diff @@
## main #1378 +/- ##
=======================================
Coverage 90.87% 90.87%
=======================================
Files 74 74
Lines 41405 41477 +72
Branches 41405 41477 +72
=======================================
+ Hits 37626 37693 +67
- Misses 3779 3784 +5
Continue to review full report at Codecov.
|
0724de0 to
d4987dd
Compare
|
Addressed the latest feedback, and also added htlc min/max to the create invoice functions. Also, please check that the htcl max value we use to create rust-lightning/lightning/src/ln/channelmanager.rs Line 2557 in 7671ae5 I intentionally kept this as it is, due to the comment in the Is this the value we prefer to use, or would we rather use the |
TheBlueMatt
left a comment
There was a problem hiding this comment.
Sorry for the delay in reviewing this!
| (30, is_usable, required), | ||
| (32, is_public, required), | ||
| (33, inbound_htlc_minimum_msat, required), | ||
| (34, inbound_htlc_maximum_msat, option), |
There was a problem hiding this comment.
We shouldn't be adding new even fields here unless we really need to - even means old versions of LDK will refuse to read the new objects which we don't generally want.
| // Read fields for `CounterpartyForwardingInfo`. As the `outbound_htlc_maximum_msat` is a | ||
| // TLV Optional variant, we construct the `CounterpartyForwardingInfo` struct after it ha | ||
| // been read. | ||
| let (has_forwarding_info, fee_base_msat, fee_proportional_millionths, cltv_expiry_delta) = match <u8 as Readable>::read(reader)? { |
There was a problem hiding this comment.
This may be more readable if we just create a CounterpartyForwardingInfo object with the htlc_maximum_msat and htlc_minimum_msat fields initialized to some default.
| // Construct the `CounterpartyForwardingInfo` after the TLV Optional variant for | ||
| // `outbound_htlc_maximum_msat` has been read. | ||
| let counterparty_forwarding_info = match has_forwarding_info { | ||
| false => None, |
There was a problem hiding this comment.
I dont think we can do this - if we're reading an object serialized by an older version of LDK this would imply we jump back to the channel being uninitialized and we'll panic on some unwraps above - we need to initialize the fields to some default.
| (4, fee_proportional_millionths, required), | ||
| (6, cltv_expiry_delta, required), | ||
| (7, outbound_htlc_minimum_msat, required), | ||
| (8, outbound_htlc_maximum_msat, option), |
There was a problem hiding this comment.
Same here, can't add new even fields.
| (2, fee_base_msat, required), | ||
| (4, fee_proportional_millionths, required), | ||
| (6, cltv_expiry_delta, required), | ||
| (7, outbound_htlc_minimum_msat, required), |
There was a problem hiding this comment.
We shouldnt add new required fields - this will mean we refuse to read objects serialized by older LDK versions.
|
Thanks a lot for the review and feedback! Updated the PR with the requested changes. @TheBlueMatt do you possibly have any tips of how I can manually test that my code is backwards compatible (or any other way to tell that it is)? It would be really helpful and I'd appreciate it a lot, so I don't end up making the same mistakes for future PRs :). In commits I pushed 5 days ago ( 7c25533 ), I tried testing it by setting up a sample node with an older version of LDK that I set up a few channels with. I then upgraded LDK for the node to the my local version with the new min/max fields support, set up one more channel, and then finally downgraded LDK to the old version again. The serialization/deserialization of the channels seemed successful throughout the process from what I could tell, but I might be verifying that in an incorrect way.... |
| counterparty_forwarding_info = Some(CounterpartyForwardingInfo { | ||
| fee_base_msat: cf_info.fee_base_msat, | ||
| fee_proportional_millionths: cf_info.fee_proportional_millionths, | ||
| cltv_expiry_delta: cf_info.cltv_expiry_delta, | ||
| outbound_htlc_minimum_msat: cf_info.outbound_htlc_minimum_msat, | ||
| outbound_htlc_maximum_msat: outbound_htlc_maximum_msat | ||
| }); |
There was a problem hiding this comment.
Made this create a new CounterpartyForwardingInfo object with the retrieved outbound_htlc_maximum_msat value, to not require the outbound_htlc_maximum_msat field for CounterpartyForwardingInfo be mutable. But I can of course change that if we prefer the alternative!
There was a problem hiding this comment.
It should be less code to make it mutable - can just change the as_ref(), above, to as_mut() and it'll be mutable :)
TheBlueMatt
left a comment
There was a problem hiding this comment.
Nice this looks great now! Feel free to squash down the fixup commits and we'll get you another reviewer ;)
| (17, self.announcement_sigs_state, required), | ||
| (19, self.latest_inbound_scid_alias, option), | ||
| (21, self.outbound_scid_alias, required), | ||
| (23, self.counterparty_forwarding_info.as_ref().map(|info| info.outbound_htlc_maximum_msat).unwrap_or(None), option), |
There was a problem hiding this comment.
nit: You don't need the unwrap_or(None) - its already None if the unwrap fails :)
There was a problem hiding this comment.
Removing the unwrap_or(None) would result in Some(Some(outbound_htlc_maximum_msat_value)) if a value for outbound_htlc_maximum_msat exists, as both counterparty_forwarding_info and outbound_htlc_maximum_msat are optional fields. And we need it to result in a Optional<u64> value only. But perhaps there is a neater way to solve that :)
| /// such that the outgoing HTLC is forwardable to this counterparty. See `msgs::ChannelUpdate`'s | ||
| /// `cltv_expiry_delta` for more details. | ||
| pub cltv_expiry_delta: u16, | ||
| /// The smallest value HTLC (in msat) the remote peer will accept, for this channel. |
There was a problem hiding this comment.
May be worth noting that this is only None for objects serialized by versions of LDK prior to 0.0.107 (assuming this lands for 107).
Currently we've been doing this on a very ad-hoc basis. Its not good, not at all. #1089 exists to track the wishlist item of having some test framework for it, but one thing you can do today is build a functional test that runs a node to where you want, then dumps out the serialized data. Run it on the last release and then get the hex and check that in to a new test as input. |
Ok, thanks a lot for clarifying! Ah I see, that would be very helpful :). Great thanks, I'll remember that from now on! |
3bd0191 to
d426d6f
Compare
|
Addressed the latest feedback, and squashed the commits! Ps. can you please verify that I'm handling which htcl max value we use to create |
TheBlueMatt
left a comment
There was a problem hiding this comment.
Sorry for the review delay here. I think this is basically good modulo one comment below.
| pub cltv_expiry_delta: u16, | ||
| /// The smallest value HTLC (in msat) the remote peer will accept, for this channel. This field | ||
| /// is only None for `CounterpartyForwardingInfo` objects serialized prior to LDK 0.0.107 | ||
| pub outbound_htlc_minimum_msat: Option<u64>, |
There was a problem hiding this comment.
Now that we have this here we should probably delete the copy at Channel::counterparty_htlc_minimum_msat - its entirely redundant and somewhat confusing to have two copies of the same field in the same object.
There was a problem hiding this comment.
Sorry for the delay in continuing this PR. @TheBlueMatt I'd need some guidance on how to continue given your comment.
In the current version of LDK, the Channel::counterparty_htlc_minimum_msat value is a non optional field that is assigned through the OpenChannel msg for inbound channels in the new_from_req function, and through the AcceptChannel msg in the accept_channel function in for outbound channels. The new Channel::counterparty_forwarding_info::outbound_htlc_minimum_msat would be an Optional field that's assigned at a later stage in the message flow.
The Channel::counterparty_forwarding_info field is first assigned through the first ChannelUpdate msg in the channel_update function, which with this PR will assign the new outbound_htlc_minimum_msat field.
The channel_update function never updates the value for the Channel::counterparty_htlc_minimum_msat field in the current version of LDK, which already proves your point about why it's a bad idea two have two copies of the same field, even though they should be the same value in practice.
I can't really assign the Channel::counterparty_forwarding_info::outbound_htlc_minimum_msat field through the info in the OpenChannel msg or the AcceptChannel msg, as the rest of the information to construct the Channel::counterparty_forwarding_info field doesn't exist yet, unless I assign dummy values to the other fields of the CounterpartyForwardingInfo.
Given the above, how do we want to proceed?
- Simply ignoring the values for
htlc_minimum_msatin theOpenChannelandAcceptChannelmsgs is quite bad and it's a bit of a downgrade from the current version of LDK, as the value would then only be available in theChannelstruct at a later state in the message flow (after the firstChannelUpdatemsg).
But the alternative of creating aChannel::counterparty_forwarding_infofield with dummy values is also bad practice. To avoid assigning dummy values, I guess we could make all fields ofCounterpartyForwardingInfooptional, that that's not great either. How should this be handled? - How should the
htlc_minimum_msatvalue passed in theChannelUpdatemsg be handled? Should it be ignored like the current version of LDK, or would we like it to update the channel's value for everyChannelUpdatemsg received?
(Or is all the above maybe a sign that the outbound_htlc_minimum_msat isn't really suitable to place in the CounterpartyForwardingInfo struct?)
There was a problem hiding this comment.
Ah! Sorry for the confusion here, yea. It really sucks the protocol has two somewhat redundant fields for this :(. I think, however, we should set the outbound_htlc_minimum_msat field to the maximum of what the peer told us in their init and what's in the channel update.
There was a problem hiding this comment.
Would the ChannelUpdate be more relevant than what was in the OpenChannel/AcceptChannel message? Just wondering why we would take the max if we later learn a different value.
There was a problem hiding this comment.
Ok thanks, so would we like to keep the Channel::counterparty_htlc_minimum_msat field then as the protocol also has two fields for htlc_minimum_msat (and maybe rename it to counterparty_init_htlc_minimum_msat)?
Or should we just stick to the counterparty_forwarding_info::outbound_htlc_minimum_msat field and assign it in when receiving the OpenChannel/AcceptChannel msgs as well?
There was a problem hiding this comment.
Okay, I'm an idiot, for the fourth time here.The htlc_min/max included in the channel_update we receive from our peer is re-announcing our limits. The updates can also be things announced publicly and give routing information for how to get from our peer to us, so it has to be the limits we apply on HTLCs. Our current code is correct to ignore them - we set the inbound HTLC limits, not our counterparty.
There was a problem hiding this comment.
Oh! Ok, then assigning the counterparty_forwarding_info::outbound_htlc_minimum_msat based on the ChannelUpdate msg like my code currently does is also incorrect. I guess it should be assigned using the same logic as the get_holder_htlc_maximum_msat function I've created, but using counterparty_max_htlc_value_in_flight_msat instead of holder_max_htlc_value_in_flight_msat correct?
There was a problem hiding this comment.
Hmmm, yea, that's kinda tricky. I definitely think the public API needs to remain the same - forwarding_info in the ChannelDetails needs to remain None until we have the feerate info from our counterparty. How that translates to storage inside the Channel, though, I don't have a super strong opinion. Another thing to think about is maybe moving the new fields to ChannelCounterparty instead of CounterpartyForwardingInfo. This would line up a bit better with the docs on ChannelCounterparty::forwarding_info anyway, which says "Information on the fees and requirements that the counterparty requires when forwarding payments to us through this channel" - the new fields are about forwarding outbound from us.
There was a problem hiding this comment.
Ok thanks! Yes definitely makes sense that we don't want to change the public API.
Great, removing counterparty_htlc_minimum_msat from the forwarding_info would help a lot. Would we be ok with keeping the Channel::counterparty_htlc_minimum_msat field in the Channel struct then, and use the public functions of the Channel to fetch that value when creating the ChannelCounterparty in ChannelManager::list_channels_with_filter, to assign the new ChannelCounterparty::outbound_htlc_minimum_msat field?
| match &self.counterparty_selected_channel_reserve_satoshis { | ||
| None => None, | ||
| Some(counterparty_reserve) => { |
There was a problem hiding this comment.
Rust's Option::map function is pretty handy for removing this boilerplate.
self.counterparty_selected_channel_reserve_satoshis.map(|counterparty_reserve| {
// ...
})There was a problem hiding this comment.
Ah nice, thanks!
| Some((self.channel_value_satoshis - counterparty_reserve - holder_reserve) * 1000), | ||
| Some(self.holder_max_htlc_value_in_flight_msat) |
There was a problem hiding this comment.
Which would also allow you to remove the Some wrappings here.
| /// True if this channel is (or will be) publicly-announced. | ||
| pub is_public: bool, | ||
| /// The smallest value HTLC (in msat) we will accept, for this channel. This field | ||
| /// is only None for `ChannelDetails` objects serialized prior to LDK 0.0.107 |
| pub cltv_expiry_delta: u16, | ||
| /// The smallest value HTLC (in msat) the remote peer will accept, for this channel. This field | ||
| /// is only None for `CounterpartyForwardingInfo` objects serialized prior to LDK 0.0.107 | ||
| pub outbound_htlc_minimum_msat: Option<u64>, |
There was a problem hiding this comment.
Would the ChannelUpdate be more relevant than what was in the OpenChannel/AcceptChannel message? Just wondering why we would take the max if we later learn a different value.
34131c4 to
b3dc51d
Compare
ViktorT-11
left a comment
There was a problem hiding this comment.
Addressed the latest feedback. I have added some comments regarding my implementation that might be helpful during the review, now that the Channel::counterparty_htlc_minimum_msat field has been removed.
| counterparty_forwarding_info: Some(CounterpartyForwardingInfo { | ||
| fee_base_msat: 0, | ||
| fee_proportional_millionths: 0, | ||
| cltv_expiry_delta: 0, | ||
| outbound_htlc_minimum_msat: Some(msg.htlc_minimum_msat), | ||
| outbound_htlc_maximum_msat: None, | ||
| }), |
There was a problem hiding this comment.
As I mentioned in the conversation above, I need to create a CounterpartyForwardingInfo object already here now, in order to be able to store the msg.htlc_minimum_msat value now that the Channel::counterparty_htlc_minimum_msat has been removed. This forced me to set dummy values for fee_base_msat, fee_proportional_millionths and cltv_expiry_delta as we don't have the values for those yet. This isn't great... But making all of them optional values isn't that great either... Not sure what's best here.
| self.counterparty_forwarding_info = Some(CounterpartyForwardingInfo { | ||
| fee_base_msat: 0, | ||
| fee_proportional_millionths: 0, | ||
| cltv_expiry_delta: 0, | ||
| outbound_htlc_minimum_msat: Some(msg.htlc_minimum_msat), | ||
| outbound_htlc_maximum_msat: self.get_counterparty_htlc_maximum_msat(), | ||
| }); |
There was a problem hiding this comment.
The same as above applies here.
| @@ -4303,7 +4317,22 @@ impl<Signer: Sign> Channel<Signer> { | |||
|
|
|||
| /// Allowed in any state (including after shutdown) | |||
| pub fn get_counterparty_htlc_minimum_msat(&self) -> u64 { | |||
There was a problem hiding this comment.
Decided to keep the return type of this function as u64, and not bleed out to the rest of the project that this is now stored as a Option<u64> type, and return 0 in case it doesn't exist, as this was the same value it was initially set to previously in the new_outbound function.
The only time the outbound_htlc_minimum_msat shouldn't exist, is when when an outbound channel has been created by the new_outbound function, which has not yet been accepted by the peer.
If you'd prefer that we make this function private and force that the field is accessed directly through the CounterpartyForwardingInfo::outbound_htlc_minimum_msat that's also an option. Or if you think it's more appropriate that this function returns a Option<u64> type, I can of course change to that as well.
| } | ||
|
|
||
| #[test] | ||
| fn test_forwarding_info_not_assigned_channel_excluded_from_hints() { |
There was a problem hiding this comment.
This test required that the Channel::counterparty_forwarding_info field wasn't assigned until the first ChannelUpdate msg. The Channel::counterparty_forwarding_info field will now be assigned as soon as the channel is accepted by both parties.
|
|
||
| #[test] | ||
| #[cfg(feature = "std")] | ||
| fn test_multi_node_forwarding_info_not_assigned_channel_excluded_from_hints() { |
There was a problem hiding this comment.
This test required that the Channel::counterparty_forwarding_info field wasn't assigned until the first ChannelUpdate msg. The Channel::counterparty_forwarding_info field will now be assigned as soon as the channel is accepted by both parties.
b3dc51d to
c15c0a5
Compare
c15c0a5 to
cb5835c
Compare
|
Added the outbound htlc min/max to the Had to rebase on upstream to not make the checks fail, so made the decision to squash the commits as also much of the previous code was dropped in addressing the feedback. Sorry about that! |
TheBlueMatt
left a comment
There was a problem hiding this comment.
One API note but looks good.
| features: InitFeatures::empty(), | ||
| unspendable_punishment_reserve: to_remote_reserve_satoshis, | ||
| forwarding_info: channel.counterparty_forwarding_info(), | ||
| outbound_htlc_minimum_msat: Some(channel.get_counterparty_htlc_minimum_msat()), |
There was a problem hiding this comment.
We should check the channel state and only assign this if the counterparty has given us a value. We could change the channel internal API to return an option too but that's a good chunk more work.
|
Addressed the latest feedback! Let me know if it looks ok and I'll squash the commit. |
d259043 to
7bc9462
Compare
| // message (as they are always the first message from the counterparty). | ||
| // Else `Channel::get_counterparty_htlc_minimum_msat` could return the | ||
| // default `0` value set by `Channel::new_outbound`. | ||
| outbound_htlc_minimum_msat: channel.have_received_message().then(|| channel.get_counterparty_htlc_minimum_msat()), |
There was a problem hiding this comment.
Won't be able to use then because our MSRV is 1.41.1. Will have to use an if-else expression.
There was a problem hiding this comment.
Oh ok thanks! I'll update immediately
7bc9462 to
9769122
Compare
|
Hmmmm sorry I just want to raise my comment #1378 (comment) once more as I never received any direct reply to it:
|
That is correct, use the existing one.
Gah, yep, that's an issue, but the wrong solution - we need to use the counterparty max_value_in_flight for the announced_htlc_max_msat. |
|
That said, maybe lets fix that in a followup PR - in the mean time can you squash the fixup here and we can land this? |
9769122 to
dd4a3ed
Compare
Ah ok!
Sure sounds good! Squashed without changes. |
dd4a3ed to
3c67687
Compare
|
Updated the docs for |
Closes #1287.
Continues the implementation after PR #1297 was closed.
Note:
Please specifically check the serialization I've implemented, as I'm definitely not that confident that I'm doing it correctly.
Pushing the PR at this state to get some feedback of wether it looks ok. But I'll make sure to add these fields for the invoice creation in
lightning-invoice/src/utils.rsas well as some tests later if this looks ok.