@@ -1522,6 +1522,7 @@ impl<SP: Deref> Channel<SP> where
15221522
15231523 pub fn maybe_handle_error_without_close<F: Deref, L: Deref>(
15241524 &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
1525+ user_config: &UserConfig, their_features: &InitFeatures,
15251526 ) -> Result<Option<OpenChannelMessage>, ()>
15261527 where
15271528 F::Target: FeeEstimator,
@@ -1532,13 +1533,17 @@ impl<SP: Deref> Channel<SP> where
15321533 ChannelPhase::Funded(_) => Ok(None),
15331534 ChannelPhase::UnfundedOutboundV1(chan) => {
15341535 let logger = WithChannelContext::from(logger, &chan.context, None);
1535- chan.maybe_handle_error_without_close(chain_hash, fee_estimator, &&logger)
1536+ chan.maybe_handle_error_without_close(
1537+ chain_hash, fee_estimator, &&logger, user_config, their_features,
1538+ )
15361539 .map(|msg| Some(OpenChannelMessage::V1(msg)))
15371540 },
15381541 ChannelPhase::UnfundedInboundV1(_) => Ok(None),
15391542 ChannelPhase::UnfundedV2(chan) => {
15401543 if chan.funding.is_outbound() {
1541- chan.maybe_handle_error_without_close(chain_hash, fee_estimator)
1544+ chan.maybe_handle_error_without_close(
1545+ chain_hash, fee_estimator, user_config, their_features,
1546+ )
15421547 .map(|msg| Some(OpenChannelMessage::V2(msg)))
15431548 } else {
15441549 Ok(None)
@@ -4833,7 +4838,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
48334838 /// of the channel type we tried, not of our ability to open any channel at all. We can see if a
48344839 /// downgrade of channel features would be possible so that we can still open the channel.
48354840 pub(crate) fn maybe_downgrade_channel_features<F: Deref>(
4836- &mut self, funding: &mut FundingScope, fee_estimator: &LowerBoundedFeeEstimator<F>
4841+ &mut self, funding: &mut FundingScope, fee_estimator: &LowerBoundedFeeEstimator<F>,
4842+ user_config: &UserConfig, their_features: &InitFeatures,
48374843 ) -> Result<(), ()>
48384844 where
48394845 F::Target: FeeEstimator
@@ -4854,21 +4860,33 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
48544860 // features one by one until we've either arrived at our default or the counterparty has
48554861 // accepted one.
48564862 //
4857- // Due to the order below, we may not negotiate `option_anchors_zero_fee_htlc_tx` if the
4858- // counterparty doesn't support `option_scid_privacy`. Since `get_initial_channel_type`
4859- // checks whether the counterparty supports every feature, this would only happen if the
4860- // counterparty is advertising the feature, but rejecting channels proposing the feature for
4861- // whatever reason.
4862- let channel_type = &mut funding.channel_transaction_parameters.channel_type_features;
4863+ // To downgrade the current channel type, we start with the remote party's full set of
4864+ // feature bits and un-set any features that are set for the current channel type or any
4865+ // channel types that come before it in our order of preference. This will allow us to
4866+ // negotiate the "next best" channel type per our ranking in `get_initial_channel_type`.
4867+ let channel_type = &funding.channel_transaction_parameters.channel_type_features;
4868+ let mut eligible_features = their_features.clone();
4869+
48634870 if channel_type.supports_anchors_zero_fee_htlc_tx() {
4864- channel_type.clear_anchors_zero_fee_htlc_tx();
4865- self.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
4866- assert!(!channel_type.supports_anchors_nonzero_fee_htlc_tx());
4871+ eligible_features.clear_anchors_zero_fee_htlc_tx();
4872+ assert!(!eligible_features.supports_anchors_nonzero_fee_htlc_tx());
48674873 } else if channel_type.supports_scid_privacy() {
4868- channel_type.clear_scid_privacy();
4869- } else {
4870- *channel_type = ChannelTypeFeatures::only_static_remote_key();
4874+ eligible_features.clear_scid_privacy();
4875+ eligible_features.clear_anchors_zero_fee_htlc_tx();
4876+ assert!(!eligible_features.supports_scid_privacy());
4877+ assert!(!eligible_features.supports_anchors_nonzero_fee_htlc_tx());
48714878 }
4879+
4880+ let next_channel_type = get_initial_channel_type(user_config, &eligible_features);
4881+
4882+ let conf_target = if next_channel_type.supports_anchors_zero_fee_htlc_tx() {
4883+ ConfirmationTarget::AnchorChannelFee
4884+ } else {
4885+ ConfirmationTarget::NonAnchorChannelFee
4886+ };
4887+ self.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(conf_target);
4888+ funding.channel_transaction_parameters.channel_type_features = next_channel_type;
4889+
48724890 Ok(())
48734891 }
48744892
@@ -9720,13 +9738,16 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
97209738 /// not of our ability to open any channel at all. Thus, on error, we should first call this
97219739 /// and see if we get a new `OpenChannel` message, otherwise the channel is failed.
97229740 pub(crate) fn maybe_handle_error_without_close<F: Deref, L: Deref>(
9723- &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
9741+ &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
9742+ user_config: &UserConfig, their_features: &InitFeatures,
97249743 ) -> Result<msgs::OpenChannel, ()>
97259744 where
97269745 F::Target: FeeEstimator,
97279746 L::Target: Logger,
97289747 {
9729- self.context.maybe_downgrade_channel_features(&mut self.funding, fee_estimator)?;
9748+ self.context.maybe_downgrade_channel_features(
9749+ &mut self.funding, fee_estimator, user_config, their_features,
9750+ )?;
97309751 self.get_open_channel(chain_hash, logger).ok_or(())
97319752 }
97329753
@@ -10234,12 +10255,15 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1023410255 /// not of our ability to open any channel at all. Thus, on error, we should first call this
1023510256 /// and see if we get a new `OpenChannelV2` message, otherwise the channel is failed.
1023610257 pub(crate) fn maybe_handle_error_without_close<F: Deref>(
10237- &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>
10258+ &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>,
10259+ user_config: &UserConfig, their_features: &InitFeatures,
1023810260 ) -> Result<msgs::OpenChannelV2, ()>
1023910261 where
1024010262 F::Target: FeeEstimator
1024110263 {
10242- self.context.maybe_downgrade_channel_features(&mut self.funding, fee_estimator)?;
10264+ self.context.maybe_downgrade_channel_features(
10265+ &mut self.funding, fee_estimator, user_config, their_features,
10266+ )?;
1024310267 Ok(self.get_open_channel_v2(chain_hash))
1024410268 }
1024510269
0 commit comments