@@ -3153,47 +3153,70 @@ static struct wally_psbt_output *find_channel_output(struct peer *peer,
3153
3153
return NULL ;
3154
3154
}
3155
3155
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 )
3157
3158
{
3158
- size_t weight = 0 ;
3159
+ size_t lweight = 0 , weight = 0 ;
3159
3160
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;" );
3173
3163
3174
3164
/* BOLT #2:
3175
3165
* The rest of the transaction bytes' fees are the responsibility of
3176
3166
* the peer who contributed that input or output via `tx_add_input` or
3177
3167
* `tx_add_output`, at the agreed upon `feerate`.
3178
3168
*/
3179
- for (size_t i = 0 ; i < psbt -> num_inputs ; i ++ )
3169
+ for (size_t i = 0 ; i < psbt -> num_inputs ; i ++ ) {
3180
3170
if (is_initiators_serial (& psbt -> inputs [i ].unknowns )) {
3181
3171
if (role == TX_INITIATOR )
3182
- weight += psbt_input_get_weight (psbt , i );
3172
+ weight += psbt_input_get_weight (psbt , i , PSBT_GUESS_2OF2 );
3183
3173
}
3184
- else
3174
+ else {
3185
3175
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
+ }
3187
3183
3188
- for (size_t i = 0 ; i < psbt -> num_outputs ; i ++ )
3184
+ for (size_t i = 0 ; i < psbt -> num_outputs ; i ++ ) {
3189
3185
if (is_initiators_serial (& psbt -> outputs [i ].unknowns )) {
3190
3186
if (role == TX_INITIATOR )
3191
3187
weight += psbt_output_get_weight (psbt , i );
3192
3188
}
3193
- else
3189
+ else {
3194
3190
if (role != TX_INITIATOR )
3195
3191
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
+ }
3196
3198
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 );
3197
3220
return weight ;
3198
3221
}
3199
3222
@@ -3294,8 +3317,7 @@ static struct amount_sat check_balances(struct peer *peer,
3294
3317
{
3295
3318
struct amount_sat min_initiator_fee , min_accepter_fee ,
3296
3319
max_initiator_fee , max_accepter_fee ,
3297
- funding_amount_res , min_multiplied ,
3298
- initiator_penalty_fee , accepter_penalty_fee ;
3320
+ funding_amount_res ;
3299
3321
struct amount_msat funding_amount ,
3300
3322
initiator_fee , accepter_fee ;
3301
3323
struct amount_msat in [NUM_TX_ROLES ], out [NUM_TX_ROLES ],
@@ -3444,33 +3466,26 @@ static struct amount_sat check_balances(struct peer *peer,
3444
3466
"amount_sat_less / amount_sat_sub mismtach" );
3445
3467
3446
3468
min_initiator_fee = amount_tx_fee (peer -> splicing -> feerate_per_kw ,
3447
- calc_weight (TX_INITIATOR , psbt ));
3469
+ calc_weight (TX_INITIATOR , psbt , false ));
3448
3470
min_accepter_fee = amount_tx_fee (peer -> splicing -> feerate_per_kw ,
3449
- calc_weight (TX_ACCEPTER , psbt ));
3471
+ calc_weight (TX_ACCEPTER , psbt , false ));
3450
3472
3451
3473
/* As a safeguard max feerate is checked (only) locally, if it's
3452
3474
* particularly high we fail and tell the user but allow them to
3453
3475
* 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
+ }
3474
3489
3475
3490
/* Check initiator fee */
3476
3491
if (amount_msat_less_sat (initiator_fee , min_initiator_fee )) {
@@ -3487,23 +3502,38 @@ static struct amount_sat check_balances(struct peer *peer,
3487
3502
&& amount_msat_greater_sat (initiator_fee , max_initiator_fee )) {
3488
3503
msg = towire_channeld_splice_feerate_error (NULL , initiator_fee ,
3489
3504
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
+
3490
3513
wire_sync_write (MASTER_FD , take (msg ));
3514
+
3491
3515
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 ));
3496
3523
}
3497
3524
/* Check accepter fee */
3498
3525
if (amount_msat_less_sat (accepter_fee , min_accepter_fee )) {
3499
3526
msg = towire_channeld_splice_feerate_error (NULL , accepter_fee ,
3500
3527
false);
3501
3528
wire_sync_write (MASTER_FD , take (msg ));
3502
3529
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 );
3507
3537
}
3508
3538
if (!peer -> splicing -> force_feerate && !opener
3509
3539
&& amount_msat_greater_sat (accepter_fee , max_accepter_fee )) {
@@ -3956,6 +3986,9 @@ static void resume_splice_negotiation(struct peer *peer,
3956
3986
3957
3987
peer -> splicing = tal_free (peer -> splicing );
3958
3988
3989
+ if (our_role == TX_INITIATOR )
3990
+ calc_weight (TX_INITIATOR , current_psbt , true);
3991
+
3959
3992
final_tx = bitcoin_tx_with_psbt (tmpctx , current_psbt );
3960
3993
msg = towire_channeld_splice_confirmed_signed (tmpctx , final_tx ,
3961
3994
new_output_index );
0 commit comments