@@ -232,13 +232,14 @@ struct anchor_details *create_anchor_details(const tal_t *ctx,
232
232
return adet ;
233
233
}
234
234
235
- /* total_weight includes the commitment tx we're trying to push! */
235
+ /* total_weight includes the commitment tx we're trying to push, and the anchor fee output */
236
236
static struct wally_psbt * anchor_psbt (const tal_t * ctx ,
237
237
struct channel * channel ,
238
238
const struct one_anchor * anch ,
239
239
struct utxo * * utxos ,
240
240
u32 feerate_target ,
241
- size_t total_weight )
241
+ size_t total_weight ,
242
+ bool insufficient_funds )
242
243
{
243
244
struct lightningd * ld = channel -> peer -> ld ;
244
245
struct wally_psbt * psbt ;
@@ -263,12 +264,23 @@ static struct wally_psbt *anchor_psbt(const tal_t *ctx,
263
264
AMOUNT_SAT (330 ));
264
265
psbt_input_add_pubkey (psbt , psbt -> num_inputs - 1 , & channel -> local_funding_pubkey , false);
265
266
266
- /* A zero-output tx is invalid: we must have change, even if not really economic */
267
+ /* Calculate fee we need, given rate and weight */
268
+ fee = amount_tx_fee (feerate_target , total_weight );
269
+ /* Some fee already paid by commitment tx */
270
+ if (!amount_sat_sub (& fee , fee , anch -> info .commitment_fee ))
271
+ fee = AMOUNT_SAT (0 );
272
+
273
+ /* How much do we have? */
267
274
change = psbt_compute_fee (psbt );
268
- /* Assume we add a change output, what would the total fee be? */
269
- fee = amount_tx_fee ( feerate_target , total_weight + change_weight ());
275
+
276
+ /* We have to pay dust, at least! */
270
277
if (!amount_sat_sub (& change , change , fee )
271
278
|| amount_sat_less (change , chainparams -> dust_limit )) {
279
+ /* If we didn't run out of UTXOs, this implies our estimation was wrong! */
280
+ if (!insufficient_funds )
281
+ log_broken (channel -> log , "anchor: could not afford fee %s from change %s, reducing fee" ,
282
+ fmt_amount_sat (tmpctx , fee ),
283
+ fmt_amount_sat (tmpctx , psbt_compute_fee (psbt )));
272
284
change = chainparams -> dust_limit ;
273
285
}
274
286
@@ -293,6 +305,7 @@ static struct wally_psbt *try_anchor_psbt(const tal_t *ctx,
293
305
struct lightningd * ld = channel -> peer -> ld ;
294
306
struct wally_psbt * psbt ;
295
307
struct amount_sat fee ;
308
+ bool insufficient_funds ;
296
309
297
310
/* Ask for some UTXOs which could meet this feerate, and since
298
311
* we need one output, meet the minumum output requirement */
@@ -303,11 +316,11 @@ static struct wally_psbt *try_anchor_psbt(const tal_t *ctx,
303
316
anch -> info .commitment_fee ,
304
317
chainparams -> dust_limit ,
305
318
feerate_target ,
306
- total_weight , NULL );
319
+ total_weight , & insufficient_funds );
307
320
308
321
/* Create a new candidate PSBT */
309
322
psbt = anchor_psbt (ctx , channel , anch , * utxos , feerate_target ,
310
- * total_weight );
323
+ * total_weight , insufficient_funds );
311
324
* fee_spent = psbt_compute_fee (psbt );
312
325
313
326
/* Add in base commitment fee to calculate *overall* package feerate */
@@ -335,11 +348,11 @@ static struct bitcoin_tx *spend_anchor(const tal_t *ctx,
335
348
const u8 * msg ;
336
349
337
350
/* Estimate weight of anchorspend tx plus commitment_tx (not including any UTXO we add) */
338
- base_weight = bitcoin_tx_core_weight (2 , 1 )
351
+ base_weight = bitcoin_tx_core_weight (1 , 1 )
339
352
+ bitcoin_tx_input_weight (false,
340
353
bitcoin_tx_input_sig_weight ()
341
354
+ 1 + tal_bytelen (anch -> adet -> anchor_wscript ))
342
- + bitcoin_tx_output_weight ( BITCOIN_SCRIPTPUBKEY_P2WPKH_LEN )
355
+ + change_weight ( )
343
356
+ anch -> info .commitment_weight ;
344
357
345
358
total_value = AMOUNT_MSAT (0 );
@@ -372,15 +385,17 @@ static struct bitcoin_tx *spend_anchor(const tal_t *ctx,
372
385
& fee ,
373
386
& feerate ,
374
387
& utxos );
388
+ log_debug (channel -> log , "candidate_psbt total weight = %zu (commitment weight %u, anchor %zu)" ,
389
+ weight , anch -> info .commitment_weight , weight - anch -> info .commitment_weight );
375
390
376
391
/* Is it even worth spending this fee to meet the deadline? */
377
392
if (!amount_msat_greater_sat (total_value , fee )) {
378
393
log_debug (channel -> log ,
379
- "Not worth fee %s for %s commit tx to get %s in %u blocks at feerate %uperkw" ,
394
+ "Not worth fee %s for %s commit tx to get %s at block %u (%+i) at feerate %uperkw" ,
380
395
fmt_amount_sat (tmpctx , fee ),
381
396
anch -> commit_side == LOCAL ? "local" : "remote" ,
382
397
fmt_amount_msat (tmpctx , val -> msat ),
383
- val -> block , feerate_target );
398
+ val -> block , val -> block - get_block_height ( ld -> topology ), feerate_target );
384
399
break ;
385
400
}
386
401
@@ -405,11 +420,11 @@ static struct bitcoin_tx *spend_anchor(const tal_t *ctx,
405
420
break ;
406
421
}
407
422
408
- log_debug (channel -> log , "Worth fee %s for %s commit tx to get %s in %u blocks at feerate %uperkw" ,
423
+ log_debug (channel -> log , "Worth fee %s for %s commit tx to get %s at block %u (%+i) at feerate %uperkw" ,
409
424
fmt_amount_sat (tmpctx , fee ),
410
425
anch -> commit_side == LOCAL ? "local" : "remote" ,
411
426
fmt_amount_msat (tmpctx , val -> msat ),
412
- val -> block , feerate );
427
+ val -> block , val -> block - get_block_height ( ld -> topology ), feerate );
413
428
psbt = candidate_psbt ;
414
429
psbt_fee = fee ;
415
430
psbt_weight = weight ;
@@ -513,6 +528,7 @@ static struct bitcoin_tx *spend_anchor(const tal_t *ctx,
513
528
tx -> wtx = psbt_final_tx (tx , signed_psbt );
514
529
assert (tx -> wtx );
515
530
tx -> psbt = tal_steal (tx , signed_psbt );
531
+ log_debug (channel -> log , "anchor actual weight: %zu" , bitcoin_tx_weight (tx ));
516
532
517
533
return tx ;
518
534
}
0 commit comments