Skip to content

Commit cbf96ba

Browse files
committed
splice: Add new funding output balance
The prior spec left this value at 0 to be calculted later but the current spec requires we fill it in in advance. Changelog-None
1 parent ba07377 commit cbf96ba

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

channeld/channeld.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2910,6 +2910,60 @@ relative_splice_balance_fundee(struct peer *peer,
29102910
return amount_msat(push_value);
29112911
}
29122912

2913+
static struct amount_sat calc_balance(struct peer *peer)
2914+
{
2915+
struct amount_sat funding_amount_res;
2916+
struct amount_msat funding_amount;
2917+
struct amount_msat pending_htlcs;
2918+
struct htlc_map_iter it;
2919+
const struct htlc *htlc;
2920+
2921+
/* pending_htlcs holds the value of all pending htlcs for each side */
2922+
pending_htlcs = AMOUNT_MSAT(0);
2923+
for (htlc = htlc_map_first(peer->channel->htlcs, &it);
2924+
htlc;
2925+
htlc = htlc_map_next(peer->channel->htlcs, &it)) {
2926+
if (!amount_msat_accumulate(&pending_htlcs, htlc->amount))
2927+
peer_failed_warn(peer->pps, &peer->channel_id,
2928+
"Unable to add HTLC balance");
2929+
}
2930+
2931+
/* Calculate original channel output amount */
2932+
if (!amount_msat_add(&funding_amount,
2933+
peer->channel->view->owed[LOCAL],
2934+
peer->channel->view->owed[REMOTE]))
2935+
peer_failed_warn(peer->pps, &peer->channel_id,
2936+
"Unable to calculate starting channel amount");
2937+
if (!amount_msat_accumulate(&funding_amount,
2938+
pending_htlcs))
2939+
peer_failed_warn(peer->pps, &peer->channel_id,
2940+
"Unable to calculate starting channel amount");
2941+
2942+
if (!amount_msat_add_sat_s64(&funding_amount, funding_amount,
2943+
peer->splicing->opener_relative))
2944+
peer_failed_warn(peer->pps, &peer->channel_id,
2945+
"Unable to add opener funding");
2946+
2947+
if (!amount_msat_add_sat_s64(&funding_amount, funding_amount,
2948+
peer->splicing->accepter_relative))
2949+
peer_failed_warn(peer->pps, &peer->channel_id,
2950+
"Unable to add accepter funding");
2951+
2952+
if (!amount_msat_to_sat(&funding_amount_res, funding_amount)) {
2953+
status_failed(STATUS_FAIL_INTERNAL_ERROR,
2954+
"splice error: msat of total funding %s should"
2955+
" always add up to a full sat. original local bal"
2956+
" %s, original remote bal %s,",
2957+
fmt_amount_msat(tmpctx, funding_amount),
2958+
fmt_amount_msat(tmpctx,
2959+
peer->channel->view->owed[LOCAL]),
2960+
fmt_amount_msat(tmpctx,
2961+
peer->channel->view->owed[REMOTE]));
2962+
}
2963+
2964+
return funding_amount_res;
2965+
}
2966+
29132967
/* Returns the total channel funding output amount if all checks pass.
29142968
* Otherwise, exits via peer_failed_warn. DTODO: Change to `tx_abort`. */
29152969
static struct amount_sat check_balances(struct peer *peer,
@@ -3838,7 +3892,7 @@ static void splice_initiator(struct peer *peer, const u8 *inmsg)
38383892
*/
38393893
psbt_append_output(ictx->desired_psbt,
38403894
scriptpubkey_p2wsh(ictx->desired_psbt, wit_script),
3841-
amount_sat(0));
3895+
calc_balance(peer));
38423896

38433897
psbt_add_serials(ictx->desired_psbt, ictx->our_role);
38443898

0 commit comments

Comments
 (0)