Skip to content

Commit 64af5db

Browse files
committed
lightningd: generalize peer_any_channel to filter on entire channel, not just state.
We're going to use this to ask if there are any channels which make it important to reconnect to the peer. Signed-off-by: Rusty Russell <[email protected]>
1 parent 4ee59e7 commit 64af5db

9 files changed

+75
-28
lines changed

lightningd/channel.c

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -653,28 +653,56 @@ const char *channel_state_str(enum channel_state state)
653653
return "unknown";
654654
}
655655

656-
struct channel *peer_any_channel(struct peer *peer,
657-
bool (*channel_state_filter)(enum channel_state),
658-
bool *others)
656+
#define peer_any_channel(peer, filter, arg, others) \
657+
peer_any_channel_((peer), \
658+
typesafe_cb_preargs(bool, void *, \
659+
(filter), (arg), \
660+
const struct channel *), \
661+
(arg), \
662+
others)
663+
664+
struct channel *peer_any_channel_(struct peer *peer,
665+
bool (*filter)(const struct channel *,
666+
void *arg),
667+
void *arg,
668+
bool *others)
659669
{
660670
struct channel *channel, *ret = NULL;
661671

662672
list_for_each(&peer->channels, channel, list) {
663-
if (channel_state_filter && !channel_state_filter(channel->state))
673+
if (filter && !filter(channel, arg))
664674
continue;
665675
/* Already found one? */
666676
if (ret) {
667-
if (others)
668-
*others = true;
677+
*others = true;
669678
} else {
670679
if (others)
671680
*others = false;
672681
ret = channel;
673682
}
683+
684+
/* Don't keep searching if others is NULL (they don't care). */
685+
if (!others)
686+
break;
674687
}
675688
return ret;
676689
}
677690

