mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
connectd: hold peer until we're interested.
Either because lightningd tells us it wants to talk, or because the peer says something about a channel. We also introduce a behavior change: we disconnect after a failed open. We might want to modify this later, but we it's a side-effect of openingd not holding onto idle connections. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -205,6 +205,7 @@ struct open_attempt *new_channel_open_attempt(struct channel *channel)
|
||||
oa->our_upfront_shutdown_script = NULL;
|
||||
oa->cmd = NULL;
|
||||
oa->aborted = false;
|
||||
oa->open_msg = NULL;
|
||||
|
||||
return oa;
|
||||
}
|
||||
|
||||
@@ -67,6 +67,9 @@ struct open_attempt {
|
||||
struct command *cmd;
|
||||
struct amount_sat funding;
|
||||
const u8 *our_upfront_shutdown_script;
|
||||
|
||||
/* First msg to send to dualopend (to make it create channel) */
|
||||
const u8 *open_msg;
|
||||
};
|
||||
|
||||
struct channel {
|
||||
|
||||
@@ -419,6 +419,7 @@ static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fd
|
||||
case WIRE_CONNECTD_DISCARD_PEER:
|
||||
case WIRE_CONNECTD_DEV_MEMLEAK:
|
||||
case WIRE_CONNECTD_PEER_FINAL_MSG:
|
||||
case WIRE_CONNECTD_PEER_MAKE_ACTIVE:
|
||||
case WIRE_CONNECTD_PING:
|
||||
case WIRE_CONNECTD_SEND_ONIONMSG:
|
||||
case WIRE_CONNECTD_CUSTOMMSG_OUT:
|
||||
@@ -434,9 +435,13 @@ static unsigned connectd_msg(struct subd *connectd, const u8 *msg, const int *fd
|
||||
break;
|
||||
|
||||
case WIRE_CONNECTD_PEER_CONNECTED:
|
||||
peer_connected(connectd->ld, msg);
|
||||
break;
|
||||
|
||||
case WIRE_CONNECTD_PEER_ACTIVE:
|
||||
if (tal_count(fds) != 1)
|
||||
return 1;
|
||||
peer_connected(connectd->ld, msg, fds[0]);
|
||||
peer_active(connectd->ld, msg, fds[0]);
|
||||
break;
|
||||
|
||||
case WIRE_CONNECTD_PEER_DISCONNECT_DONE:
|
||||
@@ -629,8 +634,7 @@ static struct command_result *json_sendcustommsg(struct command *cmd,
|
||||
type_to_string(cmd, struct node_id, dest));
|
||||
}
|
||||
|
||||
/* FIXME: This won't work once connectd keeps peers */
|
||||
if (!peer_get_owning_subd(peer)) {
|
||||
if (!peer->is_connected) {
|
||||
return command_fail(cmd, JSONRPC2_INVALID_REQUEST,
|
||||
"Peer is not connected: %s",
|
||||
type_to_string(cmd, struct node_id, dest));
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <common/psbt_open.h>
|
||||
#include <common/shutdown_scriptpubkey.h>
|
||||
#include <common/type_to_string.h>
|
||||
#include <connectd/connectd_wiregen.h>
|
||||
#include <errno.h>
|
||||
#include <hsmd/capabilities.h>
|
||||
#include <lightningd/chaintopology.h>
|
||||
@@ -2525,7 +2526,6 @@ static struct command_result *json_openchannel_init(struct command *cmd,
|
||||
struct open_attempt *oa;
|
||||
struct lease_rates *rates;
|
||||
struct command_result *res;
|
||||
u8 *msg;
|
||||
|
||||
if (!param(cmd, buffer, params,
|
||||
p_req("id", param_node_id, &id),
|
||||
@@ -2591,9 +2591,12 @@ static struct command_result *json_openchannel_init(struct command *cmd,
|
||||
}
|
||||
|
||||
channel = peer_unsaved_channel(peer);
|
||||
if (!channel || !channel->owner)
|
||||
return command_fail(cmd, FUNDING_PEER_NOT_CONNECTED,
|
||||
"Peer not connected");
|
||||
if (!channel) {
|
||||
channel = new_unsaved_channel(peer,
|
||||
peer->ld->config.fee_base,
|
||||
peer->ld->config.fee_per_satoshi);
|
||||
}
|
||||
|
||||
if (channel->open_attempt
|
||||
|| !list_empty(&channel->inflights))
|
||||
return command_fail(cmd, FUNDING_STATE_INVALID,
|
||||
@@ -2670,7 +2673,7 @@ static struct command_result *json_openchannel_init(struct command *cmd,
|
||||
} else
|
||||
our_upfront_shutdown_script_wallet_index = NULL;
|
||||
|
||||
msg = towire_dualopend_opener_init(NULL,
|
||||
oa->open_msg = towire_dualopend_opener_init(oa,
|
||||
psbt, *amount,
|
||||
oa->our_upfront_shutdown_script,
|
||||
our_upfront_shutdown_script_wallet_index,
|
||||
@@ -2682,7 +2685,10 @@ static struct command_result *json_openchannel_init(struct command *cmd,
|
||||
false,
|
||||
rates);
|
||||
|
||||
subd_send_msg(channel->owner, take(msg));
|
||||
/* Tell connectd to hand us this so we can start dualopend */
|
||||
subd_send_msg(peer->ld->connectd,
|
||||
take(towire_connectd_peer_make_active(NULL, &peer->id,
|
||||
NULL)));
|
||||
return command_still_pending(cmd);
|
||||
}
|
||||
|
||||
@@ -3056,7 +3062,6 @@ static struct command_result *json_queryrates(struct command *cmd,
|
||||
struct wally_psbt *psbt;
|
||||
struct open_attempt *oa;
|
||||
u32 *our_upfront_shutdown_script_wallet_index;
|
||||
u8 *msg;
|
||||
struct command_result *res;
|
||||
|
||||
if (!param(cmd, buffer, params,
|
||||
@@ -3077,6 +3082,10 @@ static struct command_result *json_queryrates(struct command *cmd,
|
||||
return command_fail(cmd, FUNDING_UNKNOWN_PEER, "Unknown peer");
|
||||
}
|
||||
|
||||
if (!peer->is_connected)
|
||||
return command_fail(cmd, FUNDING_PEER_NOT_CONNECTED,
|
||||
"Peer not connected");
|
||||
|
||||
/* We can't query rates for a peer we have a channel with */
|
||||
channel = peer_active_channel(peer);
|
||||
if (channel)
|
||||
@@ -3086,9 +3095,12 @@ static struct command_result *json_queryrates(struct command *cmd,
|
||||
channel_state_name(channel));
|
||||
|
||||
channel = peer_unsaved_channel(peer);
|
||||
if (!channel || !channel->owner)
|
||||
return command_fail(cmd, FUNDING_PEER_NOT_CONNECTED,
|
||||
"Peer not connected");
|
||||
if (!channel) {
|
||||
channel = new_unsaved_channel(peer,
|
||||
peer->ld->config.fee_base,
|
||||
peer->ld->config.fee_per_satoshi);
|
||||
}
|
||||
|
||||
if (channel->open_attempt
|
||||
|| !list_empty(&channel->inflights))
|
||||
return command_fail(cmd, FUNDING_STATE_INVALID,
|
||||
@@ -3140,7 +3152,7 @@ static struct command_result *json_queryrates(struct command *cmd,
|
||||
} else
|
||||
our_upfront_shutdown_script_wallet_index = NULL;
|
||||
|
||||
msg = towire_dualopend_opener_init(NULL,
|
||||
oa->open_msg = towire_dualopend_opener_init(oa,
|
||||
psbt, *amount,
|
||||
oa->our_upfront_shutdown_script,
|
||||
our_upfront_shutdown_script_wallet_index,
|
||||
@@ -3152,7 +3164,10 @@ static struct command_result *json_queryrates(struct command *cmd,
|
||||
true,
|
||||
NULL);
|
||||
|
||||
subd_send_msg(channel->owner, take(msg));
|
||||
/* Tell connectd to hand us this so we can start dualopend */
|
||||
subd_send_msg(peer->ld->connectd,
|
||||
take(towire_connectd_peer_make_active(NULL, &peer->id,
|
||||
NULL)));
|
||||
return command_still_pending(cmd);
|
||||
|
||||
}
|
||||
@@ -3212,9 +3227,9 @@ AUTODATA(json_command, &openchannel_signed_command);
|
||||
AUTODATA(json_command, &openchannel_bump_command);
|
||||
AUTODATA(json_command, &openchannel_abort_command);
|
||||
|
||||
static void start_fresh_dualopend(struct peer *peer,
|
||||
struct peer_fd *peer_fd,
|
||||
struct channel *channel)
|
||||
bool peer_start_dualopend(struct peer *peer,
|
||||
struct peer_fd *peer_fd,
|
||||
struct channel *channel)
|
||||
{
|
||||
int hsmfd;
|
||||
u32 max_to_self_delay;
|
||||
@@ -3242,7 +3257,7 @@ static void start_fresh_dualopend(struct peer *peer,
|
||||
channel_internal_error(channel,
|
||||
"Running lightningd_dualopend: %s",
|
||||
strerror(errno));
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
channel_config(peer->ld, &channel->our_config,
|
||||
@@ -3268,7 +3283,7 @@ static void start_fresh_dualopend(struct peer *peer,
|
||||
&channel->local_funding_pubkey,
|
||||
channel->minimum_depth);
|
||||
subd_send_msg(channel->owner, take(msg));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void peer_restart_dualopend(struct peer *peer,
|
||||
@@ -3284,7 +3299,7 @@ void peer_restart_dualopend(struct peer *peer,
|
||||
u8 *msg;
|
||||
|
||||
if (channel_unsaved(channel)) {
|
||||
start_fresh_dualopend(peer, peer_fd, channel);
|
||||
peer_start_dualopend(peer, peer_fd, channel);
|
||||
return;
|
||||
}
|
||||
hsmfd = hsm_get_client_fd(peer->ld, &peer->id, channel->dbid,
|
||||
@@ -3374,16 +3389,3 @@ void peer_restart_dualopend(struct peer *peer,
|
||||
|
||||
subd_send_msg(channel->owner, take(msg));
|
||||
}
|
||||
|
||||
void peer_start_dualopend(struct peer *peer, struct peer_fd *peer_fd)
|
||||
{
|
||||
struct channel *channel;
|
||||
|
||||
/* And we never touch this. */
|
||||
assert(!peer_unsaved_channel(peer));
|
||||
channel = new_unsaved_channel(peer,
|
||||
peer->ld->config.fee_base,
|
||||
peer->ld->config.fee_per_satoshi);
|
||||
|
||||
start_fresh_dualopend(peer, peer_fd, channel);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
|
||||
struct peer_fd;
|
||||
|
||||
void peer_start_dualopend(struct peer *peer, struct peer_fd *peer_fd);
|
||||
bool peer_start_dualopend(struct peer *peer, struct peer_fd *peer_fd,
|
||||
struct channel *channel);
|
||||
|
||||
void peer_restart_dualopend(struct peer *peer,
|
||||
struct peer_fd *peer_fd,
|
||||
|
||||
@@ -71,6 +71,7 @@ new_uncommitted_channel(struct peer *peer)
|
||||
tal_add_destructor(uc, destroy_uncommitted_channel);
|
||||
|
||||
uc->got_offer = false;
|
||||
uc->open_daemon = NULL;
|
||||
|
||||
return uc;
|
||||
}
|
||||
|
||||
@@ -87,6 +87,9 @@ struct funding_channel {
|
||||
/* Whether or not this is in the middle of getting funded */
|
||||
bool inflight;
|
||||
|
||||
/* Initial openingd_funder_start msg */
|
||||
const u8 *open_msg;
|
||||
|
||||
/* Any commands trying to cancel us. */
|
||||
struct command **cancels;
|
||||
|
||||
@@ -95,8 +98,7 @@ struct funding_channel {
|
||||
struct peer_fd *peer_fd;
|
||||
};
|
||||
|
||||
struct uncommitted_channel *
|
||||
new_uncommitted_channel(struct peer *peer);
|
||||
struct uncommitted_channel *new_uncommitted_channel(struct peer *peer);
|
||||
|
||||
void opend_channel_errmsg(struct uncommitted_channel *uc,
|
||||
struct peer_fd *peer_fd,
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <common/json_tok.h>
|
||||
#include <common/param.h>
|
||||
#include <common/type_to_string.h>
|
||||
#include <connectd/connectd_wiregen.h>
|
||||
#include <errno.h>
|
||||
#include <hsmd/capabilities.h>
|
||||
#include <lightningd/chaintopology.h>
|
||||
@@ -894,7 +895,7 @@ static unsigned int openingd_msg(struct subd *openingd,
|
||||
return 0;
|
||||
}
|
||||
|
||||
void peer_start_openingd(struct peer *peer, struct peer_fd *peer_fd)
|
||||
bool peer_start_openingd(struct peer *peer, struct peer_fd *peer_fd)
|
||||
{
|
||||
int hsmfd;
|
||||
u32 max_to_self_delay;
|
||||
@@ -902,9 +903,9 @@ void peer_start_openingd(struct peer *peer, struct peer_fd *peer_fd)
|
||||
struct uncommitted_channel *uc;
|
||||
const u8 *msg;
|
||||
|
||||
assert(!peer->uncommitted_channel);
|
||||
|
||||
uc = peer->uncommitted_channel = new_uncommitted_channel(peer);
|
||||
assert(peer->uncommitted_channel);
|
||||
uc = peer->uncommitted_channel;
|
||||
assert(!uc->open_daemon);
|
||||
|
||||
hsmfd = hsm_get_client_fd(peer->ld, &uc->peer->id, uc->dbid,
|
||||
HSM_CAP_COMMITMENT_POINT
|
||||
@@ -925,7 +926,7 @@ void peer_start_openingd(struct peer *peer, struct peer_fd *peer_fd)
|
||||
"Running lightning_openingd: %s",
|
||||
strerror(errno)));
|
||||
tal_free(uc);
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
channel_config(peer->ld, &uc->our_config,
|
||||
@@ -954,6 +955,7 @@ void peer_start_openingd(struct peer *peer, struct peer_fd *peer_fd)
|
||||
feerate_max(peer->ld, NULL),
|
||||
IFDEV(peer->ld->dev_force_tmp_channel_id, NULL));
|
||||
subd_send_msg(uc->open_daemon, take(msg));
|
||||
return true;
|
||||
}
|
||||
|
||||
static struct command_result *json_fundchannel_complete(struct command *cmd,
|
||||
@@ -986,12 +988,15 @@ static struct command_result *json_fundchannel_complete(struct command *cmd,
|
||||
return command_fail(cmd, LIGHTNINGD, "Peer already %s",
|
||||
channel_state_name(channel));
|
||||
|
||||
if (!peer->uncommitted_channel)
|
||||
if (!peer->is_connected)
|
||||
return command_fail(cmd, FUNDING_PEER_NOT_CONNECTED,
|
||||
"Peer not connected");
|
||||
|
||||
if (!peer->uncommitted_channel->fc || !peer->uncommitted_channel->fc->inflight)
|
||||
if (!peer->uncommitted_channel
|
||||
|| !peer->uncommitted_channel->fc
|
||||
|| !peer->uncommitted_channel->fc->inflight)
|
||||
return command_fail(cmd, LIGHTNINGD, "No channel funding in progress.");
|
||||
|
||||
if (peer->uncommitted_channel->fc->cmd)
|
||||
return command_fail(cmd, LIGHTNINGD, "Channel funding in progress.");
|
||||
|
||||
@@ -1082,6 +1087,8 @@ static struct command_result *json_fundchannel_cancel(struct command *cmd,
|
||||
return command_still_pending(cmd);
|
||||
}
|
||||
|
||||
log_debug(cmd->ld->log, "fundchannel_cancel no uncommitted_channel!");
|
||||
|
||||
/* Handle `fundchannel_cancel` after `fundchannel_complete`. */
|
||||
return cancel_channel_before_broadcast(cmd, peer);
|
||||
}
|
||||
@@ -1101,7 +1108,6 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
|
||||
bool *announce_channel;
|
||||
u32 *feerate_per_kw;
|
||||
|
||||
u8 *msg = NULL;
|
||||
struct amount_sat *amount;
|
||||
struct amount_msat *push_msat;
|
||||
u32 *upfront_shutdown_script_wallet_index;
|
||||
@@ -1154,6 +1160,10 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
|
||||
return command_fail(cmd, FUNDING_UNKNOWN_PEER, "Unknown peer");
|
||||
}
|
||||
|
||||
if (!peer->is_connected)
|
||||
return command_fail(cmd, FUNDING_PEER_NOT_CONNECTED,
|
||||
"Peer not connected");
|
||||
|
||||
channel = peer_active_channel(peer);
|
||||
if (channel) {
|
||||
return command_fail(cmd, LIGHTNINGD, "Peer already %s",
|
||||
@@ -1170,9 +1180,10 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
|
||||
" must use `openchannel_init` not"
|
||||
" `fundchannel_start`.");
|
||||
|
||||
return command_fail(cmd, FUNDING_PEER_NOT_CONNECTED,
|
||||
"Peer not connected");
|
||||
}
|
||||
log_debug(cmd->ld->log, "fundchannel_start: allocating uncommitted_channel");
|
||||
peer->uncommitted_channel = new_uncommitted_channel(peer);
|
||||
} else
|
||||
log_debug(cmd->ld->log, "fundchannel_start: reusing uncommitted_channel");
|
||||
|
||||
if (peer->uncommitted_channel->fc) {
|
||||
return command_fail(cmd, LIGHTNINGD, "Already funding channel");
|
||||
@@ -1228,7 +1239,8 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
|
||||
} else
|
||||
upfront_shutdown_script_wallet_index = NULL;
|
||||
|
||||
msg = towire_openingd_funder_start(NULL,
|
||||
fc->open_msg
|
||||
= towire_openingd_funder_start(fc,
|
||||
*amount,
|
||||
fc->push,
|
||||
fc->our_upfront_shutdown_script,
|
||||
@@ -1236,7 +1248,10 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
|
||||
*feerate_per_kw,
|
||||
fc->channel_flags);
|
||||
|
||||
subd_send_msg(peer->uncommitted_channel->open_daemon, take(msg));
|
||||
/* Tell connectd to make this active; when it does, we can continue */
|
||||
subd_send_msg(peer->ld->connectd,
|
||||
take(towire_connectd_peer_make_active(NULL, &peer->id,
|
||||
NULL)));
|
||||
return command_still_pending(cmd);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ struct uncommitted_channel;
|
||||
void json_add_uncommitted_channel(struct json_stream *response,
|
||||
const struct uncommitted_channel *uc);
|
||||
|
||||
void peer_start_openingd(struct peer *peer,
|
||||
bool peer_start_openingd(struct peer *peer,
|
||||
struct peer_fd *peer_fd);
|
||||
|
||||
struct subd *peer_get_owning_subd(struct peer *peer);
|
||||
|
||||
@@ -944,7 +944,6 @@ struct peer_connected_hook_payload {
|
||||
struct wireaddr *remote_addr;
|
||||
bool incoming;
|
||||
struct peer *peer;
|
||||
struct peer_fd *peer_fd;
|
||||
u8 *error;
|
||||
};
|
||||
|
||||
@@ -1028,11 +1027,6 @@ static void peer_connected_hook_final(struct peer_connected_hook_payload *payloa
|
||||
}
|
||||
case DUALOPEND_OPEN_INIT:
|
||||
case DUALOPEND_AWAITING_LOCKIN:
|
||||
assert(!channel->owner);
|
||||
channel->peer->addr = addr;
|
||||
channel->peer->connected_incoming = payload->incoming;
|
||||
peer_restart_dualopend(peer, payload->peer_fd, channel);
|
||||
return;
|
||||
case CHANNELD_AWAITING_LOCKIN:
|
||||
case CHANNELD_NORMAL:
|
||||
case CHANNELD_SHUTTING_DOWN:
|
||||
@@ -1040,31 +1034,31 @@ static void peer_connected_hook_final(struct peer_connected_hook_payload *payloa
|
||||
assert(!channel->owner);
|
||||
channel->peer->addr = addr;
|
||||
channel->peer->connected_incoming = payload->incoming;
|
||||
peer_start_channeld(channel, payload->peer_fd, NULL, true,
|
||||
NULL);
|
||||
return;
|
||||
goto make_active;
|
||||
}
|
||||
abort();
|
||||
}
|
||||
|
||||
/* If we get here, it means we have no channel */
|
||||
assert(!channel);
|
||||
if (feature_negotiated(ld->our_features,
|
||||
peer->their_features,
|
||||
OPT_DUAL_FUND)) {
|
||||
peer_start_dualopend(peer, payload->peer_fd);
|
||||
} else
|
||||
peer_start_openingd(peer, payload->peer_fd);
|
||||
return;
|
||||
|
||||
send_error:
|
||||
log_debug(ld->log, "Telling connectd to send error %s",
|
||||
tal_hex(tmpctx, error));
|
||||
log_peer_debug(ld->log, &peer->id, "Telling connectd to send error %s",
|
||||
tal_hex(tmpctx, error));
|
||||
/* Get connectd to send error and close. */
|
||||
subd_send_msg(ld->connectd,
|
||||
take(towire_connectd_peer_final_msg(NULL, &peer->id,
|
||||
error)));
|
||||
tal_free(payload->peer_fd);
|
||||
return;
|
||||
|
||||
make_active:
|
||||
log_peer_debug(ld->log, &peer->id,
|
||||
"Telling connectd to make active, state %s",
|
||||
channel_state_name(channel));
|
||||
subd_send_msg(ld->connectd,
|
||||
take(towire_connectd_peer_make_active(NULL, &peer->id,
|
||||
&channel->cid)));
|
||||
}
|
||||
|
||||
static bool
|
||||
@@ -1179,8 +1173,8 @@ REGISTER_PLUGIN_HOOK(peer_connected,
|
||||
struct peer_connected_hook_payload *);
|
||||
|
||||
/* Connectd tells us a peer has connected: it never hands us duplicates, since
|
||||
* it holds them until we say peer_died. */
|
||||
void peer_connected(struct lightningd *ld, const u8 *msg, int peer_fd)
|
||||
* it holds them until we say peer_disconnected. */
|
||||
void peer_connected(struct lightningd *ld, const u8 *msg)
|
||||
{
|
||||
struct node_id id;
|
||||
u8 *their_features;
|
||||
@@ -1198,8 +1192,6 @@ void peer_connected(struct lightningd *ld, const u8 *msg, int peer_fd)
|
||||
fatal("Connectd gave bad CONNECT_PEER_CONNECTED message %s",
|
||||
tal_hex(msg, msg));
|
||||
|
||||
hook_payload->peer_fd = new_peer_fd(hook_payload, peer_fd);
|
||||
|
||||
/* If we're already dealing with this peer, hand off to correct
|
||||
* subdaemon. Otherwise, we'll hand to openingd to wait there. */
|
||||
peer = peer_by_id(ld, &id);
|
||||
@@ -1237,6 +1229,137 @@ void peer_connected(struct lightningd *ld, const u8 *msg, int peer_fd)
|
||||
plugin_hook_call_peer_connected(ld, hook_payload);
|
||||
}
|
||||
|
||||
/* connectd tells us a peer has an interesting message, and hands us an
|
||||
* fd to give to the correct subdaemon. Unlike peer_connected, this is racy:
|
||||
* we might have just told it to disconnect peer. */
|
||||
void peer_active(struct lightningd *ld, const u8 *msg, int fd)
|
||||
{
|
||||
struct node_id id;
|
||||
u16 *msgtype;
|
||||
struct channel *channel;
|
||||
struct channel_id *channel_id;
|
||||
struct peer *peer;
|
||||
bool dual_fund;
|
||||
u8 *error;
|
||||
struct peer_fd *peer_fd = new_peer_fd(tmpctx, fd);
|
||||
|
||||
/* FIXME: Use msgtype to determine what to do! */
|
||||
if (!fromwire_connectd_peer_active(msg, msg, &id, &msgtype, &channel_id))
|
||||
fatal("Connectd gave bad CONNECTD_PEER_ACTIVE message %s",
|
||||
tal_hex(msg, msg));
|
||||
|
||||
peer = peer_by_id(ld, &id);
|
||||
if (!peer) {
|
||||
/* This race is possible, but I want to see it in CI. */
|
||||
log_broken(ld->log, "Unknown active peer %s",
|
||||
type_to_string(tmpctx, struct node_id, &id));
|
||||
return;
|
||||
}
|
||||
|
||||
channel = peer_active_channel(peer);
|
||||
|
||||
/* It might be v2 opening, though, since we hang onto these */
|
||||
if (!channel)
|
||||
channel = peer_unsaved_channel(peer);
|
||||
|
||||
if (channel) {
|
||||
switch (channel->state) {
|
||||
case ONCHAIN:
|
||||
case FUNDING_SPEND_SEEN:
|
||||
case CLOSINGD_COMPLETE:
|
||||
/* Channel is supposed to be active!*/
|
||||
abort();
|
||||
case CLOSED:
|
||||
/* Channel should not have been loaded */
|
||||
abort();
|
||||
|
||||
/* We consider this "active" but we only send an error */
|
||||
case AWAITING_UNILATERAL: {
|
||||
/* channel->error is not saved in db, so this can
|
||||
* happen if we restart. */
|
||||
error = towire_errorfmt(tmpctx, &channel->cid,
|
||||
"Awaiting unilateral close");
|
||||
goto send_error;
|
||||
}
|
||||
case DUALOPEND_OPEN_INIT:
|
||||
/* We asked for this, to open? */
|
||||
if (!msgtype
|
||||
&& channel->open_attempt
|
||||
&& channel->open_attempt->open_msg) {
|
||||
if (peer_start_dualopend(peer, peer_fd, channel))
|
||||
subd_send_msg(channel->owner, channel->open_attempt->open_msg);
|
||||
return;
|
||||
}
|
||||
/* Fall through. */
|
||||
case DUALOPEND_AWAITING_LOCKIN:
|
||||
assert(!channel->owner);
|
||||
peer_restart_dualopend(peer, peer_fd, channel);
|
||||
return;
|
||||
case CHANNELD_AWAITING_LOCKIN:
|
||||
case CHANNELD_NORMAL:
|
||||
case CHANNELD_SHUTTING_DOWN:
|
||||
case CLOSINGD_SIGEXCHANGE:
|
||||
assert(!channel->owner);
|
||||
peer_start_channeld(channel,
|
||||
peer_fd,
|
||||
NULL, true,
|
||||
NULL);
|
||||
return;
|
||||
}
|
||||
abort();
|
||||
}
|
||||
|
||||
dual_fund = feature_negotiated(ld->our_features,
|
||||
peer->their_features,
|
||||
OPT_DUAL_FUND);
|
||||
|
||||
/* Did we ask for this? */
|
||||
if (!msgtype) {
|
||||
/* If it was dual_fund, it will have peer_unsaved_channel above */
|
||||
if (dual_fund) {
|
||||
log_broken(ld->log, "Unsolicited active df peer %s?",
|
||||
type_to_string(tmpctx, struct node_id,
|
||||
&peer->id));
|
||||
} else {
|
||||
const struct uncommitted_channel *uc
|
||||
= peer->uncommitted_channel;
|
||||
|
||||
if (!uc->open_daemon
|
||||
&& uc->fc
|
||||
&& uc->fc->open_msg) {
|
||||
if (peer_start_openingd(peer, peer_fd)) {
|
||||
subd_send_msg(uc->open_daemon,
|
||||
uc->fc->open_msg);
|
||||
}
|
||||
} else {
|
||||
log_broken(ld->log, "Unsolicited active peer %s?",
|
||||
type_to_string(tmpctx, struct node_id,
|
||||
&peer->id));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* OK, it's unsolicited. What kind of open do they want? */
|
||||
if (dual_fund) {
|
||||
channel = new_unsaved_channel(peer,
|
||||
peer->ld->config.fee_base,
|
||||
peer->ld->config.fee_per_satoshi);
|
||||
peer_start_dualopend(peer, peer_fd, channel);
|
||||
} else {
|
||||
peer->uncommitted_channel = new_uncommitted_channel(peer);
|
||||
peer_start_openingd(peer, peer_fd);
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
||||
send_error:
|
||||
log_peer_debug(ld->log, &peer->id, "Telling connectd to send error %s",
|
||||
tal_hex(tmpctx, error));
|
||||
/* Get connectd to send error and close. */
|
||||
subd_send_msg(ld->connectd,
|
||||
take(towire_connectd_peer_final_msg(NULL, &peer->id,
|
||||
error)));
|
||||
}
|
||||
|
||||
struct disconnect_command {
|
||||
struct list_node list;
|
||||
/* Command structure. This is the parent of the close command. */
|
||||
@@ -1262,8 +1385,13 @@ void peer_disconnect_done(struct lightningd *ld, const u8 *msg)
|
||||
|
||||
/* If we still have peer, it's disconnected now */
|
||||
p = peer_by_id(ld, &id);
|
||||
if (p)
|
||||
if (p) {
|
||||
p->is_connected = false;
|
||||
/* If we only cared about peer because of connectd, free it. */
|
||||
if (list_empty(&p->channels) && !p->uncommitted_channel) {
|
||||
tal_free(p);
|
||||
}
|
||||
}
|
||||
|
||||
/* Fire off plugin notifications */
|
||||
notify_disconnect(ld, &id);
|
||||
@@ -1744,6 +1872,9 @@ static struct command_result *json_disconnect(struct command *cmd,
|
||||
|
||||
peer = peer_by_id(cmd->ld, id);
|
||||
if (!peer) {
|
||||
return command_fail(cmd, LIGHTNINGD, "Unknown peer");
|
||||
}
|
||||
if (!peer->is_connected) {
|
||||
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
|
||||
}
|
||||
channel = peer_active_channel(peer);
|
||||
@@ -1761,11 +1892,15 @@ static struct command_result *json_disconnect(struct command *cmd,
|
||||
channel_unsaved_close_conn(channel, "disconnect command");
|
||||
goto wait_for_connectd;
|
||||
}
|
||||
if (!peer->uncommitted_channel) {
|
||||
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
|
||||
if (peer->uncommitted_channel) {
|
||||
kill_uncommitted_channel(peer->uncommitted_channel,
|
||||
"disconnect command");
|
||||
goto wait_for_connectd;
|
||||
}
|
||||
kill_uncommitted_channel(peer->uncommitted_channel,
|
||||
"disconnect command");
|
||||
|
||||
/* It's just sitting in connectd. */
|
||||
subd_send_msg(cmd->ld->connectd,
|
||||
take(towire_connectd_discard_peer(NULL, id)));
|
||||
|
||||
wait_for_connectd:
|
||||
/* Connectd tells us when it's finally disconnected */
|
||||
|
||||
@@ -67,8 +67,9 @@ struct peer *peer_from_json(struct lightningd *ld,
|
||||
const char *buffer,
|
||||
const jsmntok_t *peeridtok);
|
||||
|
||||
void peer_connected(struct lightningd *ld, const u8 *msg, int peer_fd);
|
||||
void peer_connected(struct lightningd *ld, const u8 *msg);
|
||||
void peer_disconnect_done(struct lightningd *ld, const u8 *msg);
|
||||
void peer_active(struct lightningd *ld, const u8 *msg, int peer_fd);
|
||||
|
||||
/* Could be configurable. */
|
||||
#define OUR_CHANNEL_FLAGS CHANNEL_FLAGS_ANNOUNCE_CHANNEL
|
||||
|
||||
@@ -204,6 +204,9 @@ bool fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
|
||||
/* Generated stub for fromwire_channeld_dev_memleak_reply */
|
||||
bool fromwire_channeld_dev_memleak_reply(const void *p UNNEEDED, bool *leak UNNEEDED)
|
||||
{ fprintf(stderr, "fromwire_channeld_dev_memleak_reply called!\n"); abort(); }
|
||||
/* Generated stub for fromwire_connectd_peer_active */
|
||||
bool fromwire_connectd_peer_active(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct node_id *id UNNEEDED, u16 **msgtype UNNEEDED, struct channel_id **channel_id UNNEEDED)
|
||||
{ fprintf(stderr, "fromwire_connectd_peer_active called!\n"); abort(); }
|
||||
/* Generated stub for fromwire_connectd_peer_connected */
|
||||
bool fromwire_connectd_peer_connected(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct node_id *id UNNEEDED, struct wireaddr_internal *addr UNNEEDED, struct wireaddr **remote_addr UNNEEDED, bool *incoming UNNEEDED, u8 **features UNNEEDED)
|
||||
{ fprintf(stderr, "fromwire_connectd_peer_connected called!\n"); abort(); }
|
||||
@@ -460,6 +463,14 @@ struct oneshot *new_reltimer_(struct timers *timers UNNEEDED,
|
||||
struct timerel expire UNNEEDED,
|
||||
void (*cb)(void *) UNNEEDED, void *arg UNNEEDED)
|
||||
{ fprintf(stderr, "new_reltimer_ called!\n"); abort(); }
|
||||
/* Generated stub for new_uncommitted_channel */
|
||||
struct uncommitted_channel *new_uncommitted_channel(struct peer *peer UNNEEDED)
|
||||
{ fprintf(stderr, "new_uncommitted_channel called!\n"); abort(); }
|
||||
/* Generated stub for new_unsaved_channel */
|
||||
struct channel *new_unsaved_channel(struct peer *peer UNNEEDED,
|
||||
u32 feerate_base UNNEEDED,
|
||||
u32 feerate_ppm UNNEEDED)
|
||||
{ fprintf(stderr, "new_unsaved_channel called!\n"); abort(); }
|
||||
/* Generated stub for node_id_cmp */
|
||||
int node_id_cmp(const struct node_id *a UNNEEDED, const struct node_id *b UNNEEDED)
|
||||
{ fprintf(stderr, "node_id_cmp called!\n"); abort(); }
|
||||
@@ -586,10 +597,11 @@ void peer_start_channeld(struct channel *channel UNNEEDED,
|
||||
const u8 *reestablish_only UNNEEDED)
|
||||
{ fprintf(stderr, "peer_start_channeld called!\n"); abort(); }
|
||||
/* Generated stub for peer_start_dualopend */
|
||||
void peer_start_dualopend(struct peer *peer UNNEEDED, struct peer_fd *peer_fd UNNEEDED)
|
||||
bool peer_start_dualopend(struct peer *peer UNNEEDED, struct peer_fd *peer_fd UNNEEDED,
|
||||
struct channel *channel UNNEEDED)
|
||||
{ fprintf(stderr, "peer_start_dualopend called!\n"); abort(); }
|
||||
/* Generated stub for peer_start_openingd */
|
||||
void peer_start_openingd(struct peer *peer UNNEEDED,
|
||||
bool peer_start_openingd(struct peer *peer UNNEEDED,
|
||||
struct peer_fd *peer_fd UNNEEDED)
|
||||
{ fprintf(stderr, "peer_start_openingd called!\n"); abort(); }
|
||||
/* Generated stub for peer_unsaved_channel */
|
||||
@@ -640,9 +652,15 @@ u8 *towire_channeld_dev_memleak(const tal_t *ctx UNNEEDED)
|
||||
/* Generated stub for towire_channeld_dev_reenable_commit */
|
||||
u8 *towire_channeld_dev_reenable_commit(const tal_t *ctx UNNEEDED)
|
||||
{ fprintf(stderr, "towire_channeld_dev_reenable_commit called!\n"); abort(); }
|
||||
/* Generated stub for towire_connectd_discard_peer */
|
||||
u8 *towire_connectd_discard_peer(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED)
|
||||
{ fprintf(stderr, "towire_connectd_discard_peer called!\n"); abort(); }
|
||||
/* Generated stub for towire_connectd_peer_final_msg */
|
||||
u8 *towire_connectd_peer_final_msg(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED, const u8 *msg UNNEEDED)
|
||||
{ fprintf(stderr, "towire_connectd_peer_final_msg called!\n"); abort(); }
|
||||
/* Generated stub for towire_connectd_peer_make_active */
|
||||
u8 *towire_connectd_peer_make_active(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED, const struct channel_id *channel_id UNNEEDED)
|
||||
{ fprintf(stderr, "towire_connectd_peer_make_active called!\n"); abort(); }
|
||||
/* Generated stub for towire_dualopend_dev_memleak */
|
||||
u8 *towire_dualopend_dev_memleak(const tal_t *ctx UNNEEDED)
|
||||
{ fprintf(stderr, "towire_dualopend_dev_memleak called!\n"); abort(); }
|
||||
|
||||
Reference in New Issue
Block a user