Skip to content

Commit 2507a50

Browse files
committed
renepay: use plugin_get_data API
Use plugin_get_data API and make less use of the access to the global plugin state when possible. Changelog-None Signed-off-by: Lagrang3 <[email protected]>
1 parent b7d46c8 commit 2507a50

File tree

13 files changed

+164
-138
lines changed

13 files changed

+164
-138
lines changed

plugins/renepay/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PLUGIN_RENEPAY_SRC := \
2-
plugins/renepay/main.c \
2+
plugins/renepay/renepay.c \
33
plugins/renepay/payment.c \
44
plugins/renepay/route.c \
55
plugins/renepay/routetracker.c \
@@ -11,7 +11,7 @@ PLUGIN_RENEPAY_SRC := \
1111
plugins/renepay/json.c
1212

1313
PLUGIN_RENEPAY_HDRS := \
14-
plugins/renepay/payplugin.h \
14+
plugins/renepay/renepay.h \
1515
plugins/renepay/payment.h \
1616
plugins/renepay/payment_info.h \
1717
plugins/renepay/renepayconfig.h \

plugins/renepay/mods.c

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
#include <common/memleak.h>
99
#include <plugins/renepay/json.h>
1010
#include <plugins/renepay/mods.h>
11-
#include <plugins/renepay/payplugin.h>
11+
#include <plugins/renepay/renepay.h>
1212
#include <plugins/renepay/renepayconfig.h>
1313
#include <plugins/renepay/route.h>
1414
#include <plugins/renepay/routetracker.h>
15+
#include <plugins/renepay/utils.h>
1516
#include <unistd.h>
1617
#include <wire/bolt12_wiregen.h>
1718

@@ -32,7 +33,7 @@ struct command_result *payment_continue(struct payment *payment)
3233
void *op = payment_virtual_program[payment->exec_state++];
3334

