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

Function adjust_offer

closingd/closingd.c:469–540  ·  view source on GitHub ↗

Figure out what we should offer now. */

Source from the content-addressed store, hash-verified

467
468/* Figure out what we should offer now. */
469static struct amount_sat
470adjust_offer(struct per_peer_state *pps, const struct channel_id *channel_id,
471 const struct feerange *feerange, struct amount_sat remote_offer,
472 struct amount_sat min_fee_to_accept, u64 fee_negotiation_step,
473 u8 fee_negotiation_step_unit)
474{
475 struct amount_sat min_plus_one, range_len, step_sat, result;
476 struct amount_msat step_msat;
477
478 /* Within 1 satoshi? Agree. */
479 if (!amount_sat_add(&min_plus_one, feerange->min, AMOUNT_SAT(1)))
480 peer_failed_warn(pps, channel_id,
481 "Fee offer %s min too large",
482 fmt_amount_sat(tmpctx, feerange->min));
483
484 if (amount_sat_greater_eq(min_plus_one, feerange->max))
485 return remote_offer;
486
487 /* feerange has already been adjusted so that our new offer is ok to be
488 * any number in [feerange->min, feerange->max] and after the following
489 * min_fee_to_accept is in that range. Thus, pick a fee in
490 * [min_fee_to_accept, feerange->max]. */
491 if (amount_sat_greater(feerange->min, min_fee_to_accept))
492 min_fee_to_accept = feerange->min;
493
494 /* Max is below our minimum acceptable? */
495 if (!amount_sat_sub(&range_len, feerange->max, min_fee_to_accept))
496 peer_failed_warn(pps, channel_id,
497 "Feerange %s-%s"
498 " below minimum acceptable %s",
499 fmt_amount_sat(tmpctx, feerange->min),
500 fmt_amount_sat(tmpctx, feerange->max),
501 fmt_amount_sat(tmpctx, min_fee_to_accept));
502
503 if (fee_negotiation_step_unit ==
504 CLOSING_FEE_NEGOTIATION_STEP_UNIT_SATOSHI) {
505 /* -1 because the range boundary has already been adjusted with
506 * one from our previous proposal. So, if the user requested a
507 * step of 1 satoshi at a time we should just return our end of
508 * the range from this function. */
509 step_msat = amount_msat((fee_negotiation_step - 1)
510 * MSAT_PER_SAT);
511 } else {
512 /* fee_negotiation_step is e.g. 20 to designate 20% from
513 * range_len (which is in satoshi), so:
514 * range_len * fee_negotiation_step / 100 [sat]
515 * is equivalent to:
516 * range_len * fee_negotiation_step * 10 [msat] */
517 step_msat = amount_msat(range_len.satoshis /* Raw: % calc */ *
518 fee_negotiation_step * 10);
519 }
520
521 step_sat = amount_msat_to_sat_round_down(step_msat);
522
523 if (feerange->higher_side == LOCAL) {
524 if (!amount_sat_sub(&result, feerange->max, step_sat))
525 /* step_sat > feerange->max, unlikely */
526 return min_fee_to_accept;

Callers 1

mainFunction · 0.85

Calls 9

peer_failed_warnFunction · 0.85
amount_sat_greaterFunction · 0.85
amount_sat_less_eqFunction · 0.85
amount_msatClass · 0.70
amount_sat_addFunction · 0.50
fmt_amount_satFunction · 0.50
amount_sat_greater_eqFunction · 0.50
amount_sat_subFunction · 0.50

Tested by

no test coverage detected