Skip to content

Commit dc4e0a4

Browse files
ddustinrustyrussell
authored andcommitted
Splice: Better balance checking
* Regression test added for Issue #6572 (issuecomment-1730808863) w/stuck HTLC * `check_balance` adjusted to calculate pending HTLCs explicitly * Test confirmed to fail prior to PR #6713 ChangeLog-Fixed: Issue splicing with pending / stuck HTLCs fixed.
1 parent 13fec8d commit dc4e0a4

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed

channeld/channeld.c

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,13 +2939,35 @@ static struct amount_sat check_balances(struct peer *peer,
29392939
funding_amount_res, min_multiplied;
29402940
struct amount_msat funding_amount,
29412941
initiator_fee, accepter_fee;
2942-
struct amount_msat in[NUM_TX_ROLES], out[NUM_TX_ROLES];
2942+
struct amount_msat in[NUM_TX_ROLES], out[NUM_TX_ROLES],
2943+
pending_htlcs[NUM_TX_ROLES];
2944+
struct htlc_map_iter it;
2945+
const struct htlc *htlc;
29432946
bool opener = our_role == TX_INITIATOR;
29442947
u8 *msg;
29452948

2949+
/* The channel funds less any pending htlcs */
29462950
in[TX_INITIATOR] = peer->channel->view->owed[opener ? LOCAL : REMOTE];
29472951
in[TX_ACCEPTER] = peer->channel->view->owed[opener ? REMOTE : LOCAL];
29482952

2953+
/* pending_htlcs holds the value of all pending htlcs for each side */
2954+
pending_htlcs[TX_INITIATOR] = AMOUNT_MSAT(0);
2955+
pending_htlcs[TX_ACCEPTER] = AMOUNT_MSAT(0);
2956+
for (htlc = htlc_map_first(peer->channel->htlcs, &it);
2957+
htlc;
2958+
htlc = htlc_map_next(peer->channel->htlcs, &it)) {
2959+
struct amount_msat *itr;
2960+
2961+
if (htlc_owner(htlc) == opener ? LOCAL : REMOTE)
2962+
itr = &pending_htlcs[TX_INITIATOR];
2963+
else
2964+
itr = &pending_htlcs[TX_ACCEPTER];
2965+
2966+
if (!amount_msat_add(itr, *itr, htlc->amount))
2967+
peer_failed_warn(peer->pps, &peer->channel_id,
2968+
"Unable to add HTLC balance");
2969+
}
2970+
29492971
for (size_t i = 0; i < psbt->num_inputs; i++)
29502972
if (i != chan_input_index)
29512973
add_amount_to_side(peer, in,
@@ -2963,12 +2985,22 @@ static struct amount_sat check_balances(struct peer *peer,
29632985
psbt_output_get_amount(psbt, i),
29642986
&psbt->outputs[i].unknowns);
29652987

2966-
/* Calculate total channel output amount */
2988+
/* Calculate original channel output amount */
29672989
if (!amount_msat_add(&funding_amount,
29682990
peer->channel->view->owed[LOCAL],
29692991
peer->channel->view->owed[REMOTE]))
29702992
peer_failed_warn(peer->pps, &peer->channel_id,
29712993
"Unable to calculate starting channel amount");
2994+
if (!amount_msat_add(&funding_amount,
2995+
funding_amount,
2996+
pending_htlcs[TX_INITIATOR]))
2997+
peer_failed_warn(peer->pps, &peer->channel_id,
2998+
"Unable to calculate starting channel amount");
2999+
if (!amount_msat_add(&funding_amount,
3000+
funding_amount,
3001+
pending_htlcs[TX_ACCEPTER]))
3002+
peer_failed_warn(peer->pps, &peer->channel_id,
3003+
"Unable to calculate starting channel amount");
29723004

29733005
/* Tasks:
29743006
* Add up total funding_amount
@@ -3011,9 +3043,13 @@ static struct amount_sat check_balances(struct peer *peer,
30113043
peer_failed_warn(peer->pps, &peer->channel_id,
30123044
"Initiator funding is less than commited"
30133045
" amount. Initiator contributing %s but they"
3014-
" committed to %s.",
3046+
" committed to %s. Pending offered HTLC"
3047+
" balance of %s is not available for this"
3048+
" operation.",
30153049
fmt_amount_msat(tmpctx, in[TX_INITIATOR]),
3016-
fmt_amount_msat(tmpctx, out[TX_INITIATOR]));
3050+
fmt_amount_msat(tmpctx, out[TX_INITIATOR]),
3051+
fmt_amount_msat(tmpctx,
3052+
pending_htlcs[TX_INITIATOR]));
30173053
}
30183054

30193055
if (!amount_msat_sub(&initiator_fee, in[TX_INITIATOR], out[TX_INITIATOR]))
@@ -3029,9 +3065,13 @@ static struct amount_sat check_balances(struct peer *peer,
30293065
peer_failed_warn(peer->pps, &peer->channel_id,
30303066
"Accepter funding is less than commited"
30313067
" amount. Accepter contributing %s but they"
3032-
" committed to %s.",
3068+
" committed to %s. Pending offered HTLC"
3069+
" balance of %s is not available for this"
3070+
" operation.",
30333071
fmt_amount_msat(tmpctx, in[TX_INITIATOR]),
3034-
fmt_amount_msat(tmpctx, out[TX_INITIATOR]));
3072+
fmt_amount_msat(tmpctx, out[TX_INITIATOR]),
3073+
fmt_amount_msat(tmpctx,
3074+
pending_htlcs[TX_INITIATOR]));
30353075
}
30363076

30373077
if (!amount_msat_sub(&accepter_fee, in[TX_ACCEPTER], out[TX_ACCEPTER]))

tests/test_splicing.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,44 @@ def test_commit_crash_splice(node_factory, bitcoind):
276276
# Check that the splice doesn't generate a unilateral close transaction
277277
time.sleep(5)
278278
assert l1.db_query("SELECT count(*) as c FROM channeltxs;")[0]['c'] == 0
279+
280+
281+
@pytest.mark.openchannel('v1')
282+
@pytest.mark.openchannel('v2')
283+
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
284+
def test_splice_stuck_htlc(node_factory, bitcoind, executor):
285+
l1, l2, l3 = node_factory.line_graph(3, wait_for_announce=True, opts={'experimental-splicing': None})
286+
287+
l3.rpc.dev_ignore_htlcs(id=l2.info['id'], ignore=True)
288+
289+
inv = l3.rpc.invoice(10000000, '1', 'no_1')
290+
executor.submit(l1.rpc.pay, inv['bolt11'])
291+
l3.daemon.wait_for_log('their htlc 0 dev_ignore_htlcs')
292+
293+
# Now we should have a stuck invoice between l1 -> l2
294+
295+
chan_id = l1.get_channel_id(l2)
296+
297+
# add extra sats to pay fee
298+
funds_result = l1.rpc.fundpsbt("109000sat", "slow", 166, excess_as_change=True)
299+
300+
result = l1.rpc.splice_init(chan_id, 100000, funds_result['psbt'])
301+
result = l1.rpc.splice_update(chan_id, result['psbt'])
302+
result = l1.rpc.signpsbt(result['psbt'])
303+
result = l1.rpc.splice_signed(chan_id, result['signed_psbt'])
304+
305+
l2.daemon.wait_for_log(r'CHANNELD_NORMAL to CHANNELD_AWAITING_SPLICE')
306+
l1.daemon.wait_for_log(r'CHANNELD_NORMAL to CHANNELD_AWAITING_SPLICE')
307+
308+
mempool = bitcoind.rpc.getrawmempool(True)
309+
assert len(list(mempool.keys())) == 1
310+
assert result['txid'] in list(mempool.keys())
311+
312+
bitcoind.generate_block(6, wait_for_mempool=1)
313+
314+
l2.daemon.wait_for_log(r'CHANNELD_AWAITING_SPLICE to CHANNELD_NORMAL')
315+
l1.daemon.wait_for_log(r'CHANNELD_AWAITING_SPLICE to CHANNELD_NORMAL')
316+
317+
# Check that the splice doesn't generate a unilateral close transaction
318+
time.sleep(5)
319+
assert l1.db_query("SELECT count(*) as c FROM channeltxs;")[0]['c'] == 0

0 commit comments

Comments
 (0)