Skip to content

Commit a6a7dd8

Browse files
committed
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>
1 parent ddc199f commit a6a7dd8

File tree

6 files changed

+21
-24
lines changed

6 files changed

+21
-24
lines changed

common/route.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static bool dijkstra_to_hops(struct route_hop **hops,
8181
const struct gossmap_node *next;
8282
size_t num_hops = tal_count(*hops);
8383
const struct half_chan *h;
84-
struct amount_sat total;
84+
struct amount_msat total_msat;
8585

8686
if (dist == 0)
8787
return true;
@@ -105,8 +105,8 @@ static bool dijkstra_to_hops(struct route_hop **hops,
105105
if (!dijkstra_to_hops(hops, gossmap, dij, next, amount, cltv))
106106
return false;
107107

108-
gossmap_chan_get_capacity(gossmap, c, &total);
109-
(*hops)[num_hops].capacity = total;
108+
total_msat = gossmap_chan_get_capacity(gossmap, c);
109+
(*hops)[num_hops].capacity = total_msat;
110110
(*hops)[num_hops].amount = *amount;
111111
(*hops)[num_hops].delay = *cltv;
112112

common/route.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct route_hop {
2626
int direction;
2727
struct node_id node_id;
2828
struct amount_msat amount;
29-
struct amount_sat capacity;
29+
struct amount_msat capacity;
3030
u32 delay;
3131
};
3232

plugins/channel_hint.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void channel_hint_to_json(const char *name, const struct channel_hint *hint,
99
json_add_short_channel_id_dir(dest, "scid", hint->scid);
1010
json_add_amount_msat(dest, "estimated_capacity_msat",
1111
hint->estimated_capacity);
12-
json_add_amount_sat(dest, "capacity_sat", hint->capacity);
12+
json_add_amount_msat(dest, "total_capacity_msat", hint->capacity);
1313
json_add_bool(dest, "enabled", hint->enabled);
1414
json_object_end(dest);
1515
}
@@ -55,9 +55,7 @@ bool channel_hint_update(const struct timeabs now, struct channel_hint *hint)
5555
* overall / refill_rate`.
5656
*/
5757
struct amount_msat refill;
58-
struct amount_msat capacity;
59-
if (!amount_sat_to_msat(&capacity, hint->capacity))
60-
abort();
58+
struct amount_msat capacity = hint->capacity;
6159

6260
if (now.ts.tv_sec < hint->timestamp + PAY_REFILL_HYSTERESIS)
6361
return true;
@@ -109,7 +107,7 @@ struct channel_hint *
109107
channel_hint_set_add(struct channel_hint_set *self, u32 timestamp,
110108
const struct short_channel_id_dir *scidd, bool enabled,
111109
const struct amount_msat *estimated_capacity,
112-
const struct amount_sat capacity, u16 *htlc_budget)
110+
const struct amount_msat capacity, u16 *htlc_budget)
113111
{
114112
struct channel_hint *copy, *old, *newhint;
115113

@@ -143,7 +141,7 @@ channel_hint_set_add(struct channel_hint_set *self, u32 timestamp,
143141
* being told. This is because in some cases, such as
144142
* routehints, we're not actually being told the total
145143
* capacity, just lower values. */
146-
if (amount_sat_greater(capacity, old->capacity))
144+
if (amount_msat_greater(capacity, old->capacity))
147145
old->capacity = capacity;
148146

149147
return copy;
@@ -169,11 +167,11 @@ struct channel_hint *channel_hint_from_json(const tal_t *ctx,
169167
struct channel_hint *hint = tal(ctx, struct channel_hint);
170168

171169
ret = json_scan(ctx, buffer, jhint,
172-
"{timestamp:%,scid:%,estimated_capacity_msat:%,capacity_sat:%,enabled:%}",
170+
"{timestamp:%,scid:%,estimated_capacity_msat:%,total_capacity_msat:%,enabled:%}",
173171
JSON_SCAN(json_to_u32, &hint->timestamp),
174172
JSON_SCAN(json_to_short_channel_id_dir, &hint->scid),
175173
JSON_SCAN(json_to_msat, &hint->estimated_capacity),
176-
JSON_SCAN(json_to_sat, &hint->capacity),
174+
JSON_SCAN(json_to_msat, &hint->capacity),
177175
JSON_SCAN(json_to_bool, &hint->enabled));
178176

179177
if (ret != NULL)

plugins/channel_hint.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ struct channel_hint {
3939
/* Non-null if we are one endpoint of this channel */
4040
struct local_hint *local;
4141

42-
/* The total `amount_sat` that were used to fund the
43-
* channel. This is always smaller gte the
44-
* estimated_capacity (after normalization) */
45-
struct amount_sat capacity;
42+
/* The total `amount_msat` that were used to fund the
43+
* channel. This is always smaller gte the estimated_capacity
44+
* (after normalization) */
45+
struct amount_msat capacity;
4646
};
4747

4848
/* 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,
8484
const struct short_channel_id_dir *scidd,
8585
bool enabled,
8686
const struct amount_msat *estimated_capacity,
87-
const struct amount_sat overall_capacity,
87+
const struct amount_msat overall_capacity,
8888
u16 *htlc_budget);
8989

9090
/**

plugins/libplugin-pay.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ static void channel_hints_update(struct payment *p,
411411
const struct short_channel_id scid,
412412
int direction, bool enabled, bool local,
413413
const struct amount_msat *estimated_capacity,
414-
const struct amount_sat overall_capacity,
414+
const struct amount_msat overall_capacity,
415415
u16 *htlc_budget)
416416
{
417417
struct payment *root = payment_root(p);
@@ -2553,7 +2553,7 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer,
25532553
const jsmntok_t *toks, struct payment *p)
25542554
{
25552555
struct listpeers_channel **chans;
2556-
struct amount_sat capacity;
2556+
struct amount_msat capacity;
25572557

25582558
chans = json_to_listpeers_channels(tmpctx, buffer, toks);
25592559

@@ -2578,8 +2578,7 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer,
25782578
else
25792579
htlc_budget = chans[i]->max_accepted_htlcs - chans[i]->num_htlcs;
25802580

2581-
if(!amount_msat_to_sat(&capacity, chans[i]->total_msat))
2582-
abort();
2581+
capacity = chans[i]->total_msat;
25832582

25842583
/* If we have both a scid and a local alias we want to
25852584
* use the scid, and mark the alias as
@@ -3096,7 +3095,7 @@ static void routehint_step_cb(struct routehints_data *d, struct payment *p)
30963095
hop.amount = dest_amount;
30973096
hop.delay = route_cltv(d->final_cltv, routehint + i + 1,
30983097
tal_count(routehint) - i - 1);
3099-
hop.capacity = amount_msat_to_sat_round_down(estimate);
3098+
hop.capacity = estimate;
31003099

31013100
/* Should we get a failure inside the routehint we'll
31023101
* need the direction so we can exclude it. Luckily
@@ -3866,7 +3865,7 @@ static void route_exclusions_step_cb(struct route_exclusions_data *d,
38663865
struct route_exclusion *e = exclusions[i];
38673866

38683867
/* We don't need the details if we skip anyway. */
3869-
struct amount_sat total = AMOUNT_SAT(0);
3868+
struct amount_msat total = AMOUNT_MSAT(0);
38703869

38713870
if (e->type == EXCLUDE_CHANNEL) {
38723871
channel_hints_update(p, e->u.chan_id.scid,

plugins/pay.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ static struct command_result *handle_channel_hint_update(struct command *cmd,
14871487
".estimate = %s, .capacity = %s }",
14881488
fmt_short_channel_id_dir(tmpctx, &hint->scid), hint->enabled,
14891489
fmt_amount_msat(tmpctx, hint->estimated_capacity),
1490-
fmt_amount_sat(tmpctx, hint->capacity)
1490+
fmt_amount_msat(tmpctx, hint->capacity)
14911491
);
14921492
channel_hint_set_add(global_hints, time_now().ts.tv_sec, &hint->scid,
14931493
hint->enabled, &hint->estimated_capacity,

0 commit comments

Comments
 (0)