Skip to content

Commit 1ca8750

Browse files
committed
fix: rfc ElementsProject#740 requires 100% feespike margin
1 parent 71174a7 commit 1ca8750

File tree

4 files changed

+55
-34
lines changed

4 files changed

+55
-34
lines changed

channeld/full_channel.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ static struct amount_sat fee_for_htlcs(const struct channel *channel,
390390
return commit_tx_base_fee(feerate, untrimmed);
391391
}
392392

393-
/* There is a corner case where the funder can spend so much that the
393+
/*
394+
* There is a corner case where the funder can spend so much that the
394395
* non-funder can't add any non-dust HTLCs (since the funder would
395396
* have to pay the additional fee, but it can't afford to). This
396397
* leads to the channel starving at the feast! This was reported by
@@ -399,17 +400,30 @@ static struct amount_sat fee_for_htlcs(const struct channel *channel,
399400
* demonstrated with c-lightning by @m-schmook
400401
* (https://github.com/ElementsProject/lightning/pull/3498).
401402
*
403+
* This will be mitigated by applying a 2-times fee spike margin:
404+
* https://github.com/lightningnetwork/lightning-rfc/issues/740
405+
*
406+
* BOLT #2: Adding an HTLC: `update_add_htlc`
407+
*
408+
* A sending node, if it is responsible for paying the Bitcoin fee,
409+
* SHOULD NOT offer amount_msat if, after adding that HTLC to its
410+
* commitment transaction, its remaining balance doesn't allow it to
411+
* pay the fee for a future additional non-dust HTLC at 2*feerate_per_kw
412+
* while maintaining its channel reserve ("fee spike buffer").
413+
*
414+
*
402415
* To mostly avoid this situation, at least from our side, we apply an
403416
* additional constraint when we're funder trying to add an HTLC: make
404-
* sure we can afford one more HTLC, even if fees increase 50%.
417+
* sure we can afford one more HTLC, even if fees increase by 100%.
405418
*
406419
* We could do this for the peer, as well, by rejecting their HTLC
407420
* immediately in this case. But rejecting a remote HTLC here causes
408421
* us to get upset with them and close the channel: we're not well
409422
* architected to reject HTLCs in channeld (it's usually lightningd's
410423
* job, but it doesn't have all the channel balance change calculation
411424
* logic. So we look after ourselves for now, and hope other nodes start
412-
* self-regulating too. */
425+
* self-regulating too.
426+
*/
413427
static bool local_funder_has_fee_headroom(const struct channel *channel,
414428
struct amount_msat remainder,
415429
const struct htlc **committed,
@@ -428,14 +442,14 @@ static bool local_funder_has_fee_headroom(const struct channel *channel,
428442
feerate,
429443
committed, adding, removing);
430444

431-
/* Now, how much would it cost us if feerate increases 50% and we added
445+
/* Now, how much would it cost us if feerate increases 100% and we added
432446
* another HTLC? */
433-
fee = commit_tx_base_fee(feerate + feerate/2, untrimmed + 1);
447+
fee = commit_tx_base_fee(2 * feerate, untrimmed + 1);
434448
if (amount_msat_greater_eq_sat(remainder, fee))
435449
return true;
436450

437-
status_debug("Adding HTLC would leave us only %s:"
438-
" we need %s for another HTLC if fees increase 50%% to %uperkw",
451+
status_debug("Adding HTLC would leave us only %s: we need %s for"
452+
" another HTLC if fees increase by 100%% to %uperkw",
439453
type_to_string(tmpctx, struct amount_msat, &remainder),
440454
type_to_string(tmpctx, struct amount_sat, &fee),
441455
feerate + feerate/2);

lightningd/peer_control.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,17 @@ static struct amount_sat commit_txfee_spend(const struct channel *channel,
551551
num_untrimmed_htlcs++;
552552
}
553553

554-
/* Funder is conservative: makes sure it allows an extra HTLC
555-
* even if feerate increases 50% */
556-
return commit_tx_base_fee(local_feerate + local_feerate / 2,
557-
num_untrimmed_htlcs + 1);
554+
/*
555+
* BOLT #2:
556+
* Adding an HTLC - update_add_htlc
557+
*
558+
* A sending node, if it is responsible for paying the Bitcoin fee,
559+
* SHOULD NOT offer amount_msat if, after adding that HTLC to its
560+
* commitment transaction, its remaining balance doesn't allow it to pay
561+
* the fee for a future additional non-dust HTLC at 2 * feerate_per_kw
562+
* while maintaining its channel reserve ("fee spike buffer").
563+
*/
564+
return commit_tx_base_fee(2 * local_feerate, num_untrimmed_htlcs + 1);
558565
}
559566

560567
/* Fee a commitment transaction would currently cost when receiving */
@@ -598,10 +605,17 @@ static struct amount_sat commit_txfee_recv(const struct channel *channel,
598605
num_untrimmed_htlcs++;
599606
}
600607

601-
/* Funder is conservative: makes sure it allows an extra HTLC
602-
* even if feerate increases 50% */
603-
return commit_tx_base_fee(feerate + feerate / 2,
604-
num_untrimmed_htlcs + 1);
608+
/*
609+
* BOLT #2:
610+
* Adding an HTLC - update_add_htlc
611+
*
612+
* A sending node, if it is responsible for paying the Bitcoin fee,
613+
* SHOULD NOT offer amount_msat if, after adding that HTLC to its
614+
* commitment transaction, its remaining balance doesn't allow it to pay
615+
* the fee for a future additional non-dust HTLC at 2 * feerate_per_kw
616+
* while maintaining its channel reserve ("fee spike buffer").
617+
*/
618+
return commit_tx_base_fee(2 * feerate, num_untrimmed_htlcs + 1);
605619
}
606620

