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

Function grind_htlc_tx_fee

onchaind/onchaind.c:425–476  ·  view source on GitHub ↗

We vary feerate until signature they offered matches. */

Source from the content-addressed store, hash-verified

423
424/* We vary feerate until signature they offered matches. */
425static bool grind_htlc_tx_fee(struct amount_sat *fee,
426 struct bitcoin_tx *tx,
427 const struct bitcoin_signature *remotesig,
428 const u8 *wscript,
429 u64 weight)
430{
431 struct amount_sat prev_fee = AMOUNT_SAT(UINT64_MAX), input_amt;
432 input_amt = psbt_input_get_amount(tx->psbt, 0);
433
434 for (u64 i = min_possible_feerate; i <= max_possible_feerate; i++) {
435 /* BOLT #3:
436 *
437 * The fee for an HTLC-timeout transaction:
438 * - If `option_anchors_zero_fee_htlc_tx` applies:
439 * 1. MUST be 0.
440 * - Otherwise, MUST be calculated to match:
441 * 1. Multiply `feerate_per_kw` by 663
442 * (666 if `option_anchor_outputs` applies)
443 * and divide by 1000 (rounding down).
444 *
445 * The fee for an HTLC-success transaction:
446 * - If `option_anchors_zero_fee_htlc_tx` applies:
447 * 1. MUST be 0.
448 * - Otherwise, MUST be calculated to match:
449 * 1. Multiply `feerate_per_kw` by 703
450 * (706 if `option_anchor_outputs` applies)
451 * and divide by 1000 (rounding down).
452 */
453 struct amount_sat out;
454
455 *fee = amount_tx_fee(i, weight);
456
457 /* Minor optimization: don't check same fee twice */
458 if (amount_sat_eq(*fee, prev_fee))
459 continue;
460
461 prev_fee = *fee;
462 if (!amount_sat_sub(&out, input_amt, *fee))
463 break;
464
465 bitcoin_tx_output_set_amount(tx, 0, out);
466 bitcoin_tx_finalize(tx);
467 if (!check_tx_sig(tx, 0, NULL, wscript,
468 &keyset->other_htlc_key, remotesig))
469 continue;
470
471 status_debug("grind feerate_per_kw for %"PRIu64" = %"PRIu64,
472 weight, i);
473 return true;
474 }
475 return false;
476}
477
478static bool set_htlc_timeout_fee(struct bitcoin_tx *tx,
479 const struct bitcoin_signature *remotesig,

Callers 3

set_htlc_timeout_feeFunction · 0.85
set_htlc_success_feeFunction · 0.85
mainFunction · 0.85

Calls 7

bitcoin_tx_finalizeFunction · 0.85
check_tx_sigFunction · 0.85
psbt_input_get_amountFunction · 0.50
amount_tx_feeFunction · 0.50
amount_sat_eqFunction · 0.50
amount_sat_subFunction · 0.50

Tested by 1

mainFunction · 0.68