From a6a7dd8f711c81b18768579ec3024abf8dab1499 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 3 Oct 2024 18:02:14 +0200 Subject: [PATCH] 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> --- common/route.c | 6 +++--- common/route.h | 2 +- plugins/channel_hint.c | 14 ++++++-------- plugins/channel_hint.h | 10 +++++----- plugins/libplugin-pay.c | 11 +++++------ plugins/pay.c | 2 +- 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/common/route.c b/common/route.c index 1c732e85bf77..b699742abe90 100644 --- a/common/route.c +++ b/common/route.c @@ -81,7 +81,7 @@ static bool dijkstra_to_hops(struct route_hop **hops, const struct gossmap_node *next; size_t num_hops = tal_count(*hops); const struct half_chan *h; - struct amount_sat total; + struct amount_msat total_msat; if (dist == 0) 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)) return false; - gossmap_chan_get_capacity(gossmap, c, &total); - (*hops)[num_hops].capacity = total; + total_msat = gossmap_chan_get_capacity(gossmap, c); + (*hops)[num_hops].capacity = total_msat; (*hops)[num_hops].amount = *amount; (*hops)[num_hops].delay = *cltv; diff --git a/common/route.h b/common/route.h index ce57cd875339..716065d557d5 100644 --- a/common/route.h +++ b/common/route.h @@ -26,7 +26,7 @@ struct route_hop { int direction; struct node_id node_id; struct amount_msat amount; - struct amount_sat capacity; + struct amount_msat capacity; u32 delay; }; diff --git a/plugins/channel_hint.c b/plugins/channel_hint.c index 7a7cd3eaceb9..4dc065410b20 100644 --- a/plugins/channel_hint.c +++ b/plugins/channel_hint.c @@ -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_amount_msat(dest, "estimated_capacity_msat", 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_object_end(dest); } @@ -55,9 +55,7 @@ bool channel_hint_update(const struct timeabs now, struct channel_hint *hint) * overall / refill_rate`. */ struct amount_msat refill; - struct amount_msat capacity; - if (!amount_sat_to_msat(&capacity, hint->capacity)) - abort(); + struct amount_msat capacity = hint->capacity; if (now.ts.tv_sec < hint->timestamp + PAY_REFILL_HYSTERESIS) return true; @@ -109,7 +107,7 @@ 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, - const struct amount_sat capacity, u16 *htlc_budget) + const struct amount_msat capacity, u16 *htlc_budget) { 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 * routehints, we're not actually being told the total * capacity, just lower values. */ - if (amount_sat_greater(capacity, old->capacity)) + if (amount_msat_greater(capacity, old->capacity)) old->capacity = capacity; 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); 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_short_channel_id_dir, &hint->scid), 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)); if (ret != NULL) diff --git a/plugins/channel_hint.h b/plugins/channel_hint.h index d53cc10d58d7..c7cc9cca8474 100644 --- a/plugins/channel_hint.h +++ b/plugins/channel_hint.h @@ -39,10 +39,10 @@ struct channel_hint { /* Non-null if we are one endpoint of this channel */ struct local_hint *local; - /* The total `amount_sat` that were used to fund the - * channel. This is always smaller gte the - * estimated_capacity (after normalization) */ - struct amount_sat capacity; + /* The total `amount_msat` that were used to fund the + * channel. This is always smaller gte the estimated_capacity + * (after normalization) */ + struct amount_msat capacity; }; /* 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, bool enabled, const struct amount_msat *estimated_capacity, - const struct amount_sat overall_capacity, + const struct amount_msat overall_capacity, u16 *htlc_budget); /** diff --git a/plugins/libplugin-pay.c b/plugins/libplugin-pay.c index ccf2009b2b1c..2e406337f760 100644 --- a/plugins/libplugin-pay.c +++ b/plugins/libplugin-pay.c @@ -411,7 +411,7 @@ static void channel_hints_update(struct payment *p, const struct short_channel_id scid, int direction, bool enabled, bool local, const struct amount_msat *estimated_capacity, - const struct amount_sat overall_capacity, + const struct amount_msat overall_capacity, u16 *htlc_budget) { 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) { struct listpeers_channel **chans; - struct amount_sat capacity; + struct amount_msat capacity; chans = json_to_listpeers_channels(tmpctx, buffer, toks); @@ -2578,8 +2578,7 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer, else htlc_budget = chans[i]->max_accepted_htlcs - chans[i]->num_htlcs; - if(!amount_msat_to_sat(&capacity, chans[i]->total_msat)) - abort(); + capacity = chans[i]->total_msat; /* If we have both a scid and a local alias we want to * 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.delay = route_cltv(d->final_cltv, 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 * 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]; /* 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) { channel_hints_update(p, e->u.chan_id.scid, diff --git a/plugins/pay.c b/plugins/pay.c index 8907fbdc3028..967d97123fb0 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -1487,7 +1487,7 @@ static struct command_result *handle_channel_hint_update(struct command *cmd, ".estimate = %s, .capacity = %s }", fmt_short_channel_id_dir(tmpctx, &hint->scid), hint->enabled, 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, hint->enabled, &hint->estimated_capacity,