From d149ba2f3adef535b7ee55090bcc57837989863b Mon Sep 17 00:00:00 2001 From: trueptolemy Date: Fri, 20 Sep 2019 02:50:58 +0800 Subject: [PATCH] JSON-API: `fundchannel_start` uses `amount` fieldname to replace `satoshi` --- contrib/pylightning/lightning/lightning.py | 38 +++++++++++++++----- lightningd/opening_control.c | 42 +++++++++++++++++----- plugins/fundchannel.c | 6 +++- 3 files changed, 68 insertions(+), 18 deletions(-) diff --git a/contrib/pylightning/lightning/lightning.py b/contrib/pylightning/lightning/lightning.py index 1233b2624..4ac87afdd 100644 --- a/contrib/pylightning/lightning/lightning.py +++ b/contrib/pylightning/lightning/lightning.py @@ -540,9 +540,22 @@ class LightningRpc(UnixDomainSocketRpc): return _fundchannel(node_id, *args, **kwargs) - def fundchannel_start(self, node_id, satoshi, feerate=None, announce=True): + def _deprecated_fundchannel_start(self, node_id, satoshi, feerate=None, announce=True): + warnings.warn("fundchannel_start: the 'satoshi' field is renamed 'amount' : expect removal" + " in Mid-2020", + DeprecationWarning) + + payload = { + "id": node_id, + "satoshi": satoshi, + "feerate": feerate, + "announce": announce, + } + return self.call("fundchannel_start", payload) + + def fundchannel_start(self, node_id, *args, **kwargs): """ - Start channel funding with {id} for {satoshi} satoshis + Start channel funding with {id} for {amount} satoshis with feerate of {feerate} (uses default feerate if unset). If {announce} is False, don't send channel announcements. Returns a Bech32 {funding_address} for an external wallet @@ -550,13 +563,20 @@ class LightningRpc(UnixDomainSocketRpc): 'fundchannel_complete' to complete channel establishment with peer. """ - payload = { - "id": node_id, - "satoshi": satoshi, - "feerate": feerate, - "announce": announce, - } - return self.call("fundchannel_start", payload) + + if 'satoshi' in kwargs: + return self._deprecated_fundchannel_start(node_id, *args, **kwargs) + + def _fundchannel_start(node_id, amount, feerate=None, announce=True): + payload = { + "id": node_id, + "amount": amount, + "feerate": feerate, + "announce": announce + } + return self.call("fundchannel_start", payload) + + return _fundchannel_start(node_id, *args, **kwargs) def fundchannel_cancel(self, node_id): """ diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index d76ca1d73..ad83ebb71 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -1069,16 +1070,41 @@ static struct command_result *json_fund_channel_start(struct command *cmd, fc->cancels = tal_arr(fc, struct command *, 0); fc->uc = NULL; fc->inflight = false; - if (!param(fc->cmd, buffer, params, - p_req("id", param_node_id, &id), - p_req("satoshi", param_sat, &amount), - p_opt("feerate", param_feerate, &feerate_per_kw), - p_opt_def("announce", param_bool, &announce_channel, true), - NULL)) - return command_param_failed(); + + /* For generating help, give new-style. */ + if (!params || !deprecated_apis || params->type == JSMN_ARRAY) { + if (!param(fc->cmd, buffer, params, + p_req("id", param_node_id, &id), + p_req("amount", param_sat, &amount), + p_opt("feerate", param_feerate, &feerate_per_kw), + p_opt_def("announce", param_bool, &announce_channel, true), + NULL)) + return command_param_failed(); + } else { + /* For json object type when allow deprecated api, 'check' command + * can't find the error if we don't set 'amount' nor 'satoshi'. + */ + struct amount_sat *satoshi; + if (!param(fc->cmd, buffer, params, + p_req("id", param_node_id, &id), + p_opt("amount", param_sat, &amount), + p_opt("satoshi", param_sat, &satoshi), + p_opt("feerate", param_feerate, &feerate_per_kw), + p_opt_def("announce", param_bool, &announce_channel, true), + NULL)) + return command_param_failed(); + + if (!amount) { + if (satoshi) + amount = satoshi; + else + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "Need set 'amount' field"); + } + } if (amount_sat_greater(*amount, max_funding_satoshi)) - return command_fail(cmd, FUND_MAX_EXCEEDED, + return command_fail(cmd, FUND_MAX_EXCEEDED, "Amount exceeded %s", type_to_string(tmpctx, struct amount_sat, &max_funding_satoshi)); diff --git a/plugins/fundchannel.c b/plugins/fundchannel.c index 86ef2be04..eacbc4086 100644 --- a/plugins/fundchannel.c +++ b/plugins/fundchannel.c @@ -312,7 +312,11 @@ static struct command_result *fundchannel_start(struct command *cmd, json_out_start(ret, NULL, '{'); json_out_addstr(ret, "id", node_id_to_hexstr(tmpctx, fr->id)); - json_out_addstr(ret, "satoshi", fr->funding_str); + + if (deprecated_apis) + json_out_addstr(ret, "satoshi", fr->funding_str); + else + json_out_addstr(ret, "amount", fr->funding_str); if (fr->feerate_str) json_out_addstr(ret, "feerate", fr->feerate_str);