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

Function grind_htlc_tx_fee

onchaind/onchaind.c:389–438  ·  view source on GitHub ↗

We vary feerate until signature they offered matches. */

Source from the content-addressed store, hash-verified

387
388/* We vary feerate until signature they offered matches. */
389static bool grind_htlc_tx_fee(struct amount_sat *fee,
390 struct bitcoin_tx *tx,
391 const struct bitcoin_signature *remotesig,
392 const u8 *wscript,
393 u64 weight)
394{
395 struct amount_sat prev_fee = AMOUNT_SAT(UINT64_MAX), input_amt;
396 input_amt = psbt_input_get_amount(tx->psbt, 0);
397
398 for (u64 i = min_possible_feerate; i <= max_possible_feerate; i++) {
399 /* BOLT #3:
400 *
401 * The fee for an HTLC-timeout transaction:
402 * - If `option_anchors` applies:
403 * 1. MUST be 0.
404 * - Otherwise, MUST be calculated to match:
405 * 1. Multiply `feerate_per_kw` by 663
406 * and divide by 1000 (rounding down).
407 *
408 * The fee for an HTLC-success transaction:
409 * - If `option_anchors` applies:
410 * 1. MUST be 0.
411 * - Otherwise, MUST be calculated to match:
412 * 1. Multiply `feerate_per_kw` by 703
413 * and divide by 1000 (rounding down).
414 */
415 struct amount_sat out;
416
417 *fee = amount_tx_fee(i, weight);
418
419 /* Minor optimization: don't check same fee twice */
420 if (amount_sat_eq(*fee, prev_fee))
421 continue;
422
423 prev_fee = *fee;
424 if (!amount_sat_sub(&out, input_amt, *fee))
425 break;
426
427 bitcoin_tx_output_set_amount(tx, 0, out);
428 bitcoin_tx_finalize(tx);
429 if (!check_tx_sig(tx, 0, NULL, wscript,
430 &keyset->other_htlc_key, remotesig))
431 continue;
432
433 status_debug("grind feerate_per_kw for %"PRIu64" = %"PRIu64,
434 weight, i);
435 return true;
436 }
437 return false;
438}
439
440static bool set_htlc_timeout_fee(struct bitcoin_tx *tx,
441 const struct bitcoin_signature *remotesig,

Callers 3

set_htlc_timeout_feeFunction · 0.85
get_htlc_success_feeFunction · 0.85
mainFunction · 0.85

Calls 7

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

Tested by 1

mainFunction · 0.68