@@ -2961,13 +2961,35 @@ static struct amount_sat check_balances(struct peer *peer,
2961
2961
funding_amount_res , min_multiplied ;
2962
2962
struct amount_msat funding_amount ,
2963
2963
initiator_fee , accepter_fee ;
2964
- struct amount_msat in [NUM_TX_ROLES ], out [NUM_TX_ROLES ];
2964
+ struct amount_msat in [NUM_TX_ROLES ], out [NUM_TX_ROLES ],
2965
+ pending_htlcs [NUM_TX_ROLES ];
2966
+ struct htlc_map_iter it ;
2967
+ const struct htlc * htlc ;
2965
2968
bool opener = our_role == TX_INITIATOR ;
2966
2969
u8 * msg ;
2967
2970
2971
+ /* The channel funds less any pending htlcs */
2968
2972
in [TX_INITIATOR ] = peer -> channel -> view -> owed [opener ? LOCAL : REMOTE ];
2969
2973
in [TX_ACCEPTER ] = peer -> channel -> view -> owed [opener ? REMOTE : LOCAL ];
2970
2974
2975
+ /* pending_htlcs holds the value of all pending htlcs for each side */
2976
+ pending_htlcs [TX_INITIATOR ] = AMOUNT_MSAT (0 );
2977
+ pending_htlcs [TX_ACCEPTER ] = AMOUNT_MSAT (0 );
2978
+ for (htlc = htlc_map_first (peer -> channel -> htlcs , & it );
2979
+ htlc ;
2980
+ htlc = htlc_map_next (peer -> channel -> htlcs , & it )) {
2981
+ struct amount_msat * itr ;
2982
+
2983
+ if (htlc_owner (htlc ) == opener ? LOCAL : REMOTE )
2984
+ itr = & pending_htlcs [TX_INITIATOR ];
2985
+ else
2986
+ itr = & pending_htlcs [TX_ACCEPTER ];
2987
+
2988
+ if (!amount_msat_add (itr , * itr , htlc -> amount ))
2989
+ peer_failed_warn (peer -> pps , & peer -> channel_id ,
2990
+ "Unable to add HTLC balance" );
2991
+ }
2992
+
2971
2993
for (size_t i = 0 ; i < psbt -> num_inputs ; i ++ )
2972
2994
if (i != chan_input_index )
2973
2995
add_amount_to_side (peer , in ,
@@ -2985,12 +3007,22 @@ static struct amount_sat check_balances(struct peer *peer,
2985
3007
psbt_output_get_amount (psbt , i ),
2986
3008
& psbt -> outputs [i ].unknowns );
2987
3009
2988
- /* Calculate total channel output amount */
3010
+ /* Calculate original channel output amount */
2989
3011
if (!amount_msat_add (& funding_amount ,
2990
3012
peer -> channel -> view -> owed [LOCAL ],
2991
3013
peer -> channel -> view -> owed [REMOTE ]))
2992
3014
peer_failed_warn (peer -> pps , & peer -> channel_id ,
2993
3015
"Unable to calculate starting channel amount" );
3016
+ if (!amount_msat_add (& funding_amount ,
3017
+ funding_amount ,
3018
+ pending_htlcs [TX_INITIATOR ]))
3019
+ peer_failed_warn (peer -> pps , & peer -> channel_id ,
3020
+ "Unable to calculate starting channel amount" );
3021
+ if (!amount_msat_add (& funding_amount ,
3022
+ funding_amount ,
3023
+ pending_htlcs [TX_ACCEPTER ]))
3024
+ peer_failed_warn (peer -> pps , & peer -> channel_id ,
3025
+ "Unable to calculate starting channel amount" );
2994
3026
2995
3027
/* Tasks:
2996
3028
* Add up total funding_amount
@@ -3033,9 +3065,13 @@ static struct amount_sat check_balances(struct peer *peer,
3033
3065
peer_failed_warn (peer -> pps , & peer -> channel_id ,
3034
3066
"Initiator funding is less than commited"
3035
3067
" amount. Initiator contributing %s but they"
3036
- " committed to %s." ,
3068
+ " committed to %s. Pending offered HTLC"
3069
+ " balance of %s is not available for this"
3070
+ " operation." ,
3037
3071
fmt_amount_msat (tmpctx , in [TX_INITIATOR ]),
3038
- 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 ]));
3039
3075
}
3040
3076
3041
3077
if (!amount_msat_sub (& initiator_fee , in [TX_INITIATOR ], out [TX_INITIATOR ]))
@@ -3051,9 +3087,13 @@ static struct amount_sat check_balances(struct peer *peer,
3051
3087
peer_failed_warn (peer -> pps , & peer -> channel_id ,
3052
3088
"Accepter funding is less than commited"
3053
3089
" amount. Accepter contributing %s but they"
3054
- " committed to %s." ,
3090
+ " committed to %s. Pending offered HTLC"
3091
+ " balance of %s is not available for this"
3092
+ " operation." ,
3055
3093
fmt_amount_msat (tmpctx , in [TX_INITIATOR ]),
3056
- fmt_amount_msat (tmpctx , out [TX_INITIATOR ]));
3094
+ fmt_amount_msat (tmpctx , out [TX_INITIATOR ]),
3095
+ fmt_amount_msat (tmpctx ,
3096
+ pending_htlcs [TX_INITIATOR ]));
3057
3097
}
3058
3098
3059
3099
if (!amount_msat_sub (& accepter_fee , in [TX_ACCEPTER ], out [TX_ACCEPTER ]))
0 commit comments