Skip to content

Commit 0f52dc1

Browse files
committed
offers: encapsulate globals in plugin_get_data()
This is how modern plugins do it, and it has the benefit of not requiring extra code for memleak tracking. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 7f1e555 commit 0f52dc1

File tree

6 files changed

+107
-82
lines changed

6 files changed

+107
-82
lines changed

plugins/fetchinvoice.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ static struct command_result *establish_path_fail(struct command *cmd,
576576
static struct command_result *try_establish(struct command *cmd,
577577
struct establishing_paths *epaths)
578578
{
579+
const struct offers_data *od = get_offers_data(cmd->plugin);
579580
struct pubkey target;
580581

581582
if (epaths->sent->direct_dest) {
@@ -596,8 +597,8 @@ static struct command_result *try_establish(struct command *cmd,
596597
epaths->sent->issuer_key = &bpath->path[tal_count(bpath->path)-1]->blinded_node_id;
597598
}
598599

599-
return establish_onion_path(cmd, get_gossmap(cmd->plugin), &id, &target,
600-
disable_connect,
600+
return establish_onion_path(cmd, get_gossmap(cmd->plugin), &od->id, &target,
601+
od->disable_connect,
601602
establish_path_done,
602603
establish_path_fail,
603604
epaths);
@@ -799,15 +800,16 @@ static struct command_result *param_dev_reply_path(struct command *cmd, const ch
799800
return NULL;
800801
}
801802

802-
static bool payer_key(const u8 *public_tweak, size_t public_tweak_len,
803+
static bool payer_key(const struct offers_data *od,
804+
const u8 *public_tweak, size_t public_tweak_len,
803805
struct pubkey *key)
804806
{
805807
struct sha256 tweakhash;
806808

807-
bolt12_alias_tweak(&nodealias_base, public_tweak, public_tweak_len,
809+
bolt12_alias_tweak(&od->nodealias_base, public_tweak, public_tweak_len,
808810
&tweakhash);
809811

810-
*key = id;
812+
*key = od->id;
811813
return secp256k1_ec_pubkey_tweak_add(secp256k1_ctx,
812814
&key->pubkey,
813815
tweakhash.u.u8) == 1;
@@ -818,6 +820,7 @@ struct command_result *json_fetchinvoice(struct command *cmd,
818820
const char *buffer,
819821
const jsmntok_t *params)
820822
{
823+
const struct offers_data *od = get_offers_data(cmd->plugin);
821824
struct amount_msat *msat;
822825
const char *rec_label, *payer_note;
823826
u8 *payer_metadata;
@@ -988,7 +991,7 @@ struct command_result *json_fetchinvoice(struct command *cmd,
988991
rec_label,
989992
strlen(rec_label));
990993

991-
bolt12_alias_tweak(&nodealias_base,
994+
bolt12_alias_tweak(&od->nodealias_base,
992995
tweak_input,
993996
tal_bytelen(tweak_input),
994997
&tweak);
@@ -1054,7 +1057,7 @@ struct command_result *json_fetchinvoice(struct command *cmd,
10541057

10551058
/* We derive transient payer_id from invreq_metadata */
10561059
invreq->invreq_payer_id = tal(invreq, struct pubkey);
1057-
if (!payer_key(invreq->invreq_metadata,
1060+
if (!payer_key(od, invreq->invreq_metadata,
10581061
tal_bytelen(invreq->invreq_metadata),
10591062
invreq->invreq_payer_id)) {
10601063
/* Doesn't happen! */
@@ -1345,6 +1348,7 @@ struct command_result *json_sendinvoice(struct command *cmd,
13451348
const char *buffer,
13461349
const jsmntok_t *params)
13471350
{
1351+
const struct offers_data *od = get_offers_data(cmd->plugin);
13481352
struct amount_msat *msat;
13491353
u32 *timeout;
13501354
struct sent *sent = tal(cmd, struct sent);
@@ -1417,7 +1421,7 @@ struct command_result *json_sendinvoice(struct command *cmd,
14171421
* - MUST set `invoice_node_id` to the final `blinded_node_id` on the path it received the invoice request
14181422
*/
14191423
sent->inv->invoice_node_id = tal(sent->inv, struct pubkey);
1420-
sent->inv->invoice_node_id->pubkey = id.pubkey;
1424+
sent->inv->invoice_node_id->pubkey = od->id.pubkey;
14211425

14221426
/* BOLT #12:
14231427
* - if the expiry for accepting payment is not 7200 seconds

plugins/offers.c

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,31 @@
3030
#define HEADER_LEN crypto_secretstream_xchacha20poly1305_HEADERBYTES
3131
#define ABYTES crypto_secretstream_xchacha20poly1305_ABYTES
3232

33-
struct pubkey id;
34-
u32 blockheight;
35-
u16 cltv_final;
36-
bool disable_connect;
37-
bool dev_invoice_bpath_scid;
38-
struct short_channel_id *dev_invoice_internal_scid;
39-
struct secret invoicesecret_base;
40-
struct secret offerblinding_base;
41-
struct secret nodealias_base;
42-
static struct gossmap *global_gossmap;
43-
44-
static void init_gossmap(struct plugin *plugin)
33+
struct offers_data *get_offers_data(struct plugin *plugin)
4534
{
46-
global_gossmap
47-
= notleak_with_children(gossmap_load(plugin,
48-
GOSSIP_STORE_FILENAME,
49-
plugin_gossmap_logcb,
50-
plugin));
51-
if (!global_gossmap)
35+
return plugin_get_data(plugin, struct offers_data);
36+
}
37+
38+
static void init_gossmap(struct plugin *plugin,
39+
struct offers_data *od)
40+
{
41+
od->global_gossmap_ = gossmap_load(plugin,
42+
GOSSIP_STORE_FILENAME,
43+
plugin_gossmap_logcb,
44+
plugin);
45+
if (!od->global_gossmap_)
5246
plugin_err(plugin, "Could not load gossmap %s: %s",
5347
GOSSIP_STORE_FILENAME, strerror(errno));
5448
}
5549

5650
struct gossmap *get_gossmap(struct plugin *plugin)
5751
{
58-
if (!global_gossmap)
59-
init_gossmap(plugin);
52+
struct offers_data *od = get_offers_data(plugin);
53+
if (!od->global_gossmap_)
54+
init_gossmap(plugin, od);
6055
else
61-
gossmap_refresh(global_gossmap);
62-
return global_gossmap;
56+
gossmap_refresh(od->global_gossmap_);
57+
return od->global_gossmap_;
6358
}
6459

6560
/* BOLT #12:
@@ -69,6 +64,7 @@ struct gossmap *get_gossmap(struct plugin *plugin)
6964
*/
7065
bool we_want_blinded_path(struct plugin *plugin, bool for_payment)
7166
{
67+
const struct offers_data *od = get_offers_data(plugin);
7268
struct node_id local_nodeid;
7369
const struct gossmap_node *node;
7470
const u8 *nannounce;
@@ -80,7 +76,7 @@ bool we_want_blinded_path(struct plugin *plugin, bool for_payment)
8076
u8 rgb_color[3], alias[32];
8177
struct tlv_node_ann_tlvs *na_tlvs;
8278

83-
node_id_from_pubkey(&local_nodeid, &id);
79+
node_id_from_pubkey(&local_nodeid, &od->id);
8480

8581
node = gossmap_find_node(gossmap, &local_nodeid);
8682
if (!node)
@@ -235,6 +231,7 @@ send_onion_reply(struct command *cmd,
235231
struct blinded_path *reply_path,
236232
struct tlv_onionmsg_tlv *payload)
237233
{
234+
const struct offers_data *od = get_offers_data(cmd->plugin);
238235
struct onion_reply *onion_reply;
239236

240237
onion_reply = tal(cmd, struct onion_reply);
@@ -251,8 +248,8 @@ send_onion_reply(struct command *cmd,
251248
}
252249

253250
return establish_onion_path(cmd, get_gossmap(cmd->plugin),
254-
&id, &onion_reply->reply_path->first_node_id.pubkey,
255-
disable_connect,
251+
&od->id, &onion_reply->reply_path->first_node_id.pubkey,
252+
od->disable_connect,
256253
send_onion_reply_after_established,
257254
send_onion_reply_not_established,
258255
onion_reply);
@@ -425,8 +422,9 @@ static struct command_result *block_added_notify(struct command *cmd,
425422
const char *buf,
426423
const jsmntok_t *params)
427424
{
425+
struct offers_data *od = get_offers_data(cmd->plugin);
428426
const char *err = json_scan(cmd, buf, params, "{block_added:{height:%}}",
429-
JSON_SCAN(json_to_u32, &blockheight));
427+
JSON_SCAN(json_to_u32, &od->blockheight));
430428
if (err)
431429
plugin_err(cmd->plugin, "Failed to parse block_added (%.*s): %s",
432430
json_tok_full_len(params),
@@ -1515,34 +1513,36 @@ static const char *init(struct command *init_cmd,
15151513
const char *buf UNUSED,
15161514
const jsmntok_t *config UNUSED)
15171515
{
1516+
struct offers_data *od = get_offers_data(init_cmd->plugin);
1517+
15181518
rpc_scan(init_cmd, "getinfo",
15191519
take(json_out_obj(NULL, NULL, NULL)),
1520-
"{id:%}", JSON_SCAN(json_to_pubkey, &id));
1520+
"{id:%}", JSON_SCAN(json_to_pubkey, &od->id));
15211521

15221522
rpc_scan(init_cmd, "getchaininfo",
15231523
take(json_out_obj(NULL, "last_height", NULL)),
1524-
"{headercount:%}", JSON_SCAN(json_to_u32, &blockheight));
1524+
"{headercount:%}", JSON_SCAN(json_to_u32, &od->blockheight));
15251525

15261526
rpc_scan(init_cmd, "listconfigs",
15271527
take(json_out_obj(NULL, NULL, NULL)),
15281528
"{configs:"
15291529
"{cltv-final:{value_int:%}}}",
1530-
JSON_SCAN(json_to_u16, &cltv_final));
1530+
JSON_SCAN(json_to_u16, &od->cltv_final));
15311531

15321532
rpc_scan(init_cmd, "makesecret",
15331533
take(json_out_obj(NULL, "string", BOLT12_ID_BASE_STRING)),
15341534
"{secret:%}",
1535-
JSON_SCAN(json_to_secret, &invoicesecret_base));
1535+
JSON_SCAN(json_to_secret, &od->invoicesecret_base));
15361536

15371537
rpc_scan(init_cmd, "makesecret",
15381538
take(json_out_obj(NULL, "string", "offer-blinded-path")),
15391539
"{secret:%}",
1540-
JSON_SCAN(json_to_secret, &offerblinding_base));
1540+
JSON_SCAN(json_to_secret, &od->offerblinding_base));
15411541

15421542
rpc_scan(init_cmd, "makesecret",
15431543
take(json_out_obj(NULL, "string", NODE_ALIAS_BASE_STRING)),
15441544
"{secret:%}",
1545-
JSON_SCAN(json_to_secret, &nodealias_base));
1545+
JSON_SCAN(json_to_secret, &od->nodealias_base));
15461546

15471547
return NULL;
15481548
}
@@ -1599,22 +1599,27 @@ static bool scid_jsonfmt(struct plugin *plugin, struct json_stream *js, const ch
15991599
int main(int argc, char *argv[])
16001600
{
16011601
setup_locale();
1602+
struct offers_data *od = tal(NULL, struct offers_data);
1603+
1604+
od->disable_connect = false;
1605+
od->dev_invoice_bpath_scid = false;
1606+
od->dev_invoice_internal_scid = NULL;
16021607

16031608
/* We deal in UTC; mktime() uses local time */
16041609
setenv("TZ", "", 1);
1605-
plugin_main(argv, init, NULL, PLUGIN_RESTARTABLE, true, NULL,
1610+
plugin_main(argv, init, take(od), PLUGIN_RESTARTABLE, true, NULL,
16061611
commands, ARRAY_SIZE(commands),
16071612
notifications, ARRAY_SIZE(notifications),
16081613
hooks, ARRAY_SIZE(hooks),
16091614
NULL, 0,
16101615
plugin_option("fetchinvoice-noconnect", "flag",
16111616
"Don't try to connect directly to fetch/pay an invoice.",
1612-
flag_option, flag_jsonfmt, &disable_connect),
1617+
flag_option, flag_jsonfmt, &od->disable_connect),
16131618
plugin_option_dev("dev-invoice-bpath-scid", "flag",
16141619
"Use short_channel_id instead of pubkey when creating a blinded payment path",
1615-
flag_option, flag_jsonfmt, &dev_invoice_bpath_scid),
1620+
flag_option, flag_jsonfmt, &od->dev_invoice_bpath_scid),
16161621
plugin_option_dev("dev-invoice-internal-scid", "string",
16171622
"Use short_channel_id instead of pubkey when creating a blinded payment path",
1618-
scid_option, scid_jsonfmt, &dev_invoice_internal_scid),
1623+
scid_option, scid_jsonfmt, &od->dev_invoice_internal_scid),
16191624
NULL);
16201625
}

plugins/offers.h

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,31 @@ struct command;
77
struct onion_message;
88
struct plugin;
99

10-
/* This is me. */
11-
extern struct pubkey id;
12-
/* --fetchinvoice-noconnect */
13-
extern bool disable_connect;
14-
/* --cltv-final */
15-
extern u16 cltv_final;
16-
/* Current header_count */
17-
extern u32 blockheight;
18-
/* Basis for invoice path_secrets */
19-
extern struct secret invoicesecret_base;
20-
/* Base for offers path_secrets */
21-
extern struct secret offerblinding_base;
22-
/* Base for node aliases for invoice requests */
23-
extern struct secret nodealias_base;
24-
/* --dev-invoice-bpath-scid */
25-
extern bool dev_invoice_bpath_scid;
26-
/* --dev-invoice-internal-scid */
27-
extern struct short_channel_id *dev_invoice_internal_scid;
28-
/* This is me. */
29-
extern struct pubkey id;
10+
/* plugin_data for this plugin */
11+
struct offers_data {
12+
/* This is me. */
13+
struct pubkey id;
14+
/* --fetchinvoice-noconnect */
15+
bool disable_connect;
16+
/* --cltv-final */
17+
u16 cltv_final;
18+
/* Current header_count */
19+
u32 blockheight;
20+
/* Basis for invoice path_secrets */
21+
struct secret invoicesecret_base;
22+
/* Base for offers path_secrets */
23+
struct secret offerblinding_base;
24+
/* Base for node aliases for invoice requests */
25+
struct secret nodealias_base;
26+
/* --dev-invoice-bpath-scid */
27+
bool dev_invoice_bpath_scid;
28+
/* --dev-invoice-internal-scid */
29+
struct short_channel_id *dev_invoice_internal_scid;
30+
/* Use get_gossmap() to access this! */
31+
struct gossmap *global_gossmap_;
32+
};
33+
34+
struct offers_data *get_offers_data(struct plugin *plugin);
3035

3136
/* Helper to send a reply (connecting if required), and discard result */
3237
struct command_result *WARN_UNUSED_RESULT

plugins/offers_inv_hook.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ struct command_result *handle_invoice(struct command *cmd,
217217
struct blinded_path *reply_path STEALS,
218218
const struct secret *secret)
219219
{
220+
const struct offers_data *od = get_offers_data(cmd->plugin);
220221
size_t len = tal_count(invbin);
221222
struct inv *inv = tal(cmd, struct inv);
222223
struct out_req *req;
@@ -242,7 +243,7 @@ struct command_result *handle_invoice(struct command *cmd,
242243
invoice_invreq_id(inv->inv, &invreq_id_nopath);
243244
inv->inv->invreq_paths = invreq_paths;
244245

245-
path_secret = bolt12_path_id(tmpctx, &offerblinding_base, &invreq_id_nopath);
246+
path_secret = bolt12_path_id(tmpctx, &od->offerblinding_base, &invreq_id_nopath);
246247
if (!memeq(path_secret, tal_count(path_secret),
247248
secret, sizeof(*secret))) {
248249
if (command_dev_apis(cmd))

0 commit comments

Comments
 (0)