Skip to content

Commit 3772a02

Browse files
cdeckerrustyrussell
authored andcommitted
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> Header from folded patch 'fixup!_pay__switch_to_msat_for_total_capacity.patch': fixup! pay: Switch to msat for total_capacity Signed-off-by: Rusty Russell <[email protected]>
1 parent 3181bb6 commit 3772a02

File tree

8 files changed

+22
-30
lines changed

8 files changed

+22
-30
lines changed

common/route.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ 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+
if (!gossmap_chan_get_capacity(gossmap, c, &total)
109+
|| !amount_sat_to_msat(&(*hops)[num_hops].capacity, total))
110+
return false;
110111
(*hops)[num_hops].amount = *amount;
111112
(*hops)[num_hops].delay = *cltv;
112113

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);
@@ -2557,7 +2557,7 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer,
25572557
const jsmntok_t *toks, struct payment *p)
25582558
{
25592559
struct listpeers_channel **chans;
2560-
struct amount_sat capacity;
2560+
struct amount_msat capacity;
25612561

25622562
chans = json_to_listpeers_channels(tmpctx, buffer, toks);
25632563

@@ -2582,8 +2582,7 @@ local_channel_hints_listpeerchannels(struct command *cmd, const char *buffer,
25822582
else
25832583
htlc_budget = chans[i]->max_accepted_htlcs - chans[i]->num_htlcs;
25842584

2585-
if(!amount_msat_to_sat(&capacity, chans[i]->total_msat))
2586-
abort();
2585+
capacity = chans[i]->total_msat;
25872586

25882587
/* If we have both a scid and a local alias we want to
25892588
* use the scid, and mark the alias as
@@ -3100,7 +3099,7 @@ static void routehint_step_cb(struct routehints_data *d, struct payment *p)
31003099
hop.amount = dest_amount;
31013100
hop.delay = route_cltv(d->final_cltv, routehint + i + 1,
31023101
tal_count(routehint) - i - 1);
3103-
hop.capacity = amount_msat_to_sat_round_down(estimate);
3102+
hop.capacity = estimate;
31043103

31053104
/* Should we get a failure inside the routehint we'll
31063105
* need the direction so we can exclude it. Luckily
@@ -3870,7 +3869,7 @@ static void route_exclusions_step_cb(struct route_exclusions_data *d,
38703869
struct route_exclusion *e = exclusions[i];
38713870

38723871
/* We don't need the details if we skip anyway. */
3873-
struct amount_sat total = AMOUNT_SAT(0);
3872+
struct amount_msat total = AMOUNT_MSAT(0);
38743873

38753874
if (e->type == EXCLUDE_CHANNEL) {
38763875
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
@@ -1523,7 +1523,7 @@ static struct command_result *handle_channel_hint_update(struct command *cmd,
15231523
".estimate = %s, .capacity = %s }",
15241524
fmt_short_channel_id_dir(tmpctx, &hint->scid), hint->enabled,
15251525
fmt_amount_msat(tmpctx, hint->estimated_capacity),
1526-
fmt_amount_sat(tmpctx, hint->capacity)
1526+
fmt_amount_msat(tmpctx, hint->capacity)
15271527
);
15281528
channel_hint_set_add(global_hints, time_now().ts.tv_sec, &hint->scid,
15291529
hint->enabled, &hint->estimated_capacity,

plugins/test/run-route-calc.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ void json_add_amount_msat(struct json_stream *result UNNEEDED,
8282
struct amount_msat msat)
8383

8484
{ fprintf(stderr, "json_add_amount_msat called!\n"); abort(); }
85-
/* Generated stub for json_add_amount_sat */
86-
void json_add_amount_sat(struct json_stream *result UNNEEDED,
87-
const char *satfieldname UNNEEDED,
88-
struct amount_sat sat)
89-
90-
{ fprintf(stderr, "json_add_amount_sat called!\n"); abort(); }
9185
/* Generated stub for json_add_bool */
9286
void json_add_bool(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED,
9387
bool value UNNEEDED)

wallet/test/run-wallet.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ void topology_add_sync_waiter_(const tal_t *ctx UNNEEDED,
10021002
u8 *towire_announcement_signatures(const tal_t *ctx UNNEEDED, const struct channel_id *channel_id UNNEEDED, struct short_channel_id short_channel_id UNNEEDED, const secp256k1_ecdsa_signature *node_signature UNNEEDED, const secp256k1_ecdsa_signature *bitcoin_signature UNNEEDED)
10031003
{ fprintf(stderr, "towire_announcement_signatures called!\n"); abort(); }
10041004
/* Generated stub for towire_channel_reestablish */
1005-
u8 *towire_channel_reestablish(const tal_t *ctx UNNEEDED, const struct channel_id *channel_id UNNEEDED, u64 next_commitment_number UNNEEDED, u64 next_revocation_number UNNEEDED, const struct secret *your_last_per_commitment_secret UNNEEDED, const struct pubkey *my_current_per_commitment_point UNNEEDED, const struct tlv_channel_reestablish_tlvs *tlvs UNNEEDED)
1005+
u8 *towire_channel_reestablish(const tal_t *ctx UNNEEDED, const struct channel_id *channel_id UNNEEDED, u64 next_commitment_number UNNEEDED, u64 next_revocation_number UNNEEDED, const struct secret *your_last_per_commitment_secret UNNEEDED, const struct pubkey *my_current_per_commitment_point UNNEEDED, const struct tlv_channel_reestablish_tlvs *channel_reestablish UNNEEDED)
10061006
{ fprintf(stderr, "towire_channel_reestablish called!\n"); abort(); }
10071007
/* Generated stub for towire_channeld_dev_memleak */
10081008
u8 *towire_channeld_dev_memleak(const tal_t *ctx UNNEEDED)

0 commit comments

Comments
 (0)