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

Function select_inchan

lightningd/invoice.c:534–599  ·  view source on GitHub ↗

* From array of incoming channels [inchan], find suitable ones for * a payment-to-us of [amount_needed], using criteria: * 1. Channel's peer is known, in state CHANNELD_NORMAL and is online. * 2. Channel's peer capacity to pay us is sufficient. * * Then use weighted reservoir sampling, which makes probing channel balances * harder, to choose one channel from the set of suitable channels. It

Source from the content-addressed store, hash-verified

532 * channels that have less balance on our side as fraction of their capacity.
533 */
534static struct route_info **select_inchan(const tal_t *ctx,
535 struct lightningd *ld,
536 struct amount_msat amount_needed,
537 const struct routehint_candidate
538 *candidates)
539{
540 /* BOLT-11 struct wants an array of arrays (can provide multiple routes) */
541 struct route_info **r = NULL;
542 double total_weight = 0.0;
543
544 /* Collect suitable channels and assign each a weight. */
545 for (size_t i = 0; i < tal_count(candidates); i++) {
546 struct amount_msat excess, capacity;
547 struct amount_sat cumulative_reserve;
548 double excess_frac;
549
550 /* Does the peer have sufficient balance to pay us,
551 * even after having taken into account their reserve? */
552 if (!amount_msat_sub(&excess, candidates[i].capacity,
553 amount_needed))
554 continue;
555
556 /* Channel balance as seen by our node:
557
558 |<----------------- capacity ----------------->|
559 . .
560 . |<------------------ their_msat -------------------->|
561 . | . |
562 . |<----- capacity_to_pay_us ----->|<- their_reserve ->|
563 . | | |
564 . |<- amount_needed --><- excess ->| |
565 . | | |
566 |-------|-------------|--------------------------------|-------------------|
567 0 ^ ^ ^ funding
568 our_reserve our_msat */
569
570 /* Find capacity and calculate its excess fraction */
571 if (!amount_sat_add(&cumulative_reserve,
572 candidates[i].c->our_config.channel_reserve,
573 candidates[i].c->channel_info.their_config.channel_reserve)
574 || !amount_sat_to_msat(&capacity, candidates[i].c->funding_sats)
575 || !amount_msat_deduct_sat(&capacity, cumulative_reserve)) {
576 log_broken(ld->log, "Channel %s capacity overflow!",
577 fmt_short_channel_id(tmpctx, *candidates[i].c->scid));
578 continue;
579 }
580
581 /* We don't want a 0 probability if 0 excess; it might be the
582 * only one! So bump it by 1 msat */
583 if (!amount_msat_accumulate(&excess, AMOUNT_MSAT(1))) {
584 log_broken(ld->log, "Channel %s excess overflow!",
585 fmt_short_channel_id(tmpctx,
586 *candidates[i].c->scid));
587 continue;
588 }
589 excess_frac = amount_msat_ratio(excess, capacity);
590
591 if (random_select(excess_frac, &total_weight)) {

Callers 2

add_routehintsFunction · 0.85
mainFunction · 0.85

Calls 9

amount_msat_subFunction · 0.85
amount_msat_deduct_satFunction · 0.85
fmt_short_channel_idFunction · 0.85
amount_msat_accumulateFunction · 0.85
amount_msat_ratioFunction · 0.85
random_selectFunction · 0.85
tal_freeFunction · 0.85
amount_sat_addFunction · 0.50
amount_sat_to_msatFunction · 0.50

Tested by 1

mainFunction · 0.68