Skip to content

Commit f9ece98

Browse files
committed
lightningd: make channel-query functions all take state.
It has the information we need, now. Signed-off-by: Rusty Russell <[email protected]>
1 parent 2b4c491 commit f9ece98

13 files changed

+57
-57
lines changed

lightningd/channel.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,13 +590,13 @@ const char *channel_state_str(enum channel_state state)
590590
}
591591

592592
struct channel *peer_any_channel(struct peer *peer,
593-
bool (*channel_state_filter)(const struct channel *),
593+
bool (*channel_state_filter)(enum channel_state),
594594
bool *others)
595595
{
596596
struct channel *channel, *ret = NULL;
597597

598598
list_for_each(&peer->channels, channel, list) {
599-
if (channel_state_filter && !channel_state_filter(channel))
599+
if (channel_state_filter && !channel_state_filter(channel->state))
600600
continue;
601601
/* Already found one? */
602602
if (ret) {
@@ -873,7 +873,7 @@ void channel_fail_permanent(struct channel *channel,
873873
/* Drop non-cooperatively (unilateral) to chain. */
874874
drop_to_chain(ld, channel, false);
875875

876-
if (channel_wants_onchain_fail(channel))
876+
if (channel_state_wants_onchain_fail(channel->state))
877877
channel_set_state(channel,
878878
channel->state,
879879
AWAITING_UNILATERAL,
@@ -969,7 +969,7 @@ void channel_internal_error(struct channel *channel, const char *fmt, ...)
969969
channel_cleanup_commands(channel, why);
970970

971971
/* Nothing ventured, nothing lost! */
972-
if (channel_state_uncommitted(channel)) {
972+
if (channel_state_uncommitted(channel->state)) {
973973
channel_set_owner(channel, NULL);
974974
delete_channel(channel);
975975
tal_free(why);

lightningd/channel.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,9 @@ const char *channel_state_name(const struct channel *channel);
403403
const char *channel_state_str(enum channel_state state);
404404

405405
/* Can this channel send an HTLC? */
406-
static inline bool channel_can_add_htlc(const struct channel *channel)
406+
static inline bool channel_state_can_add_htlc(enum channel_state state)
407407
{
408-
switch (channel->state) {
408+
switch (state) {
409409
case CHANNELD_AWAITING_LOCKIN:
410410
case CHANNELD_SHUTTING_DOWN:
411411
case CLOSINGD_SIGEXCHANGE:
@@ -426,9 +426,9 @@ static inline bool channel_can_add_htlc(const struct channel *channel)
426426
}
427427

428428
/* Can this channel remove an HTLC? */
429-
static inline bool channel_can_remove_htlc(const struct channel *channel)
429+
static inline bool channel_state_can_remove_htlc(enum channel_state state)
430430
{
431-
switch (channel->state) {
431+
switch (state) {
432432
case CHANNELD_AWAITING_LOCKIN:
433433
case CLOSINGD_SIGEXCHANGE:
434434
case CLOSINGD_COMPLETE:
@@ -559,9 +559,9 @@ static inline bool channel_state_closed(enum channel_state state)
559559
}
560560

561561
/* Established enough, that we could reach out to peer to discuss */
562-
static inline bool channel_wants_peercomms(const struct channel *channel)
562+
static inline bool channel_state_wants_peercomms(enum channel_state state)
563563
{
564-
switch (channel->state) {
564+
switch (state) {
565565
case CHANNELD_AWAITING_LOCKIN:
566566
case DUALOPEND_AWAITING_LOCKIN:
567567
case DUALOPEND_OPEN_COMMITTED:
@@ -582,9 +582,9 @@ static inline bool channel_wants_peercomms(const struct channel *channel)
582582
}
583583

584584
/* Not even int the database yet? */
585-
static inline bool channel_state_uncommitted(const struct channel *channel)
585+
static inline bool channel_state_uncommitted(enum channel_state state)
586586
{
587-
switch (channel->state) {
587+
switch (state) {
588588
case DUALOPEND_OPEN_INIT:
589589
return true;
590590
case DUALOPEND_OPEN_COMMITTED:
@@ -605,9 +605,9 @@ static inline bool channel_state_uncommitted(const struct channel *channel)
605605
}
606606

607607
/* Established enough, that we have to fail onto chain */
608-
static inline bool channel_wants_onchain_fail(const struct channel *channel)
608+
static inline bool channel_state_wants_onchain_fail(enum channel_state state)
609609
{
610-
switch (channel->state) {
610+
switch (state) {
611611
case CHANNELD_AWAITING_LOCKIN:
612612
case DUALOPEND_OPEN_COMMITTED:
613613
case DUALOPEND_AWAITING_LOCKIN:
@@ -658,7 +658,7 @@ const char *channel_change_state_reason_str(enum state_change reason);
658658
/* Find a channel which is passes filter, if any: sets *others if there
659659
* is more than one. */
660660
struct channel *peer_any_channel(struct peer *peer,
661-
bool (*channel_state_filter)(const struct channel *),
661+
bool (*channel_state_filter)(enum channel_state),
662662
bool *others);
663663

664664
struct channel *channel_by_dbid(struct lightningd *ld, const u64 dbid);

lightningd/channel_control.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ void channel_notify_new_block(struct lightningd *ld,
15871587
peer;
15881588
peer = peer_node_id_map_next(ld->peers, &it)) {
15891589
list_for_each(&peer->channels, channel, list) {
1590-
if (channel_state_uncommitted(channel))
1590+
if (channel_state_uncommitted(channel->state))
15911591
continue;
15921592
if (is_fundee_should_forget(ld, channel, block_height)) {
15931593
tal_arr_expand(&to_forget, channel);
@@ -2013,7 +2013,7 @@ static struct command_result *json_dev_feerate(struct command *cmd,
20132013
if (!peer)
20142014
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
20152015

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

20752075
/* FIXME: If this becomes a real API, check for OPT_QUIESCE! */
2076-
channel = peer_any_channel(peer, channel_wants_peercomms, &more_than_one);
2076+
channel = peer_any_channel(peer, channel_state_wants_peercomms, &more_than_one);
20772077
if (!channel || !channel->owner)
20782078
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
20792079
/* This is a dev command: fix the api if you need this! */

lightningd/closing_control.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,9 @@ struct some_channel {
562562
struct uncommitted_channel *uc;
563563
};
564564

565-
static bool channel_state_can_close(const struct channel *channel)
565+
static bool channel_state_can_close(enum channel_state state)
566566
{
567-
switch (channel->state) {
567+
switch (state) {
568568
case CHANNELD_NORMAL:
569569
case CHANNELD_AWAITING_SPLICE:
570570
case CHANNELD_AWAITING_LOCKIN:
@@ -609,7 +609,7 @@ static struct command_result *param_channel_or_peer(struct command *cmd,
609609
if (res)
610610
return res;
611611
assert((*sc)->channel);
612-
if (!channel_state_can_close((*sc)->channel))
612+
if (!channel_state_can_close((*sc)->channel->state))
613613
return command_fail_badparam(cmd, name, buffer, tok,
614614
tal_fmt(tmpctx, "Channel in state %s",
615615
channel_state_name((*sc)->channel)));

lightningd/connect_control.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ static void try_connect(const tal_t *ctx,
355355
if (peer) {
356356
struct channel *channel;
357357
list_for_each(&peer->channels, channel, list) {
358-
if (!channel_wants_peercomms(channel))
358+
if (!channel_state_wants_peercomms(channel->state))
359359
continue;
360360
channel_set_billboard(channel, false,
361361
tal_fmt(tmpctx,
@@ -426,7 +426,7 @@ static void connect_failed(struct lightningd *ld,
426426

427427
/* If we have an active channel, then reconnect. */
428428
peer = peer_by_id(ld, id);
429-
if (peer && peer_any_channel(peer, channel_wants_peercomms, NULL)) {
429+
if (peer && peer_any_channel(peer, channel_state_wants_peercomms, NULL)) {
430430
try_reconnect(peer, peer, addrhint);
431431
} else
432432
log_peer_debug(ld->log, id, "Not reconnecting: %s",

lightningd/dual_open_control.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static void channel_disconnect(struct channel *channel,
5858
void channel_unsaved_close_conn(struct channel *channel, const char *why)
5959
{
6060
/* Gotta be unsaved */
61-
assert(channel_state_uncommitted(channel));
61+
assert(channel_state_uncommitted(channel->state));
6262
log_info(channel->log, "Unsaved peer failed."
6363
" Disconnecting and deleting channel. Reason: %s",
6464
why);
@@ -77,7 +77,7 @@ static void channel_saved_err_broken_reconn(struct channel *channel,
7777
const char *errmsg;
7878

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

8282
va_start(ap, fmt);
8383
errmsg = tal_vfmt(tmpctx, fmt, ap);
@@ -97,7 +97,7 @@ static void channel_err_broken(struct channel *channel,
9797
errmsg = tal_vfmt(tmpctx, fmt, ap);
9898
va_end(ap);
9999

100-
if (channel_state_uncommitted(channel)) {
100+
if (channel_state_uncommitted(channel->state)) {
101101
log_broken(channel->log, "%s", errmsg);
102102
channel_unsaved_close_conn(channel, errmsg);
103103
} else
@@ -1220,7 +1220,7 @@ wallet_commit_channel(struct lightningd *ld,
12201220
{
12211221
struct amount_msat our_msat, lease_fee_msat;
12221222
struct channel_inflight *inflight;
1223-
bool any_active = peer_any_channel(channel->peer, channel_wants_peercomms, NULL);
1223+
bool any_active = peer_any_channel(channel->peer, channel_state_wants_peercomms, NULL);
12241224

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

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

3625-
if (channel_state_uncommitted(channel)) {
3625+
if (channel_state_uncommitted(channel->state)) {
36263626
log_info(channel->log, "%s", "Unsaved peer failed."
36273627
" Deleting channel.");
36283628
delete_channel(channel);
@@ -3786,7 +3786,7 @@ bool peer_restart_dualopend(struct peer *peer,
37863786
u32 *local_shutdown_script_wallet_index;
37873787
u8 *msg;
37883788

3789-
if (channel_state_uncommitted(channel))
3789+
if (channel_state_uncommitted(channel->state))
37903790
return peer_start_dualopend(peer, peer_fd, channel);
37913791

37923792
hsmfd = hsm_get_client_fd(peer->ld, &peer->id, channel->dbid,

lightningd/opening_control.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ wallet_commit_channel(struct lightningd *ld,
112112
u32 lease_start_blockheight = 0; /* No leases on v1 */
113113
struct short_channel_id *alias_local;
114114
struct timeabs timestamp;
115-
bool any_active = peer_any_channel(uc->peer, channel_wants_peercomms, NULL);
115+
bool any_active = peer_any_channel(uc->peer, channel_state_wants_peercomms, NULL);
116116

117117
/* We cannot both be the fundee *and* have a `fundchannel_start`
118118
* command running!

lightningd/pay.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -847,20 +847,20 @@ find_channel_for_htlc_add(struct lightningd *ld, const struct node_id *node,
847847
return NULL;
848848

849849
channel = find_channel_by_scid(peer, scid_or_alias);
850-
if (channel && channel_can_add_htlc(channel)) {
850+
if (channel && channel_state_can_add_htlc(channel->state)) {
851851
goto found;
852852
}
853853

854854
channel = find_channel_by_alias(peer, scid_or_alias, LOCAL);
855-
if (channel && channel_can_add_htlc(channel)) {
855+
if (channel && channel_state_can_add_htlc(channel->state)) {
856856
goto found;
857857
}
858858

859859
/* We used to ignore scid: now all-zero means "any" */
860860
if (!channel && (ld->deprecated_apis ||
861861
memeqzero(scid_or_alias, sizeof(*scid_or_alias)))) {
862862
list_for_each(&peer->channels, channel, list) {
863-
if (channel_can_add_htlc(channel) &&
863+
if (channel_state_can_add_htlc(channel->state) &&
864864
amount_msat_greater(channel->our_msat, *amount)) {
865865
goto found;
866866
}

lightningd/peer_control.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ static void peer_channels_cleanup(struct lightningd *ld,
178178

179179
for (size_t i = 0; i < tal_count(channels); i++) {
180180
c = channels[i];
181-
if (channel_wants_peercomms(c)) {
181+
if (channel_state_wants_peercomms(c->state)) {
182182
channel_cleanup_commands(c, "Disconnected");
183183
channel_fail_transient(c, "Disconnected");
184-
} else if (channel_state_uncommitted(c)) {
184+
} else if (channel_state_uncommitted(c->state)) {
185185
channel_unsaved_close_conn(c, "Disconnected");
186186
}
187187
}
@@ -401,7 +401,7 @@ void channel_errmsg(struct channel *channel,
401401
/* Clean up any in-progress open attempts */
402402
channel_cleanup_commands(channel, desc);
403403

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

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

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

21632163
list_for_each(&p->channels, channel, list) {
2164-
if (channel_state_uncommitted(channel))
2164+
if (channel_state_uncommitted(channel->state))
21652165
json_add_unsaved_channel(response, channel, NULL);
21662166
else
21672167
json_add_channel(ld, response, NULL, channel, NULL);
@@ -2280,7 +2280,7 @@ static void json_add_peerchannels(struct lightningd *ld,
22802280

22812281
json_add_uncommitted_channel(response, peer->uncommitted_channel, peer);
22822282
list_for_each(&peer->channels, channel, list) {
2283-
if (channel_state_uncommitted(channel))
2283+
if (channel_state_uncommitted(channel->state))
22842284
json_add_unsaved_channel(response, channel, peer);
22852285
else
22862286
json_add_channel(ld, response, NULL, channel, peer);
@@ -2350,7 +2350,7 @@ command_find_channel(struct command *cmd,
23502350
peer;
23512351
peer = peer_node_id_map_next(ld->peers, &it)) {
23522352
list_for_each(&peer->channels, (*channel), list) {
2353-
if (!channel_wants_peercomms(*channel))
2353+
if (!channel_state_wants_peercomms((*channel)->state))
23542354
continue;
23552355
if (channel_id_eq(&(*channel)->cid, &cid))
23562356
return NULL;
@@ -2378,7 +2378,7 @@ static void setup_peer(struct peer *peer, u32 delay)
23782378
bool connect = false;
23792379

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

23942394
channel_watch_inflight(ld, channel, inflight);
23952395
}
2396-
if (channel_wants_peercomms(channel))
2396+
if (channel_state_wants_peercomms(channel->state))
23972397
connect = true;
23982398
}
23992399

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

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

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

3302-
if (!channel_state_uncommitted(forget->channel))
3302+
if (!channel_state_uncommitted(forget->channel->state))
33033303
bitcoind_getutxout(cmd->ld->topology->bitcoind,
33043304
&forget->channel->funding,
33053305
process_dev_forget_channel, forget);

0 commit comments

Comments
 (0)