@@ -3178,47 +3178,70 @@ static struct wally_psbt_output *find_channel_output(struct peer *peer,
31783178 return NULL ;
31793179}
31803180
3181- static size_t calc_weight (enum tx_role role , const struct wally_psbt * psbt )
3181+ static size_t calc_weight (enum tx_role role , const struct wally_psbt * psbt ,
3182+ bool log_math )
31823183{
3183- size_t weight = 0 ;
3184+ size_t lweight = 0 , weight = 0 ;
31843185
3185- /* BOLT #2:
3186- * The *initiator* is responsible for paying the fees for the following fields,
3187- * to be referred to as the `common fields`.
3188- *
3189- * - version
3190- * - segwit marker + flag
3191- * - input count
3192- * - output count
3193- * - locktime
3194- */
3195- if (role == TX_INITIATOR )
3196- weight += bitcoin_tx_core_weight (psbt -> num_inputs ,
3197- psbt -> num_outputs );
3186+ if (log_math )
3187+ status_debug ("Counting tx weight;" );
31983188
31993189 /* BOLT #2:
32003190 * The rest of the transaction bytes' fees are the responsibility of
32013191 * the peer who contributed that input or output via `tx_add_input` or
32023192 * `tx_add_output`, at the agreed upon `feerate`.
32033193 */
3204- for (size_t i = 0 ; i < psbt -> num_inputs ; i ++ )
3194+ for (size_t i = 0 ; i < psbt -> num_inputs ; i ++ ) {
32053195 if (is_initiators_serial (& psbt -> inputs [i ].unknowns )) {
32063196 if (role == TX_INITIATOR )
3207- weight += psbt_input_get_weight (psbt , i );
3197+ weight += psbt_input_get_weight (psbt , i , PSBT_GUESS_2OF2 );
32083198 }
3209- else
3199+ else {
32103200 if (role != TX_INITIATOR )
3211- weight += psbt_input_get_weight (psbt , i );
3201+ weight += psbt_input_get_weight (psbt , i , PSBT_GUESS_2OF2 );
3202+ }
3203+ if (log_math )
3204+ status_debug (" Adding input"
3205+ " %lu; weight: %lu" , i , weight - lweight );
3206+ lweight = weight ;
3207+ }
32123208
3213- for (size_t i = 0 ; i < psbt -> num_outputs ; i ++ )
3209+ for (size_t i = 0 ; i < psbt -> num_outputs ; i ++ ) {
32143210 if (is_initiators_serial (& psbt -> outputs [i ].unknowns )) {
32153211 if (role == TX_INITIATOR )
32163212 weight += psbt_output_get_weight (psbt , i );
32173213 }
3218- else
3214+ else {
32193215 if (role != TX_INITIATOR )
32203216 weight += psbt_output_get_weight (psbt , i );
3217+ }
3218+ if (log_math )
3219+ status_debug (" Adding output"
3220+ " %lu; weight: %lu" , i , weight - lweight );
3221+ lweight = weight ;
3222+ }
32213223
3224+ /* BOLT #2:
3225+ * The *initiator* is responsible for paying the fees for the following fields,
3226+ * to be referred to as the `common fields`.
3227+ *
3228+ * - version
3229+ * - segwit marker + flag
3230+ * - input count
3231+ * - output count
3232+ * - locktime
3233+ */
3234+ if (role == TX_INITIATOR ) {
3235+ weight += bitcoin_tx_core_weight (psbt -> num_inputs ,
3236+ psbt -> num_outputs );
3237+ if (log_math )
3238+ status_debug (" Adding bitcoin_tx_core_weight;"
3239+ " weight: %lu" , weight - lweight );
3240+ lweight = weight ;
3241+ }
3242+
3243+ if (log_math )
3244+ status_debug ("Total weight: %lu" , weight );
32223245 return weight ;
32233246}
32243247
@@ -3319,8 +3342,7 @@ static struct amount_sat check_balances(struct peer *peer,
33193342{
33203343 struct amount_sat min_initiator_fee , min_accepter_fee ,
33213344 max_initiator_fee , max_accepter_fee ,
3322- funding_amount_res , min_multiplied ,
3323- initiator_penalty_fee , accepter_penalty_fee ;
3345+ funding_amount_res ;
33243346 struct amount_msat funding_amount ,
33253347 initiator_fee , accepter_fee ;
33263348 struct amount_msat in [NUM_TX_ROLES ], out [NUM_TX_ROLES ],
@@ -3469,33 +3491,26 @@ static struct amount_sat check_balances(struct peer *peer,
34693491 "amount_sat_less / amount_sat_sub mismtach" );
34703492
34713493 min_initiator_fee = amount_tx_fee (peer -> splicing -> feerate_per_kw ,
3472- calc_weight (TX_INITIATOR , psbt ));
3494+ calc_weight (TX_INITIATOR , psbt , false ));
34733495 min_accepter_fee = amount_tx_fee (peer -> splicing -> feerate_per_kw ,
3474- calc_weight (TX_ACCEPTER , psbt ));
3496+ calc_weight (TX_ACCEPTER , psbt , false ));
34753497
34763498 /* As a safeguard max feerate is checked (only) locally, if it's
34773499 * particularly high we fail and tell the user but allow them to
34783500 * override with `splice_force_feerate` */
3479- max_accepter_fee = amount_tx_fee (peer -> feerate_max ,
3480- calc_weight (TX_ACCEPTER , psbt ));
3481- max_initiator_fee = amount_tx_fee (peer -> feerate_max ,
3482- calc_weight (TX_INITIATOR , psbt ));
3483- initiator_penalty_fee = amount_tx_fee (peer -> feerate_penalty ,
3484- calc_weight (TX_INITIATOR , psbt ));
3485- accepter_penalty_fee = amount_tx_fee (peer -> feerate_penalty ,
3486- calc_weight (TX_ACCEPTER , psbt ));
3487-
3488- /* Sometimes feerate_max is some absurdly high value, in that case we
3489- * give a fee warning based of a multiple of the min value. */
3490- amount_sat_mul (& min_multiplied , min_accepter_fee , 5 );
3491- max_accepter_fee = SAT_MIN (min_multiplied , max_accepter_fee );
3492- if (amount_sat_greater (accepter_penalty_fee , max_accepter_fee ))
3493- max_accepter_fee = accepter_penalty_fee ;
3494-
3495- amount_sat_mul (& min_multiplied , min_initiator_fee , 5 );
3496- max_initiator_fee = SAT_MIN (min_multiplied , max_initiator_fee );
3497- if (amount_sat_greater (initiator_penalty_fee , max_initiator_fee ))
3498- max_initiator_fee = initiator_penalty_fee ;
3501+ max_accepter_fee = amount_tx_fee (peer -> feerate_opening ,
3502+ calc_weight (TX_ACCEPTER , psbt , false));
3503+ max_initiator_fee = amount_tx_fee (peer -> feerate_opening ,
3504+ calc_weight (TX_INITIATOR , psbt , opener ));
3505+
3506+ if (opener ) {
3507+ status_debug ("User specified fee of %s. Opening feerate %" PRIu32
3508+ " * weight %lu / 1000 = %s" ,
3509+ fmt_amount_m_as_sat (tmpctx , initiator_fee ),
3510+ peer -> feerate_opening ,
3511+ calc_weight (TX_INITIATOR , psbt , false),
3512+ fmt_amount_sat (tmpctx , max_initiator_fee ));
3513+ }
34993514
35003515 /* Check initiator fee */
35013516 if (amount_msat_less_sat (initiator_fee , min_initiator_fee )) {
@@ -3512,23 +3527,38 @@ static struct amount_sat check_balances(struct peer *peer,
35123527 && amount_msat_greater_sat (initiator_fee , max_initiator_fee )) {
35133528 msg = towire_channeld_splice_feerate_error (NULL , initiator_fee ,
35143529 true);
3530+ status_debug ("Our own fee (%s) is too high to use without"
3531+ " forcing. Opening feerate %" PRIu32
3532+ " x weight %lu / 1000 = %s (max)" ,
3533+ fmt_amount_m_as_sat (tmpctx , initiator_fee ),
3534+ peer -> feerate_opening ,
3535+ calc_weight (TX_INITIATOR , psbt , false),
3536+ fmt_amount_sat (tmpctx , max_initiator_fee ));
3537+
35153538 wire_sync_write (MASTER_FD , take (msg ));
3539+
35163540 splice_abort (peer ,
3517- "Our own fee (%s) was too high, max without"
3518- " forcing is %s." ,
3519- fmt_amount_msat (tmpctx , initiator_fee ),
3520- fmt_amount_sat (tmpctx , max_initiator_fee ));
3541+ "Our own fee (%s) is too high to use without"
3542+ " forcing. Opening feerate %" PRIu32
3543+ " x weight %lu / 1000 = %s (max)" ,
3544+ fmt_amount_m_as_sat (tmpctx , initiator_fee ),
3545+ peer -> feerate_opening ,
3546+ calc_weight (TX_INITIATOR , psbt , false),
3547+ fmt_amount_sat (tmpctx , max_initiator_fee ));
35213548 }
35223549 /* Check accepter fee */
35233550 if (amount_msat_less_sat (accepter_fee , min_accepter_fee )) {
35243551 msg = towire_channeld_splice_feerate_error (NULL , accepter_fee ,
35253552 false);
35263553 wire_sync_write (MASTER_FD , take (msg ));
35273554 splice_abort (peer ,
3528- "%s fee (%s) was too low, must be at least %s" ,
3529- opener ? "Your" : "Our" ,
3530- fmt_amount_msat (tmpctx , accepter_fee ),
3531- fmt_amount_sat (tmpctx , min_accepter_fee ));
3555+ "%s fee (%s) was too low, must be at least %s"
3556+ " weight: %" PRIu64 ", feerate_max: %" PRIu32 ,
3557+ opener ? "Your" : "Our" ,
3558+ fmt_amount_msat (tmpctx , accepter_fee ),
3559+ fmt_amount_sat (tmpctx , min_accepter_fee ),
3560+ calc_weight (TX_INITIATOR , psbt , false),
3561+ peer -> feerate_opening );
35323562 }
35333563 if (!peer -> splicing -> force_feerate && !opener
35343564 && amount_msat_greater_sat (accepter_fee , max_accepter_fee )) {
@@ -3981,6 +4011,9 @@ static void resume_splice_negotiation(struct peer *peer,
39814011
39824012 peer -> splicing = tal_free (peer -> splicing );
39834013
4014+ if (our_role == TX_INITIATOR )
4015+ calc_weight (TX_INITIATOR , current_psbt , true);
4016+
39844017 final_tx = bitcoin_tx_with_psbt (tmpctx , current_psbt );
39854018 msg = towire_channeld_splice_confirmed_signed (tmpctx , final_tx ,
39864019 new_output_index );
0 commit comments