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

Function channel_hint_update

plugins/channel_hint.c:68–111  ·  view source on GitHub ↗

* Update the `channel_hint` in place, return whether it should be kept. * * This computes the refill-rate based on the overall capacity, and * the time elapsed since the last update and relaxes the upper bound * on the capacity, and resets the enabled flag if appropriate. If the * hint is no longer useful, i.e., it does not provide any additional * information on top of the structural inform

Source from the content-addressed store, hash-verified

66 * `channel_hint` may be removed.
67 */
68bool channel_hint_update(const struct timeabs now, struct channel_hint *hint)
69{
70 /* Precision is not required here, so integer division is good
71 * enough. But keep the order such that we do not round down
72 * too much. We do so by first multiplying, before
73 * dividing. The formula is `current = last + delta_t *
74 * overall / refill_rate`.
75 */
76 struct amount_msat refill;
77 struct amount_msat capacity = hint->capacity;
78
79 if (now.ts.tv_sec < hint->timestamp + PAY_REFILL_HYSTERESIS)
80 return true;
81
82 u64 seconds = now.ts.tv_sec - hint->timestamp;
83 if (!amount_msat_mul(&refill, capacity, seconds))
84 abort();
85
86 refill = amount_msat_div(refill, PAY_REFILL_TIME);
87 if (!amount_msat_add(&hint->estimated_capacity,
88 hint->estimated_capacity, refill))
89 abort();
90
91 /* Clamp the value to the `overall_capacity` */
92 if (amount_msat_greater(hint->estimated_capacity, capacity))
93 hint->estimated_capacity = capacity;
94
95 /* TODO This is rather coarse. We could map the disabled flag
96 to having 0msat capacity, and then relax from there. But it'd
97 likely be too slow of a relaxation.*/
98 if (seconds > 60)
99 hint->enabled = true;
100
101 /* Since we update in-place we should make sure that we can
102 * just call update again and the result is stable, if no time
103 * has passed. */
104 hint->timestamp = now.ts.tv_sec;
105
106 /* We report this hint as useless, if the hint does not
107 * restrict the channel, i.e., if it is enabled and the
108 * estimate is the same as the overall capacity. */
109 return !hint->enabled ||
110 amount_msat_greater(capacity, hint->estimated_capacity);
111}
112
113struct channel_hint *channel_hint_set_find(const struct channel_hint_set *self,
114 const struct short_channel_id_dir *scidd)

Callers 1

channel_hint_set_updateFunction · 0.85

Calls 5

amount_msat_mulFunction · 0.85
abortFunction · 0.85
amount_msat_divFunction · 0.85
amount_msat_addFunction · 0.85
amount_msat_greaterFunction · 0.85

Tested by

no test coverage detected