3435
if (op == OP_NULL) {
35-
plugin_err(pay_plugin->plugin,
36+
plugin_err(payment->plugin,
3637
"payment_continue reached the end of the virtual "
3738
"machine execution.");
3839
} else if (op == OP_CALL) {
@@ -41,11 +42,11 @@ struct command_result *payment_continue(struct payment *payment)
4142
payment_virtual_program[payment->exec_state++];
4243

4344
if (mod == NULL)
44-
plugin_err(pay_plugin->plugin,
45+
plugin_err(payment->plugin,
4546
"payment_continue expected payment_modifier "
4647
"but NULL found");
4748

48-
plugin_log(pay_plugin->plugin, LOG_DBG, "Calling modifier %s",
49+
plugin_log(payment->plugin, LOG_DBG, "Calling modifier %s",
4950
mod->name);
5051
return mod->step_cb(payment);
5152
} else if (op == OP_IF) {
@@ -54,11 +55,11 @@ struct command_result *payment_continue(struct payment *payment)
5455
payment_virtual_program[payment->exec_state++];
5556

5657
if (cond == NULL)
57-
plugin_err(pay_plugin->plugin,
58+
plugin_err(payment->plugin,
5859
"payment_continue expected pointer to "
5960
"condition but NULL found");
6061

61-
plugin_log(pay_plugin->plugin, LOG_DBG,
62+
plugin_log(payment->plugin, LOG_DBG,
6263
"Calling payment condition %s", cond->name);
6364

6465
const u64 position_iftrue =
@@ -69,7 +70,7 @@ struct command_result *payment_continue(struct payment *payment)
6970

7071
return payment_continue(payment);
7172
}
72-
plugin_err(pay_plugin->plugin, "payment_continue op code not defined");
73+
plugin_err(payment->plugin, "payment_continue op code not defined");
7374
return NULL;
7475
}
7576

@@ -121,7 +122,7 @@ struct success_data {
121122
};
122123

123124
/* Extracts success data from listsendpays. */
124-
static bool success_data_from_listsendpays(const char *buf,
125+
static bool success_data_from_listsendpays(struct command *cmd, const char *buf,
125126
const jsmntok_t *arr,
126127
struct success_data *success)
127128
{
@@ -144,13 +145,13 @@ static bool success_data_from_listsendpays(const char *buf,
144145
const jsmntok_t *status_tok = json_get_member(buf, t, "status");
145146
if (!status_tok)
146147
plugin_err(
147-
pay_plugin->plugin,
148+
cmd->plugin,
148149
"%s (line %d) missing status token from json.",
149150
__func__, __LINE__);
150151
const char *status = json_strdup(tmpctx, buf, status_tok);
151152
if (!status)
152153
plugin_err(
153-
pay_plugin->plugin,
154+
cmd->plugin,
154155
"%s (line %d) failed to allocate status string.",
155156
__func__, __LINE__);
156157

@@ -173,7 +174,7 @@ static bool success_data_from_listsendpays(const char *buf,
173174
JSON_SCAN(json_to_preimage, &success->preimage));
174175

175176
if (err)
176-
plugin_err(pay_plugin->plugin,
177+
plugin_err(cmd->plugin,
177178
"%s (line %d) json_scan of "
178179
"listsendpay returns the "
179180
"following error: %s",
@@ -185,7 +186,7 @@ static bool success_data_from_listsendpays(const char *buf,
185186
this_msat) ||
186187
!amount_msat_add(&success->sent_msat,
187188
success->sent_msat, this_sent))
188-
plugin_err(pay_plugin->plugin,
189+
plugin_err(cmd->plugin,
189190
"%s (line %d) amount_msat overflow.",
190191
__func__, __LINE__);
191192

@@ -211,7 +212,7 @@ static struct command_result *previoussuccess_done(struct command *cmd,
211212
}
212213

213214
struct success_data success;
214-
if (!success_data_from_listsendpays(buf, arr, &success)) {
215+
if (!success_data_from_listsendpays(cmd, buf, arr, &success)) {
215216
/* There are no success sendpays. */
216217
return payment_continue(payment);
217218
}
@@ -270,9 +271,10 @@ REGISTER_PAYMENT_MODIFIER(initial_sanity_checks, initial_sanity_checks_cb);
270271

271272
static struct command_result *refreshgossmap_cb(struct payment *payment)
272273
{
273-
assert(pay_plugin->gossmap); // gossmap must be already initialized
274274
assert(payment);
275-
gossmap_refresh(pay_plugin->gossmap);
275+
struct renepay *renepay = get_renepay(payment->plugin);
276+
assert(renepay->gossmap); // gossmap must be already initialized
277+
gossmap_refresh(renepay->gossmap);
276278
return payment_continue(payment);
277279
}
278280

@@ -454,7 +456,7 @@ static struct command_result *getroutes_done(struct command *cmd,
454456
assert(routetracker);
455457

456458
if (tal_count(routetracker->computed_routes) > 0)
457-
plugin_err(pay_plugin->plugin,
459+
plugin_err(cmd->plugin,
458460
"%s: no previously computed routes expected.",
459461
__func__);
460462

@@ -476,7 +478,7 @@ static struct command_result *getroutes_done(struct command *cmd,
476478
bool success = json_to_myroute(buf, r, route);
477479
if (!success) {
478480
plugin_err(
479-
pay_plugin->plugin,
481+
cmd->plugin,
480482
"%s: failed to parse route from getroutes, %.*s",
481483
__func__, json_tok_full_len(r),
482484
json_tok_full(buf, r));
@@ -511,19 +513,20 @@ static struct command_result *getroutes_fail(struct command *cmd,
511513

512514
static struct command_result *getroutes_cb(struct payment *payment)
513515
{
516+
struct renepay *renepay = get_renepay(payment->plugin);
514517
assert(payment->status == PAYMENT_PENDING);
515518
struct amount_msat feebudget, fees_spent, remaining;
516519

517520
/* Total feebudget */
518521
if (!amount_msat_sub(&feebudget, payment->payment_info.maxspend,
519522
payment->payment_info.amount))
520-
plugin_err(pay_plugin->plugin, "%s: fee budget is negative?",
523+
plugin_err(payment->plugin, "%s: fee budget is negative?",
521524
__func__);
522525

523526
/* Fees spent so far */
524527
if (!amount_msat_sub(&fees_spent, payment->total_sent,
525528
payment->total_delivering))
526-
plugin_err(pay_plugin->plugin,
529+
plugin_err(payment->plugin,
527530
"%s: total_delivering is greater than total_sent?",
528531
__func__);
529532

@@ -535,7 +538,7 @@ static struct command_result *getroutes_cb(struct payment *payment)
535538
if (!amount_msat_sub(&remaining, payment->payment_info.amount,
536539
payment->total_delivering) ||
537540
amount_msat_is_zero(remaining)) {
538-
plugin_log(pay_plugin->plugin, LOG_UNUSUAL,
541+
plugin_log(payment->plugin, LOG_UNUSUAL,
539542
"%s: Payment is pending with full amount already "
540543
"committed. We skip the computation of new routes.",
541544
__func__);
@@ -566,7 +569,7 @@ static struct command_result *getroutes_cb(struct payment *payment)
566569
// starts
567570
// -> command_layer should auto clean
568571

569-
json_add_node_id(req->js, "source", &pay_plugin->my_id);
572+
json_add_node_id(req->js, "source", &renepay->my_id);
570573
json_add_node_id(req->js, "destination", payment->routing_destination);
571574
json_add_amount_msat(req->js, "amount_msat", remaining);
572575
json_add_amount_msat(req->js, "maxfee_msat", feebudget);
@@ -599,7 +602,7 @@ static struct command_result *send_routes_cb(struct payment *payment)
599602
assert(routetracker);
600603
if (!routetracker->computed_routes ||
601604
tal_count(routetracker->computed_routes) == 0) {
602-
plugin_log(pay_plugin->plugin, LOG_UNUSUAL,
605+
plugin_log(payment->plugin, LOG_UNUSUAL,
603606
"%s: there are no routes to send, skipping.",
604607
__func__);
605608
return payment_continue(payment);
@@ -682,7 +685,7 @@ static struct command_result *collect_results_cb(struct payment *payment)
682685
if (!amount_msat_greater_eq(payment->total_delivering,
683686
payment->payment_info.amount)) {
684687
plugin_log(
685-
pay_plugin->plugin, LOG_UNUSUAL,
688+
payment->plugin, LOG_UNUSUAL,
686689
"%s: received a success sendpay for this "
687690
"payment but the total delivering amount %s "
688691
"is less than the payment amount %s.",
@@ -796,7 +799,7 @@ static struct command_result *pendingsendpays_done(struct command *cmd,
796799
}
797800

798801
struct success_data success;
799-
if (success_data_from_listsendpays(buf, arr, &success)) {
802+
if (success_data_from_listsendpays(cmd, buf, arr, &success)) {
800803
/* Have success data, hence the payment is complete, we stop. */
801804
payment->payment_info.start_time.ts.tv_sec = success.created_at;
802805
payment->payment_info.start_time.ts.tv_nsec = 0;
@@ -827,7 +830,7 @@ static struct command_result *pendingsendpays_done(struct command *cmd,
827830
JSON_SCAN(json_to_u64, &groupid));
828831

829832
if (err)
830-
plugin_err(pay_plugin->plugin,
833+
plugin_err(cmd->plugin,
831834
"%s json_scan of listsendpay returns the "
832835
"following error: %s",
833836
__func__, err);
@@ -864,7 +867,7 @@ static struct command_result *pendingsendpays_done(struct command *cmd,
864867
JSON_SCAN(json_to_msat, &this_sent));
865868

866869
if (err)
867-
plugin_err(pay_plugin->plugin,
870+
plugin_err(cmd->plugin,
868871
"%s json_scan of listsendpay returns the "
869872
"following error: %s",
870873
__func__, err);
@@ -887,7 +890,7 @@ static struct command_result *pendingsendpays_done(struct command *cmd,
887890
this_msat) ||
888891
!amount_msat_add(&pending_sent, pending_sent,
889892
this_sent))
890-
plugin_err(pay_plugin->plugin,
893+
plugin_err(cmd->plugin,
891894
"%s (line %d) amount_msat overflow.",
892895
__func__, __LINE__);
893896
}
@@ -911,7 +914,7 @@ static struct command_result *pendingsendpays_done(struct command *cmd,
911914
payment->total_sent = pending_sent;
912915
payment->total_delivering = pending_msat;
913916

914-
plugin_log(pay_plugin->plugin, LOG_DBG,
917+
plugin_log(cmd->plugin, LOG_DBG,
915918
"There are pending sendpays to this invoice. "
916919
"groupid = %" PRIu64 " "
917920
"delivering = %s, "
@@ -964,9 +967,10 @@ static struct command_result *age_done(struct command *cmd,
964967

965968
static struct command_result *knowledgerelax_cb(struct payment *payment)
966969
{
970+
struct renepay *renepay = get_renepay(payment->plugin);
967971
const u64 now_sec = time_now().ts.tv_sec;
968-
// const u64 time_delta = now_sec - pay_plugin->last_time;
969-
pay_plugin->last_time = now_sec;
972+
// const u64 time_delta = now_sec - renepay->last_time;
973+
renepay->last_time = now_sec;
970974
/* FIXME: implement a Markovian state relaxation, the time delta is all
971975
* we need to provide. */
972976
struct command *cmd = payment_command(payment);
@@ -1090,7 +1094,8 @@ static struct command_result *channelfilter_done(struct command *cmd,
10901094
static struct command_result *channelfilter_cb(struct payment *payment)
10911095
{
10921096
assert(payment);
1093-
assert(pay_plugin->gossmap);
1097+
struct renepay *renepay = get_renepay(payment->plugin);
1098+
assert(renepay->gossmap);
10941099
const double HTLC_MAX_FRACTION = 0.01; // 1%
10951100
const u64 HTLC_MAX_STOP_MSAT = 1000000000; // 1M sats
10961101
u64 disabled_count = 0;
@@ -1107,18 +1112,18 @@ static struct command_result *channelfilter_cb(struct payment *payment)
11071112
struct out_req *req;
11081113

11091114
for (const struct gossmap_node *node =
1110-
gossmap_first_node(pay_plugin->gossmap);
1111-
node; node = gossmap_next_node(pay_plugin->gossmap, node)) {
1115+
gossmap_first_node(renepay->gossmap);
1116+
node; node = gossmap_next_node(renepay->gossmap, node)) {
11121117
for (size_t i = 0; i < node->num_chans; i++) {
11131118
int dir;
11141119
const struct gossmap_chan *chan = gossmap_nth_chan(
1115-
pay_plugin->gossmap, node, i, &dir);
1120+
renepay->gossmap, node, i, &dir);
11161121
const u64 htlc_max =
11171122
fp16_to_u64(chan->half[dir].htlc_max);
11181123
if (htlc_max < htlc_max_threshold) {
11191124
struct short_channel_id_dir scidd = {
11201125
.scid = gossmap_chan_scid(
1121-
pay_plugin->gossmap, chan),
1126+
renepay->gossmap, chan),
11221127
.dir = dir};
11231128
req = add_to_batch(cmd, batch,
11241129
"askrene-update-channel");
@@ -1134,7 +1139,7 @@ static struct command_result *channelfilter_cb(struct payment *payment)
11341139
}
11351140
// FIXME: prune the network over other parameters, eg. capacity,
11361141
// fees, ...
1137-
plugin_log(pay_plugin->plugin, LOG_DBG,
1142+
plugin_log(payment->plugin, LOG_DBG,
11381143
"channelfilter: disabling %" PRIu64 " channels.",
11391144
disabled_count);
11401145
return batch_done(cmd, batch);

plugins/renepay/payment.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
#include <common/memleak.h>
88
#include <plugins/renepay/json.h>
99
#include <plugins/renepay/payment.h>
10-
#include <plugins/renepay/payplugin.h>
10+
#include <plugins/renepay/renepay.h>
1111
#include <plugins/renepay/routetracker.h>
1212

1313
static struct command_result *payment_finish(struct payment *p);
1414

1515
struct payment *payment_new(const tal_t *ctx, const struct sha256 *payment_hash,
16-
const char *invstr TAKES)
16+
const char *invstr TAKES,
17+
struct plugin *plugin)
1718
{
1819
struct payment *p = tal(ctx, struct payment);
1920
memset(p, 0, sizeof(struct payment));
20-
21+
p->plugin = plugin;
2122
struct payment_info *pinfo = &p->payment_info;
2223

2324
assert(payment_hash);
@@ -157,7 +158,7 @@ struct amount_msat payment_fees(const struct payment *p)
157158

158159
if (!amount_msat_sub(&fees, sent, delivered))
159160
plugin_err(
160-
pay_plugin->plugin,
161+
p->plugin,
161162
"Strange, sent amount (%s) is less than delivered (%s), "
162163
"aborting.",
163164
fmt_amount_msat(tmpctx, sent),
@@ -259,7 +260,7 @@ void payment_note(struct payment *p, enum log_level lvl, const char *fmt, ...)
259260

260261
tal_arr_expand(&p->paynotes, str);
261262
/* Log at debug, unless it's weird... */
262-
plugin_log(pay_plugin->plugin, lvl < LOG_UNUSUAL ? LOG_DBG : lvl, "%s",
263+
plugin_log(p->plugin, lvl < LOG_UNUSUAL ? LOG_DBG : lvl, "%s",
263264
str);
264265

265266
for (size_t i = 0; i < tal_count(p->cmd_array); i++) {

plugins/renepay/payment.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ struct payment {
7272

7373
/* Knowledge layer concerning this payment. */
7474
const char *payment_layer;
75+
76+
/* For logs and access to global data. */
77+
struct plugin *plugin;
7578
};
7679

7780
static inline const struct sha256 payment_hash(const struct payment *p)
@@ -104,7 +107,8 @@ HTABLE_DEFINE_NODUPS_TYPE(struct payment, payment_hash, payment_hash64,
104107
struct payment *payment_new(
105108
const tal_t *ctx,
106109
const struct sha256 *payment_hash,
107-
const char *invstr TAKES);
110+
const char *invstr TAKES,
111+
struct plugin *plugin);
108112

109113
bool payment_set_constraints(
110114
struct payment *p,

0 commit comments

Comments
 (0)