@@ -1557,6 +1557,7 @@ impl<SP: Deref> Channel<SP> where
1557
1557
1558
1558
pub fn maybe_handle_error_without_close<F: Deref, L: Deref>(
1559
1559
&mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
1560
+ user_config: &UserConfig, their_features: &InitFeatures,
1560
1561
) -> Result<Option<OpenChannelMessage>, ()>
1561
1562
where
1562
1563
F::Target: FeeEstimator,
@@ -1567,13 +1568,17 @@ impl<SP: Deref> Channel<SP> where
1567
1568
ChannelPhase::Funded(_) => Ok(None),
1568
1569
ChannelPhase::UnfundedOutboundV1(chan) => {
1569
1570
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
+ )
1571
1574
.map(|msg| Some(OpenChannelMessage::V1(msg)))
1572
1575
},
1573
1576
ChannelPhase::UnfundedInboundV1(_) => Ok(None),
1574
1577
ChannelPhase::UnfundedV2(chan) => {
1575
1578
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
+ )
1577
1582
.map(|msg| Some(OpenChannelMessage::V2(msg)))
1578
1583
} else {
1579
1584
Ok(None)
@@ -4868,7 +4873,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
4868
4873
/// of the channel type we tried, not of our ability to open any channel at all. We can see if a
4869
4874
/// downgrade of channel features would be possible so that we can still open the channel.
4870
4875
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,
4872
4878
) -> Result<(), ()>
4873
4879
where
4874
4880
F::Target: FeeEstimator
@@ -4885,25 +4891,35 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
4885
4891
// We've exhausted our options
4886
4892
return Err(());
4887
4893
}
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
+
4888
4900
// We support opening a few different types of channels. Try removing our additional
4889
4901
// 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();
4898
4906
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();
4902
4908
} 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();
4906
4911
}
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
+
4907
4923
Ok(())
4908
4924
}
4909
4925
@@ -9893,13 +9909,16 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
9893
9909
/// not of our ability to open any channel at all. Thus, on error, we should first call this
9894
9910
/// and see if we get a new `OpenChannel` message, otherwise the channel is failed.
9895
9911
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,
9897
9914
) -> Result<msgs::OpenChannel, ()>
9898
9915
where
9899
9916
F::Target: FeeEstimator,
9900
9917
L::Target: Logger,
9901
9918
{
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
+ )?;
9903
9922
self.get_open_channel(chain_hash, logger).ok_or(())
9904
9923
}
9905
9924
@@ -10405,12 +10424,15 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
10405
10424
/// not of our ability to open any channel at all. Thus, on error, we should first call this
10406
10425
/// and see if we get a new `OpenChannelV2` message, otherwise the channel is failed.
10407
10426
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,
10409
10429
) -> Result<msgs::OpenChannelV2, ()>
10410
10430
where
10411
10431
F::Target: FeeEstimator
10412
10432
{
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
+ )?;
10414
10436
Ok(self.get_open_channel_v2(chain_hash))
10415
10437
}
10416
10438
0 commit comments