@@ -1557,6 +1557,7 @@ impl<SP: Deref> Channel<SP> where
15571557
15581558 pub fn maybe_handle_error_without_close<F: Deref, L: Deref>(
15591559 &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
1560+ user_config: &UserConfig, their_features: &InitFeatures,
15601561 ) -> Result<Option<OpenChannelMessage>, ()>
15611562 where
15621563 F::Target: FeeEstimator,
@@ -1567,13 +1568,17 @@ impl<SP: Deref> Channel<SP> where
15671568 ChannelPhase::Funded(_) => Ok(None),
15681569 ChannelPhase::UnfundedOutboundV1(chan) => {
15691570 let logger = WithChannelContext::from(logger, &chan.context, None);
1570- chan.maybe_handle_error_without_close(chain_hash, fee_estimator, &&logger)
1571+ chan.maybe_handle_error_without_close(
1572+ chain_hash, fee_estimator, &&logger, user_config, their_features,
1573+ )
15711574 .map(|msg| Some(OpenChannelMessage::V1(msg)))
15721575 },
15731576 ChannelPhase::UnfundedInboundV1(_) => Ok(None),
15741577 ChannelPhase::UnfundedV2(chan) => {
15751578 if chan.funding.is_outbound() {
1576- chan.maybe_handle_error_without_close(chain_hash, fee_estimator)
1579+ chan.maybe_handle_error_without_close(
1580+ chain_hash, fee_estimator, user_config, their_features,
1581+ )
15771582 .map(|msg| Some(OpenChannelMessage::V2(msg)))
15781583 } else {
15791584 Ok(None)
@@ -4868,7 +4873,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
48684873 /// of the channel type we tried, not of our ability to open any channel at all. We can see if a
48694874 /// downgrade of channel features would be possible so that we can still open the channel.
48704875 pub(crate) fn maybe_downgrade_channel_features<F: Deref>(
4871- &mut self, funding: &mut FundingScope, fee_estimator: &LowerBoundedFeeEstimator<F>
4876+ &mut self, funding: &mut FundingScope, fee_estimator: &LowerBoundedFeeEstimator<F>,
4877+ user_config: &UserConfig, their_features: &InitFeatures,
48724878 ) -> Result<(), ()>
48734879 where
48744880 F::Target: FeeEstimator
@@ -4889,21 +4895,33 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
48894895 // features one by one until we've either arrived at our default or the counterparty has
48904896 // accepted one.
48914897 //
4892- // Due to the order below, we may not negotiate `option_anchors_zero_fee_htlc_tx` if the
4893- // counterparty doesn't support `option_scid_privacy`. Since `get_initial_channel_type`
4894- // checks whether the counterparty supports every feature, this would only happen if the
4895- // counterparty is advertising the feature, but rejecting channels proposing the feature for
4896- // whatever reason.
4897- let channel_type = &mut funding.channel_transaction_parameters.channel_type_features;
4898+ // To downgrade the current channel type, we start with the remote party's full set of
4899+ // feature bits and un-set any features that are set for the current channel type or any
4900+ // channel types that come before it in our order of preference. This will allow us to
4901+ // negotiate the "next best" channel type per our ranking in `get_initial_channel_type`.
4902+ let channel_type = &funding.channel_transaction_parameters.channel_type_features;
4903+ let mut eligible_features = their_features.clone();
4904+
48984905 if channel_type.supports_anchors_zero_fee_htlc_tx() {
4899- channel_type.clear_anchors_zero_fee_htlc_tx();
4900- self.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
4901- assert!(!channel_type.supports_anchors_nonzero_fee_htlc_tx());
4906+ eligible_features.clear_anchors_zero_fee_htlc_tx();
4907+ assert!(!eligible_features.supports_anchors_nonzero_fee_htlc_tx());
49024908 } else if channel_type.supports_scid_privacy() {
4903- channel_type.clear_scid_privacy();
4904- } else {
4905- *channel_type = ChannelTypeFeatures::only_static_remote_key();
4909+ eligible_features.clear_scid_privacy();
4910+ eligible_features.clear_anchors_zero_fee_htlc_tx();
4911+ assert!(!eligible_features.supports_scid_privacy());
4912+ assert!(!eligible_features.supports_anchors_nonzero_fee_htlc_tx());
49064913 }
4914+
4915+ let next_channel_type = get_initial_channel_type(user_config, &eligible_features);
4916+
4917+ let conf_target = if next_channel_type.supports_anchors_zero_fee_htlc_tx() {
4918+ ConfirmationTarget::AnchorChannelFee
4919+ } else {
4920+ ConfirmationTarget::NonAnchorChannelFee
4921+ };
4922+ self.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(conf_target);
4923+ funding.channel_transaction_parameters.channel_type_features = next_channel_type;
4924+
49074925 Ok(())
49084926 }
49094927
@@ -9893,13 +9911,16 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
98939911 /// not of our ability to open any channel at all. Thus, on error, we should first call this
98949912 /// and see if we get a new `OpenChannel` message, otherwise the channel is failed.
98959913 pub(crate) fn maybe_handle_error_without_close<F: Deref, L: Deref>(
9896- &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
9914+ &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
9915+ user_config: &UserConfig, their_features: &InitFeatures,
98979916 ) -> Result<msgs::OpenChannel, ()>
98989917 where
98999918 F::Target: FeeEstimator,
99009919 L::Target: Logger,
99019920 {
9902- self.context.maybe_downgrade_channel_features(&mut self.funding, fee_estimator)?;
9921+ self.context.maybe_downgrade_channel_features(
9922+ &mut self.funding, fee_estimator, user_config, their_features,
9923+ )?;
99039924 self.get_open_channel(chain_hash, logger).ok_or(())
99049925 }
99059926
@@ -10405,12 +10426,15 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1040510426 /// not of our ability to open any channel at all. Thus, on error, we should first call this
1040610427 /// and see if we get a new `OpenChannelV2` message, otherwise the channel is failed.
1040710428 pub(crate) fn maybe_handle_error_without_close<F: Deref>(
10408- &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>
10429+ &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>,
10430+ user_config: &UserConfig, their_features: &InitFeatures,
1040910431 ) -> Result<msgs::OpenChannelV2, ()>
1041010432 where
1041110433 F::Target: FeeEstimator
1041210434 {
10413- self.context.maybe_downgrade_channel_features(&mut self.funding, fee_estimator)?;
10435+ self.context.maybe_downgrade_channel_features(
10436+ &mut self.funding, fee_estimator, user_config, their_features,
10437+ )?;
1041410438 Ok(self.get_open_channel_v2(chain_hash))
1041510439 }
1041610440
0 commit comments