Skip to content

Commit

Permalink
pay: Switch to msat for total_capacity
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cdecker committed Oct 7, 2024
1 parent ddc199f commit a6a7dd8
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 24 deletions.
6 changes: 3 additions & 3 deletions common/route.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion common/route.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
14 changes: 6 additions & 8 deletions plugins/channel_hint.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions plugins/channel_hint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

/**
Expand Down
11 changes: 5 additions & 6 deletions plugins/libplugin-pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion plugins/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a6a7dd8

Please sign in to comment.