If it's possible and worth it, return signed tx. Otherwise NULL. */
| 332 | |
| 333 | /* If it's possible and worth it, return signed tx. Otherwise NULL. */ |
| 334 | static struct bitcoin_tx *spend_anchor(const tal_t *ctx, |
| 335 | struct channel *channel, |
| 336 | struct one_anchor *anch) |
| 337 | { |
| 338 | struct lightningd *ld = channel->peer->ld; |
| 339 | size_t base_weight, psbt_weight; |
| 340 | struct amount_sat psbt_fee, diff; |
| 341 | struct bitcoin_tx *tx; |
| 342 | struct utxo **psbt_utxos; |
| 343 | const struct hsm_utxo **hsm_utxos; |
| 344 | struct wally_psbt *psbt, *signed_psbt; |
| 345 | struct amount_msat total_value; |
| 346 | const struct deadline_value *unimportant_deadline; |
| 347 | const u8 *msg; |
| 348 | |
| 349 | /* Estimate weight of anchorspend tx plus commitment_tx (not including any UTXO we add) */ |
| 350 | base_weight = bitcoin_tx_core_weight(1, 1) |
| 351 | + bitcoin_tx_input_weight(false, |
| 352 | bitcoin_tx_input_sig_weight() |
| 353 | + 1 + tal_bytelen(anch->adet->anchor_wscript)) |
| 354 | + change_weight() |
| 355 | + anch->info.commitment_weight; |
| 356 | |
| 357 | total_value = AMOUNT_MSAT(0); |
| 358 | psbt = NULL; |
| 359 | unimportant_deadline = NULL; |
| 360 | |
| 361 | /* We start with furthest feerate target, and keep going backwards |
| 362 | * until it's either not worth making an anchor at that price for all |
| 363 | * the HTLCs from that point on, or we have an anchor for the closest |
| 364 | * HTLC deadline */ |
| 365 | for (int i = tal_count(anch->adet->vals) - 1; i >= 0; --i) { |
| 366 | const struct deadline_value *val = &anch->adet->vals[i]; |
| 367 | u32 feerate, feerate_target; |
| 368 | size_t weight; |
| 369 | struct amount_sat fee; |
| 370 | struct wally_psbt *candidate_psbt; |
| 371 | struct utxo **utxos; |
| 372 | |
| 373 | /* We only cover important deadlines here */ |
| 374 | if (!val->important) { |
| 375 | unimportant_deadline = val; |
| 376 | continue; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | if (!amount_msat_accumulate(&total_value, val->msat)) |
| 381 | abort(); |
| 382 | |
| 383 | feerate_target = feerate_for_target(ld->topology, val->block); |
| 384 | |
| 385 | /* If the feerate for the commitment tx is already |
| 386 | * sufficient, don't try for anchor. */ |
| 387 | if (amount_feerate(&feerate, |
| 388 | anch->info.commitment_fee, |
| 389 | anch->info.commitment_weight) |
| 390 | && feerate >= feerate_target) { |
| 391 | continue; |
no test coverage detected