Skip to content

Commit 07640a7

Browse files
carlaKCTheBlueMatt
andcommitted
ln: do not set or accept non-zero feerates in open_channel for v3
Sender: MUST set `feerate_per_kw` to zero Receiver: MUST fail the channel if `feerate_per_kw` != 0 Co-authored-by: Matt Corallo <[email protected]>
1 parent 855f4ca commit 07640a7

File tree

1 file changed

+57
-6
lines changed

1 file changed

+57
-6
lines changed

lightning/src/ln/channel.rs

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3041,12 +3041,18 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
30413041
debug_assert!(!channel_type.supports_any_optional_bits());
30423042
debug_assert!(!channel_type.requires_unknown_bits_from(&channelmanager::provided_channel_type_features(&config)));
30433043

3044-
let (commitment_conf_target, anchor_outputs_value_msat) = if channel_type.supports_anchors_zero_fee_htlc_tx() {
3045-
(ConfirmationTarget::AnchorChannelFee, ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000)
3046-
} else {
3047-
(ConfirmationTarget::NonAnchorChannelFee, 0)
3048-
};
3049-
let commitment_feerate = fee_estimator.bounded_sat_per_1000_weight(commitment_conf_target);
3044+
let (commitment_feerate, anchor_outputs_value_msat) =
3045+
if channel_type.supports_anchor_zero_fee_commitments() {
3046+
(0, 0)
3047+
} else if channel_type.supports_anchors_zero_fee_htlc_tx() {
3048+
let feerate = fee_estimator
3049+
.bounded_sat_per_1000_weight(ConfirmationTarget::AnchorChannelFee);
3050+
(feerate, ANCHOR_OUTPUT_VALUE_SATOSHI * 2 * 1000)
3051+
} else {
3052+
let feerate = fee_estimator
3053+
.bounded_sat_per_1000_weight(ConfirmationTarget::NonAnchorChannelFee);
3054+
(feerate, 0)
3055+
};
30503056

30513057
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
30523058
let commitment_tx_fee = commit_tx_fee_sat(commitment_feerate, MIN_AFFORDABLE_HTLC_COUNT, &channel_type) * 1000;
@@ -5256,6 +5262,15 @@ impl<SP: Deref> FundedChannel<SP> where
52565262
feerate_per_kw: u32, cur_feerate_per_kw: Option<u32>, logger: &L
52575263
) -> Result<(), ChannelError> where F::Target: FeeEstimator, L::Target: Logger,
52585264
{
5265+
if channel_type.supports_anchor_zero_fee_commitments() {
5266+
if feerate_per_kw != 0 {
5267+
let err = "Zero Fee Channels must never attempt to use a fee".to_owned();
5268+
return Err(ChannelError::close(err));
5269+
} else {
5270+
return Ok(());
5271+
}
5272+
}
5273+
52595274
let lower_limit_conf_target = if channel_type.supports_anchors_zero_fee_htlc_tx() {
52605275
ConfirmationTarget::MinAllowedAnchorChannelRemoteFee
52615276
} else {
@@ -13165,6 +13180,34 @@ mod tests {
1316513180
do_test_supports_channel_type(config, expected_channel_type)
1316613181
}
1316713182

13183+
#[test]
13184+
fn test_supports_zero_fee_commitments() {
13185+
// Tests that if both sides support and negotiate `anchors_zero_fee_commitments`, it is
13186+
// the resulting `channel_type`.
13187+
let mut config = UserConfig::default();
13188+
config.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
13189+
13190+
let mut expected_channel_type = ChannelTypeFeatures::empty();
13191+
expected_channel_type.set_anchor_zero_fee_commitments_required();
13192+
13193+
do_test_supports_channel_type(config, expected_channel_type)
13194+
}
13195+
13196+
#[test]
13197+
fn test_supports_zero_fee_commitments_and_htlc_tx_fee() {
13198+
// Tests that if both sides support and negotiate `anchors_zero_fee_commitments` and
13199+
// `anchors_zero_fee_htlc_tx`, the resulting `channel_type` is
13200+
// `anchors_zero_fee_commitments`.
13201+
let mut config = UserConfig::default();
13202+
config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
13203+
config.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
13204+
13205+
let mut expected_channel_type = ChannelTypeFeatures::empty();
13206+
expected_channel_type.set_anchor_zero_fee_commitments_required();
13207+
13208+
do_test_supports_channel_type(config, expected_channel_type)
13209+
}
13210+
1316813211
fn do_test_supports_channel_type(config: UserConfig, expected_channel_type: ChannelTypeFeatures) {
1316913212
let secp_ctx = Secp256k1::new();
1317013213
let fee_estimator = LowerBoundedFeeEstimator::new(&TestFeeEstimator{fee_est: 15000});
@@ -13198,6 +13241,14 @@ mod tests {
1319813241

1319913242
assert_eq!(channel_a.funding.get_channel_type(), &expected_channel_type);
1320013243
assert_eq!(channel_b.funding.get_channel_type(), &expected_channel_type);
13244+
13245+
if expected_channel_type.supports_anchor_zero_fee_commitments() {
13246+
assert_eq!(channel_a.context.feerate_per_kw, 0);
13247+
assert_eq!(channel_b.context.feerate_per_kw, 0);
13248+
} else {
13249+
assert_ne!(channel_a.context.feerate_per_kw, 0);
13250+
assert_ne!(channel_b.context.feerate_per_kw, 0);
13251+
}
1320113252
}
1320213253

1320313254
#[test]

0 commit comments

Comments
 (0)