Figure out what we should offer now. */
| 490 | |
| 491 | /* Figure out what we should offer now. */ |
| 492 | static struct amount_sat |
| 493 | adjust_offer(struct per_peer_state *pps, const struct channel_id *channel_id, |
| 494 | const struct feerange *feerange, struct amount_sat remote_offer, |
| 495 | struct amount_sat min_fee_to_accept, u64 fee_negotiation_step, |
| 496 | u8 fee_negotiation_step_unit) |
| 497 | { |
| 498 | struct amount_sat min_plus_one, range_len, step_sat, result; |
| 499 | struct amount_msat step_msat; |
| 500 | |
| 501 | /* Within 1 satoshi? Agree. */ |
| 502 | if (!amount_sat_add(&min_plus_one, feerange->min, AMOUNT_SAT(1))) |
| 503 | peer_failed_warn(pps, channel_id, |
| 504 | "Fee offer %s min too large", |
| 505 | fmt_amount_sat(tmpctx, feerange->min)); |
| 506 | |
| 507 | if (amount_sat_greater_eq(min_plus_one, feerange->max)) |
| 508 | return remote_offer; |
| 509 | |
| 510 | /* feerange has already been adjusted so that our new offer is ok to be |
| 511 | * any number in [feerange->min, feerange->max] and after the following |
| 512 | * min_fee_to_accept is in that range. Thus, pick a fee in |
| 513 | * [min_fee_to_accept, feerange->max]. */ |
| 514 | if (amount_sat_greater(feerange->min, min_fee_to_accept)) |
| 515 | min_fee_to_accept = feerange->min; |
| 516 | |
| 517 | /* Max is below our minimum acceptable? */ |
| 518 | if (!amount_sat_sub(&range_len, feerange->max, min_fee_to_accept)) |
| 519 | peer_failed_warn(pps, channel_id, |
| 520 | "Feerange %s-%s" |
| 521 | " below minimum acceptable %s", |
| 522 | fmt_amount_sat(tmpctx, feerange->min), |
| 523 | fmt_amount_sat(tmpctx, feerange->max), |
| 524 | fmt_amount_sat(tmpctx, min_fee_to_accept)); |
| 525 | |
| 526 | if (fee_negotiation_step_unit == |
| 527 | CLOSING_FEE_NEGOTIATION_STEP_UNIT_SATOSHI) { |
| 528 | /* -1 because the range boundary has already been adjusted with |
| 529 | * one from our previous proposal. So, if the user requested a |
| 530 | * step of 1 satoshi at a time we should just return our end of |
| 531 | * the range from this function. */ |
| 532 | step_msat = amount_msat((fee_negotiation_step - 1) |
| 533 | * MSAT_PER_SAT); |
| 534 | } else { |
| 535 | /* fee_negotiation_step is e.g. 20 to designate 20% from |
| 536 | * range_len (which is in satoshi), so: |
| 537 | * range_len * fee_negotiation_step / 100 [sat] |
| 538 | * is equivalent to: |
| 539 | * range_len * fee_negotiation_step * 10 [msat] */ |
| 540 | step_msat = amount_msat(range_len.satoshis /* Raw: % calc */ * |
| 541 | fee_negotiation_step * 10); |
| 542 | } |
| 543 | |
| 544 | step_sat = amount_msat_to_sat_round_down(step_msat); |
| 545 | |
| 546 | if (feerange->higher_side == LOCAL) { |
| 547 | if (!amount_sat_sub(&result, feerange->max, step_sat)) |
| 548 | /* step_sat > feerange->max, unlikely */ |
| 549 | return min_fee_to_accept; |
no test coverage detected