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.
This commit is contained in:
Christian Decker 2019-09-17 13:03:11 +02:00 committed by Rusty Russell
parent 858b3f2b93
commit 14247283b2
3 changed files with 20 additions and 8 deletions

View File

@ -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, "lightning-dir", ld->config_dir);
json_add_string(req->stream, "rpc-file", ld->rpc_filename); json_add_string(req->stream, "rpc-file", ld->rpc_filename);
json_add_bool(req->stream, "startup", plugin->plugins->startup); 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); json_object_end(req->stream);
} }

View File

@ -12,7 +12,9 @@
const char *placeholder_script = "0020b95810f824f843934fa042acd0becba52087813e260edaeebc42b5cb9abe1464"; const char *placeholder_script = "0020b95810f824f843934fa042acd0becba52087813e260edaeebc42b5cb9abe1464";
const char *placeholder_funding_addr; const char *placeholder_funding_addr;
const struct amount_sat *max_funding;
/* Populated by libplugin */
extern const struct chainparams *chainparams;
struct funding_req { struct funding_req {
struct node_id *id; struct node_id *id;
@ -333,7 +335,7 @@ static struct command_result *tx_prepare_dryrun(struct command *cmd,
const jsmntok_t *result, const jsmntok_t *result,
struct funding_req *fr) struct funding_req *fr)
{ {
const struct bitcoin_tx *tx; struct bitcoin_tx *tx;
const char *hex; const char *hex;
struct amount_sat funding; struct amount_sat funding;
bool funding_found; 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")); hex = json_strdup(tmpctx, buf, json_get_member(buf, result, "unsigned_tx"));
tx = bitcoin_tx_from_hex(fr, hex, strlen(hex)); tx = bitcoin_tx_from_hex(fr, hex, strlen(hex));
tx->chainparams = chainparams;
/* Find the funding amount */ /* Find the funding amount */
funding_found = false; 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); plugin_err("Error creating placebo funding tx, funding_out not found. %s", hex);
/* Update funding to actual amount */ /* Update funding to actual amount */
if (fr->funding_all && amount_sat_greater(funding, *max_funding)) if (fr->funding_all && amount_sat_greater(funding, chainparams->max_funding))
funding = *max_funding; funding = chainparams->max_funding;
fr->funding_str = type_to_string(fr, struct amount_sat, &funding); fr->funding_str = type_to_string(fr, struct amount_sat, &funding);
return fundchannel_start(cmd, fr); return fundchannel_start(cmd, fr);
@ -404,7 +407,6 @@ static void init(struct plugin_conn *rpc,
{ {
/* Figure out what the 'placeholder' addr is */ /* Figure out what the 'placeholder' addr is */
const char *network_name; const char *network_name;
const struct chainparams *chainparams;
u8 *placeholder = tal_hexdata(tmpctx, placeholder_script, strlen(placeholder_script)); u8 *placeholder = tal_hexdata(tmpctx, placeholder_script, strlen(placeholder_script));
network_name = rpc_delve(tmpctx, "listconfigs", 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, placeholder_funding_addr = encode_scriptpubkey_to_addr(NULL,
chainparams->bip173_name, chainparams->bip173_name,
placeholder); placeholder);
max_funding = &chainparams->max_funding;
} }

View File

@ -1,3 +1,4 @@
#include <bitcoin/chainparams.h>
#include <ccan/err/err.h> #include <ccan/err/err.h>
#include <ccan/intmap/intmap.h> #include <ccan/intmap/intmap.h>
#include <ccan/json_out/json_out.h> #include <ccan/json_out/json_out.h>
@ -34,6 +35,8 @@ static size_t in_timer;
bool deprecated_apis; bool deprecated_apis;
const struct chainparams *chainparams;
struct plugin_timer { struct plugin_timer {
struct timer timer; struct timer timer;
struct command_result *(*cb)(void); struct command_result *(*cb)(void);
@ -523,10 +526,10 @@ static struct command_result *handle_init(struct command *init_cmd,
void (*init)(struct plugin_conn *, void (*init)(struct plugin_conn *,
const char *buf, const jsmntok_t *)) 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; struct sockaddr_un addr;
size_t i; size_t i;
char *dir; char *dir, *network;
struct json_out *param_obj; struct json_out *param_obj;
configtok = json_delve(buf, params, ".configuration"); configtok = json_delve(buf, params, ".configuration");
@ -537,6 +540,11 @@ static struct command_result *handle_init(struct command *init_cmd,
if (chdir(dir) != 0) if (chdir(dir) != 0)
plugin_err("chdir to %s: %s", dir, strerror(errno)); 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"); rpctok = json_delve(buf, configtok, ".rpc-file");
rpc_conn.fd = socket(AF_UNIX, SOCK_STREAM, 0); rpc_conn.fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (rpctok->end - rpctok->start + 1 > sizeof(addr.sun_path)) if (rpctok->end - rpctok->start + 1 > sizeof(addr.sun_path))