Skip to content

Commit

Permalink
fundchannel: cancel channel on transaction broadcast error
Browse files Browse the repository at this point in the history
Changelog-Added: fundchannel will now cancel ongoing channel creation when transaction broadcast fails.

Signed-off-by: Antoine Poinsot <[email protected]>
  • Loading branch information
darosior committed Aug 7, 2020
1 parent 06372e1 commit 02748ac
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions plugins/fundchannel.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ static struct command_result *tx_abort(struct command *cmd,
{
struct out_req *req;

/* We stash the error so we can return it after we've cleaned up */
fr->error = json_strdup(fr, buf, error);
/* We stash the error so we can return it after we've cleaned up.
* If one already occured upstream, we keep it instead. */
if (!fr->error)
fr->error = json_strdup(fr, buf, error);

req = jsonrpc_request_start(cmd->plugin, cmd, "txdiscard",
send_prior, send_prior, fr);
Expand All @@ -73,6 +75,24 @@ static struct command_result *tx_abort(struct command *cmd,
return send_outreq(cmd->plugin, req);
}

static struct command_result *channel_abort(struct command *cmd,
const char *buf,
const jsmntok_t *error,
struct funding_req *fr)
{
struct out_req *req;

fr->error = json_strdup(fr, buf, error);

/* After we cancel the channel we need to unreserve the coin reserved with
* `txprepare`. */
req = jsonrpc_request_start(cmd->plugin, cmd, "fundchannel_cancel",
tx_abort, tx_abort, fr);
json_add_string(req->js, "id", node_id_to_hexstr(tmpctx, fr->id));

return send_outreq(cmd->plugin, req);
}

/* We're basically done, we just need to format the output to match
* what the original `fundchannel` returned */
static struct command_result *finish(struct command *cmd,
Expand Down Expand Up @@ -113,7 +133,7 @@ static struct command_result *send_tx(struct command *cmd,
fr->chanstr = json_strdup(fr, buf, tok);

req = jsonrpc_request_start(cmd->plugin, cmd, "txsend",
finish, tx_abort, fr);
finish, channel_abort, fr);
json_add_string(req->js, "txid",
type_to_string(tmpctx, struct bitcoin_txid, &fr->tx_id));

Expand Down Expand Up @@ -409,6 +429,7 @@ static struct command_result *json_fundchannel(struct command *cmd,
return command_param_failed();

fr->funding_all = streq(fr->funding_str, "all");
fr->error = NULL;

return connect_to_peer(cmd, fr);
}
Expand Down

0 comments on commit 02748ac

Please sign in to comment.