607621

tests/test_connection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,7 +2207,7 @@ def test_change_chaining(node_factory, bitcoind):
22072207
def test_feerate_spam(node_factory, chainparams):
22082208
l1, l2 = node_factory.line_graph(2)
22092209

2210-
slack = 35000000
2210+
slack = 45000000
22112211
# Pay almost everything to l2.
22122212
l1.pay(l2, 10**9 - slack)
22132213

@@ -2218,8 +2218,8 @@ def test_feerate_spam(node_factory, chainparams):
22182218
# Now change feerates to something l1 can't afford.
22192219
l1.set_feerates((100000, 100000, 100000))
22202220

2221-
# It will raise as far as it can (34000)
2222-
l1.daemon.wait_for_log('Setting REMOTE feerate to 34000')
2221+
# It will raise as far as it can (48000)
2222+
l1.daemon.wait_for_log('Setting REMOTE feerate to 48000')
22232223
l1.daemon.wait_for_log('peer_out WIRE_UPDATE_FEE')
22242224

22252225
# But it won't do it again once it's at max.

tests/test_pay.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -584,37 +584,30 @@ def test_sendpay_cant_afford(node_factory):
584584
opts={'feerates': (15000, 15000, 15000)})
585585

586586
# Can't pay more than channel capacity.
587-
def pay(lsrc, ldst, amt, label=None):
588-
if not label:
589-
label = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(20))
590-
rhash = ldst.rpc.invoice(amt, label, label)['payment_hash']
591-
routestep = {'msatoshi': amt, 'id': ldst.info['id'], 'delay': 5, 'channel': '1x1x1'}
592-
lsrc.rpc.sendpay([routestep], rhash)
593-
lsrc.rpc.waitsendpay(rhash)
594-
595587
with pytest.raises(RpcError):
596-
pay(l1, l2, 10**9 + 1)
588+
l1.pay(l2, 10**9 + 1)
597589

598590
# This is the fee, which needs to be taken into account for l1.
599-
available = 10**9 - 24030000
591+
print(l1.rpc.listpeers())
592+
available = 10**9 - 32040000
600593
# Reserve is 1%.
601594
reserve = 10**7
602595

603596
# Can't pay past reserve.
604597
with pytest.raises(RpcError):
605-
pay(l1, l2, available)
598+
l1.pay(l2, available)
606599
with pytest.raises(RpcError):
607-
pay(l1, l2, available - reserve + 1)
600+
l1.pay(l2, available - reserve + 1)
608601

609602
# Can pay up to reserve (1%)
610-
pay(l1, l2, available - reserve)
603+
l1.pay(l2, available - reserve)
611604

612605
# And now it can't pay back, due to its own reserve.
613606
with pytest.raises(RpcError):
614-
pay(l2, l1, available - reserve)
607+
l2.pay(l1, available - reserve)
615608

616609
# But this should work.
617-
pay(l2, l1, available - reserve * 2)
610+
l2.pay(l1, available - reserve * 2)
618611

619612

620613
def test_decodepay(node_factory):
@@ -2354,7 +2347,7 @@ def test_lockup_drain(node_factory, bitcoind):
23542347

23552348
# But if feerate increase just a little more, l2 should not be able to send
23562349
# non-fust HTLC to l1
2357-
l1.force_feerates(22502) # TODO: Why does 22501 fail? off by one in C code?
2350+
l1.force_feerates(30002) # TODO: Why does 30001 fail? off by one in C code?
23582351
with pytest.raises(RpcError, match=r".*Capacity exceeded.*"):
23592352
l2.pay(l1, total // 2)
23602353

0 commit comments

Comments
 (0)