@@ -2939,13 +2939,35 @@ static struct amount_sat check_balances(struct peer *peer,
2939
2939
funding_amount_res , min_multiplied ;
2940
2940
struct amount_msat funding_amount ,
2941
2941
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 ;
2943
2946
bool opener = our_role == TX_INITIATOR ;
2944
2947
u8 * msg ;
2945
2948
2949
+ /* The channel funds less any pending htlcs */
2946
2950
in [TX_INITIATOR ] = peer -> channel -> view -> owed [opener ? LOCAL : REMOTE ];
2947
2951
in [TX_ACCEPTER ] = peer -> channel -> view -> owed [opener ? REMOTE : LOCAL ];
2948
2952
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
+
2949
2971
for (size_t i = 0 ; i < psbt -> num_inputs ; i ++ )
2950
2972
if (i != chan_input_index )
2951
2973
add_amount_to_side (peer , in ,
@@ -2963,12 +2985,22 @@ static struct amount_sat check_balances(struct peer *peer,
2963
2985
psbt_output_get_amount (psbt , i ),
2964
2986
& psbt -> outputs [i ].unknowns );
2965
2987
2966
- /* Calculate total channel output amount */
2988
+ /* Calculate original channel output amount */
2967
2989
if (!amount_msat_add (& funding_amount ,
2968
2990
peer -> channel -> view -> owed [LOCAL ],
2969
2991
peer -> channel -> view -> owed [REMOTE ]))
2970
2992
peer_failed_warn (peer -> pps , & peer -> channel_id ,
2971
2993
"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" );
2972
3004
2973
3005
/* Tasks:
2974
3006
* Add up total funding_amount
@@ -3011,9 +3043,13 @@ static struct amount_sat check_balances(struct peer *peer,
3011
3043
peer_failed_warn (peer -> pps , & peer -> channel_id ,
3012
3044
"Initiator funding is less than commited"
3013
3045
" 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." ,
3015
3049
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 ]));
3017
3053
}
3018
3054
3019
3055
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,
3029
3065
peer_failed_warn (peer -> pps , & peer -> channel_id ,
3030
3066
"Accepter funding is less than commited"
3031
3067
" 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." ,
3033
3071
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 ]));
3035
3075
}
3036
3076
3037
3077
if (!amount_msat_sub (& accepter_fee , in [TX_ACCEPTER ], out [TX_ACCEPTER ]))
0 commit comments