From 185cd81be408320b8e078060dc2d5cff43149f0b Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 20 May 2022 11:45:26 +0200 Subject: [PATCH] jsonrpc: Add `mindepth` argument to fundchannel and multifundchannel This will eventually enable us to specify 0 for zeroconf channels. Changelog-Added: JSON-RPC: Added `mindepth` argument to specify the number of confirmations we require for `fundchannel` and `multifundchannel` --- contrib/pyln-client/pyln/client/lightning.py | 7 +++++-- doc/lightning-fundchannel.7.md | 3 ++- doc/lightning-fundchannel_start.7.md | 3 ++- doc/schemas/fundchannel.schema.json | 4 ++++ doc/schemas/fundchannel_start.schema.json | 4 ++++ lightningd/opening_common.c | 7 +++++++ lightningd/opening_control.c | 19 +++++++++++-------- plugins/spender/fundchannel.c | 6 ++++++ plugins/spender/multifundchannel.c | 5 +++++ plugins/spender/multifundchannel.h | 3 +++ 10 files changed, 49 insertions(+), 12 deletions(-) diff --git a/contrib/pyln-client/pyln/client/lightning.py b/contrib/pyln-client/pyln/client/lightning.py index 51e70ae7e..2b1844f8e 100644 --- a/contrib/pyln-client/pyln/client/lightning.py +++ b/contrib/pyln-client/pyln/client/lightning.py @@ -703,7 +703,8 @@ class LightningRpc(UnixDomainSocketRpc): def fundchannel(self, node_id, amount, feerate=None, announce=True, minconf=None, utxos=None, push_msat=None, close_to=None, - request_amt=None, compact_lease=None): + request_amt=None, compact_lease=None, + mindepth: Optional[int] = None): """ Fund channel with {id} using {amount} satoshis with feerate of {feerate} (uses default feerate if unset). @@ -728,11 +729,12 @@ class LightningRpc(UnixDomainSocketRpc): "close_to": close_to, "request_amt": request_amt, "compact_lease": compact_lease, + "mindepth": mindepth, } return self.call("fundchannel", payload) def fundchannel_start(self, node_id, amount, feerate=None, announce=True, - close_to=None): + close_to=None, mindepth: Optional[int] = None): """ Start channel funding with {id} for {amount} satoshis with feerate of {feerate} (uses default feerate if unset). @@ -748,6 +750,7 @@ class LightningRpc(UnixDomainSocketRpc): "feerate": feerate, "announce": announce, "close_to": close_to, + "mindepth": mindepth, } return self.call("fundchannel_start", payload) diff --git a/doc/lightning-fundchannel.7.md b/doc/lightning-fundchannel.7.md index 229e48424..fe44b2a21 100644 --- a/doc/lightning-fundchannel.7.md +++ b/doc/lightning-fundchannel.7.md @@ -89,6 +89,7 @@ On success, an object is returned, containing: - **outnum** (u32): The 0-based output index showing which output funded the channel - **channel_id** (hex): The channel_id of the resulting channel (always 64 characters) - **close_to** (hex, optional): The raw scriptPubkey which mutual close will go to; only present if *close_to* parameter was specified and peer supports `option_upfront_shutdown_script` +- **mindepth** (u32, optional): Number of confirmations before we consider the channel active. [comment]: # (GENERATE-FROM-SCHEMA-END) @@ -113,4 +114,4 @@ RESOURCES Main web site: -[comment]: # ( SHA256STAMP:4a8d7c524cfe257f961531929d14d3589efb6ecd182a33e92aade30af90406f8) +[comment]: # ( SHA256STAMP:4bde4785d98f4fdb74ea2415b89ddc864fdf09bd9a30f8056a376aaa668a84b8) diff --git a/doc/lightning-fundchannel_start.7.md b/doc/lightning-fundchannel_start.7.md index 7b1ba60d0..c0d22abcd 100644 --- a/doc/lightning-fundchannel_start.7.md +++ b/doc/lightning-fundchannel_start.7.md @@ -46,6 +46,7 @@ On success, an object is returned, containing: - **funding_address** (string): The address to send funding to for the channel. DO NOT SEND COINS TO THIS ADDRESS YET. - **scriptpubkey** (hex): The raw scriptPubkey for the address - **close_to** (hex, optional): The raw scriptPubkey which mutual close will go to; only present if *close_to* parameter was specified and peer supports `option_upfront_shutdown_script` +- **mindepth** (u32, optional): Number of confirmations before we consider the channel active. The following warnings may also be returned: - **warning_usage**: A warning not to prematurely broadcast the funding transaction (always present!) @@ -82,4 +83,4 @@ RESOURCES Main web site: -[comment]: # ( SHA256STAMP:acbd9ffb07219bc10e06e8f8bc6772d12ec62650bd30b6ee1084a24f53da2ac2) +[comment]: # ( SHA256STAMP:6dc3dd4d3d65baa617b285fd3f6e746d62e2806635a8f8852fd3a94ef6fe3e6a) diff --git a/doc/schemas/fundchannel.schema.json b/doc/schemas/fundchannel.schema.json index 40ee1deb1..63444352f 100644 --- a/doc/schemas/fundchannel.schema.json +++ b/doc/schemas/fundchannel.schema.json @@ -30,6 +30,10 @@ "close_to": { "type": "hex", "description": "The raw scriptPubkey which mutual close will go to; only present if *close_to* parameter was specified and peer supports `option_upfront_shutdown_script`" + }, + "mindepth": { + "type": "u32", + "description": "Number of confirmations before we consider the channel active." } } } diff --git a/doc/schemas/fundchannel_start.schema.json b/doc/schemas/fundchannel_start.schema.json index a2c9795dc..cbd420a39 100644 --- a/doc/schemas/fundchannel_start.schema.json +++ b/doc/schemas/fundchannel_start.schema.json @@ -23,6 +23,10 @@ "warning_usage": { "type": "string", "description": "A warning not to prematurely broadcast the funding transaction (always present!)" + }, + "mindepth": { + "type": "u32", + "description": "Number of confirmations before we consider the channel active." } } } diff --git a/lightningd/opening_common.c b/lightningd/opening_common.c index f866a91f5..866f135de 100644 --- a/lightningd/opening_common.c +++ b/lightningd/opening_common.c @@ -53,6 +53,13 @@ new_uncommitted_channel(struct peer *peer) uc->fc = NULL; uc->our_config.id = 0; + /* BOLT #2: + * + * The sender: + * - SHOULD set `minimum_depth` to a number of blocks it considers + * reasonable to avoid double-spending of the funding transaction. + */ + uc->minimum_depth = ld->config.anchor_confirms; memset(&uc->cid, 0xFF, sizeof(uc->cid)); diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 5eda78580..8754ba647 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -891,13 +891,6 @@ bool peer_start_openingd(struct peer *peer, struct peer_fd *peer_fd) &max_to_self_delay, &min_effective_htlc_capacity); - /* BOLT #2: - * - * The sender: - * - SHOULD set `minimum_depth` to a number of blocks it considers - * reasonable to avoid double-spending of the funding transaction. - */ - uc->minimum_depth = peer->ld->config.anchor_confirms; msg = towire_openingd_init(NULL, chainparams, @@ -1057,7 +1050,7 @@ static struct command_result *json_fundchannel_start(struct command *cmd, struct node_id *id; struct peer *peer; bool *announce_channel; - u32 *feerate_per_kw; + u32 *feerate_per_kw, *mindepth; struct amount_sat *amount; struct amount_msat *push_msat; @@ -1077,6 +1070,7 @@ static struct command_result *json_fundchannel_start(struct command *cmd, p_opt_def("announce", param_bool, &announce_channel, true), p_opt("close_to", param_bitcoin_address, &fc->our_upfront_shutdown_script), p_opt("push_msat", param_msat, &push_msat), + p_opt_def("mindepth", param_u32, &mindepth, cmd->ld->config.anchor_confirms), NULL)) return command_param_failed(); @@ -1167,6 +1161,15 @@ static struct command_result *json_fundchannel_start(struct command *cmd, peer->uncommitted_channel->fc = tal_steal(peer->uncommitted_channel, fc); fc->uc = peer->uncommitted_channel; + /* BOLT #2: + * + * The sender: + * - SHOULD set `minimum_depth` to a number of blocks it considers + * reasonable to avoid double-spending of the funding transaction. + */ + assert(mindepth != NULL); + fc->uc->minimum_depth = *mindepth; + /* Needs to be stolen away from cmd */ if (fc->our_upfront_shutdown_script) fc->our_upfront_shutdown_script diff --git a/plugins/spender/fundchannel.c b/plugins/spender/fundchannel.c index 91f6b72ff..1feb639af 100644 --- a/plugins/spender/fundchannel.c +++ b/plugins/spender/fundchannel.c @@ -44,6 +44,7 @@ json_fundchannel(struct command *cmd, const jsmntok_t *close_to; const jsmntok_t *request_amt; const jsmntok_t *compact_lease; + const jsmntok_t *mindepth; struct out_req *req; @@ -58,6 +59,7 @@ json_fundchannel(struct command *cmd, p_opt("close_to", param_tok, &close_to), p_opt("request_amt", param_tok, &request_amt), p_opt("compact_lease", param_tok, &compact_lease), + p_opt("mindepth", param_tok, &mindepth), NULL)) return command_param_failed(); @@ -84,6 +86,10 @@ json_fundchannel(struct command *cmd, json_add_tok(req->js, "request_amt", request_amt, buf); json_add_tok(req->js, "compact_lease", compact_lease, buf); } + + if (mindepth) + json_add_tok(req->js, "mindepth", mindepth, buf); + json_object_end(req->js); json_array_end(req->js); if (feerate) diff --git a/plugins/spender/multifundchannel.c b/plugins/spender/multifundchannel.c index ac0cd7ae9..7cdfaf72f 100644 --- a/plugins/spender/multifundchannel.c +++ b/plugins/spender/multifundchannel.c @@ -1113,12 +1113,16 @@ fundchannel_start_dest(struct multifundchannel_destination *dest) json_add_string(req->js, "feerate", mfc->cmtmt_feerate_str); else if (mfc->feerate_str) json_add_string(req->js, "feerate", mfc->feerate_str); + json_add_bool(req->js, "announce", dest->announce); json_add_string(req->js, "push_msat", fmt_amount_msat(tmpctx, dest->push_msat)); if (dest->close_to_str) json_add_string(req->js, "close_to", dest->close_to_str); + if (dest->mindepth) + json_add_u32(req->js, "mindepth", *dest->mindepth); + send_outreq(cmd->plugin, req); } @@ -1890,6 +1894,7 @@ param_destinations_array(struct command *cmd, const char *name, p_opt_def("request_amt", param_sat, &request_amt, AMOUNT_SAT(0)), p_opt("compact_lease", param_lease_hex, &rates), + p_opt("mindepth", param_u32, &dest->mindepth), NULL)) return command_param_failed(); diff --git a/plugins/spender/multifundchannel.h b/plugins/spender/multifundchannel.h index e9cfe5d46..e65c2785c 100644 --- a/plugins/spender/multifundchannel.h +++ b/plugins/spender/multifundchannel.h @@ -149,6 +149,9 @@ struct multifundchannel_destination { /* Channel lease rates that we expect the peer to respond with */ struct lease_rates *rates; + + /* Number of blocks to wait before sending `channel_ready`. */ + u32 *mindepth; };