Skip to content

Commit

Permalink
channel: skip unsaved channels
Browse files Browse the repository at this point in the history
Now that "peer->channels" contains `unsaved` channels, skip overthem
where appropriate
  • Loading branch information
niftynei authored and vibhaa committed Mar 24, 2021
1 parent f194b8b commit 820dd45
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 11 deletions.
9 changes: 9 additions & 0 deletions lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,15 @@ struct channel *active_channel_by_id(struct lightningd *ld,
return peer_active_channel(peer);
}

struct channel *unsaved_channel_by_id(struct lightningd *ld,
const struct node_id *id)
{
struct peer *peer = peer_by_id(ld, id);
if (!peer)
return NULL;
return peer_unsaved_channel(peer);
}

struct channel *active_channel_by_scid(struct lightningd *ld,
const struct short_channel_id *scid)
{
Expand Down
4 changes: 4 additions & 0 deletions lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ struct channel *active_channel_by_id(struct lightningd *ld,
const struct node_id *id,
struct uncommitted_channel **uc);

/* Get unsaved channel for peer */
struct channel *unsaved_channel_by_id(struct lightningd *ld,
const struct node_id *id);

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

struct channel *active_channel_by_scid(struct lightningd *ld,
Expand Down
5 changes: 4 additions & 1 deletion lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,10 +714,13 @@ void channel_notify_new_block(struct lightningd *ld,
size_t i;

list_for_each (&ld->peers, peer, list) {
list_for_each (&peer->channels, channel, list)
list_for_each (&peer->channels, channel, list) {
if (channel_unsaved(channel))
continue;
if (is_fundee_should_forget(ld, channel, block_height)) {
tal_arr_expand(&to_forget, channel);
}
}
}

/* Need to forget in a separate loop, else the above
Expand Down
9 changes: 9 additions & 0 deletions lightningd/connect_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <hsmd/capabilities.h>
#include <lightningd/channel.h>
#include <lightningd/connect_control.h>
#include <lightningd/dual_open_control.h>
#include <lightningd/hsm_control.h>
#include <lightningd/json.h>
#include <lightningd/jsonrpc.h>
Expand Down Expand Up @@ -284,6 +285,14 @@ static void peer_please_disconnect(struct lightningd *ld, const u8 *msg)
kill_uncommitted_channel(uc, "Reconnected");
else if (c)
channel_fail_reconnect(c, "Reconnected");
#if EXPERIMENTAL_FEATURES
else {
/* v2 has unsaved channels, not uncommitted_chans */
c = unsaved_channel_by_id(ld, &id);
if (c)
kill_unsaved_channel(c, "Reconnected");
}
#endif /* EXPERIMENTAL_FEATURES */
}

static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fds)
Expand Down
14 changes: 14 additions & 0 deletions lightningd/dual_open_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ unsaved_channel_disconnect(struct channel *channel,
notify_disconnect(channel->peer->ld, &channel->peer->id);
}

void kill_unsaved_channel(struct channel *channel,
const char *why)
{
log_info(channel->log, "Killing dualopend: %s", why);

/* Close dualopend */
if (channel->owner) {
subd_release_channel(channel->owner, channel);
channel->owner = NULL;
}

unsaved_channel_disconnect(channel, LOG_INFORM, why);
tal_free(channel);
}

static struct channel_inflight *
channel_current_inflight(struct channel *channel)
Expand Down
2 changes: 2 additions & 0 deletions lightningd/dual_open_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ void dualopen_tell_depth(struct subd *dualopend,
struct channel *channel,
const struct bitcoin_txid *txid,
u32 depth);
void kill_unsaved_channel(struct channel *channel,
const char *why);
#endif /* LIGHTNING_LIGHTNINGD_DUAL_OPEN_CONTROL_H */
33 changes: 28 additions & 5 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,11 @@ void peer_connected(struct lightningd *ld, const u8 *msg,
assert(!peer->uncommitted_channel);
hook_payload->channel = peer_active_channel(peer);

/* It might be v2 opening, though, since we hang onto these */
if (!hook_payload->channel)
hook_payload->channel = peer_unsaved_channel(peer);

assert(hook_payload->channel);
plugin_hook_call_peer_connected(ld, hook_payload);
}

