Skip to content

Commit

Permalink
lightningd/opening_control.c: fundchannel_cancel no longer requires…
Browse files Browse the repository at this point in the history
… a `channel_id` argument.

Fixes: #3785

Changelog-Changed: `fundchannel_cancel` no longer requires its undocumented `channel_id` argument after `fundchannel_complete`.
  • Loading branch information
ZmnSCPxj committed Jun 24, 2020
1 parent 93d04d0 commit 2c31b59
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 61 deletions.
54 changes: 22 additions & 32 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,44 +767,34 @@ static void process_check_funding_broadcast(struct bitcoind *bitcoind,
}

struct command_result *cancel_channel_before_broadcast(struct command *cmd,
const char *buffer,
struct peer *peer,
const jsmntok_t *cidtok)
struct peer *peer)
{
struct channel *cancel_channel;
struct channel_to_cancel *cc = tal(cmd, struct channel_to_cancel);
struct channel *channel;

cc->peer = peer->id;
if (!cidtok) {
struct channel *channel;

cancel_channel = NULL;
list_for_each(&peer->channels, channel, list) {
if (cancel_channel) {
return command_fail(cmd, LIGHTNINGD,
"Multiple channels:"
" please specify channel_id");
}
cancel_channel = channel;
}
if (!cancel_channel)
return command_fail(cmd, LIGHTNINGD,
"No channels matching that peer_id");
derive_channel_id(&cc->cid,
&cancel_channel->funding_txid,
cancel_channel->funding_outnum);
} else {
if (!json_tok_channel_id(buffer, cidtok, &cc->cid))
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Invalid channel_id parameter.");

cancel_channel = find_channel_by_id(peer, &cc->cid);
if (!cancel_channel)
return command_fail(cmd, LIGHTNINGD,
"Channel ID not found: '%.*s'",
cidtok->end - cidtok->start,
buffer + cidtok->start);
cancel_channel = NULL;
list_for_each(&peer->channels, channel, list) {
/* After `fundchannel_complete`, channel is in
* `CHANNELD_AWAITING_LOCKIN` state.
*
* TODO: This assumes only one channel at a time
* can be in this state, which is true at the
* time of this writing, but may change *if* we
* ever implement multiple channels per peer.
*/
if (channel->state != CHANNELD_AWAITING_LOCKIN)
continue;
cancel_channel = channel;
break;
}
if (!cancel_channel)
return command_fail(cmd, LIGHTNINGD,
"No channels matching that peer_id");
derive_channel_id(&cc->cid,
&cancel_channel->funding_txid,
cancel_channel->funding_outnum);

if (cancel_channel->opener == REMOTE)
return command_fail(cmd, LIGHTNINGD,
Expand Down
4 changes: 1 addition & 3 deletions lightningd/channel_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ void channel_notify_new_block(struct lightningd *ld,
/* Cancel the channel after `fundchannel_complete` succeeds
* but before funding broadcasts. */
struct command_result *cancel_channel_before_broadcast(struct command *cmd,
const char *buffer,
struct peer *peer,
const jsmntok_t *cidtok);
struct peer *peer);

/* Forget a channel. Deletes the channel and handles all
* associated waiting commands, if present. Notifies peer if available */
Expand Down
29 changes: 3 additions & 26 deletions lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,29 +308,7 @@ static void
cancel_after_fundchannel_complete_success(struct command *cmd,
struct channel *channel)
{
struct peer *peer;
struct channel_id cid;
/* Fake these to adapt to the existing
* cancel_channel_before_broadcast
* interface.
*/
const char *buffer;
jsmntok_t cidtok;

peer = channel->peer;
derive_channel_id(&cid,
&channel->funding_txid, channel->funding_outnum);

buffer = type_to_string(tmpctx, struct channel_id, &cid);
cidtok.type = JSMN_STRING;
cidtok.start = 0;
cidtok.end = strlen(buffer);
cidtok.size = 0;

was_pending(cancel_channel_before_broadcast(cmd,
buffer,
peer,
&cidtok));
was_pending(cancel_channel_before_broadcast(cmd, channel->peer));
}

static void funding_success(struct channel *channel)
Expand Down Expand Up @@ -1150,12 +1128,10 @@ static struct command_result *json_fund_channel_cancel(struct command *cmd,

struct node_id *id;
struct peer *peer;
const jsmntok_t *cidtok;
u8 *msg;

if (!param(cmd, buffer, params,
p_req("id", param_node_id, &id),
p_opt("channel_id", param_tok, &cidtok),
NULL))
return command_param_failed();

Expand All @@ -1175,7 +1151,8 @@ static struct command_result *json_fund_channel_cancel(struct command *cmd,
return command_still_pending(cmd);
}

return cancel_channel_before_broadcast(cmd, buffer, peer, cidtok);
/* Handle `fundchannel_cancel` after `fundchannel_complete`. */
return cancel_channel_before_broadcast(cmd, peer);
}

/**
Expand Down

0 comments on commit 2c31b59

Please sign in to comment.