@@ -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)
@@ -3072,12 +3077,18 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3072
3077
debug_assert!(!channel_type.supports_any_optional_bits());
3073
3078
debug_assert!(!channel_type.requires_unknown_bits_from(&channelmanager::provided_channel_type_features(&config)));
3074
3079
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
+ };
3081
3092
3082
3093
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
3083
3094
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 {
4868
4879
/// of the channel type we tried, not of our ability to open any channel at all. We can see if a
4869
4880
/// downgrade of channel features would be possible so that we can still open the channel.
4870
4881
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,
4872
4884
) -> Result<(), ()>
4873
4885
where
4874
4886
F::Target: FeeEstimator
@@ -4885,25 +4897,47 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
4885
4897
// We've exhausted our options
4886
4898
return Err(());
4887
4899
}
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
+
4888
4906
// We support opening a few different types of channels. Try removing our additional
4889
4907
// 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();
4902
4917
} 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
4904
4935
} 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
+
4907
4941
Ok(())
4908
4942
}
4909
4943
@@ -5262,6 +5296,15 @@ impl<SP: Deref> FundedChannel<SP> where
5262
5296
feerate_per_kw: u32, cur_feerate_per_kw: Option<u32>, logger: &L
5263
5297
) -> Result<(), ChannelError> where F::Target: FeeEstimator, L::Target: Logger,
5264
5298
{
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
+
5265
5308
let lower_limit_conf_target = if channel_type.supports_anchors_zero_fee_htlc_tx() {
5266
5309
ConfirmationTarget::MinAllowedAnchorChannelRemoteFee
5267
5310
} else {
@@ -9893,13 +9936,16 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
9893
9936
/// not of our ability to open any channel at all. Thus, on error, we should first call this
9894
9937
/// and see if we get a new `OpenChannel` message, otherwise the channel is failed.
9895
9938
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,
9897
9941
) -> Result<msgs::OpenChannel, ()>
9898
9942
where
9899
9943
F::Target: FeeEstimator,
9900
9944
L::Target: Logger,
9901
9945
{
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
+ )?;
9903
9949
self.get_open_channel(chain_hash, logger).ok_or(())
9904
9950
}
9905
9951
@@ -10078,8 +10124,9 @@ pub(super) fn channel_type_from_open_channel(
10078
10124
10079
10125
// We only support the channel types defined by the `ChannelManager` in
10080
10126
// `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() {
10083
10130
return Err(ChannelError::close("Channel Type was not understood - we require static remote key".to_owned()));
10084
10131
}
10085
10132
// 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 {
10405
10452
/// not of our ability to open any channel at all. Thus, on error, we should first call this
10406
10453
/// and see if we get a new `OpenChannelV2` message, otherwise the channel is failed.
10407
10454
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,
10409
10457
) -> Result<msgs::OpenChannelV2, ()>
10410
10458
where
10411
10459
F::Target: FeeEstimator
10412
10460
{
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
+ )?;
10414
10464
Ok(self.get_open_channel_v2(chain_hash))
10415
10465
}
10416
10466
@@ -10663,10 +10713,21 @@ fn get_initial_channel_type(config: &UserConfig, their_features: &InitFeatures)
10663
10713
ret.set_scid_privacy_required();
10664
10714
}
10665
10715
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 &&
10670
10731
their_features.supports_anchors_zero_fee_htlc_tx() {
10671
10732
ret.set_anchors_zero_fee_htlc_tx_required();
10672
10733
}
@@ -13267,6 +13328,45 @@ mod tests {
13267
13328
fn test_supports_anchors_zero_htlc_tx_fee() {
13268
13329
// Tests that if both sides support and negotiate `anchors_zero_fee_htlc_tx`, it is the
13269
13330
// 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) {
13270
13370
let secp_ctx = Secp256k1::new();
13271
13371
let fee_estimator = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
13272
13372
let network = Network::Testnet;
@@ -13276,21 +13376,13 @@ mod tests {
13276
13376
let node_id_a = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[1; 32]).unwrap());
13277
13377
let node_id_b = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[2; 32]).unwrap());
13278
13378
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.
13284
13380
let channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
13285
13381
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
13286
13382
&channelmanager::provided_init_features(&UserConfig::default()), 10000000, 100000, 42,
13287
13383
&config, 0, 42, None, &logger
13288
13384
).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());
13294
13386
13295
13387
let mut channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
13296
13388
&fee_estimator, &&keys_provider, &&keys_provider, node_id_b,
@@ -13307,6 +13399,14 @@ mod tests {
13307
13399
13308
13400
assert_eq!(channel_a.funding.get_channel_type(), &expected_channel_type);
13309
13401
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
+ }
13310
13410
}
13311
13411
13312
13412
#[test]
0 commit comments