Expand Down Expand Up @@ -1472,6 +1477,12 @@ static struct command_result *json_close(struct command *cmd,

return command_success(cmd, json_stream_success(cmd));
}
#if EXPERIMENTAL_FEATURES
if ((channel = peer_unsaved_channel(peer))) {
kill_unsaved_channel(channel, "close command called");
return command_success(cmd, json_stream_success(cmd));
}
#endif /* EXPERIMENTAL_FEATURES */
return command_fail(cmd, LIGHTNINGD,
"Peer has no active channel");
}
Expand Down Expand Up @@ -1641,6 +1652,8 @@ static void activate_peer(struct peer *peer, u32 delay)
}

list_for_each(&peer->channels, channel, list) {
if (channel_unsaved(channel))
continue;
/* Watching lockin may be unnecessary, but it's harmless. */
channel_watch_funding(ld, channel);
}
Expand Down Expand Up @@ -1735,6 +1748,13 @@ static struct command_result *json_disconnect(struct command *cmd,
return command_fail(cmd, LIGHTNINGD, "Peer is in state %s",
channel_state_name(channel));
}
#if EXPERIMENTAL_FEATURES
channel = peer_unsaved_channel(peer);
if (channel) {
kill_unsaved_channel(channel, "disconnect command");
return command_success(cmd, json_stream_success(cmd));
}
#endif /* EXPERIMENTAL_FEATURES */
if (!peer->uncommitted_channel) {
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
}
Expand Down Expand Up @@ -1775,7 +1795,9 @@ static struct command_result *json_getinfo(struct command *cmd,
num_peers++;

list_for_each(&peer->channels, channel, list) {
if (channel->state == CHANNELD_AWAITING_LOCKIN) {
if (channel->state == CHANNELD_AWAITING_LOCKIN
|| channel->state == DUALOPEND_AWAITING_LOCKIN
|| channel->state == DUALOPEND_OPEN_INIT) {
pending_channels++;
} else if (channel_active(channel)) {
active_channels++;
Expand Down Expand Up @@ -2339,10 +2361,11 @@ static struct command_result *json_dev_forget_channel(struct command *cmd,
"or `dev-fail` instead.");
}

bitcoind_getutxout(cmd->ld->topology->bitcoind,
&forget->channel->funding_txid,
forget->channel->funding_outnum,
process_dev_forget_channel, forget);
if (!channel_unsaved(forget->channel))
bitcoind_getutxout(cmd->ld->topology->bitcoind,
&forget->channel->funding_txid,
forget->channel->funding_outnum,
process_dev_forget_channel, forget);
return command_still_pending(cmd);
}

Expand Down
4 changes: 4 additions & 0 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ bool json_tok_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEE
void kill_uncommitted_channel(struct uncommitted_channel *uc UNNEEDED,
const char *why UNNEEDED)
{ fprintf(stderr, "kill_uncommitted_channel called!\n"); abort(); }
/* Generated stub for kill_unsaved_channel */
void kill_unsaved_channel(struct channel *channel UNNEEDED,
const char *why UNNEEDED)
{ fprintf(stderr, "kill_unsaved_channel called!\n"); abort(); }
/* Generated stub for log_ */
void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED,
const struct node_id *node_id UNNEEDED,
Expand Down
2 changes: 1 addition & 1 deletion wallet/db_postgres_sqlgen.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wallet/db_sqlite3_sqlgen.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions wallet/statements_gettextgen.po

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@ bool json_tok_streq(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
void kill_uncommitted_channel(struct uncommitted_channel *uc UNNEEDED,
const char *why UNNEEDED)
{ fprintf(stderr, "kill_uncommitted_channel called!\n"); abort(); }
/* Generated stub for kill_unsaved_channel */
void kill_unsaved_channel(struct channel *channel UNNEEDED,
const char *why UNNEEDED)
{ fprintf(stderr, "kill_unsaved_channel called!\n"); abort(); }
/* Generated stub for new_channel_mvt_invoice_hin */
struct channel_coin_mvt *new_channel_mvt_invoice_hin(const tal_t *ctx UNNEEDED,
struct htlc_in *hin UNNEEDED,
Expand Down
3 changes: 3 additions & 0 deletions wallet/walletrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ static struct command_result *json_listfunds(struct command *cmd,
list_for_each(&cmd->ld->peers, p, list) {
struct channel *c;
list_for_each(&p->channels, c, list) {
/* We don't print out uncommitted channels */
if (channel_unsaved(c))
continue;
json_object_start(response, NULL);
json_add_node_id(response, "peer_id", &p->id);
/* Mirrors logic in listpeers */
Expand Down

0 comments on commit 820dd45

Please sign in to comment.