pay: Switch to msat for total_capacity

This minimizes the need to convert back and forth from and to sat
values, and it also removes a new instance of sats in the public
interface (`channel_hints`).

Suggested-By: Rusty Russell <@rustyrussell>
This commit is contained in:
Christian Decker 2024-10-03 18:02:14 +02:00
parent ddc199ff41
commit a6a7dd8f71
6 changed files with 21 additions and 24 deletions

View File

@ -81,7 +81,7 @@ static bool dijkstra_to_hops(struct route_hop **hops,
const struct gossmap_node *next; const struct gossmap_node *next;
size_t num_hops = tal_count(*hops); size_t num_hops = tal_count(*hops);
const struct half_chan *h; const struct half_chan *h;
struct amount_sat total; struct amount_msat total_msat;
if (dist == 0) if (dist == 0)
return true; return true;
@ -105,8 +105,8 @@ static bool dijkstra_to_hops(struct route_hop **hops,
if (!dijkstra_to_hops(hops, gossmap, dij, next, amount, cltv)) if (!dijkstra_to_hops(hops, gossmap, dij, next, amount, cltv))
return false; return false;
gossmap_chan_get_capacity(gossmap, c, &total); total_msat = gossmap_chan_get_capacity(gossmap, c);
(*hops)[num_hops].capacity = total; (*hops)[num_hops].capacity = total_msat;
(*hops)[num_hops].amount = *amount; (*hops)[num_hops].amount = *amount;
(*hops)[num_hops].delay = *cltv; (*hops)[num_hops].delay = *cltv;

View File

@ -26,7 +26,7 @@ struct route_hop {
int direction; int direction;
struct node_id node_id; struct node_id node_id;
struct amount_msat amount; struct amount_msat amount;
struct amount_sat capacity; struct amount_msat capacity;
u32 delay; u32 delay;
}; };

View File

@ -9,7 +9,7 @@ void channel_hint_to_json(const char *name, const struct channel_hint *hint,
json_add_short_channel_id_dir(dest, "scid", hint->scid); json_add_short_channel_id_dir(dest, "scid", hint->scid);
json_add_amount_msat(dest, "estimated_capacity_msat", json_add_amount_msat(dest, "estimated_capacity_msat",
hint->estimated_capacity); hint->estimated_capacity);
json_add_amount_sat(dest, "capacity_sat", hint->capacity); json_add_amount_msat(dest, "total_capacity_msat", hint->capacity);
json_add_bool(dest, "enabled", hint->enabled); json_add_bool(dest, "enabled", hint->enabled);
json_object_end(dest); json_object_end(dest);
} }
@ -55,9 +55,7 @@ bool channel_hint_update(const struct timeabs now, struct channel_hint *hint)
* overall / refill_rate`. * overall / refill_rate`.
*/ */
struct amount_msat refill; struct amount_msat refill;
struct amount_msat capacity; struct amount_msat capacity = hint->capacity;
if (!amount_sat_to_msat(&capacity, hint->capacity))
abort();
if (now.ts.tv_sec < hint->timestamp + PAY_REFILL_HYSTERESIS) if (now.ts.tv_sec < hint->timestamp + PAY_REFILL_HYSTERESIS)
return true; return true;
@ -109,7 +107,7 @@ struct channel_hint *
channel_hint_set_add(struct channel_hint_set *self, u32 timestamp, channel_hint_set_add(struct channel_hint_set *self, u32 timestamp,
const struct short_channel_id_dir *scidd, bool enabled, const struct short_channel_id_dir *scidd, bool enabled,
const struct amount_msat *estimated_capacity, const struct amount_msat *estimated_capacity,
const struct amount_sat capacity, u16 *htlc_budget) const struct amount_msat capacity, u16 *htlc_budget)
{ {
struct channel_hint *copy, *old, *newhint; struct channel_hint *copy, *old, *newhint;
@ -143,7 +141,7 @@ channel_hint_set_add(struct channel_hint_set *self, u32 timestamp,
* being told. This is because in some cases, such as * being told. This is because in some cases, such as
* routehints, we're not actually being told the total * routehints, we're not actually being told the total
* capacity, just lower values. */ * capacity, just lower values. */
if (amount_sat_greater(capacity, old->capacity)) if (amount_msat_greater(capacity, old->capacity))
old->capacity = capacity; old->capacity = capacity;
return copy; return copy;
@ -169,11 +167,11 @@ struct channel_hint *channel_hint_from_json(const tal_t *ctx,
struct channel_hint *hint = tal(ctx, struct channel_hint); struct channel_hint *hint = tal(ctx, struct channel_hint);
ret = json_scan(ctx, buffer, jhint, ret = json_scan(ctx, buffer, jhint,
"{timestamp:%,scid:%,estimated_capacity_msat:%,capacity_sat:%,enabled:%}", "{timestamp:%,scid:%,estimated_capacity_msat:%,total_capacity_msat:%,enabled:%}",
JSON_SCAN(json_to_u32, &hint->timestamp), JSON_SCAN(json_to_u32, &hint->timestamp),
JSON_SCAN(json_to_short_channel_id_dir, &hint->scid), JSON_SCAN(json_to_short_channel_id_dir, &hint->scid),
JSON_SCAN(json_to_msat, &hint->estimated_capacity), JSON_SCAN(json_to_msat, &hint->estimated_capacity),
JSON_SCAN(json_to_sat, &hint->capacity), JSON_SCAN(json_to_msat, &hint->capacity),
JSON_SCAN(json_to_bool, &hint->enabled)); JSON_SCAN(json_to_bool, &hint->enabled));
if (ret != NULL) if (ret != NULL)

View File

@ -39,10 +39,10 @@ struct channel_hint {
/* Non-null if we are one endpoint of this channel */ /* Non-null if we are one endpoint of this channel */
struct local_hint *local; struct local_hint *local;
/* The total `amount_sat` that were used to fund the /* The total `amount_msat` that were used to fund the
* channel. This is always smaller gte the * channel. This is always smaller gte the estimated_capacity
* estimated_capacity (after normalization) */ * (after normalization) */
struct amount_sat capacity; struct amount_msat capacity;
}; };
/* A collection of channel_hint instances, allowing us to handle and /* A collection of channel_hint instances, allowing us to handle and
@ -84,7 +84,7 @@ struct channel_hint *channel_hint_set_add(struct channel_hint_set *self,
const struct short_channel_id_dir *scidd, const struct short_channel_id_dir *scidd,
bool enabled, bool enabled,
const struct amount_msat *estimated_capacity, const struct amount_msat *estimated_capacity,
const struct amount_sat overall_capacity, const struct amount_msat overall_capacity,
u16 *htlc_budget); u16 *htlc_budget);
/** /**

View File

@ -411,7 +411,7 @@ static void channel_hints_update(struct payment *p,
const struct short_channel_id scid, const struct short_channel_id scid,
int direction, bool enabled, bool local, int direction, bool enabled, bool local,
const struct amount_msat *estimated_capacity, const struct amount_msat *estimated_capacity,
const struct amount_sat overall_capacity, const struct amount_msat overall_capacity,
u16 *htlc_budget) u16 *htlc_budget)
{ {
struct payment *root = payment_root(p); struct payment *root = payment_root(p);
@ -2553,7 +2553,7 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer,
const jsmntok_t *toks, struct payment *p) const jsmntok_t *toks, struct payment *p)
{ {
struct listpeers_channel **chans; struct listpeers_channel **chans;
struct amount_sat capacity; struct amount_msat capacity;
chans = json_to_listpeers_channels(tmpctx, buffer, toks); chans = json_to_listpeers_channels(tmpctx, buffer, toks);
@ -2578,8 +2578,7 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer,
else else
htlc_budget = chans[i]->max_accepted_htlcs - chans[i]->num_htlcs; htlc_budget = chans[i]->max_accepted_htlcs - chans[i]->num_htlcs;
if(!amount_msat_to_sat(&capacity, chans[i]->total_msat)) capacity = chans[i]->total_msat;
abort();
/* If we have both a scid and a local alias we want to /* If we have both a scid and a local alias we want to
* use the scid, and mark the alias as * use the scid, and mark the alias as
@ -3096,7 +3095,7 @@ static void routehint_step_cb(struct routehints_data *d, struct payment *p)
hop.amount = dest_amount; hop.amount = dest_amount;
hop.delay = route_cltv(d->final_cltv, routehint + i + 1, hop.delay = route_cltv(d->final_cltv, routehint + i + 1,
tal_count(routehint) - i - 1); tal_count(routehint) - i - 1);
hop.capacity = amount_msat_to_sat_round_down(estimate); hop.capacity = estimate;
/* Should we get a failure inside the routehint we'll /* Should we get a failure inside the routehint we'll
* need the direction so we can exclude it. Luckily * need the direction so we can exclude it. Luckily
@ -3866,7 +3865,7 @@ static void route_exclusions_step_cb(struct route_exclusions_data *d,
struct route_exclusion *e = exclusions[i]; struct route_exclusion *e = exclusions[i];
/* We don't need the details if we skip anyway. */ /* We don't need the details if we skip anyway. */
struct amount_sat total = AMOUNT_SAT(0); struct amount_msat total = AMOUNT_MSAT(0);
if (e->type == EXCLUDE_CHANNEL) { if (e->type == EXCLUDE_CHANNEL) {
channel_hints_update(p, e->u.chan_id.scid, channel_hints_update(p, e->u.chan_id.scid,

View File

@ -1487,7 +1487,7 @@ static struct command_result *handle_channel_hint_update(struct command *cmd,
".estimate = %s, .capacity = %s }", ".estimate = %s, .capacity = %s }",
fmt_short_channel_id_dir(tmpctx, &hint->scid), hint->enabled, fmt_short_channel_id_dir(tmpctx, &hint->scid), hint->enabled,
fmt_amount_msat(tmpctx, hint->estimated_capacity), fmt_amount_msat(tmpctx, hint->estimated_capacity),
fmt_amount_sat(tmpctx, hint->capacity) fmt_amount_msat(tmpctx, hint->capacity)
); );
channel_hint_set_add(global_hints, time_now().ts.tv_sec, &hint->scid, channel_hint_set_add(global_hints, time_now().ts.tv_sec, &hint->scid,
hint->enabled, &hint->estimated_capacity, hint->enabled, &hint->estimated_capacity,