Skip to content

Commit e142e4a

Browse files
committed
Add maybe_handle_error_without_close for OutboundV2Channel
1 parent 909130b commit e142e4a

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

lightning/src/ln/channel.rs

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3364,6 +3364,49 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
33643364
_ => todo!()
33653365
}
33663366
}
3367+
3368+
/// If we receive an error message when attempting to open a channel, it may only be a rejection
3369+
/// of the channel type we tried, not of our ability to open any channel at all. We can see if a
3370+
/// downgrade of channel features would be possible so that we can still open the channel.
3371+
pub(crate) fn maybe_downgrade_channel_features<F: Deref>(
3372+
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>
3373+
) -> Result<(), ()>
3374+
where
3375+
F::Target: FeeEstimator
3376+
{
3377+
if !self.is_outbound() ||
3378+
!matches!(
3379+
self.channel_state, ChannelState::NegotiatingFunding(flags)
3380+
if flags == NegotiatingFundingFlags::OUR_INIT_SENT
3381+
)
3382+
{
3383+
return Err(());
3384+
}
3385+
if self.channel_type == ChannelTypeFeatures::only_static_remote_key() {
3386+
// We've exhausted our options
3387+
return Err(());
3388+
}
3389+
// We support opening a few different types of channels. Try removing our additional
3390+
// features one by one until we've either arrived at our default or the counterparty has
3391+
// accepted one.
3392+
//
3393+
// Due to the order below, we may not negotiate `option_anchors_zero_fee_htlc_tx` if the
3394+
// counterparty doesn't support `option_scid_privacy`. Since `get_initial_channel_type`
3395+
// checks whether the counterparty supports every feature, this would only happen if the
3396+
// counterparty is advertising the feature, but rejecting channels proposing the feature for
3397+
// whatever reason.
3398+
if self.channel_type.supports_anchors_zero_fee_htlc_tx() {
3399+
self.channel_type.clear_anchors_zero_fee_htlc_tx();
3400+
self.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
3401+
assert!(!self.channel_transaction_parameters.channel_type_features.supports_anchors_nonzero_fee_htlc_tx());
3402+
} else if self.channel_type.supports_scid_privacy() {
3403+
self.channel_type.clear_scid_privacy();
3404+
} else {
3405+
self.channel_type = ChannelTypeFeatures::only_static_remote_key();
3406+
}
3407+
self.channel_transaction_parameters.channel_type_features = self.channel_type.clone();
3408+
Ok(())
3409+
}
33673410
}
33683411

33693412
// Internal utility functions for channels
@@ -7302,37 +7345,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
73027345
where
73037346
F::Target: FeeEstimator
73047347
{
7305-
if !self.context.is_outbound() ||
7306-
!matches!(
7307-
self.context.channel_state, ChannelState::NegotiatingFunding(flags)
7308-
if flags == NegotiatingFundingFlags::OUR_INIT_SENT
7309-
)
7310-
{
7311-
return Err(());
7312-
}
7313-
if self.context.channel_type == ChannelTypeFeatures::only_static_remote_key() {
7314-
// We've exhausted our options
7315-
return Err(());
7316-
}
7317-
// We support opening a few different types of channels. Try removing our additional
7318-
// features one by one until we've either arrived at our default or the counterparty has
7319-
// accepted one.
7320-
//
7321-
// Due to the order below, we may not negotiate `option_anchors_zero_fee_htlc_tx` if the
7322-
// counterparty doesn't support `option_scid_privacy`. Since `get_initial_channel_type`
7323-
// checks whether the counterparty supports every feature, this would only happen if the
7324-
// counterparty is advertising the feature, but rejecting channels proposing the feature for
7325-
// whatever reason.
7326-
if self.context.channel_type.supports_anchors_zero_fee_htlc_tx() {
7327-
self.context.channel_type.clear_anchors_zero_fee_htlc_tx();
7328-
self.context.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
7329-
assert!(!self.context.channel_transaction_parameters.channel_type_features.supports_anchors_nonzero_fee_htlc_tx());
7330-
} else if self.context.channel_type.supports_scid_privacy() {
7331-
self.context.channel_type.clear_scid_privacy();
7332-
} else {
7333-
self.context.channel_type = ChannelTypeFeatures::only_static_remote_key();
7334-
}
7335-
self.context.channel_transaction_parameters.channel_type_features = self.context.channel_type.clone();
7348+
self.context.maybe_downgrade_channel_features(fee_estimator)?;
73367349
Ok(self.get_open_channel(chain_hash))
73377350
}
73387351

@@ -7966,6 +7979,19 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
79667979
Ok(chan)
79677980
}
79687981

7982+
/// If we receive an error message, it may only be a rejection of the channel type we tried,
7983+
/// not of our ability to open any channel at all. Thus, on error, we should first call this
7984+
/// and see if we get a new `OpenChannelV2` message, otherwise the channel is failed.
7985+
pub(crate) fn maybe_handle_error_without_close<F: Deref>(
7986+
&mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>
7987+
) -> Result<msgs::OpenChannelV2, ()>
7988+
where
7989+
F::Target: FeeEstimator
7990+
{
7991+
self.context.maybe_downgrade_channel_features(fee_estimator)?;
7992+
Ok(self.get_open_channel_v2(chain_hash))
7993+
}
7994+
79697995
pub fn get_open_channel_v2(&self, chain_hash: ChainHash) -> msgs::OpenChannelV2 {
79707996
if self.context.have_received_message() {
79717997
debug_assert!(false, "Cannot generate an open_channel2 after we've moved forward");

0 commit comments

Comments
 (0)