@@ -3153,47 +3153,70 @@ static struct wally_psbt_output *find_channel_output(struct peer *peer,
31533153 return NULL ;
31543154}
31553155
3156- static size_t calc_weight (enum tx_role role , const struct wally_psbt * psbt )
3156+ static size_t calc_weight (enum tx_role role , const struct wally_psbt * psbt ,
3157+ bool log_math )
31573158{
3158- size_t weight = 0 ;
3159+ size_t lweight = 0 , weight = 0 ;
31593160
3160- /* BOLT #2:
3161- * The *initiator* is responsible for paying the fees for the following fields,
3162- * to be referred to as the `common fields`.
3163- *
3164- * - version
3165- * - segwit marker + flag
3166- * - input count
3167- * - output count
3168- * - locktime
3169- */
3170- if (role == TX_INITIATOR )
3171- weight += bitcoin_tx_core_weight (psbt -> num_inputs ,
3172- psbt -> num_outputs );
3161+ if (log_math )
3162+ status_debug ("Counting tx weight;" );
31733163
31743164 /* BOLT #2:
31753165 * The rest of the transaction bytes' fees are the responsibility of
31763166 * the peer who contributed that input or output via `tx_add_input` or
31773167 * `tx_add_output`, at the agreed upon `feerate`.
31783168 */
3179- for (size_t i = 0 ; i < psbt -> num_inputs ; i ++ )
3169+ for (size_t i = 0 ; i < psbt -> num_inputs ; i ++ ) {
31803170 if (is_initiators_serial (& psbt -> inputs [i ].unknowns )) {
31813171 if (role == TX_INITIATOR )
3182- weight += psbt_input_get_weight (psbt , i );
3172+ weight += psbt_input_get_weight (psbt , i , PSBT_GUESS_2OF2 );
31833173 }
3184- else
3174+ else {
31853175 if (role != TX_INITIATOR )
3186- weight += psbt_input_get_weight (psbt , i );
3176+ weight += psbt_input_get_weight (psbt , i , PSBT_GUESS_2OF2 );
3177+ }
3178+ if (log_math )
3179+ status_debug (" Adding input"
3180+ " %lu; weight: %lu" , i , weight - lweight );
3181+ lweight = weight ;
3182+ }
31873183
3188- for (size_t i = 0 ; i < psbt -> num_outputs ; i ++ )
3184+ for (size_t i = 0 ; i < psbt -> num_outputs ; i ++ ) {
31893185 if (is_initiators_serial (& psbt -> outputs [i ].unknowns )) {
31903186 if (role == TX_INITIATOR )
31913187 weight += psbt_output_get_weight (psbt , i );
31923188 }
3193- else
3189+ else {
31943190 if (role != TX_INITIATOR )
31953191 weight += psbt_output_get_weight (psbt , i );
3192+ }
3193+ if (log_math )
3194+ status_debug (" Adding output"
3195+ " %lu; weight: %lu" , i , weight - lweight );
3196+ lweight = weight ;
3197+ }
31963198
3199+ /* BOLT #2:
3200+ * The *initiator* is responsible for paying the fees for the following fields,
3201+ * to be referred to as the `common fields`.
3202+ *
3203+ * - version
3204+ * - segwit marker + flag
3205+ * - input count
3206+ * - output count
3207+ * - locktime
3208+ */
3209+ if (role == TX_INITIATOR ) {
3210+ weight += bitcoin_tx_core_weight (psbt -> num_inputs ,
3211+ psbt -> num_outputs );
3212+ if (log_math )
3213+ status_debug (" Adding bitcoin_tx_core_weight;"
3214+ " weight: %lu" , weight - lweight );
3215+ lweight = weight ;
3216+ }
3217+
3218+ if (log_math )
3219+ status_debug ("Total weight: %lu" , weight );
31973220 return weight ;
31983221}
31993222
@@ -3294,8 +3317,7 @@ static struct amount_sat check_balances(struct peer *peer,
32943317{
32953318 struct amount_sat min_initiator_fee , min_accepter_fee ,
32963319 max_initiator_fee , max_accepter_fee ,
3297- funding_amount_res , min_multiplied ,
3298- initiator_penalty_fee , accepter_penalty_fee ;
3320+ funding_amount_res ;
32993321 struct amount_msat funding_amount ,
33003322 initiator_fee , accepter_fee ;
33013323 struct amount_msat in [NUM_TX_ROLES ], out [NUM_TX_ROLES ],
@@ -3444,33 +3466,26 @@ static struct amount_sat check_balances(struct peer *peer,
34443466 "amount_sat_less / amount_sat_sub mismtach" );
34453467
34463468 min_initiator_fee = amount_tx_fee (peer -> splicing -> feerate_per_kw ,
3447- calc_weight (TX_INITIATOR , psbt ));
3469+ calc_weight (TX_INITIATOR , psbt , false ));
34483470 min_accepter_fee = amount_tx_fee (peer -> splicing -> feerate_per_kw ,
3449- calc_weight (TX_ACCEPTER , psbt ));
3471+ calc_weight (TX_ACCEPTER , psbt , false ));
34503472
34513473 /* As a safeguard max feerate is checked (only) locally, if it's
34523474 * particularly high we fail and tell the user but allow them to
34533475 * override with `splice_force_feerate` */
3454- max_accepter_fee = amount_tx_fee (peer -> feerate_max ,
3455- calc_weight (TX_ACCEPTER , psbt ));
3456- max_initiator_fee = amount_tx_fee (peer -> feerate_max ,
3457- calc_weight (TX_INITIATOR , psbt ));
3458- initiator_penalty_fee = amount_tx_fee (peer -> feerate_penalty ,
3459- calc_weight (TX_INITIATOR , psbt ));
3460- accepter_penalty_fee = amount_tx_fee (peer -> feerate_penalty ,
3461- calc_weight (TX_ACCEPTER , psbt ));
3462-
3463- /* Sometimes feerate_max is some absurdly high value, in that case we
3464- * give a fee warning based of a multiple of the min value. */
3465- amount_sat_mul (& min_multiplied , min_accepter_fee , 5 );
3466- max_accepter_fee = SAT_MIN (min_multiplied , max_accepter_fee );
3467- if (amount_sat_greater (accepter_penalty_fee , max_accepter_fee ))
3468- max_accepter_fee = accepter_penalty_fee ;
3469-
3470- amount_sat_mul (& min_multiplied , min_initiator_fee , 5 );
3471- max_initiator_fee = SAT_MIN (min_multiplied , max_initiator_fee );
3472- if (amount_sat_greater (initiator_penalty_fee , max_initiator_fee ))
3473- max_initiator_fee = initiator_penalty_fee ;
3476+ max_accepter_fee = amount_tx_fee (peer -> feerate_opening ,
3477+ calc_weight (TX_ACCEPTER , psbt , false));
3478+ max_initiator_fee = amount_tx_fee (peer -> feerate_opening ,
3479+ calc_weight (TX_INITIATOR , psbt , opener ));
3480+
3481+ if (opener ) {
3482+ status_debug ("User specified fee of %s. Opening feerate %" PRIu32
3483+ " * weight %lu / 1000 = %s" ,
3484+ fmt_amount_m_as_sat (tmpctx , initiator_fee ),
3485+ peer -> feerate_opening ,
3486+ calc_weight (TX_INITIATOR , psbt , false),
3487+ fmt_amount_sat (tmpctx , max_initiator_fee ));
3488+ }
34743489
34753490 /* Check initiator fee */
34763491 if (amount_msat_less_sat (initiator_fee , min_initiator_fee )) {
@@ -3487,23 +3502,38 @@ static struct amount_sat check_balances(struct peer *peer,
34873502 && amount_msat_greater_sat (initiator_fee , max_initiator_fee )) {
34883503 msg = towire_channeld_splice_feerate_error (NULL , initiator_fee ,
34893504 true);
3505+ status_debug ("Our own fee (%s) is too high to use without"
3506+ " forcing. Opening feerate %" PRIu32
3507+ " x weight %lu / 1000 = %s (max)" ,
3508+ fmt_amount_m_as_sat (tmpctx , initiator_fee ),
3509+ peer -> feerate_opening ,
3510+ calc_weight (TX_INITIATOR , psbt , false),
3511+ fmt_amount_sat (tmpctx , max_initiator_fee ));
3512+
34903513 wire_sync_write (MASTER_FD , take (msg ));
3514+
34913515 splice_abort (peer ,
3492- "Our own fee (%s) was too high, max without"
3493- " forcing is %s." ,
3494- fmt_amount_msat (tmpctx , initiator_fee ),
3495- fmt_amount_sat (tmpctx , max_initiator_fee ));
3516+ "Our own fee (%s) is too high to use without"
3517+ " forcing. Opening feerate %" PRIu32
3518+ " x weight %lu / 1000 = %s (max)" ,
3519+ fmt_amount_m_as_sat (tmpctx , initiator_fee ),
3520+ peer -> feerate_opening ,
3521+ calc_weight (TX_INITIATOR , psbt , false),
3522+ fmt_amount_sat (tmpctx , max_initiator_fee ));
34963523 }
34973524 /* Check accepter fee */
34983525 if (amount_msat_less_sat (accepter_fee , min_accepter_fee )) {
34993526 msg = towire_channeld_splice_feerate_error (NULL , accepter_fee ,
35003527 false);
35013528 wire_sync_write (MASTER_FD , take (msg ));
35023529 splice_abort (peer ,
3503- "%s fee (%s) was too low, must be at least %s" ,
3504- opener ? "Your" : "Our" ,
3505- fmt_amount_msat (tmpctx , accepter_fee ),
3506- fmt_amount_sat (tmpctx , min_accepter_fee ));
3530+ "%s fee (%s) was too low, must be at least %s"
3531+ " weight: %" PRIu64 ", feerate_max: %" PRIu32 ,
3532+ opener ? "Your" : "Our" ,
3533+ fmt_amount_msat (tmpctx , accepter_fee ),
3534+ fmt_amount_sat (tmpctx , min_accepter_fee ),
3535+ calc_weight (TX_INITIATOR , psbt , false),
3536+ peer -> feerate_opening );
35073537 }
35083538 if (!peer -> splicing -> force_feerate && !opener
35093539 && amount_msat_greater_sat (accepter_fee , max_accepter_fee )) {
@@ -3956,6 +3986,9 @@ static void resume_splice_negotiation(struct peer *peer,
39563986
39573987 peer -> splicing = tal_free (peer -> splicing );
39583988
3989+ if (our_role == TX_INITIATOR )
3990+ calc_weight (TX_INITIATOR , current_psbt , true);
3991+
39593992 final_tx = bitcoin_tx_with_psbt (tmpctx , current_psbt );
39603993 msg = towire_channeld_splice_confirmed_signed (tmpctx , final_tx ,
39613994 new_output_index );
0 commit comments