We vary feerate until signature they offered matches. */
| 387 | |
| 388 | /* We vary feerate until signature they offered matches. */ |
| 389 | static 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 | |
| 440 | static bool set_htlc_timeout_fee(struct bitcoin_tx *tx, |
| 441 | const struct bitcoin_signature *remotesig, |