Skip to content

Commit bacb181

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

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
@@ -2928,13 +2928,35 @@ static struct amount_sat check_balances(struct peer *peer,
29282928
funding_amount_res, min_multiplied;
29292929
struct amount_msat funding_amount,
29302930
initiator_fee, accepter_fee;
2931-
struct amount_msat in[NUM_TX_ROLES], out[NUM_TX_ROLES];
2931+
struct amount_msat in[NUM_TX_ROLES], out[NUM_TX_ROLES],
2932+
pending_htlcs[NUM_TX_ROLES];
2933+
struct htlc_map_iter it;
2934+
const struct htlc *htlc;
29322935
bool opener = our_role == TX_INITIATOR;
29332936
u8 *msg;
29342937

2938+
/* The channel funds less any pending htlcs */
29352939
in[TX_INITIATOR] = peer->channel->view->owed[opener ? LOCAL : REMOTE];
29362940
in[TX_ACCEPTER] = peer->channel->view->owed[opener ? REMOTE : LOCAL];
29372941

2942+
/* pending_htlcs holds the value of all pending htlcs for each side */
2943+
pending_htlcs[TX_INITIATOR] = AMOUNT_MSAT(0);
2944+
pending_htlcs[TX_ACCEPTER] = AMOUNT_MSAT(0);
2945+
for (htlc = htlc_map_first(peer->channel->htlcs, &it);
2946+
htlc;
2947+
htlc = htlc_map_next(peer->channel->htlcs, &it)) {
2948+
struct amount_msat *itr;
2949+
2950+
if (htlc_owner(htlc) == opener ? LOCAL : REMOTE)
2951+
itr = &pending_htlcs[TX_INITIATOR];
2952+
else
2953+
itr = &pending_htlcs[TX_ACCEPTER];
2954+
2955+
if (!amount_msat_add(itr, *itr, htlc->amount))
2956+
peer_failed_warn(peer->pps, &peer->channel_id,
2957+
"Unable to add HTLC balance");
2958+
}
2959+
29382960
for (size_t i = 0; i < psbt->num_inputs; i++)
29392961
if (i != chan_input_index)
29402962
add_amount_to_side(peer, in,
@@ -2952,12 +2974,22 @@ static struct amount_sat check_balances(struct peer *peer,
29522974
psbt_output_get_amount(psbt, i),
29532975
&psbt->outputs[i].unknowns);
29542976

2955-
/* Calculate total channel output amount */
2977+
/* Calculate original channel output amount */
29562978
if (!amount_msat_add(&funding_amount,
29572979
peer->channel->view->owed[LOCAL],
29582980
peer->channel->view->owed[REMOTE]))
29592981
peer_failed_warn(peer->pps, &peer->channel_id,
29602982
"Unable to calculate starting channel amount");
2983+
if (!amount_msat_add(&funding_amount,
2984+
funding_amount,
2985+
pending_htlcs[TX_INITIATOR]))
2986+
peer_failed_warn(peer->pps, &peer->channel_id,
2987+
"Unable to calculate starting channel amount");
2988+
if (!amount_msat_add(&funding_amount,
2989+
funding_amount,
2990+
pending_htlcs[TX_ACCEPTER]))
2991+
peer_failed_warn(peer->pps, &peer->channel_id,
2992+
"Unable to calculate starting channel amount");
29612993

29622994
/* Tasks:
29632995
* Add up total funding_amount
@@ -3001,9 +3033,13 @@ static struct amount_sat check_balances(struct peer *peer,
30013033
peer_failed_warn(peer->pps, &peer->channel_id,
30023034
"Initiator funding is less than commited"
30033035
" amount. Initiator contributing %s but they"
3004-
" committed to %s.",
3036+
" committed to %s. Pending offered HTLC"
3037+
" balance of %s is not available for this"
3038+
" operation.",
30053039
fmt_amount_msat(tmpctx, in[TX_INITIATOR]),
3006-
fmt_amount_msat(tmpctx, out[TX_INITIATOR]));
3040+
fmt_amount_msat(tmpctx, out[TX_INITIATOR]),
3041+
fmt_amount_msat(tmpctx,
3042+
pending_htlcs[TX_INITIATOR]));
30073043
}
30083044

30093045
if (!amount_msat_sub(&initiator_fee, in[TX_INITIATOR], out[TX_INITIATOR]))
@@ -3019,9 +3055,13 @@ static struct amount_sat check_balances(struct peer *peer,
30193055
peer_failed_warn(peer->pps, &peer->channel_id,
30203056
"Accepter funding is less than commited"
30213057
" amount. Accepter contributing %s but they"
3022-
" committed to %s.",
3058+
" committed to %s. Pending offered HTLC"
3059+
" balance of %s is not available for this"
3060+
" operation.",
30233061
fmt_amount_msat(tmpctx, in[TX_INITIATOR]),
3024-
fmt_amount_msat(tmpctx, out[TX_INITIATOR]));
3062+
fmt_amount_msat(tmpctx, out[TX_INITIATOR]),
3063+
fmt_amount_msat(tmpctx,
3064+
pending_htlcs[TX_INITIATOR]));
30253065
}
30263066

30273067
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)