@@ -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)
@@ -3072,12 +3077,18 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30723077 debug_assert!(!channel_type.supports_any_optional_bits());
30733078 debug_assert!(!channel_type.requires_unknown_bits_from(&channelmanager::provided_channel_type_features(&config)));
30743079
3075- let (commitment_conf_target, anchor_outputs_value_msat) = if channel_type.supports_anchors_zero_fee_htlc_tx() {
3076- (ConfirmationTarget::AnchorChannelFee, ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000)
3077- } else {
3078- (ConfirmationTarget::NonAnchorChannelFee, 0)
3079- };
3080- let commitment_feerate = fee_estimator.bounded_sat_per_1000_weight(commitment_conf_target);
3080+ let (commitment_feerate, anchor_outputs_value_msat) =
3081+ if channel_type.supports_anchor_zero_fee_commitments() {
3082+ (0, 0)
3083+ } else if channel_type.supports_anchors_zero_fee_htlc_tx() {
3084+ let feerate = fee_estimator
3085+ .bounded_sat_per_1000_weight(ConfirmationTarget::AnchorChannelFee);
3086+ (feerate, ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000)
3087+ } else {
3088+ let feerate = fee_estimator
3089+ .bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
3090+ (feerate, 0)
3091+ };
30813092
30823093 let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
30833094 let commitment_tx_fee = commit_tx_fee_sat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type) * 1000;
@@ -4868,7 +4879,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
48684879 /// of the channel type we tried, not of our ability to open any channel at all. We can see if a
48694880 /// downgrade of channel features would be possible so that we can still open the channel.
48704881 pub(crate) fn maybe_downgrade_channel_features<F: Deref>(
4871- &mut self, funding: &mut FundingScope, fee_estimator: &LowerBoundedFeeEstimator<F>
4882+ &mut self, funding: &mut FundingScope, fee_estimator: &LowerBoundedFeeEstimator<F>,
4883+ user_config: &UserConfig, their_features: &InitFeatures,
48724884 ) -> Result<(), ()>
48734885 where
48744886 F::Target: FeeEstimator
@@ -4885,25 +4897,47 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
48854897 // We've exhausted our options
48864898 return Err(());
48874899 }
4900+
4901+ // We should never have negotiated `anchors_nonzero_fee_htlc_tx` because it can result in a
4902+ // loss of funds.
4903+ let channel_type = &funding.channel_transaction_parameters.channel_type_features;
4904+ assert!(!channel_type.supports_anchors_nonzero_fee_htlc_tx());
4905+
48884906 // We support opening a few different types of channels. Try removing our additional
48894907 // 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;
4898- 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());
4908+ // accepted one. Features are un-set for the current channel type or any that come before
4909+ // it in our order of preference, allowing us to negotiate the "next best" based on the
4910+ // counterparty's remaining features per our ranking in `get_initial_channel_type`.
4911+ let mut eligible_features = their_features.clone();
4912+ if channel_type.supports_anchor_zero_fee_commitments() {
4913+ eligible_features.clear_anchor_zero_fee_commitments();
4914+ } else if channel_type.supports_anchors_zero_fee_htlc_tx() {
4915+ eligible_features.clear_anchor_zero_fee_commitments();
4916+ eligible_features.clear_anchors_zero_fee_htlc_tx();
49024917 } else if channel_type.supports_scid_privacy() {
4903- channel_type.clear_scid_privacy();
4918+ eligible_features.clear_scid_privacy();
4919+ eligible_features.clear_anchors_zero_fee_htlc_tx();
4920+ eligible_features.clear_anchor_zero_fee_commitments();
4921+ }
4922+
4923+ let next_channel_type = get_initial_channel_type(user_config, &eligible_features);
4924+
4925+ // Note that we can't get `anchor_zero_fee_commitments` type here, which requires zero
4926+ // fees, because we downgrade from this channel type first. If there were a superior
4927+ // channel type that downgrades to `anchor_zero_fee_commitments`, we'd need to handle
4928+ // fee setting differently here. If we proceeded to open a `anchor_zero_fee_commitments`
4929+ // channel with non-zero fees, we could produce a non-standard commitment transaction that
4930+ // puts us at risk of losing funds. We would expect our peer to reject such a channel
4931+ // open, but we don't want to rely on their validation.
4932+ assert!(!next_channel_type.supports_anchor_zero_fee_commitments());
4933+ let conf_target = if next_channel_type.supports_anchors_zero_fee_htlc_tx() {
4934+ ConfirmationTarget::AnchorChannelFee
49044935 } else {
4905- *channel_type = ChannelTypeFeatures::only_static_remote_key();
4906- }
4936+ ConfirmationTarget::NonAnchorChannelFee
4937+ };
4938+ self.feerate_per_kw = fee_estimator.bounded_sat_per_1000_weight(conf_target);
4939+ funding.channel_transaction_parameters.channel_type_features = next_channel_type;
4940+
49074941 Ok(())
49084942 }
49094943
@@ -5262,6 +5296,15 @@ impl<SP: Deref> FundedChannel<SP> where
52625296 feerate_per_kw: u32, cur_feerate_per_kw: Option<u32>, logger: &L
52635297 ) -> Result<(), ChannelError> where F::Target: FeeEstimator, L::Target: Logger,
52645298 {
5299+ if channel_type.supports_anchor_zero_fee_commitments() {
5300+ if feerate_per_kw != 0 {
5301+ let err = "Zero Fee Channels must never attempt to use a fee".to_owned();
5302+ return Err(ChannelError::close(err));
5303+ } else {
5304+ return Ok(());
5305+ }
5306+ }
5307+
52655308 let lower_limit_conf_target = if channel_type.supports_anchors_zero_fee_htlc_tx() {
52665309 ConfirmationTarget::MinAllowedAnchorChannelRemoteFee
52675310 } else {
@@ -9893,13 +9936,16 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
98939936 /// not of our ability to open any channel at all. Thus, on error, we should first call this
98949937 /// and see if we get a new `OpenChannel` message, otherwise the channel is failed.
98959938 pub(crate) fn maybe_handle_error_without_close<F: Deref, L: Deref>(
9896- &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L
9939+ &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
9940+ user_config: &UserConfig, their_features: &InitFeatures,
98979941 ) -> Result<msgs::OpenChannel, ()>
98989942 where
98999943 F::Target: FeeEstimator,
99009944 L::Target: Logger,
99019945 {
9902- self.context.maybe_downgrade_channel_features(&mut self.funding, fee_estimator)?;
9946+ self.context.maybe_downgrade_channel_features(
9947+ &mut self.funding, fee_estimator, user_config, their_features,
9948+ )?;
99039949 self.get_open_channel(chain_hash, logger).ok_or(())
99049950 }
99059951
@@ -10078,8 +10124,9 @@ pub(super) fn channel_type_from_open_channel(
1007810124
1007910125 // We only support the channel types defined by the `ChannelManager` in
1008010126 // `provided_channel_type_features`. The channel type must always support
10081- // `static_remote_key`.
10082- if !channel_type.requires_static_remote_key() {
10127+ // `static_remote_key`, either implicitly with `option_zero_fee_commitments`
10128+ // or explicitly.
10129+ if !channel_type.requires_static_remote_key() && !channel_type.requires_anchor_zero_fee_commitments() {
1008310130 return Err(ChannelError::close("Channel Type was not understood - we require static remote key".to_owned()));
1008410131 }
1008510132 // Make sure we support all of the features behind the channel type.
@@ -10405,12 +10452,15 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1040510452 /// not of our ability to open any channel at all. Thus, on error, we should first call this
1040610453 /// and see if we get a new `OpenChannelV2` message, otherwise the channel is failed.
1040710454 pub(crate) fn maybe_handle_error_without_close<F: Deref>(
10408- &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>
10455+ &mut self, chain_hash: ChainHash, fee_estimator: &LowerBoundedFeeEstimator<F>,
10456+ user_config: &UserConfig, their_features: &InitFeatures,
1040910457 ) -> Result<msgs::OpenChannelV2, ()>
1041010458 where
1041110459 F::Target: FeeEstimator
1041210460 {
10413- self.context.maybe_downgrade_channel_features(&mut self.funding, fee_estimator)?;
10461+ self.context.maybe_downgrade_channel_features(
10462+ &mut self.funding, fee_estimator, user_config, their_features,
10463+ )?;
1041410464 Ok(self.get_open_channel_v2(chain_hash))
1041510465 }
1041610466
@@ -10663,10 +10713,21 @@ fn get_initial_channel_type(config: &UserConfig, their_features: &InitFeatures)
1066310713 ret.set_scid_privacy_required();
1066410714 }
1066510715
10666- // Optionally, if the user would like to negotiate the `anchors_zero_fee_htlc_tx` option, we
10667- // set it now. If they don't understand it, we'll fall back to our default of
10668- // `only_static_remotekey`.
10669- if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
10716+ // Optionally, if the user would like to negotiate `option_zero_fee_commitments` we set it now.
10717+ // If they don't understand it (or we don't want it), we check the same conditions for
10718+ // `option_anchors_zero_fee_htlc_tx`. The counterparty can still refuse the channel and we'll
10719+ // try to fall back (all the way to `only_static_remotekey`).
10720+ #[cfg(not(test))]
10721+ let negotiate_zero_fee_commitments = false;
10722+
10723+ #[cfg(test)]
10724+ let negotiate_zero_fee_commitments = config.channel_handshake_config.negotiate_anchor_zero_fee_commitments;
10725+
10726+ if negotiate_zero_fee_commitments && their_features.supports_anchor_zero_fee_commitments() {
10727+ ret.set_anchor_zero_fee_commitments_required();
10728+ // `option_static_remote_key` is assumed by `option_zero_fee_commitments`.
10729+ ret.clear_static_remote_key();
10730+ } else if config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx &&
1067010731 their_features.supports_anchors_zero_fee_htlc_tx() {
1067110732 ret.set_anchors_zero_fee_htlc_tx_required();
1067210733 }
@@ -13267,6 +13328,45 @@ mod tests {
1326713328 fn test_supports_anchors_zero_htlc_tx_fee() {
1326813329 // Tests that if both sides support and negotiate `anchors_zero_fee_htlc_tx`, it is the
1326913330 // resulting `channel_type`.
13331+ let mut config = UserConfig::default();
13332+ config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
13333+
13334+ let mut expected_channel_type = ChannelTypeFeatures::empty();
13335+ expected_channel_type.set_static_remote_key_required();
13336+ expected_channel_type.set_anchors_zero_fee_htlc_tx_required();
13337+
13338+ do_test_supports_channel_type(config, expected_channel_type)
13339+ }
13340+
13341+ #[test]
13342+ fn test_supports_zero_fee_commitments() {
13343+ // Tests that if both sides support and negotiate `anchors_zero_fee_commitments`, it is
13344+ // the resulting `channel_type`.
13345+ let mut config = UserConfig::default();
13346+ config.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
13347+
13348+ let mut expected_channel_type = ChannelTypeFeatures::empty();
13349+ expected_channel_type.set_anchor_zero_fee_commitments_required();
13350+
13351+ do_test_supports_channel_type(config, expected_channel_type)
13352+ }
13353+
13354+ #[test]
13355+ fn test_supports_zero_fee_commitments_and_htlc_tx_fee() {
13356+ // Tests that if both sides support and negotiate `anchors_zero_fee_commitments` and
13357+ // `anchors_zero_fee_htlc_tx`, the resulting `channel_type` is
13358+ // `anchors_zero_fee_commitments`.
13359+ let mut config = UserConfig::default();
13360+ config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
13361+ config.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
13362+
13363+ let mut expected_channel_type = ChannelTypeFeatures::empty();
13364+ expected_channel_type.set_anchor_zero_fee_commitments_required();
13365+
13366+ do_test_supports_channel_type(config, expected_channel_type)
13367+ }
13368+
13369+ fn do_test_supports_channel_type(config: UserConfig, expected_channel_type: ChannelTypeFeatures) {
1327013370 let secp_ctx = Secp256k1::new();
1327113371 let fee_estimator = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
1327213372 let network = Network::Testnet;
@@ -13276,21 +13376,13 @@ mod tests {
1327613376 let node_id_a = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
1327713377 let node_id_b = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
1327813378
13279- let mut config = UserConfig::default();
13280- config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
13281-
13282- // It is not enough for just the initiator to signal `option_anchors_zero_fee_htlc_tx`, both
13283- // need to signal it.
13379+ // Assert that we get `static_remotekey` when no custom config is negotiated.
1328413380 let channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
1328513381 &fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
1328613382 &channelmanager::provided_init_features(&UserConfig::default()), 10000000, 100000, 42,
1328713383 &config, 0, 42, None, &logger
1328813384 ).unwrap();
13289- assert!(!channel_a.funding.get_channel_type().supports_anchors_zero_fee_htlc_tx());
13290-
13291- let mut expected_channel_type = ChannelTypeFeatures::empty();
13292- expected_channel_type.set_static_remote_key_required();
13293- expected_channel_type.set_anchors_zero_fee_htlc_tx_required();
13385+ assert_eq!(channel_a.funding.get_channel_type(), &ChannelTypeFeatures::only_static_remote_key());
1329413386
1329513387 let mut channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
1329613388 &fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
@@ -13307,6 +13399,14 @@ mod tests {
1330713399
1330813400 assert_eq!(channel_a.funding.get_channel_type(), &expected_channel_type);
1330913401 assert_eq!(channel_b.funding.get_channel_type(), &expected_channel_type);
13402+
13403+ if expected_channel_type.supports_anchor_zero_fee_commitments() {
13404+ assert_eq!(channel_a.context.feerate_per_kw, 0);
13405+ assert_eq!(channel_b.context.feerate_per_kw, 0);
13406+ } else {
13407+ assert_ne!(channel_a.context.feerate_per_kw, 0);
13408+ assert_ne!(channel_b.context.feerate_per_kw, 0);
13409+ }
1331013410 }
1331113411
1331213412 #[test]
0 commit comments