2024-08-02 18:28:45 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include <plugins/channel_hint.h>
|
|
|
|
|
|
|
|
void channel_hint_to_json(const char *name, const struct channel_hint *hint,
|
|
|
|
struct json_stream *dest)
|
|
|
|
{
|
|
|
|
json_object_start(dest, name);
|
|
|
|
json_add_u32(dest, "timestamp", hint->timestamp);
|
|
|
|
json_add_short_channel_id_dir(dest, "scid", hint->scid);
|
|
|
|
json_add_amount_msat(dest, "estimated_capacity_msat",
|
|
|
|
hint->estimated_capacity);
|
2024-10-03 18:02:14 +02:00
|
|
|
json_add_amount_msat(dest, "total_capacity_msat", hint->capacity);
|
2024-08-02 18:28:45 +02:00
|
|
|
json_add_bool(dest, "enabled", hint->enabled);
|
|
|
|
json_object_end(dest);
|
|
|
|
}
|
|
|
|
|
2024-08-23 14:52:19 +02:00
|
|
|
/* How long until even a channel whose estimate is down at 0msat will
|
|
|
|
* be considered fully refilled. The refill rate is the inverse of
|
|
|
|
* this times the channel size. The refilling is a linear
|
|
|
|
* approximation, with a small hysteresis applied in order to prevent
|
|
|
|
* a single payment relaxing its own constraints thus causing it to
|
|
|
|
* prematurely retry an already attempted channel.
|
|
|
|
*/
|
2024-08-02 18:28:45 +02:00
|
|
|
#define PAY_REFILL_TIME 7200
|
|
|
|
|
2024-08-23 14:52:19 +02:00
|
|
|
/* Add an artificial delay before accepting updates. This ensures we
|
|
|
|
* don't actually end up relaxing a tight constraint inbetween the
|
|
|
|
* attempt that added it and the next retry. If we were to relax right
|
|
|
|
* away, then we could end up retrying the exact same path we just
|
|
|
|
* failed at. If the `time_between_attempts * refill > 1msat`, we'd
|
|
|
|
* end up not actually constraining at all, because we set the
|
|
|
|
* estimate to `attempt - 1msat`. This also results in the updates
|
|
|
|
* being limited to once every minute, and causes a stairway
|
|
|
|
* pattern. The hysteresis has to be >60s otherwise a single payment
|
|
|
|
* can already end up retrying a previously excluded channel.
|
|
|
|
*/
|
|
|
|
#define PAY_REFILL_HYSTERESIS 60
|
2024-08-02 18:28:45 +02:00
|
|
|
/**
|
|
|
|
* 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 information we've learned from
|
|
|
|
* the gossip, then we return `false` to signal that the
|
|
|
|
* `channel_hint` may be removed.
|
|
|
|
*/
|
|
|
|
bool channel_hint_update(const struct timeabs now, struct channel_hint *hint)
|
|
|
|
{
|
|
|
|
/* Precision is not required here, so integer division is good
|
|
|
|
* enough. But keep the order such that we do not round down
|
|
|
|
* too much. We do so by first multiplying, before
|
|
|
|
* dividing. The formula is `current = last + delta_t *
|
|
|
|
* overall / refill_rate`.
|
|
|
|
*/
|
|
|
|
struct amount_msat refill;
|
2024-10-03 18:02:14 +02:00
|
|
|
struct amount_msat capacity = hint->capacity;
|
2024-08-16 19:04:15 +02:00
|
|
|
|
2024-08-23 14:52:19 +02:00
|
|
|
if (now.ts.tv_sec < hint->timestamp + PAY_REFILL_HYSTERESIS)
|
|
|
|
return true;
|
|
|
|
|
2024-08-02 18:28:45 +02:00
|
|
|
u64 seconds = now.ts.tv_sec - hint->timestamp;
|
2024-08-16 19:04:15 +02:00
|
|
|
if (!amount_msat_mul(&refill, capacity, seconds))
|
2024-08-02 18:28:45 +02:00
|
|
|
abort();
|
|
|
|
|
|
|
|
refill = amount_msat_div(refill, PAY_REFILL_TIME);
|
|
|
|
if (!amount_msat_add(&hint->estimated_capacity,
|
|
|
|
hint->estimated_capacity, refill))
|
|
|
|
abort();
|
|
|
|
|
|
|
|
/* Clamp the value to the `overall_capacity` */
|
2024-08-16 19:04:15 +02:00
|
|
|
if (amount_msat_greater(hint->estimated_capacity, capacity))
|
|
|
|
hint->estimated_capacity = capacity;
|
2024-08-02 18:28:45 +02:00
|
|
|
|
|
|
|
/* TODO This is rather coarse. We could map the disabled flag
|
|
|
|
to having 0msat capacity, and then relax from there. But it'd
|
|
|
|
likely be too slow of a relaxation.*/
|
|
|
|
if (seconds > 60)
|
|
|
|
hint->enabled = true;
|
|
|
|
|
|
|
|
/* Since we update in-place we should make sure that we can
|
|
|
|
* just call update again and the result is stable, if no time
|
|
|
|
* has passed. */
|
|
|
|
hint->timestamp = now.ts.tv_sec;
|
|
|
|
|
|
|
|
/* We report this hint as useless, if the hint does not
|
|
|
|
* restrict the channel, i.e., if it is enabled and the
|
|
|
|
* estimate is the same as the overall capacity. */
|
2024-08-16 19:04:15 +02:00
|
|
|
return !hint->enabled ||
|
|
|
|
amount_msat_greater(capacity, hint->estimated_capacity);
|
2024-08-02 18:28:45 +02:00
|
|
|
}
|
|
|
|
|
2024-08-22 18:34:38 +02:00
|
|
|
struct channel_hint *channel_hint_set_find(struct channel_hint_set *self,
|
|
|
|
const struct short_channel_id_dir *scidd)
|
|
|
|
{
|
|
|
|
for (size_t i=0; i<tal_count(self->hints); i++) {
|
|
|
|
struct channel_hint *hint = &self->hints[i];
|
|
|
|
if (short_channel_id_dir_eq(&hint->scid, scidd))
|
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See header */
|
|
|
|
struct channel_hint *
|
|
|
|
channel_hint_set_add(struct channel_hint_set *self, u32 timestamp,
|
|
|
|
const struct short_channel_id_dir *scidd, bool enabled,
|
|
|
|
const struct amount_msat *estimated_capacity,
|
2024-10-03 18:02:14 +02:00
|
|
|
const struct amount_msat capacity, u16 *htlc_budget)
|
2024-08-22 18:34:38 +02:00
|
|
|
{
|
2024-09-05 11:33:30 +02:00
|
|
|
struct channel_hint *copy, *old, *newhint;
|
2024-08-22 18:34:38 +02:00
|
|
|
|
|
|
|
/* If the channel is marked as enabled it must have an estimate. */
|
|
|
|
assert(!enabled || estimated_capacity != NULL);
|
|
|
|
|
2024-09-05 11:33:30 +02:00
|
|
|
/* If there was no hint, add the new one, if there was one,
|
|
|
|
* pick the one with the newer timestamp. */
|
2024-08-22 18:34:38 +02:00
|
|
|
old = channel_hint_set_find(self, scidd);
|
2024-09-05 11:33:30 +02:00
|
|
|
copy = tal_dup(tmpctx, struct channel_hint, old);
|
2024-08-22 18:34:38 +02:00
|
|
|
if (old == NULL) {
|
2024-09-05 11:33:30 +02:00
|
|
|
newhint = tal(tmpctx, struct channel_hint);
|
|
|
|
newhint->enabled = enabled;
|
|
|
|
newhint->scid = *scidd;
|
|
|
|
newhint->capacity = capacity;
|
|
|
|
if (estimated_capacity != NULL)
|
|
|
|
newhint->estimated_capacity = *estimated_capacity;
|
|
|
|
newhint->local = NULL;
|
|
|
|
newhint->timestamp = timestamp;
|
2024-08-22 18:34:38 +02:00
|
|
|
tal_arr_expand(&self->hints, *newhint);
|
|
|
|
return &self->hints[tal_count(self->hints) - 1];
|
2024-09-05 11:33:30 +02:00
|
|
|
} else if (old->timestamp <= timestamp) {
|
|
|
|
/* `local` is kept, since we do not pass in those
|
|
|
|
* annotations here. */
|
|
|
|
old->enabled = enabled;
|
2024-08-22 18:34:38 +02:00
|
|
|
old->timestamp = timestamp;
|
2024-09-05 11:33:30 +02:00
|
|
|
if (estimated_capacity != NULL)
|
|
|
|
old->estimated_capacity = *estimated_capacity;
|
|
|
|
|
|
|
|
/* We always pick the larger of the capacities we are
|
|
|
|
* being told. This is because in some cases, such as
|
|
|
|
* routehints, we're not actually being told the total
|
|
|
|
* capacity, just lower values. */
|
2024-10-03 18:02:14 +02:00
|
|
|
if (amount_msat_greater(capacity, old->capacity))
|
2024-09-05 11:33:30 +02:00
|
|
|
old->capacity = capacity;
|
|
|
|
|
|
|
|
return copy;
|
2024-08-22 18:34:38 +02:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-02 18:28:45 +02:00
|
|
|
/**
|
|
|
|
* Load a channel_hint from its JSON representation.
|
|
|
|
*
|
|
|
|
* @return The initialized `channel_hint` or `NULL` if we encountered a parsing
|
|
|
|
* error.
|
|
|
|
*/
|
|
|
|
struct channel_hint *channel_hint_from_json(const tal_t *ctx,
|
2024-08-09 12:25:01 +02:00
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *toks)
|
2024-08-02 18:28:45 +02:00
|
|
|
{
|
|
|
|
const char *ret;
|
|
|
|
const jsmntok_t *payload = json_get_member(buffer, toks, "payload"),
|
|
|
|
*jhint =
|
|
|
|
json_get_member(buffer, payload, "channel_hint");
|
|
|
|
struct channel_hint *hint = tal(ctx, struct channel_hint);
|
2024-08-11 15:14:37 +02:00
|
|
|
|
|
|
|
ret = json_scan(ctx, buffer, jhint,
|
2024-10-03 18:02:14 +02:00
|
|
|
"{timestamp:%,scid:%,estimated_capacity_msat:%,total_capacity_msat:%,enabled:%}",
|
2024-08-02 18:28:45 +02:00
|
|
|
JSON_SCAN(json_to_u32, &hint->timestamp),
|
|
|
|
JSON_SCAN(json_to_short_channel_id_dir, &hint->scid),
|
|
|
|
JSON_SCAN(json_to_msat, &hint->estimated_capacity),
|
2024-10-03 18:02:14 +02:00
|
|
|
JSON_SCAN(json_to_msat, &hint->capacity),
|
2024-08-02 18:28:45 +02:00
|
|
|
JSON_SCAN(json_to_bool, &hint->enabled));
|
|
|
|
|
|
|
|
if (ret != NULL)
|
|
|
|
hint = tal_free(hint);
|
|
|
|
return hint;
|
|
|
|
}
|
2024-08-09 12:25:01 +02:00
|
|
|
|
|
|
|
struct channel_hint_set *channel_hint_set_new(const tal_t *ctx)
|
|
|
|
{
|
|
|
|
struct channel_hint_set *set = tal(ctx, struct channel_hint_set);
|
|
|
|
set->hints = tal_arr(set, struct channel_hint, 0);
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
|
|
|
void channel_hint_set_update(struct channel_hint_set *set,
|
|
|
|
const struct timeabs now)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(set->hints); i++)
|
|
|
|
channel_hint_update(time_now(), &set->hints[i]);
|
|
|
|
}
|
2024-08-23 14:55:28 +02:00
|
|
|
|
|
|
|
size_t channel_hint_set_count(const struct channel_hint_set *set)
|
|
|
|
{
|
|
|
|
return tal_count(set->hints);
|
|
|
|
}
|