Get the minimum and desired fees */
| 585 | |
| 586 | /* Get the minimum and desired fees */ |
| 587 | static void calc_fee_bounds(size_t expected_weight, |
| 588 | u32 min_feerate, |
| 589 | u32 desired_feerate, |
| 590 | u32 max_feerate, |
| 591 | struct amount_sat funding, |
| 592 | enum side opener, |
| 593 | struct amount_sat *minfee, |
| 594 | struct amount_sat *desiredfee, |
| 595 | struct amount_sat *maxfee) |
| 596 | { |
| 597 | *minfee = amount_tx_fee(min_feerate, expected_weight); |
| 598 | *desiredfee = amount_tx_fee(desired_feerate, expected_weight); |
| 599 | |
| 600 | /* BOLT #2: |
| 601 | * - if it is not the funder: |
| 602 | * - SHOULD set `max_fee_satoshis` to at least the `max_fee_satoshis` received |
| 603 | */ |
| 604 | /* BOLT #2: |
| 605 | * |
| 606 | * Note that the non-funder is not paying the fee, so there is no reason for it |
| 607 | * to have a maximum feerate. |
| 608 | */ |
| 609 | if (opener == REMOTE) { |
| 610 | *maxfee = funding; |
| 611 | |
| 612 | } else { |
| 613 | /* BOLT #2: |
| 614 | * The sending node: |
| 615 | * |
| 616 | * - SHOULD set the initial `fee_satoshis` according to its |
| 617 | * estimate of cost of inclusion in a block. |
| 618 | * |
| 619 | * - SHOULD set `fee_range` according to the minimum and |
| 620 | * maximum fees it is prepared to pay for a close |
| 621 | * transaction. |
| 622 | */ |
| 623 | *maxfee = amount_tx_fee(max_feerate, expected_weight); |
| 624 | status_debug("deriving max fee from rate %u -> %s", |
| 625 | max_feerate, |
| 626 | fmt_amount_sat(tmpctx, *maxfee)); |
| 627 | } |
| 628 | |
| 629 | /* Can't exceed maxfee. */ |
| 630 | if (amount_sat_greater(*minfee, *maxfee)) |
| 631 | *minfee = *maxfee; |
| 632 | |
| 633 | if (amount_sat_less(*desiredfee, *minfee)) { |
| 634 | status_unusual("Our ideal fee is %s (%u sats/perkw)," |
| 635 | " but our minimum is %s: using that", |
| 636 | fmt_amount_sat(tmpctx, *desiredfee), |
| 637 | desired_feerate, |
| 638 | fmt_amount_sat(tmpctx, *minfee)); |
| 639 | *desiredfee = *minfee; |
| 640 | } |
| 641 | if (amount_sat_greater(*desiredfee, *maxfee)) { |
| 642 | status_unusual("Our ideal fee is %s (%u sats/perkw)," |
| 643 | " but our maximum is %s: using that", |
| 644 | fmt_amount_sat(tmpctx, *desiredfee), |
no test coverage detected