@@ -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
@@ -4885,25 +4891,35 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
48854891 // We've exhausted our options
48864892 return Err(());
48874893 }
4894+
4895+ // We should never have negotiated `anchors_nonzero_fee_htlc_tx` because it can result in a
4896+ // loss of funds.
4897+ let channel_type = &funding.channel_transaction_parameters.channel_type_features;
4898+ assert!(!channel_type.supports_anchors_nonzero_fee_htlc_tx());
4899+
48884900 // We support opening a few different types of channels. Try removing our additional
48894901 // features one by one until we've either arrived at our default or the counterparty has
4890- // accepted one.
4891- //
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;
4902+ // accepted one. Features are un-set for the current channel type or any that come before
4903+ // it in our order of preference, allowing us to negotiate the "next best" based on the
4904+ // counterparty's remaining features per our ranking in `get_initial_channel_type`.
4905+ let mut eligible_features = their_features.clone();
48984906 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());
4907+ eligible_features.clear_anchors_zero_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();
49064911 }
4912+
4913+ let next_channel_type = get_initial_channel_type(user_config, &eligible_features);
4914+
4915+ let conf_target = if next_channel_type.supports_anchors_zero_fee_htlc_tx() {
4916+ ConfirmationTarget::AnchorChannelFee
4917+ } else {
4918+ ConfirmationTarget::NonAnchorChannelFee
4919+ };
4920+ self.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(conf_target);
4921+ funding.channel_transaction_parameters.channel_type_features = next_channel_type;
4922+
49074923 Ok(())
49084924 }
49094925
@@ -9893,13 +9909,16 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
98939909 /// not of our ability to open any channel at all. Thus, on error, we should first call this
98949910 /// and see if we get a new `OpenChannel` message, otherwise the channel is failed.
98959911 pub(crate) fn maybe_handle_error_without_close<F: Deref, L: Deref>(
9896- &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
9912+ &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
9913+ user_config: &UserConfig, their_features: &InitFeatures,
98979914 ) -> Result<msgs::OpenChannel, ()>
98989915 where
98999916 F::Target: FeeEstimator,
99009917 L::Target: Logger,
99019918 {
9902- self.context.maybe_downgrade_channel_features(&mut self.funding, fee_estimator)?;
9919+ self.context.maybe_downgrade_channel_features(
9920+ &mut self.funding, fee_estimator, user_config, their_features,
9921+ )?;
99039922 self.get_open_channel(chain_hash, logger).ok_or(())
99049923 }
99059924
@@ -10405,12 +10424,15 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1040510424 /// not of our ability to open any channel at all. Thus, on error, we should first call this
1040610425 /// and see if we get a new `OpenChannelV2` message, otherwise the channel is failed.
1040710426 pub(crate) fn maybe_handle_error_without_close<F: Deref>(
10408- &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>
10427+ &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>,
10428+ user_config: &UserConfig, their_features: &InitFeatures,
1040910429 ) -> Result<msgs::OpenChannelV2, ()>
1041010430 where
1041110431 F::Target: FeeEstimator
1041210432 {
10413- self.context.maybe_downgrade_channel_features(&mut self.funding, fee_estimator)?;
10433+ self.context.maybe_downgrade_channel_features(
10434+ &mut self.funding, fee_estimator, user_config, their_features,
10435+ )?;
1041410436 Ok(self.get_open_channel_v2(chain_hash))
1041510437 }
1041610438
0 commit comments