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

Function adjust_offer

closingd/closingd.c:474–549  ·  view source on GitHub ↗

Figure out what we should offer now. */

Source from the content-addressed store, hash-verified

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

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
type_to_stringClass · 0.50
amount_sat_greater_eqFunction · 0.50
amount_sat_subFunction · 0.50

Tested by

no test coverage detected