mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-23 06:55:13 +01:00
lightningd/closing_control: remove param_tok().
It's bad form; if we do parsing inside params() then the `check` command is much more effective. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
0236d4e4da
commit
814cde5623
1 changed files with 68 additions and 37 deletions
|
@ -545,13 +545,65 @@ static struct command_result *param_feerate_range(struct command *cmd,
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Only one of these will be set! */
|
||||||
|
struct some_channel {
|
||||||
|
struct channel *channel;
|
||||||
|
struct channel *unsaved_channel;
|
||||||
|
struct uncommitted_channel *uc;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct command_result *param_channel_or_peer(struct command *cmd,
|
||||||
|
const char *name,
|
||||||
|
const char *buffer,
|
||||||
|
const jsmntok_t *tok,
|
||||||
|
struct some_channel **sc)
|
||||||
|
{
|
||||||
|
struct peer *peer = peer_from_json(cmd->ld, buffer, tok);
|
||||||
|
bool more_than_one;
|
||||||
|
|
||||||
|
(*sc)->unsaved_channel = NULL;
|
||||||
|
(*sc)->uc = NULL;
|
||||||
|
|
||||||
|
if (peer) {
|
||||||
|
(*sc)->channel = peer_any_active_channel(peer, &more_than_one);
|
||||||
|
if ((*sc)->channel) {
|
||||||
|
if (more_than_one)
|
||||||
|
goto more_than_one;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
struct command_result *res;
|
||||||
|
res = command_find_channel(cmd, buffer, tok, &(*sc)->channel);
|
||||||
|
if (res)
|
||||||
|
return res;
|
||||||
|
assert((*sc)->channel);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* OK, we have a peer, but no active channels. */
|
||||||
|
(*sc)->uc = peer->uncommitted_channel;
|
||||||
|
if ((*sc)->uc)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
(*sc)->unsaved_channel = peer_any_unsaved_channel(peer, &more_than_one);
|
||||||
|
if ((*sc)->unsaved_channel) {
|
||||||
|
if (more_than_one)
|
||||||
|
goto more_than_one;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return command_fail(cmd, LIGHTNINGD, "Peer has no active channel");
|
||||||
|
|
||||||
|
more_than_one:
|
||||||
|
return command_fail(cmd, LIGHTNINGD,
|
||||||
|
"Peer has multiple channels: use channel_id or short_channel_id");
|
||||||
|
}
|
||||||
|
|
||||||
static struct command_result *json_close(struct command *cmd,
|
static struct command_result *json_close(struct command *cmd,
|
||||||
const char *buffer,
|
const char *buffer,
|
||||||
const jsmntok_t *obj UNNEEDED,
|
const jsmntok_t *obj UNNEEDED,
|
||||||
const jsmntok_t *params)
|
const jsmntok_t *params)
|
||||||
{
|
{
|
||||||
const jsmntok_t *idtok;
|
|
||||||
struct peer *peer;
|
|
||||||
struct channel *channel;
|
struct channel *channel;
|
||||||
unsigned int *timeout;
|
unsigned int *timeout;
|
||||||
const u8 *close_to_script = NULL;
|
const u8 *close_to_script = NULL;
|
||||||
|
@ -563,9 +615,10 @@ static struct command_result *json_close(struct command *cmd,
|
||||||
u32 *feerate_range;
|
u32 *feerate_range;
|
||||||
char* end;
|
char* end;
|
||||||
bool anysegwit;
|
bool anysegwit;
|
||||||
|
struct some_channel *sc = talz(tmpctx, struct some_channel);
|
||||||
|
|
||||||
if (!param(cmd, buffer, params,
|
if (!param(cmd, buffer, params,
|
||||||
p_req("id", param_tok, &idtok),
|
p_req("id", param_channel_or_peer, &sc),
|
||||||
p_opt_def("unilateraltimeout", param_number, &timeout,
|
p_opt_def("unilateraltimeout", param_number, &timeout,
|
||||||
48 * 3600),
|
48 * 3600),
|
||||||
p_opt("destination", param_bitcoin_address, &close_to_script),
|
p_opt("destination", param_bitcoin_address, &close_to_script),
|
||||||
|
@ -578,44 +631,22 @@ static struct command_result *json_close(struct command *cmd,
|
||||||
NULL))
|
NULL))
|
||||||
return command_param_failed();
|
return command_param_failed();
|
||||||
|
|
||||||
peer = peer_from_json(cmd->ld, buffer, idtok);
|
/* Easy cases: peer can simply be forgotten. */
|
||||||
if (peer) {
|
if (sc->uc) {
|
||||||
bool more_than_one;
|
kill_uncommitted_channel(sc->uc, "close command called");
|
||||||
channel = peer_any_active_channel(peer, &more_than_one);
|
|
||||||
if (channel && more_than_one) {
|
|
||||||
return command_fail(cmd, LIGHTNINGD,
|
|
||||||
"Peer has multiple channels: use channel_id or short_channel_id");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
struct command_result *res;
|
|
||||||
res = command_find_channel(cmd, buffer, idtok, &channel);
|
|
||||||
if (res)
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!channel && peer) {
|
|
||||||
bool more_than_one;
|
|
||||||
struct uncommitted_channel *uc = peer->uncommitted_channel;
|
|
||||||
if (uc) {
|
|
||||||
/* Easy case: peer can simply be forgotten. */
|
|
||||||
kill_uncommitted_channel(uc, "close command called");
|
|
||||||
goto discard_unopened;
|
goto discard_unopened;
|
||||||
}
|
}
|
||||||
channel = peer_any_unsaved_channel(peer, &more_than_one);
|
|
||||||
if (channel) {
|
if (sc->unsaved_channel) {
|
||||||
if (more_than_one) {
|
channel_unsaved_close_conn(sc->unsaved_channel,
|
||||||
return command_fail(cmd, LIGHTNINGD,
|
|
||||||
"Peer has multiple channels: use channel_id or short_channel_id");
|
|
||||||
}
|
|
||||||
channel_unsaved_close_conn(channel,
|
|
||||||
"close command called");
|
"close command called");
|
||||||
goto discard_unopened;
|
goto discard_unopened;
|
||||||
}
|
}
|
||||||
return command_fail(cmd, LIGHTNINGD,
|
|
||||||
"Peer has no active channel");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!*force_lease_close && channel->opener != LOCAL
|
channel = sc->channel;
|
||||||
|
assert(channel);
|
||||||
|
|
||||||
|
if (!*force_lease_close && sc->channel->opener != LOCAL
|
||||||
&& get_block_height(cmd->ld->topology) < channel->lease_expiry)
|
&& get_block_height(cmd->ld->topology) < channel->lease_expiry)
|
||||||
return command_fail(cmd, LIGHTNINGD,
|
return command_fail(cmd, LIGHTNINGD,
|
||||||
"Peer leased this channel from us, we"
|
"Peer leased this channel from us, we"
|
||||||
|
|
Loading…
Add table
Reference in a new issue