Skip to content

Commit

Permalink
lightningd: make channel-query functions all take state.
Browse files Browse the repository at this point in the history
It has the information we need, now.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Sep 14, 2023
1 parent 39a80f7 commit ccb877d
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 57 deletions.
8 changes: 4 additions & 4 deletions lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,13 @@ const char *channel_state_str(enum channel_state state)
}

struct channel *peer_any_channel(struct peer *peer,
bool (*channel_state_filter)(const struct channel *),
bool (*channel_state_filter)(enum channel_state),
bool *others)
{
struct channel *channel, *ret = NULL;

list_for_each(&peer->channels, channel, list) {
if (channel_state_filter && !channel_state_filter(channel))
if (channel_state_filter && !channel_state_filter(channel->state))
continue;
/* Already found one? */
if (ret) {
Expand Down Expand Up @@ -873,7 +873,7 @@ void channel_fail_permanent(struct channel *channel,
/* Drop non-cooperatively (unilateral) to chain. */
drop_to_chain(ld, channel, false);

if (channel_wants_onchain_fail(channel))
if (channel_state_wants_onchain_fail(channel->state))
channel_set_state(channel,
channel->state,
AWAITING_UNILATERAL,
Expand Down Expand Up @@ -969,7 +969,7 @@ void channel_internal_error(struct channel *channel, const char *fmt, ...)
channel_cleanup_commands(channel, why);

/* Nothing ventured, nothing lost! */
if (channel_state_uncommitted(channel)) {
if (channel_state_uncommitted(channel->state)) {
channel_set_owner(channel, NULL);
delete_channel(channel);
tal_free(why);
Expand Down
22 changes: 11 additions & 11 deletions lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,9 @@ const char *channel_state_name(const struct channel *channel);
const char *channel_state_str(enum channel_state state);

/* Can this channel send an HTLC? */
static inline bool channel_can_add_htlc(const struct channel *channel)
static inline bool channel_state_can_add_htlc(enum channel_state state)
{
switch (channel->state) {
switch (state) {
case CHANNELD_AWAITING_LOCKIN:
case CHANNELD_SHUTTING_DOWN:
case CLOSINGD_SIGEXCHANGE:
Expand All @@ -426,9 +426,9 @@ static inline bool channel_can_add_htlc(const struct channel *channel)
}

/* Can this channel remove an HTLC? */
static inline bool channel_can_remove_htlc(const struct channel *channel)
static inline bool channel_state_can_remove_htlc(enum channel_state state)
{
switch (channel->state) {
switch (state) {
case CHANNELD_AWAITING_LOCKIN:
case CLOSINGD_SIGEXCHANGE:
case CLOSINGD_COMPLETE:
Expand Down Expand Up @@ -559,9 +559,9 @@ static inline bool channel_state_closed(enum channel_state state)
}

/* Established enough, that we could reach out to peer to discuss */
static inline bool channel_wants_peercomms(const struct channel *channel)
static inline bool channel_state_wants_peercomms(enum channel_state state)
{
switch (channel->state) {
switch (state) {
case CHANNELD_AWAITING_LOCKIN:
case DUALOPEND_AWAITING_LOCKIN:
case DUALOPEND_OPEN_COMMITTED:
Expand All @@ -582,9 +582,9 @@ static inline bool channel_wants_peercomms(const struct channel *channel)
}

/* Not even int the database yet? */
static inline bool channel_state_uncommitted(const struct channel *channel)
static inline bool channel_state_uncommitted(enum channel_state state)
{
switch (channel->state) {
switch (state) {
case DUALOPEND_OPEN_INIT:
return true;
case DUALOPEND_OPEN_COMMITTED:
Expand All @@ -605,9 +605,9 @@ static inline bool channel_state_uncommitted(const struct channel *channel)
}

/* Established enough, that we have to fail onto chain */
static inline bool channel_wants_onchain_fail(const struct channel *channel)
static inline bool channel_state_wants_onchain_fail(enum channel_state state)
{
switch (channel->state) {
switch (state) {
case CHANNELD_AWAITING_LOCKIN:
case DUALOPEND_OPEN_COMMITTED:
case DUALOPEND_AWAITING_LOCKIN:
Expand Down Expand Up @@ -658,7 +658,7 @@ const char *channel_change_state_reason_str(enum state_change reason);
/* Find a channel which is passes filter, if any: sets *others if there
* is more than one. */
struct channel *peer_any_channel(struct peer *peer,
bool (*channel_state_filter)(const struct channel *),
bool (*channel_state_filter)(enum channel_state),
bool *others);

struct channel *channel_by_dbid(struct lightningd *ld, const u64 dbid);
Expand Down
6 changes: 3 additions & 3 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ void channel_notify_new_block(struct lightningd *ld,
peer;
peer = peer_node_id_map_next(ld->peers, &it)) {
list_for_each(&peer->channels, channel, list) {
if (channel_state_uncommitted(channel))
if (channel_state_uncommitted(channel->state))
continue;
if (is_fundee_should_forget(ld, channel, block_height)) {
tal_arr_expand(&to_forget, channel);
Expand Down Expand Up @@ -2013,7 +2013,7 @@ static struct command_result *json_dev_feerate(struct command *cmd,
if (!peer)
return command_fail(cmd, LIGHTNINGD, "Peer not connected");

channel = peer_any_channel(peer, channel_can_add_htlc, &more_than_one);
channel = peer_any_channel(peer, channel_state_can_add_htlc, &more_than_one);
if (!channel || !channel->owner)
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
/* This is a dev command: fix the api if you need this! */
Expand Down Expand Up @@ -2073,7 +2073,7 @@ static struct command_result *json_dev_quiesce(struct command *cmd,
return command_fail(cmd, LIGHTNINGD, "Peer not connected");

/* FIXME: If this becomes a real API, check for OPT_QUIESCE! */
channel = peer_any_channel(peer, channel_wants_peercomms, &more_than_one);
channel = peer_any_channel(peer, channel_state_wants_peercomms, &more_than_one);
if (!channel || !channel->owner)
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
/* This is a dev command: fix the api if you need this! */
Expand Down
6 changes: 3 additions & 3 deletions lightningd/closing_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,9 @@ struct some_channel {
struct uncommitted_channel *uc;
};

static bool channel_state_can_close(const struct channel *channel)
static bool channel_state_can_close(enum channel_state state)
{
switch (channel->state) {
switch (state) {
case CHANNELD_NORMAL:
case CHANNELD_AWAITING_SPLICE:
case CHANNELD_AWAITING_LOCKIN:
Expand Down Expand Up @@ -609,7 +609,7 @@ static struct command_result *param_channel_or_peer(struct command *cmd,
if (res)
return res;
assert((*sc)->channel);
if (!channel_state_can_close((*sc)->channel))
if (!channel_state_can_close((*sc)->channel->state))
return command_fail_badparam(cmd, name, buffer, tok,
tal_fmt(tmpctx, "Channel in state %s",
channel_state_name((*sc)->channel)));
Expand Down
4 changes: 2 additions & 2 deletions lightningd/connect_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ static void try_connect(const tal_t *ctx,
if (peer) {
struct channel *channel;
list_for_each(&peer->channels, channel, list) {
if (!channel_wants_peercomms(channel))
if (!channel_state_wants_peercomms(channel->state))
continue;
channel_set_billboard(channel, false,
tal_fmt(tmpctx,
Expand Down Expand Up @@ -426,7 +426,7 @@ static void connect_failed(struct lightningd *ld,

/* If we have an active channel, then reconnect. */
peer = peer_by_id(ld, id);
if (peer && peer_any_channel(peer, channel_wants_peercomms, NULL)) {
if (peer && peer_any_channel(peer, channel_state_wants_peercomms, NULL)) {
try_reconnect(peer, peer, addrhint);
} else
log_peer_debug(ld->log, id, "Not reconnecting: %s",
Expand Down
14 changes: 7 additions & 7 deletions lightningd/dual_open_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static void channel_disconnect(struct channel *channel,
void channel_unsaved_close_conn(struct channel *channel, const char *why)
{
/* Gotta be unsaved */
assert(channel_state_uncommitted(channel));
assert(channel_state_uncommitted(channel->state));
log_info(channel->log, "Unsaved peer failed."
" Disconnecting and deleting channel. Reason: %s",
why);
Expand All @@ -77,7 +77,7 @@ static void channel_saved_err_broken_reconn(struct channel *channel,
const char *errmsg;

/* We only reconnect to 'saved' channel peers */
assert(!channel_state_uncommitted(channel));
assert(!channel_state_uncommitted(channel->state));

va_start(ap, fmt);
errmsg = tal_vfmt(tmpctx, fmt, ap);
Expand All @@ -97,7 +97,7 @@ static void channel_err_broken(struct channel *channel,
errmsg = tal_vfmt(tmpctx, fmt, ap);
va_end(ap);

if (channel_state_uncommitted(channel)) {
if (channel_state_uncommitted(channel->state)) {
log_broken(channel->log, "%s", errmsg);
channel_unsaved_close_conn(channel, errmsg);
} else
Expand Down Expand Up @@ -1220,7 +1220,7 @@ wallet_commit_channel(struct lightningd *ld,
{
struct amount_msat our_msat, lease_fee_msat;
struct channel_inflight *inflight;
bool any_active = peer_any_channel(channel->peer, channel_wants_peercomms, NULL);
bool any_active = peer_any_channel(channel->peer, channel_state_wants_peercomms, NULL);

if (!amount_sat_to_msat(&our_msat, our_funding)) {
log_broken(channel->log, "Unable to convert funds");
Expand Down Expand Up @@ -1390,7 +1390,7 @@ static void handle_peer_wants_to_close(struct subd *dualopend,
OPT_ANCHORS_ZERO_FEE_HTLC_TX);

/* We shouldn't get this message while we're waiting to finish */
if (channel_state_uncommitted(channel)) {
if (channel_state_uncommitted(channel->state)) {
log_broken(dualopend->ld->log, "Channel in wrong state for"
" shutdown, still has uncommitted"
" channel pending.");
Expand Down Expand Up @@ -3622,7 +3622,7 @@ void static dualopen_errmsg(struct channel *channel,
/* Clean up any in-progress open attempts */
channel_cleanup_commands(channel, desc);

if (channel_state_uncommitted(channel)) {
if (channel_state_uncommitted(channel->state)) {
log_info(channel->log, "%s", "Unsaved peer failed."
" Deleting channel.");
delete_channel(channel);
Expand Down Expand Up @@ -3786,7 +3786,7 @@ bool peer_restart_dualopend(struct peer *peer,
u32 *local_shutdown_script_wallet_index;
u8 *msg;

if (channel_state_uncommitted(channel))
if (channel_state_uncommitted(channel->state))
return peer_start_dualopend(peer, peer_fd, channel);

hsmfd = hsm_get_client_fd(peer->ld, &peer->id, channel->dbid,
Expand Down
2 changes: 1 addition & 1 deletion lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ wallet_commit_channel(struct lightningd *ld,
u32 lease_start_blockheight = 0; /* No leases on v1 */
struct short_channel_id *alias_local;
struct timeabs timestamp;
bool any_active = peer_any_channel(uc->peer, channel_wants_peercomms, NULL);
bool any_active = peer_any_channel(uc->peer, channel_state_wants_peercomms, NULL);

/* We cannot both be the fundee *and* have a `fundchannel_start`
* command running!
Expand Down
6 changes: 3 additions & 3 deletions lightningd/pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -847,20 +847,20 @@ find_channel_for_htlc_add(struct lightningd *ld, const struct node_id *node,
return NULL;

channel = find_channel_by_scid(peer, scid_or_alias);
if (channel && channel_can_add_htlc(channel)) {
if (channel && channel_state_can_add_htlc(channel->state)) {
goto found;
}

channel = find_channel_by_alias(peer, scid_or_alias, LOCAL);
if (channel && channel_can_add_htlc(channel)) {
if (channel && channel_state_can_add_htlc(channel->state)) {
goto found;
}

/* We used to ignore scid: now all-zero means "any" */
if (!channel && (ld->deprecated_apis ||
memeqzero(scid_or_alias, sizeof(*scid_or_alias)))) {
list_for_each(&peer->channels, channel, list) {
if (channel_can_add_htlc(channel) &&
if (channel_state_can_add_htlc(channel->state) &&
amount_msat_greater(channel->our_msat, *amount)) {
goto found;
}
Expand Down
26 changes: 13 additions & 13 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ static void peer_channels_cleanup(struct lightningd *ld,

for (size_t i = 0; i < tal_count(channels); i++) {
c = channels[i];
if (channel_wants_peercomms(c)) {
if (channel_state_wants_peercomms(c->state)) {
channel_cleanup_commands(c, "Disconnected");
channel_fail_transient(c, "Disconnected");
} else if (channel_state_uncommitted(c)) {
} else if (channel_state_uncommitted(c->state)) {
channel_unsaved_close_conn(c, "Disconnected");
}
}
Expand Down Expand Up @@ -401,7 +401,7 @@ void channel_errmsg(struct channel *channel,
/* Clean up any in-progress open attempts */
channel_cleanup_commands(channel, desc);

if (channel_state_uncommitted(channel)) {
if (channel_state_uncommitted(channel->state)) {
log_info(channel->log, "%s", "Unsaved peer failed."
" Deleting channel.");
delete_channel(channel);
Expand Down Expand Up @@ -1295,7 +1295,7 @@ static void peer_connected_hook_final(struct peer_connected_hook_payload *payloa
/* connect appropriate subds for all (active) channels! */
list_for_each(&peer->channels, channel, list) {
/* FIXME: It can race by opening a channel before this! */
if (channel_wants_peercomms(channel) && !channel->owner) {
if (channel_state_wants_peercomms(channel->state) && !channel->owner) {
log_debug(channel->log, "Peer has reconnected, state %s: connecting subd",
channel_state_name(channel));

Expand Down Expand Up @@ -1545,7 +1545,7 @@ void peer_spoke(struct lightningd *ld, const u8 *msg)

/* If channel is active, we raced, so ignore this:
* subd will get it soon. */
if (channel_wants_peercomms(channel)) {
if (channel_state_wants_peercomms(channel->state)) {
log_debug(channel->log,
"channel already active");
if (!channel->owner &&
Expand Down Expand Up @@ -2161,7 +2161,7 @@ static void json_add_peer(struct lightningd *ld,
json_add_uncommitted_channel(response, p->uncommitted_channel, NULL);

list_for_each(&p->channels, channel, list) {
if (channel_state_uncommitted(channel))
if (channel_state_uncommitted(channel->state))
json_add_unsaved_channel(response, channel, NULL);
else
json_add_channel(ld, response, NULL, channel, NULL);
Expand Down Expand Up @@ -2280,7 +2280,7 @@ static void json_add_peerchannels(struct lightningd *ld,

json_add_uncommitted_channel(response, peer->uncommitted_channel, peer);
list_for_each(&peer->channels, channel, list) {
if (channel_state_uncommitted(channel))
if (channel_state_uncommitted(channel->state))
json_add_unsaved_channel(response, channel, peer);
else
json_add_channel(ld, response, NULL, channel, peer);
Expand Down Expand Up @@ -2350,7 +2350,7 @@ command_find_channel(struct command *cmd,
peer;
peer = peer_node_id_map_next(ld->peers, &it)) {
list_for_each(&peer->channels, (*channel), list) {
if (!channel_wants_peercomms(*channel))
if (!channel_state_wants_peercomms((*channel)->state))
continue;
if (channel_id_eq(&(*channel)->cid, &cid))
return NULL;
Expand Down Expand Up @@ -2378,7 +2378,7 @@ static void setup_peer(struct peer *peer, u32 delay)
bool connect = false;

list_for_each(&peer->channels, channel, list) {
if (channel_state_uncommitted(channel))
if (channel_state_uncommitted(channel->state))
continue;
/* Watching lockin may be unnecessary, but it's harmless. */
channel_watch_funding(ld, channel);
Expand All @@ -2393,7 +2393,7 @@ static void setup_peer(struct peer *peer, u32 delay)

channel_watch_inflight(ld, channel, inflight);
}
if (channel_wants_peercomms(channel))
if (channel_state_wants_peercomms(channel->state))
connect = true;
}

Expand Down Expand Up @@ -2515,7 +2515,7 @@ static struct command_result *json_disconnect(struct command *cmd,
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
}

channel = peer_any_channel(peer, channel_wants_peercomms, NULL);
channel = peer_any_channel(peer, channel_state_wants_peercomms, NULL);
if (channel && !*force) {
return command_fail(cmd, LIGHTNINGD,
"Peer has (at least one) channel in state %s",
Expand Down Expand Up @@ -3072,7 +3072,7 @@ static struct command_result *param_dev_channel(struct command *cmd,
if (res)
return res;

*channel = peer_any_channel(peer, channel_wants_peercomms, &more_than_one);
*channel = peer_any_channel(peer, channel_state_wants_peercomms, &more_than_one);
if (!*channel)
return command_fail_badparam(cmd, name, buffer, tok,
"No channel with that peer");
Expand Down Expand Up @@ -3299,7 +3299,7 @@ static struct command_result *json_dev_forget_channel(struct command *cmd,
"or `dev-fail` instead.");
}

if (!channel_state_uncommitted(forget->channel))
if (!channel_state_uncommitted(forget->channel->state))
bitcoind_getutxout(cmd->ld->topology->bitcoind,
&forget->channel->funding,
process_dev_forget_channel, forget);
Expand Down
Loading

0 comments on commit ccb877d

Please sign in to comment.