Skip to content

Commit 661ffbd

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 3581068 commit 661ffbd

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
/* Not even int the database yet? */
562-
static inline bool channel_state_uncommitted(const struct channel *channel)
562+
static inline bool channel_state_uncommitted(enum channel_state state)
563563
{
564-
switch (channel->state) {
564+
switch (state) {
565565
case DUALOPEND_OPEN_INIT:
566566
return true;
567567
case DUALOPEND_OPEN_COMMITTED:
@@ -582,9 +582,9 @@ static inline bool channel_state_uncommitted(const struct channel *channel)
582582
}
583583

584584
/* Established enough, that we could reach out to peer to discuss */
585-
static inline bool channel_wants_peercomms(const struct channel *channel)
585+
static inline bool channel_state_wants_peercomms(enum channel_state state)
586586
{
587-
switch (channel->state) {
587+
switch (state) {
588588
case CHANNELD_AWAITING_LOCKIN:
589589
case DUALOPEND_AWAITING_LOCKIN:
590590
case DUALOPEND_OPEN_COMMITTED:
@@ -605,9 +605,9 @@ static inline bool channel_wants_peercomms(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:
@@ -659,7 +659,7 @@ const char *channel_change_state_reason_str(enum state_change reason);
659659
/* Find a channel which is passes filter, if any: sets *others if there
660660
* is more than one. */
661661
struct channel *peer_any_channel(struct peer *peer,
662-
bool (*channel_state_filter)(const struct channel *),
662+
bool (*channel_state_filter)(enum channel_state),
663663
bool *others);
664664

665665
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
@@ -1585,7 +1585,7 @@ void channel_notify_new_block(struct lightningd *ld,
15851585
peer;
15861586
peer = peer_node_id_map_next(ld->peers, &it)) {
15871587
list_for_each(&peer->channels, channel, list) {
1588-
if (channel_state_uncommitted(channel))
1588+
if (channel_state_uncommitted(channel->state))
15891589
continue;
15901590
if (is_fundee_should_forget(ld, channel, block_height)) {
15911591
tal_arr_expand(&to_forget, channel);
@@ -2010,7 +2010,7 @@ static struct command_result *json_dev_feerate(struct command *cmd,
20102010
if (!peer)
20112011
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
20122012

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

20732073
/* FIXME: If this becomes a real API, check for OPT_QUIESCE! */
2074-
channel = peer_any_channel(peer, channel_wants_peercomms, &more_than_one);
2074+
channel = peer_any_channel(peer, channel_state_wants_peercomms, &more_than_one);
20752075
if (!channel || !channel->owner)
20762076
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
20772077
/* 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,
@@ -425,7 +425,7 @@ static void connect_failed(struct lightningd *ld,
425425

426426
/* If we have an active channel, then reconnect. */
427427
peer = peer_by_id(ld, id);
428-
if (peer && peer_any_channel(peer, channel_wants_peercomms, NULL)) {
428+
if (peer && peer_any_channel(peer, channel_state_wants_peercomms, NULL)) {
429429
try_reconnect(peer, peer, addrhint);
430430
} else
431431
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.");
@@ -3620,7 +3620,7 @@ static void dualopen_errmsg(struct channel *channel,
36203620
/* Clean up any in-progress open attempts */
36213621
channel_cleanup_commands(channel, desc);
36223622

3623-
if (channel_state_uncommitted(channel)) {
3623+
if (channel_state_uncommitted(channel->state)) {
36243624
log_info(channel->log, "%s", "Unsaved peer failed."
36253625
" Deleting channel.");
36263626
delete_channel(channel);
@@ -3785,7 +3785,7 @@ bool peer_restart_dualopend(struct peer *peer,
37853785
u32 *local_shutdown_script_wallet_index;
37863786
u8 *msg;
37873787

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

37913791
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
@@ -176,10 +176,10 @@ static void peer_channels_cleanup(struct lightningd *ld,
176176

177177
for (size_t i = 0; i < tal_count(channels); i++) {
178178
c = channels[i];
179-
if (channel_wants_peercomms(c)) {
179+
if (channel_state_wants_peercomms(c->state)) {
180180
channel_cleanup_commands(c, "Disconnected");
181181
channel_fail_transient(c, true, "Disconnected");
182-
} else if (channel_state_uncommitted(c)) {
182+
} else if (channel_state_uncommitted(c->state)) {
183183
channel_unsaved_close_conn(c, "Disconnected");
184184
}
185185
}
@@ -398,7 +398,7 @@ void channel_errmsg(struct channel *channel,
398398
/* Clean up any in-progress open attempts */
399399
channel_cleanup_commands(channel, desc);
400400

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

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

15491549
/* If channel is active, we raced, so ignore this:
15501550
* subd will get it soon. */
1551-
if (channel_wants_peercomms(channel)) {
1551+
if (channel_state_wants_peercomms(channel->state)) {
15521552
log_debug(channel->log,
15531553
"channel already active");
15541554
if (!channel->owner &&
@@ -2164,7 +2164,7 @@ static void json_add_peer(struct lightningd *ld,
21642164
json_add_uncommitted_channel(response, p->uncommitted_channel, NULL);
21652165

21662166
list_for_each(&p->channels, channel, list) {
2167-
if (channel_state_uncommitted(channel))
2167+
if (channel_state_uncommitted(channel->state))
21682168
json_add_unsaved_channel(response, channel, NULL);
21692169
else
21702170
json_add_channel(ld, response, NULL, channel, NULL);
@@ -2283,7 +2283,7 @@ static void json_add_peerchannels(struct lightningd *ld,
22832283

22842284
json_add_uncommitted_channel(response, peer->uncommitted_channel, peer);
22852285
list_for_each(&peer->channels, channel, list) {
2286-
if (channel_state_uncommitted(channel))
2286+
if (channel_state_uncommitted(channel->state))
22872287
json_add_unsaved_channel(response, channel, peer);
22882288
else
22892289
json_add_channel(ld, response, NULL, channel, peer);
@@ -2353,7 +2353,7 @@ command_find_channel(struct command *cmd,
23532353
peer;
23542354
peer = peer_node_id_map_next(ld->peers, &it)) {
23552355
list_for_each(&peer->channels, (*channel), list) {
2356-
if (!channel_wants_peercomms(*channel))
2356+
if (!channel_state_wants_peercomms((*channel)->state))
23572357
continue;
23582358
if (channel_id_eq(&(*channel)->cid, &cid))
23592359
return NULL;
@@ -2381,7 +2381,7 @@ static void setup_peer(struct peer *peer, u32 delay)
23812381
bool connect = false;
23822382

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

23972397
channel_watch_inflight(ld, channel, inflight);
23982398
}
2399-
if (channel_wants_peercomms(channel))
2399+
if (channel_state_wants_peercomms(channel->state))
24002400
connect = true;
24012401
}
24022402

@@ -2518,7 +2518,7 @@ static struct command_result *json_disconnect(struct command *cmd,
25182518
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
25192519
}
25202520

2521-
channel = peer_any_channel(peer, channel_wants_peercomms, NULL);
2521+
channel = peer_any_channel(peer, channel_state_wants_peercomms, NULL);
25222522
if (channel && !*force) {
25232523
return command_fail(cmd, LIGHTNINGD,
25242524
"Peer has (at least one) channel in state %s",
@@ -3074,7 +3074,7 @@ static struct command_result *param_dev_channel(struct command *cmd,
30743074
if (res)
30753075
return res;
30763076

3077-
*channel = peer_any_channel(peer, channel_wants_peercomms, &more_than_one);
3077+
*channel = peer_any_channel(peer, channel_state_wants_peercomms, &more_than_one);
30783078
if (!*channel)
30793079
return command_fail_badparam(cmd, name, buffer, tok,
30803080
"No channel with that peer");
@@ -3304,7 +3304,7 @@ static struct command_result *json_dev_forget_channel(struct command *cmd,
33043304
"or `dev-fail` instead.");
33053305
}
33063306

3307-
if (!channel_state_uncommitted(forget->channel))
3307+
if (!channel_state_uncommitted(forget->channel->state))
33083308
bitcoind_getutxout(cmd->ld->topology->bitcoind,
33093309
&forget->channel->funding,
33103310
process_dev_forget_channel, forget);

0 commit comments

Comments
 (0)