MCPcopy Create free account
hub / github.com/ElementsProject/lightning / spend_anchor

Function spend_anchor

lightningd/anchorspend.c:333–557  ·  view source on GitHub ↗

If it's possible and worth it, return signed tx. Otherwise NULL. */

Source from the content-addressed store, hash-verified

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

Callers 2

refresh_anchor_spendFunction · 0.85

Calls 15

bitcoin_tx_core_weightFunction · 0.85
bitcoin_tx_input_weightFunction · 0.85
tal_bytelenFunction · 0.85
change_weightFunction · 0.85
amount_msat_accumulateFunction · 0.85
abortFunction · 0.85
feerate_for_targetFunction · 0.85
try_anchor_psbtFunction · 0.85
amount_msat_greater_satFunction · 0.85
tal_freeFunction · 0.85
amount_sat_lessFunction · 0.85

Tested by

no test coverage detected