From 14247283b2934d0dc352f09e3dc4c389097008d6 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 17 Sep 2019 13:03:11 +0200 Subject: [PATCH] plugin: Tell the plugin which network we run on The fundchannel plugin needs to know how to build a transaction, so we need to tell it which chainparams to use. Also adds `chainparams` as a global, since that seems to be the way to do things in plugins. --- lightningd/plugin.c | 3 +++ plugins/fundchannel.c | 13 +++++++------ plugins/libplugin.c | 12 ++++++++++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lightningd/plugin.c b/lightningd/plugin.c index eba3c1826..b0e33c8af 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -1049,6 +1049,9 @@ plugin_populate_init_request(struct plugin *plugin, struct jsonrpc_request *req) json_add_string(req->stream, "lightning-dir", ld->config_dir); json_add_string(req->stream, "rpc-file", ld->rpc_filename); json_add_bool(req->stream, "startup", plugin->plugins->startup); + json_add_string( + req->stream, "network", + plugin->plugins->ld->topology->bitcoind->chainparams->network_name); json_object_end(req->stream); } diff --git a/plugins/fundchannel.c b/plugins/fundchannel.c index a213138c0..0f22acd82 100644 --- a/plugins/fundchannel.c +++ b/plugins/fundchannel.c @@ -12,7 +12,9 @@ const char *placeholder_script = "0020b95810f824f843934fa042acd0becba52087813e260edaeebc42b5cb9abe1464"; const char *placeholder_funding_addr; -const struct amount_sat *max_funding; + +/* Populated by libplugin */ +extern const struct chainparams *chainparams; struct funding_req { struct node_id *id; @@ -333,7 +335,7 @@ static struct command_result *tx_prepare_dryrun(struct command *cmd, const jsmntok_t *result, struct funding_req *fr) { - const struct bitcoin_tx *tx; + struct bitcoin_tx *tx; const char *hex; struct amount_sat funding; bool funding_found; @@ -347,6 +349,7 @@ static struct command_result *tx_prepare_dryrun(struct command *cmd, hex = json_strdup(tmpctx, buf, json_get_member(buf, result, "unsigned_tx")); tx = bitcoin_tx_from_hex(fr, hex, strlen(hex)); + tx->chainparams = chainparams; /* Find the funding amount */ funding_found = false; @@ -363,8 +366,8 @@ static struct command_result *tx_prepare_dryrun(struct command *cmd, plugin_err("Error creating placebo funding tx, funding_out not found. %s", hex); /* Update funding to actual amount */ - if (fr->funding_all && amount_sat_greater(funding, *max_funding)) - funding = *max_funding; + if (fr->funding_all && amount_sat_greater(funding, chainparams->max_funding)) + funding = chainparams->max_funding; fr->funding_str = type_to_string(fr, struct amount_sat, &funding); return fundchannel_start(cmd, fr); @@ -404,7 +407,6 @@ static void init(struct plugin_conn *rpc, { /* Figure out what the 'placeholder' addr is */ const char *network_name; - const struct chainparams *chainparams; u8 *placeholder = tal_hexdata(tmpctx, placeholder_script, strlen(placeholder_script)); network_name = rpc_delve(tmpctx, "listconfigs", @@ -415,7 +417,6 @@ static void init(struct plugin_conn *rpc, placeholder_funding_addr = encode_scriptpubkey_to_addr(NULL, chainparams->bip173_name, placeholder); - max_funding = &chainparams->max_funding; } diff --git a/plugins/libplugin.c b/plugins/libplugin.c index 02094939d..c66e8be89 100644 --- a/plugins/libplugin.c +++ b/plugins/libplugin.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -34,6 +35,8 @@ static size_t in_timer; bool deprecated_apis; +const struct chainparams *chainparams; + struct plugin_timer { struct timer timer; struct command_result *(*cb)(void); @@ -523,10 +526,10 @@ static struct command_result *handle_init(struct command *init_cmd, void (*init)(struct plugin_conn *, const char *buf, const jsmntok_t *)) { - const jsmntok_t *configtok, *rpctok, *dirtok, *opttok, *t; + const jsmntok_t *configtok, *rpctok, *dirtok, *opttok, *nettok, *t; struct sockaddr_un addr; size_t i; - char *dir; + char *dir, *network; struct json_out *param_obj; configtok = json_delve(buf, params, ".configuration"); @@ -537,6 +540,11 @@ static struct command_result *handle_init(struct command *init_cmd, if (chdir(dir) != 0) plugin_err("chdir to %s: %s", dir, strerror(errno)); + nettok = json_delve(buf, configtok, ".network"); + network = json_strdup(tmpctx, buf, nettok); + chainparams = chainparams_for_network(network); + is_elements = chainparams->is_elements; + rpctok = json_delve(buf, configtok, ".rpc-file"); rpc_conn.fd = socket(AF_UNIX, SOCK_STREAM, 0); if (rpctok->end - rpctok->start + 1 > sizeof(addr.sun_path))