Skip to content

Commit eb55611

Browse files
committed
f use a const for htlc affordability count
1 parent 4c49fb9 commit eb55611

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

lightning/src/ln/channel.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,11 @@ pub const FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE: u64 = 2;
378378
#[cfg(not(fuzzing))]
379379
const FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE: u64 = 2;
380380

381+
/// When a channel is opened, we check that the funding amount is enough to pay for relevant
382+
/// commitment transaction fees, with at least this many HTLCs present on the commitment
383+
/// transaction (not counting the value of the HTLCs themselves).
384+
pub(crate) const MIN_AFFORDABLE_HTLC_COUNT: usize = 4;
385+
381386
// TODO: We should refactor this to be an Inbound/OutboundChannel until initial setup handshaking
382387
// has been completed, and then turn into a Channel to get compiler-time enforcement of things like
383388
// calling channel_id() before we're set up or things like get_outbound_funding_signed on an
@@ -672,7 +677,7 @@ impl<Signer: Sign> Channel<Signer> {
672677
let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
673678

674679
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
675-
let commitment_tx_fee = Self::commit_tx_fee_msat(feerate, 4);
680+
let commitment_tx_fee = Self::commit_tx_fee_msat(feerate, MIN_AFFORDABLE_HTLC_COUNT);
676681
if value_to_self_msat < commitment_tx_fee {
677682
return Err(APIError::APIMisuseError{ err: format!("Funding amount ({}) can't even pay fee for initial commitment transaction fee of {}.", value_to_self_msat / 1000, commitment_tx_fee / 1000) });
678683
}
@@ -929,10 +934,9 @@ impl<Signer: Sign> Channel<Signer> {
929934
}
930935

931936
// check if the funder's amount for the initial commitment tx is sufficient
932-
// for full fee payment. We somewhat arbitrarily pick 4 HTLC outputs here - the funder
933-
// should be able to afford at least that many.
937+
// for full fee payment plus a few HTLCs to ensure the channel will be useful.
934938
let funders_amount_msat = msg.funding_satoshis * 1000 - msg.push_msat;
935-
let commitment_tx_fee = Self::commit_tx_fee_msat(msg.feerate_per_kw, 4) / 1000;
939+
let commitment_tx_fee = Self::commit_tx_fee_msat(msg.feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT) / 1000;
936940
if funders_amount_msat / 1000 < commitment_tx_fee {
937941
return Err(ChannelError::Close(format!("Funding amount ({} sats) can't even pay fee for initial commitment transaction fee of {} sats.", funders_amount_msat / 1000, commitment_tx_fee)));
938942
}

lightning/src/ln/functional_tests.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use chain::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PER
1818
use chain::transaction::OutPoint;
1919
use chain::keysinterface::BaseSign;
2020
use ln::{PaymentPreimage, PaymentSecret, PaymentHash};
21-
use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
21+
use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC, MIN_AFFORDABLE_HTLC_COUNT};
2222
use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, PaymentId, RAACommitmentOrder, PaymentSendFailure, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA};
2323
use ln::channel::{Channel, ChannelError};
2424
use ln::{chan_utils, onion_utils};
@@ -1398,16 +1398,15 @@ fn test_chan_reserve_violation_outbound_htlc_inbound_chan() {
13981398
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
13991399

14001400
let mut push_amt = 100_000_000;
1401-
push_amt -= commit_tx_fee_msat(feerate_per_kw, 4);
1401+
push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64);
14021402
push_amt -= Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100_000) * 1000;
14031403

14041404
let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, push_amt, InitFeatures::known(), InitFeatures::known());
14051405

14061406
// Sending exactly enough to hit the reserve amount should be accepted
1407-
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1408-
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1409-
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1410-
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1407+
for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
1408+
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1409+
}
14111410

14121411
// However one more HTLC should be significantly over the reserve amount and fail.
14131412
let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 1_000_000);
@@ -1429,15 +1428,14 @@ fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
14291428
// channel reserve violation (so their balance is channel reserve (1000 sats) + commitment
14301429
// transaction fee with 0 HTLCs (183 sats)).
14311430
let mut push_amt = 100_000_000;
1432-
push_amt -= commit_tx_fee_msat(feerate_per_kw, 4);
1431+
push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64);
14331432
push_amt -= Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100_000) * 1000;
14341433
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, push_amt, InitFeatures::known(), InitFeatures::known());
14351434

14361435
// Send four HTLCs to cover the initial push_msat buffer we're required to include
1437-
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1438-
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1439-
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1440-
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1436+
for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
1437+
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1438+
}
14411439

14421440
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 700_000);
14431441
// Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
@@ -1449,7 +1447,7 @@ fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
14491447
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash);
14501448
let msg = msgs::UpdateAddHTLC {
14511449
channel_id: chan.2,
1452-
htlc_id: 4,
1450+
htlc_id: MIN_AFFORDABLE_HTLC_COUNT as u64,
14531451
amount_msat: htlc_msat,
14541452
payment_hash: payment_hash,
14551453
cltv_expiry: htlc_cltv,
@@ -1481,7 +1479,7 @@ fn test_chan_reserve_dust_inbound_htlcs_outbound_chan() {
14811479
// channel reserve violation (so their balance is channel reserve (1000 sats) + commitment
14821480
// transaction fee with 0 HTLCs (183 sats)).
14831481
let mut push_amt = 100_000_000;
1484-
push_amt -= commit_tx_fee_msat(feerate_per_kw, 4);
1482+
push_amt -= commit_tx_fee_msat(feerate_per_kw, MIN_AFFORDABLE_HTLC_COUNT as u64);
14851483
push_amt -= Channel::<EnforcingSigner>::get_holder_selected_channel_reserve_satoshis(100_000) * 1000;
14861484
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, push_amt, InitFeatures::known(), InitFeatures::known());
14871485

@@ -1493,10 +1491,9 @@ fn test_chan_reserve_dust_inbound_htlcs_outbound_chan() {
14931491
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], dust_amt);
14941492

14951493
// Send four HTLCs to cover the initial push_msat buffer we're required to include
1496-
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1497-
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1498-
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1499-
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1494+
for _ in 0..MIN_AFFORDABLE_HTLC_COUNT {
1495+
let (_, _, _) = route_payment(&nodes[1], &[&nodes[0]], 1_000_000);
1496+
}
15001497

15011498
// One more than the dust amt should fail, however.
15021499
let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], dust_amt + 1);

0 commit comments

Comments
 (0)