691+
static bool filter_by_state(const struct channel *c,
692+
bool (*channel_state_filter)(enum channel_state))
693+
{
694+
return channel_state_filter(c->state);
695+
}
696+
697+
struct channel *peer_any_channel_bystate(struct peer *peer,
698+
bool (*channel_state_filter)(enum channel_state),
699+
bool *others)
700+
{
701+
return peer_any_channel(peer,
702+
filter_by_state, channel_state_filter,
703+
others);
704+
}
705+
678706
struct channel_inflight *channel_inflight_find(struct channel *channel,
679707
const struct bitcoin_txid *txid)
680708
{

lightningd/channel.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -771,9 +771,24 @@ const char *channel_change_state_reason_str(enum state_change reason);
771771

772772
/* Find a channel which is passes filter, if any: sets *others if there
773773
* is more than one. */
774-
struct channel *peer_any_channel(struct peer *peer,
775-
bool (*channel_state_filter)(enum channel_state),
776-
bool *others);
774+
#define peer_any_channel(peer, filter, arg, others) \
775+
peer_any_channel_((peer), \
776+
typesafe_cb_preargs(bool, void *, \
777+
(filter), (arg), \
778+
const struct channel *), \
779+
(arg), \
780+
others)
781+
782+
struct channel *peer_any_channel_(struct peer *peer,
783+
bool (*filter)(const struct channel *,
784+
void *arg),
785+
void *arg,
786+
bool *others);
787+
788+
/* More common version for filtering by state */
789+
struct channel *peer_any_channel_bystate(struct peer *peer,
790+
bool (*channel_state_filter)(enum channel_state),
791+
bool *others);
777792

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

lightningd/channel_control.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,7 +2576,8 @@ static struct command_result *json_dev_feerate(struct command *cmd,
25762576
if (!peer)
25772577
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
25782578

2579-
channel = peer_any_channel(peer, channel_state_can_add_htlc, &more_than_one);
2579+
channel = peer_any_channel_bystate(peer, channel_state_can_add_htlc,
2580+
&more_than_one);
25802581
if (!channel || !channel->owner)
25812582
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
25822583
/* This is a dev command: fix the api if you need this! */
@@ -2628,7 +2629,7 @@ static struct command_result *json_dev_peer_shachain(struct command *cmd,
26282629
if (!peer)
26292630
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
26302631

2631-
channel = peer_any_channel(peer, channel_state_can_add_htlc, &more_than_one);
2632+
channel = peer_any_channel_bystate(peer, channel_state_can_add_htlc, &more_than_one);
26322633
if (!channel || !channel->owner)
26332634
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
26342635
/* This is a dev command: fix the api if you need this! */
@@ -2683,7 +2684,8 @@ static struct command_result *json_dev_quiesce(struct command *cmd,
26832684
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
26842685

26852686
/* FIXME: If this becomes a real API, check for OPT_QUIESCE! */
2686-
channel = peer_any_channel(peer, channel_state_wants_peercomms, &more_than_one);
2687+
channel = peer_any_channel_bystate(peer, channel_state_wants_peercomms,
2688+
&more_than_one);
26872689
if (!channel || !channel->owner)
26882690
return command_fail(cmd, LIGHTNINGD, "Peer bad state");
26892691
/* This is a dev command: fix the api if you need this! */

lightningd/closing_control.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ static struct command_result *param_channel_or_peer(struct command *cmd,
608608
(*sc)->uc = NULL;
609609

610610
if (peer) {
611-
(*sc)->channel = peer_any_channel(peer, channel_state_can_close, &more_than_one);
611+
(*sc)->channel = peer_any_channel_bystate(peer, channel_state_can_close, &more_than_one);
612612
if ((*sc)->channel) {
613613
if (more_than_one)
614614
goto more_than_one;
@@ -632,7 +632,7 @@ static struct command_result *param_channel_or_peer(struct command *cmd,
632632
if ((*sc)->uc)
633633
return NULL;
634634

635-
(*sc)->unsaved_channel = peer_any_channel(peer, channel_state_uncommitted, &more_than_one);
635+
(*sc)->unsaved_channel = peer_any_channel_bystate(peer, channel_state_uncommitted, &more_than_one);
636636
if ((*sc)->unsaved_channel) {
637637
if (more_than_one)
638638
goto more_than_one;

lightningd/connect_control.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static void do_connect(struct delayed_reconnect *d)
291291

292292
/* We consider this transient unless we have a channel */
293293
peer = peer_by_id(d->ld, &d->id);
294-
transient = !peer || !peer_any_channel(peer, channel_state_wants_peercomms, NULL);
294+
transient = !peer || !peer_any_channel_bystate(peer, channel_state_wants_peercomms, NULL);
295295

296296
connectmsg = towire_connectd_connect_to_peer(NULL,
297297
&d->id,
@@ -427,7 +427,7 @@ static void connect_failed(struct lightningd *ld,
427427

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

lightningd/dual_open_control.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ wallet_commit_channel(struct lightningd *ld,
13871387
bool anysegwit = !chainparams->is_elements && feature_negotiated(channel->peer->ld->our_features,
13881388
channel->peer->their_features,
13891389
OPT_SHUTDOWN_ANYSEGWIT);
1390-
bool any_active = peer_any_channel(channel->peer, channel_state_wants_peercomms, NULL);
1390+
bool any_active = peer_any_channel_bystate(channel->peer, channel_state_wants_peercomms, NULL);
13911391

13921392
if (!amount_sat_to_msat(&our_msat, our_funding)) {
13931393
log_broken(channel->log, "Unable to convert funds");

lightningd/opening_control.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ wallet_commit_channel(struct lightningd *ld,
108108
u64 static_remotekey_start;
109109
u32 lease_start_blockheight = 0; /* No leases on v1 */
110110
struct timeabs timestamp;
111-
bool any_active = peer_any_channel(uc->peer, channel_state_wants_peercomms, NULL);
111+
bool any_active = peer_any_channel_bystate(uc->peer, channel_state_wants_peercomms, NULL);
112112
struct channel_stats zero_channel_stats;
113113
enum addrtype addrtype;
114114

lightningd/peer_control.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,9 +1546,9 @@ static const struct wireaddr *best_remote_addr(const tal_t *ctx,
15461546
continue;
15471547
if (peer->remote_addr->type != atype)
15481548
continue;
1549-
daddr.preferred = peer_any_channel(peer,
1550-
channel_state_relationship,
1551-
NULL);
1549+
daddr.preferred = peer_any_channel_bystate(peer,
1550+
channel_state_relationship,
1551+
NULL);
15521552
daddr.addr = *peer->remote_addr;
15531553
daddr.addr.port = ld->config.ip_discovery_port;
15541554
log_debug(ld->log, "best_remote_addr: peer %s gave addr %s (%s)",
@@ -2647,7 +2647,8 @@ static struct command_result *json_disconnect(struct command *cmd,
26472647
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
26482648
}
26492649

2650-
channel = peer_any_channel(peer, channel_state_wants_peercomms, NULL);
2650+
channel = peer_any_channel_bystate(peer, channel_state_wants_peercomms,
2651+
NULL);
26512652
if (channel && !*force) {
26522653
return command_fail(cmd, LIGHTNINGD,
26532654
"Peer has (at least one) channel in state %s",
@@ -3195,7 +3196,8 @@ static struct command_result *param_dev_channel(struct command *cmd,
31953196
if (res)
31963197
return res;
31973198

3198-
*channel = peer_any_channel(peer, channel_state_wants_peercomms, &more_than_one);
3199+
*channel = peer_any_channel_bystate(peer, channel_state_wants_peercomms,
3200+
&more_than_one);
31993201
if (!*channel)
32003202
return command_fail_badparam(cmd, name, buffer, tok,
32013203
"No channel with that peer");

lightningd/test/run-invoice-select-inchan.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -875,11 +875,11 @@ struct command_result *param_u64(struct command *cmd UNNEEDED, const char *name
875875
const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
876876
uint64_t **num UNNEEDED)
877877
{ fprintf(stderr, "param_u64 called!\n"); abort(); }
878-
/* Generated stub for peer_any_channel */
879-
struct channel *peer_any_channel(struct peer *peer UNNEEDED,
880-
bool (*channel_state_filter)(enum channel_state) UNNEEDED,
881-
bool *others UNNEEDED)
882-
{ fprintf(stderr, "peer_any_channel called!\n"); abort(); }
878+
/* Generated stub for peer_any_channel_bystate */
879+
struct channel *peer_any_channel_bystate(struct peer *peer UNNEEDED,
880+
bool (*channel_state_filter)(enum channel_state) UNNEEDED,
881+
bool *others UNNEEDED)
882+
{ fprintf(stderr, "peer_any_channel_bystate called!\n"); abort(); }
883883
/* Generated stub for peer_restart_dualopend */
884884
bool peer_restart_dualopend(struct peer *peer UNNEEDED,
885885
struct peer_fd *peer_fd UNNEEDED,

0 commit comments

Comments
 (0)