@@ -2928,13 +2928,35 @@ static struct amount_sat check_balances(struct peer *peer,
2928
2928
funding_amount_res , min_multiplied ;
2929
2929
struct amount_msat funding_amount ,
2930
2930
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 ;
2932
2935
bool opener = our_role == TX_INITIATOR ;
2933
2936
u8 * msg ;
2934
2937
2938
+ /* The channel funds less any pending htlcs */
2935
2939
in [TX_INITIATOR ] = peer -> channel -> view -> owed [opener ? LOCAL : REMOTE ];
2936
2940
in [TX_ACCEPTER ] = peer -> channel -> view -> owed [opener ? REMOTE : LOCAL ];
2937
2941
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
+
2938
2960
for (size_t i = 0 ; i < psbt -> num_inputs ; i ++ )
2939
2961
if (i != chan_input_index )
2940
2962
add_amount_to_side (peer , in ,
@@ -2952,12 +2974,22 @@ static struct amount_sat check_balances(struct peer *peer,
2952
2974
psbt_output_get_amount (psbt , i ),
2953
2975
& psbt -> outputs [i ].unknowns );
2954
2976
2955
- /* Calculate total channel output amount */
2977
+ /* Calculate original channel output amount */
2956
2978
if (!amount_msat_add (& funding_amount ,
2957
2979
peer -> channel -> view -> owed [LOCAL ],
2958
2980
peer -> channel -> view -> owed [REMOTE ]))
2959
2981
peer_failed_warn (peer -> pps , & peer -> channel_id ,
2960
2982
"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" );
2961
2993
2962
2994
/* Tasks:
2963
2995
* Add up total funding_amount
@@ -3001,9 +3033,13 @@ static struct amount_sat check_balances(struct peer *peer,
3001
3033
peer_failed_warn (peer -> pps , & peer -> channel_id ,
3002
3034
"Initiator funding is less than commited"
3003
3035
" 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." ,
3005
3039
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 ]));
3007
3043
}
3008
3044
3009
3045
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,
3019
3055
peer_failed_warn (peer -> pps , & peer -> channel_id ,
3020
3056
"Accepter funding is less than commited"
3021
3057
" 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." ,
3023
3061
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 ]));
3025
3065
}
3026
3066
3027
3067
if (!amount_msat_sub (& accepter_fee , in [TX_ACCEPTER ], out [TX_ACCEPTER ]))
0 commit comments