@@ -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 */
@@ -336,11 +349,11 @@ static struct bitcoin_tx *spend_anchor(const tal_t *ctx,
336
349
const u8 * msg ;
337
350
338
351
/* Estimate weight of anchorspend tx plus commitment_tx (not including any UTXO we add) */
339
- base_weight = bitcoin_tx_core_weight (2 , 1 )
352
+ base_weight = bitcoin_tx_core_weight (1 , 1 )
340
353
+ bitcoin_tx_input_weight (false,
341
354
bitcoin_tx_input_sig_weight ()
342
355
+ 1 + tal_bytelen (anch -> adet -> anchor_wscript ))
343
- + bitcoin_tx_output_weight ( BITCOIN_SCRIPTPUBKEY_P2WPKH_LEN )
356
+ + change_weight ( )
344
357
+ anch -> info .commitment_weight ;
345
358
346
359
total_value = AMOUNT_MSAT (0 );
@@ -387,15 +400,17 @@ static struct bitcoin_tx *spend_anchor(const tal_t *ctx,
387
400
& fee ,
388
401
& feerate ,
389
402
& utxos );
403
+ log_debug (channel -> log , "candidate_psbt total weight = %zu (commitment weight %u, anchor %zu)" ,
404
+ weight , anch -> info .commitment_weight , weight - anch -> info .commitment_weight );
390
405
391
406
/* Is it even worth spending this fee to meet the deadline? */
392
407
if (!amount_msat_greater_sat (total_value , fee )) {
393
408
log_debug (channel -> log ,
394
- "Not worth fee %s for %s commit tx to get %s in %u blocks at feerate %uperkw" ,
409
+ "Not worth fee %s for %s commit tx to get %s at block %u (%+i) at feerate %uperkw" ,
395
410
fmt_amount_sat (tmpctx , fee ),
396
411
anch -> commit_side == LOCAL ? "local" : "remote" ,
397
412
fmt_amount_msat (tmpctx , val -> msat ),
398
- val -> block , feerate_target );
413
+ val -> block , val -> block - get_block_height ( ld -> topology ), feerate_target );
399
414
break ;
400
415
}
401
416
@@ -420,11 +435,11 @@ static struct bitcoin_tx *spend_anchor(const tal_t *ctx,
420
435
break ;
421
436
}
422
437
423
- log_debug (channel -> log , "Worth fee %s for %s commit tx to get %s in %u blocks at feerate %uperkw" ,
438
+ log_debug (channel -> log , "Worth fee %s for %s commit tx to get %s at block %u (%+i) at feerate %uperkw" ,
424
439
fmt_amount_sat (tmpctx , fee ),
425
440
anch -> commit_side == LOCAL ? "local" : "remote" ,
426
441
fmt_amount_msat (tmpctx , val -> msat ),
427
- val -> block , feerate );
442
+ val -> block , val -> block - get_block_height ( ld -> topology ), feerate );
428
443
psbt = candidate_psbt ;
429
444
psbt_fee = fee ;
430
445
psbt_weight = weight ;
@@ -539,6 +554,7 @@ static struct bitcoin_tx *spend_anchor(const tal_t *ctx,
539
554
tx -> wtx = psbt_final_tx (tx , signed_psbt );
540
555
assert (tx -> wtx );
541
556
tx -> psbt = tal_steal (tx , signed_psbt );
557
+ log_debug (channel -> log , "anchor actual weight: %zu" , bitcoin_tx_weight (tx ));
542
558
543
559
return tx ;
544
560
}
0 commit comments