| 476 | } |
| 477 | |
| 478 | static bool set_htlc_timeout_fee(struct bitcoin_tx *tx, |
| 479 | const struct bitcoin_signature *remotesig, |
| 480 | const u8 *wscript) |
| 481 | { |
| 482 | static struct amount_sat amount, fee = AMOUNT_SAT_INIT(UINT64_MAX); |
| 483 | struct amount_asset asset = bitcoin_tx_output_get_amount(tx, 0); |
| 484 | size_t weight; |
| 485 | |
| 486 | /* BOLT #3: |
| 487 | * |
| 488 | * The fee for an HTLC-timeout transaction: |
| 489 | * - If `option_anchors_zero_fee_htlc_tx` applies: |
| 490 | * 1. MUST be 0. |
| 491 | * - Otherwise, MUST be calculated to match: |
| 492 | * 1. Multiply `feerate_per_kw` by 663 (666 if `option_anchor_outputs` |
| 493 | * applies) and divide by 1000 (rounding down). |
| 494 | */ |
| 495 | if (option_anchor_outputs) |
| 496 | weight = 666; |
| 497 | else |
| 498 | weight = 663; |
| 499 | weight += elements_tx_overhead(chainparams, 1, 1); |
| 500 | |
| 501 | assert(amount_asset_is_main(&asset)); |
| 502 | amount = amount_asset_to_sat(&asset); |
| 503 | |
| 504 | if (amount_sat_eq(fee, AMOUNT_SAT(UINT64_MAX))) { |
| 505 | struct amount_sat grindfee; |
| 506 | if (grind_htlc_tx_fee(&grindfee, tx, remotesig, wscript, weight)) { |
| 507 | /* Cache this for next time */ |
| 508 | fee = grindfee; |
| 509 | return true; |
| 510 | } |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | if (!amount_sat_sub(&amount, amount, fee)) |
| 515 | status_failed(STATUS_FAIL_INTERNAL_ERROR, |
| 516 | "Cannot deduct htlc-timeout fee %s from tx %s", |
| 517 | type_to_string(tmpctx, struct amount_sat, &fee), |
| 518 | type_to_string(tmpctx, struct bitcoin_tx, tx)); |
| 519 | |
| 520 | bitcoin_tx_output_set_amount(tx, 0, amount); |
| 521 | bitcoin_tx_finalize(tx); |
| 522 | return check_tx_sig(tx, 0, NULL, wscript, |
| 523 | &keyset->other_htlc_key, remotesig); |
| 524 | } |
| 525 | |
| 526 | static void set_htlc_success_fee(struct bitcoin_tx *tx, |
| 527 | const struct bitcoin_signature *remotesig, |
no test coverage detected