Skip to content

Commit fdf6d03

Browse files
Lagrang3rustyrussell
Lagrang3
authored andcommitted
renepay: more cleanups
- adopt "const <type> *"convention - remove use_shadow option for some pyln tests - show prob. information of flows into paynotes - show prob. of success of entire payment flow in paynotes - minflow: We were not releasing the memory of flow arrays when replacing them with a new canditate. - use memleak_scan_obj in memleak_check - replace u64 with size_t Signed-off-by: Lagrang3 <[email protected]>
1 parent 2893e85 commit fdf6d03

File tree

11 files changed

+62
-60
lines changed

11 files changed

+62
-60
lines changed

plugins/renepay/flow.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
1919
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
2020

21-
bool chan_extra_is_busy(struct chan_extra const * const ce)
21+
bool chan_extra_is_busy(const struct chan_extra *const ce)
2222
{
2323
if(ce==NULL)return false;
2424
return ce->half[0].num_htlcs || ce->half[1].num_htlcs;
@@ -624,7 +624,7 @@ struct chan_inflight_flow
624624
// TODO(eduardo): here flows should be const
625625
double flow_set_probability(
626626
struct flow ** flows,
627-
struct gossmap const*const gossmap,
627+
const struct gossmap *const gossmap,
628628
struct chan_extra_map * chan_extra_map)
629629
{
630630
tal_t *this_ctx = tal(tmpctx,tal_t);

plugins/renepay/flow.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct chan_extra {
4040
} half[2];
4141
};
4242

43-
bool chan_extra_is_busy(struct chan_extra const * const ce);
43+
bool chan_extra_is_busy(const struct chan_extra *const ce);
4444

4545
static inline const struct short_channel_id
4646
chan_extra_scid(const struct chan_extra *cd)
@@ -205,7 +205,7 @@ struct chan_extra_half *get_chan_extra_half_by_chan(const struct gossmap *gossma
205205

206206
/* An actual partial flow. */
207207
struct flow {
208-
struct gossmap_chan const **path;
208+
const struct gossmap_chan **path;
209209
/* The directions to traverse. */
210210
int *dirs;
211211
/* Amounts for this flow (fees mean this shrinks across path). */
@@ -241,7 +241,7 @@ void flow_complete(struct flow *flow,
241241
/* Compute the prob. of success of a set of concurrent set of flows. */
242242
double flow_set_probability(
243243
struct flow ** flows,
244-
struct gossmap const*const gossmap,
244+
const struct gossmap *const gossmap,
245245
struct chan_extra_map * chan_extra_map);
246246

247247
// TODO(eduardo): we probably don't need this. Instead we should have payflow

plugins/renepay/mcf.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ typedef union
273273
struct pay_parameters {
274274
/* The gossmap we are using */
275275
struct gossmap *gossmap;
276-
struct gossmap_node const*source;
277-
struct gossmap_node const*target;
276+
const struct gossmap_node *source;
277+
const struct gossmap_node *target;
278278

279279
/* Extra information we intuited about the channels */
280280
struct chan_extra_map *chan_extra_map;
@@ -865,7 +865,7 @@ static int find_optimal_path(
865865
for(size_t i=0;i<tal_count(prev);++i)
866866
prev[i].idx=INVALID_INDEX;
867867

868-
s64 const * const distance=dijkstra_distance_data(dijkstra);
868+
const s64 *const distance=dijkstra_distance_data(dijkstra);
869869

870870
dijkstra_init(dijkstra);
871871
dijkstra_update(dijkstra,source,0);
@@ -961,7 +961,7 @@ static int optimize_mcf(
961961
zero_flow(linear_network,residual_network);
962962
arc_t *prev = tal_arr(this_ctx,arc_t,linear_network->max_num_nodes);
963963

964-
s64 const*const distance = dijkstra_distance_data(dijkstra);
964+
const s64 *const distance = dijkstra_distance_data(dijkstra);
965965

966966
s64 remaining_amount = amount;
967967

@@ -1021,7 +1021,7 @@ static u32 find_positive_balance(
10211021
const u32 start_idx,
10221022
const s64 *balance,
10231023

1024-
struct gossmap_chan const** prev_chan,
1024+
const struct gossmap_chan **prev_chan,
10251025
int *prev_dir,
10261026
u32 *prev_idx)
10271027
{
@@ -1045,7 +1045,7 @@ static u32 find_positive_balance(
10451045
for(size_t i=0;i<cur->num_chans;++i)
10461046
{
10471047
int dir;
1048-
struct gossmap_chan const *c
1048+
const struct gossmap_chan *c
10491049
= gossmap_nth_chan(gossmap,
10501050
cur,i,&dir);
10511051

@@ -1112,8 +1112,8 @@ static struct flow **
11121112
const size_t max_num_nodes = gossmap_max_node_idx(gossmap);
11131113
s64 *balance = tal_arrz(this_ctx,s64,max_num_nodes);
11141114

1115-
struct gossmap_chan const **prev_chan
1116-
= tal_arr(this_ctx,struct gossmap_chan const*,max_num_nodes);
1115+
const struct gossmap_chan **prev_chan
1116+
= tal_arr(this_ctx,const struct gossmap_chan *,max_num_nodes);
11171117

11181118
int *prev_dir = tal_arr(this_ctx,int,max_num_nodes);
11191119
u32 *prev_idx = tal_arr(this_ctx,u32,max_num_nodes);
@@ -1171,7 +1171,7 @@ static struct flow **
11711171
assert(cur_idx!=INVALID_INDEX);
11721172

11731173
const int dir = prev_dir[cur_idx];
1174-
struct gossmap_chan const * const c = prev_chan[cur_idx];
1174+
const struct gossmap_chan *const c = prev_chan[cur_idx];
11751175
const u32 c_idx = gossmap_chan_idx(gossmap,c);
11761176

11771177
delta=MIN(delta,chan_flow[c_idx].half[dir]);
@@ -1186,7 +1186,7 @@ static struct flow **
11861186

11871187

11881188
struct flow *fp = tal(this_ctx,struct flow);
1189-
fp->path = tal_arr(fp,struct gossmap_chan const*,length);
1189+
fp->path = tal_arr(fp,const struct gossmap_chan *,length);
11901190
fp->dirs = tal_arr(fp,int,length);
11911191

11921192
balance[node_idx] += delta;
@@ -1200,7 +1200,7 @@ static struct flow **
12001200
assert(cur_idx!=INVALID_INDEX);
12011201

12021202
const int dir = prev_dir[cur_idx];
1203-
struct gossmap_chan const * const c = prev_chan[cur_idx];
1203+
const struct gossmap_chan *const c = prev_chan[cur_idx];
12041204
const u32 c_idx = gossmap_chan_idx(gossmap,c);
12051205

12061206
length--;
@@ -1475,17 +1475,23 @@ struct flow** minflow(
14751475
params->chan_extra_map);
14761476
struct amount_msat fee = flow_set_fee(flow_paths);
14771477

1478-
// is this better than the previous one?
1478+
/* Is this better than the previous one? */
14791479
if(!best_flow_paths ||
14801480
is_better(params->max_fee,params->min_probability,
14811481
fee,prob_success,
14821482
best_fee, best_prob_success))
14831483
{
1484+
struct flow **tmp = best_flow_paths;
14841485
best_flow_paths = tal_steal(ctx,flow_paths);
1486+
tal_free(tmp);
1487+
14851488
best_fee = fee;
14861489
best_prob_success=prob_success;
14871490
flow_paths = NULL;
14881491
}
1492+
/* I don't like this candidate. */
1493+
else
1494+
tal_free(flow_paths);
14891495

14901496
if(amount_msat_greater(fee,params->max_fee))
14911497
{
@@ -1502,9 +1508,6 @@ struct flow** minflow(
15021508
// the fees
15031509
mu_left = mu+1;
15041510
}
1505-
1506-
if(flow_paths)
1507-
tal_free(flow_paths);
15081511
}
15091512

15101513

plugins/renepay/pay.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,10 @@ void amount_msat_reduce_(struct amount_msat *dst,
6565
#if DEVELOPER
6666
static void memleak_mark(struct plugin *p, struct htable *memtable)
6767
{
68-
/* TODO(eduardo): understand the purpose of memleak_scan_obj, why use it
69-
* instead of tal_free?
70-
* 1st problem: this is executed before the plugin can process the
71-
* shutdown notification,
72-
* 2nd problem: memleak_scan_obj does not propagate to children.
73-
* For the moment let's just (incorrectly) do tal_free here
74-
* */
75-
pay_plugin->ctx = tal_free(pay_plugin->ctx);
76-
77-
// memleak_scan_obj(memtable, pay_plugin->ctx);
78-
// memleak_scan_obj(memtable, pay_plugin->gossmap);
79-
// memleak_scan_obj(memtable, pay_plugin->chan_extra_map);
80-
// memleak_scan_htable(memtable, &pay_plugin->chan_extra_map->raw);
68+
memleak_scan_obj(memtable, pay_plugin->ctx);
69+
memleak_scan_obj(memtable, pay_plugin->gossmap);
70+
memleak_scan_obj(memtable, pay_plugin->chan_extra_map);
71+
memleak_scan_htable(memtable, &pay_plugin->chan_extra_map->raw);
8172
}
8273
#endif
8374

@@ -411,12 +402,13 @@ sendpay_flows(struct command *cmd,
411402
debug_paynote(p, "Sending out batch of %zu payments", tal_count(flows));
412403

413404
for (size_t i = 0; i < tal_count(flows); i++) {
414-
const u64 path_lengh = tal_count(flows[i]->amounts);
415-
debug_paynote(p, "sendpay flow groupid=%ld, partid=%ld, delivering=%s",
405+
const size_t path_lengh = tal_count(flows[i]->amounts);
406+
debug_paynote(p, "sendpay flow groupid=%ld, partid=%ld, delivering=%s, probability=%.3lf",
416407
flows[i]->key.groupid,
417408
flows[i]->key.partid,
418409
type_to_string(tmpctx,struct amount_msat,
419-
&flows[i]->amounts[path_lengh-1]));
410+
&flows[i]->amounts[path_lengh-1]),
411+
flows[i]->success_prob);
420412
struct out_req *req;
421413
req = jsonrpc_request_start(cmd->plugin, cmd, "sendpay",
422414
flow_sent, flow_sendpay_failed,
@@ -560,7 +552,7 @@ static struct command_result *try_paying(struct command *cmd,
560552

561553
// plugin_log(pay_plugin->plugin,LOG_DBG,fmt_chan_extra_map(tmpctx,pay_plugin->chan_extra_map));
562554

563-
char const * err_msg;
555+
const char *err_msg;
564556

565557
/* We let this return an unlikely path, as it's better to try once
566558
* than simply refuse. Plus, models are not truth! */

plugins/renepay/pay_flow.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ struct pay_flow **get_payflows(struct renepay * renepay,
341341
struct amount_msat feebudget,
342342
bool unlikely_ok,
343343
bool is_entire_payment,
344-
char const ** err_msg)
344+
const char **err_msg)
345345
{
346346
*err_msg = tal_fmt(tmpctx,"[no error]");
347347

@@ -396,6 +396,13 @@ struct pay_flow **get_payflows(struct renepay * renepay,
396396
fee = flow_set_fee(flows);
397397
delay = flows_worst_delay(flows) + p->final_cltv;
398398

399+
debug_paynote(p,
400+
"we have computed a set of %ld flows with probability %.3lf, fees %s and delay %ld",
401+
tal_count(flows),
402+
prob,
403+
type_to_string(tmpctx,struct amount_msat,&fee),
404+
delay);
405+
399406
too_unlikely = (prob < p->min_prob_success);
400407
if (too_unlikely && !unlikely_ok)
401408
{

plugins/renepay/pay_flow.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static inline size_t payflow_key_hash(const struct payflow_key k)
6969
return k.payment_hash->u.u32[0] ^ (k.groupid << 32) ^ k.partid;
7070
}
7171

72-
static inline bool payflow_key_equal(struct pay_flow const *pf,
72+
static inline bool payflow_key_equal(const struct pay_flow *pf,
7373
const struct payflow_key k)
7474
{
7575
return pf->key.partid==k.partid && pf->key.groupid==k.groupid
@@ -86,7 +86,7 @@ struct pay_flow **get_payflows(struct renepay * renepay,
8686
struct amount_msat feebudget,
8787
bool unlikely_ok,
8888
bool is_entire_payment,
89-
char const ** err_msg);
89+
const char **err_msg);
9090

9191
void commit_htlc_payflow(
9292
struct chan_extra_map *chan_extra_map,

plugins/renepay/payment.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,20 @@ void payment_success(struct payment * p)
7373
p->status=PAYMENT_SUCCESS;
7474
}
7575

76-
struct amount_msat payment_sent(struct payment const * p)
76+
struct amount_msat payment_sent(const struct payment *p)
7777
{
7878
return p->total_sent;
7979
}
80-
struct amount_msat payment_delivered(struct payment const * p)
80+
struct amount_msat payment_delivered(const struct payment *p)
8181
{
8282
return p->total_delivering;
8383
}
84-
struct amount_msat payment_amount(struct payment const * p)
84+
struct amount_msat payment_amount(const struct payment *p)
8585
{
8686
return p->amount;
8787
}
8888

89-
struct amount_msat payment_fees(struct payment const*p)
89+
struct amount_msat payment_fees(const struct payment *p)
9090
{
9191
struct amount_msat fees;
9292
struct amount_msat sent = payment_sent(p),
@@ -111,7 +111,7 @@ void payment_note(struct payment *p, const char *fmt, ...)
111111
debug_info("%s",str);
112112
}
113113

114-
void payment_assert_delivering_incomplete(struct payment const * p)
114+
void payment_assert_delivering_incomplete(const struct payment *p)
115115
{
116116
if(!amount_msat_less(p->total_delivering, p->amount))
117117
{
@@ -121,7 +121,7 @@ void payment_assert_delivering_incomplete(struct payment const * p)
121121
type_to_string(tmpctx,struct amount_msat,&p->amount));
122122
}
123123
}
124-
void payment_assert_delivering_all(struct payment const * p)
124+
void payment_assert_delivering_all(const struct payment *p)
125125
{
126126
if(amount_msat_less(p->total_delivering, p->amount))
127127
{
@@ -194,7 +194,7 @@ struct command_result *renepay_fail(
194194
return command_fail(renepay->cmd,code,"%s",message);
195195
}
196196

197-
u64 renepay_parts(struct renepay const * renepay)
197+
u64 renepay_parts(const struct renepay *renepay)
198198
{
199199
return renepay->next_partid-1;
200200
}

plugins/renepay/payment.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,14 @@ void renepay_cleanup(
137137

138138
void payment_fail(struct payment * p);
139139
void payment_success(struct payment * p);
140-
struct amount_msat payment_sent(struct payment const * p);
141-
struct amount_msat payment_delivered(struct payment const * p);
142-
struct amount_msat payment_amount(struct payment const * p);
143-
struct amount_msat payment_fees(struct payment const*p);
140+
struct amount_msat payment_sent(const struct payment *p);
141+
struct amount_msat payment_delivered(const struct payment *p);
142+
struct amount_msat payment_amount(const struct payment *p);
143+
struct amount_msat payment_fees(const struct payment *p);
144144

145145
void payment_note(struct payment *p, const char *fmt, ...);
146-
void payment_assert_delivering_incomplete(struct payment const * p);
147-
void payment_assert_delivering_all(struct payment const * p);
146+
void payment_assert_delivering_incomplete(const struct payment *p);
147+
void payment_assert_delivering_all(const struct payment *p);
148148

149149

150150
int renepay_current_attempt(const struct renepay *renepay);
@@ -158,6 +158,6 @@ struct command_result *renepay_fail(
158158
enum jsonrpc_errcode code,
159159
const char *fmt, ...);
160160

161-
u64 renepay_parts(struct renepay const * renepay);
161+
u64 renepay_parts(const struct renepay *renepay);
162162

163163
#endif /* LIGHTNING_PLUGINS_RENEPAY_PAYMENT_H */

plugins/renepay/test/run-testflow.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ static void test_flow_complete(void)
630630
struct amount_msat deliver;
631631

632632
// flow 1->2
633-
F->path = tal_arr(F,struct gossmap_chan const *,1);
633+
F->path = tal_arr(F,const struct gossmap_chan *,1);
634634
F->dirs = tal_arr(F,int,1);
635635
F->path[0]=gossmap_find_chan(gossmap,&scid12);
636636
F->dirs[0]=0;
@@ -640,7 +640,7 @@ static void test_flow_complete(void)
640640
assert(fabs(F->success_prob - 0.5)<eps);
641641

642642
// flow 3->4->5
643-
F->path = tal_arr(F,struct gossmap_chan const *,2);
643+
F->path = tal_arr(F,const struct gossmap_chan *,2);
644644
F->dirs = tal_arr(F,int,2);
645645
F->path[0]=gossmap_find_chan(gossmap,&scid34);
646646
F->path[1]=gossmap_find_chan(gossmap,&scid45);
@@ -652,7 +652,7 @@ static void test_flow_complete(void)
652652
assert(fabs(F->success_prob - 1.)<eps);
653653

654654
// flow 2->3->4->5
655-
F->path = tal_arr(F,struct gossmap_chan const *,3);
655+
F->path = tal_arr(F,const struct gossmap_chan *,3);
656656
F->dirs = tal_arr(F,int,3);
657657
F->path[0]=gossmap_find_chan(gossmap,&scid23);
658658
F->path[1]=gossmap_find_chan(gossmap,&scid34);
@@ -666,7 +666,7 @@ static void test_flow_complete(void)
666666
assert(fabs(F->success_prob - 1. + 250.087534/2000)<eps);
667667

668668
// flow 1->2->3->4->5
669-
F->path = tal_arr(F,struct gossmap_chan const *,4);
669+
F->path = tal_arr(F,const struct gossmap_chan *,4);
670670
F->dirs = tal_arr(F,int,4);
671671
F->path[0]=gossmap_find_chan(gossmap,&scid12);
672672
F->path[1]=gossmap_find_chan(gossmap,&scid23);

plugins/renepay/uncertainty_network.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void uncertainty_network_add_routehints(
8686
struct chan_extra_map *chan_extra_map,
8787
struct renepay *renepay)
8888
{
89-
struct payment const * const p = renepay->payment;
89+
const struct payment *const p = renepay->payment;
9090
struct bolt11 *b11;
9191
char *fail;
9292

tests/test_renepay.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_errors(node_factory, bitcoind):
7575
.format(scid56),
7676
r'update for channel {}/1 now ACTIVE'
7777
.format(scid56)])
78-
details = l1.rpc.call('renepay', {'invstring': inv, 'use_shadow': False})
78+
details = l1.rpc.call('renepay', {'invstring': inv})
7979
assert details['status'] == 'complete'
8080
assert details['amount_msat'] == send_amount
8181
assert details['destination'] == l6.info['id']

0 commit comments

Comments
 (0)