2018-10-11 08:41:48 +02:00
|
|
|
#include "bitcoin/feerate.h"
|
2018-02-20 21:59:04 +01:00
|
|
|
#include <bitcoin/privkey.h>
|
|
|
|
#include <bitcoin/script.h>
|
|
|
|
#include <ccan/tal/str/str.h>
|
|
|
|
#include <common/channel_config.h>
|
|
|
|
#include <common/funding_tx.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/json_command.h>
|
|
|
|
#include <common/jsonrpc_errors.h>
|
2018-02-20 21:59:04 +01:00
|
|
|
#include <common/key_derive.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/param.h>
|
2019-06-03 20:11:25 +02:00
|
|
|
#include <common/per_peer_state.h>
|
2018-04-05 21:19:47 +02:00
|
|
|
#include <common/wallet_tx.h>
|
2018-02-20 21:59:04 +01:00
|
|
|
#include <common/wire_error.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <connectd/gen_connect_wire.h>
|
2018-02-20 21:59:04 +01:00
|
|
|
#include <errno.h>
|
2018-09-20 05:06:42 +02:00
|
|
|
#include <hsmd/gen_hsm_wire.h>
|
2018-02-20 21:59:04 +01:00
|
|
|
#include <lightningd/chaintopology.h>
|
2018-02-20 21:59:09 +01:00
|
|
|
#include <lightningd/channel_control.h>
|
|
|
|
#include <lightningd/closing_control.h>
|
2018-02-20 21:59:04 +01:00
|
|
|
#include <lightningd/hsm_control.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <lightningd/json.h>
|
2018-02-20 21:59:04 +01:00
|
|
|
#include <lightningd/jsonrpc.h>
|
|
|
|
#include <lightningd/lightningd.h>
|
|
|
|
#include <lightningd/log.h>
|
2018-12-13 13:58:40 +01:00
|
|
|
#include <lightningd/notification.h>
|
2018-02-20 21:59:04 +01:00
|
|
|
#include <lightningd/opening_control.h>
|
|
|
|
#include <lightningd/peer_control.h>
|
2019-05-21 00:45:20 +02:00
|
|
|
#include <lightningd/plugin_hook.h>
|
2018-02-20 21:59:04 +01:00
|
|
|
#include <lightningd/subd.h>
|
|
|
|
#include <openingd/gen_opening_wire.h>
|
2018-03-17 00:37:02 +01:00
|
|
|
#include <wire/wire.h>
|
2018-02-20 21:59:04 +01:00
|
|
|
#include <wire/wire_sync.h>
|
|
|
|
|
|
|
|
/* Channel we're still opening. */
|
|
|
|
struct uncommitted_channel {
|
|
|
|
/* peer->uncommitted_channel == this */
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
/* openingd which is running now */
|
|
|
|
struct subd *openingd;
|
|
|
|
|
|
|
|
/* Reserved dbid for if we become a real struct channel */
|
|
|
|
u64 dbid;
|
|
|
|
|
|
|
|
/* For logging */
|
|
|
|
struct log *log;
|
|
|
|
|
2018-02-23 06:53:47 +01:00
|
|
|
/* Openingd can tell us stuff. */
|
|
|
|
const char *transient_billboard;
|
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
/* If we offered channel, this contains information, otherwise NULL */
|
|
|
|
struct funding_channel *fc;
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
/* Our basepoints for the channel. */
|
|
|
|
struct basepoints local_basepoints;
|
|
|
|
|
|
|
|
/* Public key for funding tx. */
|
|
|
|
struct pubkey local_funding_pubkey;
|
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
/* These are *not* filled in by new_uncommitted_channel: */
|
|
|
|
|
|
|
|
/* Minimum funding depth (if funder == REMOTE). */
|
|
|
|
u32 minimum_depth;
|
|
|
|
|
|
|
|
/* Our channel config. */
|
|
|
|
struct channel_config our_config;
|
|
|
|
};
|
|
|
|
|
2018-04-05 21:19:47 +02:00
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
struct funding_channel {
|
2019-01-16 14:24:30 +01:00
|
|
|
struct command *cmd; /* Which initially owns us until openingd request */
|
|
|
|
|
2019-06-05 07:29:05 +02:00
|
|
|
struct wallet_tx *wtx;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat push;
|
2018-04-05 21:19:47 +02:00
|
|
|
u8 channel_flags;
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2019-01-16 14:24:30 +01:00
|
|
|
/* Variables we need to compose fields in cmd's response */
|
2019-02-14 11:34:32 +01:00
|
|
|
const char *hextx;
|
2019-01-16 14:24:30 +01:00
|
|
|
struct channel_id cid;
|
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
/* Peer we're trying to reach. */
|
|
|
|
struct pubkey peerid;
|
|
|
|
|
2019-01-16 14:24:30 +01:00
|
|
|
/* Channel, subsequent owner of us */
|
2018-02-20 21:59:04 +01:00
|
|
|
struct uncommitted_channel *uc;
|
|
|
|
};
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
static void uncommitted_channel_disconnect(struct uncommitted_channel *uc,
|
|
|
|
const char *desc)
|
|
|
|
{
|
2018-07-24 08:18:58 +02:00
|
|
|
u8 *msg = towire_connectctl_peer_disconnected(tmpctx, &uc->peer->id);
|
2018-07-24 08:18:58 +02:00
|
|
|
log_info(uc->log, "%s", desc);
|
2018-07-24 08:18:58 +02:00
|
|
|
subd_send_msg(uc->peer->ld->connectd, msg);
|
2018-07-24 08:18:58 +02:00
|
|
|
if (uc->fc)
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_fail(uc->fc->cmd, LIGHTNINGD, "%s", desc));
|
2018-12-13 13:58:40 +01:00
|
|
|
notify_disconnect(uc->peer->ld, &uc->peer->id);
|
2018-07-24 08:18:58 +02:00
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
void kill_uncommitted_channel(struct uncommitted_channel *uc,
|
|
|
|
const char *why)
|
|
|
|
{
|
|
|
|
log_info(uc->log, "Killing openingd: %s", why);
|
|
|
|
|
|
|
|
/* Close openingd. */
|
|
|
|
subd_release_channel(uc->openingd, uc);
|
2018-04-23 12:08:01 +02:00
|
|
|
uc->openingd = NULL;
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
uncommitted_channel_disconnect(uc, why);
|
2018-02-20 21:59:04 +01:00
|
|
|
tal_free(uc);
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_uncommitted_channel(struct json_stream *response,
|
2018-02-20 21:59:04 +01:00
|
|
|
const struct uncommitted_channel *uc)
|
|
|
|
{
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat total, ours;
|
2018-02-20 21:59:04 +01:00
|
|
|
if (!uc)
|
|
|
|
return;
|
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
/* If we're chatting but no channel, that's shown by connected: True */
|
|
|
|
if (!uc->fc)
|
|
|
|
return;
|
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
json_object_start(response, NULL);
|
|
|
|
json_add_string(response, "state", "OPENINGD");
|
|
|
|
json_add_string(response, "owner", "lightning_openingd");
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
json_add_string(response, "funding", "LOCAL");
|
2018-02-23 06:53:47 +01:00
|
|
|
if (uc->transient_billboard) {
|
|
|
|
json_array_start(response, "status");
|
|
|
|
json_add_string(response, NULL, uc->transient_billboard);
|
|
|
|
json_array_end(response);
|
|
|
|
}
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
/* These should never fail. */
|
2019-06-05 07:29:05 +02:00
|
|
|
if (amount_sat_to_msat(&total, uc->fc->wtx->amount)
|
2019-02-21 04:45:55 +01:00
|
|
|
&& amount_msat_sub(&ours, total, uc->fc->push)) {
|
2019-05-20 07:07:40 +02:00
|
|
|
json_add_amount_msat_compat(response, ours,
|
|
|
|
"msatoshi_to_us", "to_us_msat");
|
|
|
|
json_add_amount_msat_compat(response, total,
|
|
|
|
"msatoshi_total", "total_msat");
|
2019-02-21 04:45:55 +01:00
|
|
|
}
|
2018-02-20 21:59:04 +01:00
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
|
2018-03-07 01:06:07 +01:00
|
|
|
/* Steals fields from uncommitted_channel: returns NULL if can't generate a
|
|
|
|
* key for this channel (shouldn't happen!). */
|
2018-02-20 21:59:04 +01:00
|
|
|
static struct channel *
|
|
|
|
wallet_commit_channel(struct lightningd *ld,
|
|
|
|
struct uncommitted_channel *uc,
|
|
|
|
struct bitcoin_tx *remote_commit,
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature *remote_commit_sig,
|
2018-02-20 21:59:04 +01:00
|
|
|
const struct bitcoin_txid *funding_txid,
|
|
|
|
u16 funding_outnum,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat funding,
|
|
|
|
struct amount_msat push,
|
2018-02-20 21:59:04 +01:00
|
|
|
u8 channel_flags,
|
|
|
|
struct channel_info *channel_info,
|
2019-05-01 00:58:09 +02:00
|
|
|
u32 feerate,
|
|
|
|
const u8 *remote_upfront_shutdown_script)
|
2018-02-20 21:59:04 +01:00
|
|
|
{
|
|
|
|
struct channel *channel;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat our_msat;
|
2018-03-07 01:06:07 +01:00
|
|
|
s64 final_key_idx;
|
|
|
|
|
|
|
|
/* Get a key to use for closing outputs from this tx */
|
|
|
|
final_key_idx = wallet_get_newindex(ld);
|
|
|
|
if (final_key_idx == -1) {
|
|
|
|
log_broken(uc->log, "Can't get final key index");
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
if (uc->fc) {
|
|
|
|
if (!amount_sat_sub_msat(&our_msat, funding, push)) {
|
|
|
|
log_broken(uc->log, "push %s exceeds funding %s",
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&push),
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
|
|
|
&funding));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
our_msat = push;
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
/* Feerates begin identical. */
|
|
|
|
channel_info->feerate_per_kw[LOCAL]
|
|
|
|
= channel_info->feerate_per_kw[REMOTE]
|
|
|
|
= feerate;
|
|
|
|
|
|
|
|
/* old_remote_per_commit not valid yet, copy valid one. */
|
|
|
|
channel_info->old_remote_per_commit = channel_info->remote_per_commit;
|
|
|
|
|
|
|
|
channel = new_channel(uc->peer, uc->dbid,
|
|
|
|
NULL, /* No shachain yet */
|
|
|
|
CHANNELD_AWAITING_LOCKIN,
|
|
|
|
uc->fc ? LOCAL : REMOTE,
|
|
|
|
uc->log,
|
2018-02-23 06:53:47 +01:00
|
|
|
take(uc->transient_billboard),
|
2018-02-20 21:59:04 +01:00
|
|
|
channel_flags,
|
|
|
|
&uc->our_config,
|
|
|
|
uc->minimum_depth,
|
|
|
|
1, 1, 0,
|
|
|
|
funding_txid,
|
|
|
|
funding_outnum,
|
2019-02-21 04:45:55 +01:00
|
|
|
funding,
|
|
|
|
push,
|
2018-02-20 21:59:04 +01:00
|
|
|
false, /* !remote_funding_locked */
|
|
|
|
NULL, /* no scid yet */
|
2018-03-31 02:21:13 +02:00
|
|
|
/* The three arguments below are msatoshi_to_us,
|
|
|
|
* msatoshi_to_us_min, and msatoshi_to_us_max.
|
|
|
|
* Because, this is a newly-funded channel,
|
|
|
|
* all three are same value. */
|
2019-02-21 04:45:55 +01:00
|
|
|
our_msat,
|
|
|
|
our_msat, /* msat_to_us_min */
|
|
|
|
our_msat, /* msat_to_us_max */
|
2018-02-20 21:59:04 +01:00
|
|
|
remote_commit,
|
|
|
|
remote_commit_sig,
|
|
|
|
NULL, /* No HTLC sigs yet */
|
|
|
|
channel_info,
|
|
|
|
NULL, /* No remote_shutdown_scriptpubkey yet */
|
2018-03-07 01:06:07 +01:00
|
|
|
final_key_idx, false,
|
2018-02-20 21:59:04 +01:00
|
|
|
NULL, /* No commit sent yet */
|
2018-08-02 08:49:56 +02:00
|
|
|
/* If we're fundee, could be a little before this
|
|
|
|
* in theory, but it's only used for timing out. */
|
|
|
|
get_block_height(ld->topology),
|
2018-04-26 06:51:01 +02:00
|
|
|
feerate, feerate,
|
|
|
|
/* We are connected */
|
2018-07-23 04:23:02 +02:00
|
|
|
true,
|
|
|
|
&uc->local_basepoints,
|
2018-08-17 07:06:36 +02:00
|
|
|
&uc->local_funding_pubkey,
|
2019-02-21 01:01:33 +01:00
|
|
|
NULL,
|
|
|
|
ld->config.fee_base,
|
2019-05-01 00:57:09 +02:00
|
|
|
ld->config.fee_per_satoshi,
|
2019-05-01 00:58:09 +02:00
|
|
|
remote_upfront_shutdown_script);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
/* Now we finally put it in the database. */
|
|
|
|
wallet_channel_insert(ld->wallet, channel);
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void funding_broadcast_failed(struct channel *channel,
|
2019-01-16 14:24:30 +01:00
|
|
|
int exitstatus, const char *msg)
|
2018-02-20 21:59:04 +01:00
|
|
|
{
|
2019-01-16 14:24:30 +01:00
|
|
|
struct funding_channel *fc = channel->peer->uncommitted_channel->fc;
|
|
|
|
struct command *cmd = fc->cmd;
|
|
|
|
|
|
|
|
/* Massage output into shape so it doesn't kill the JSON serialization */
|
|
|
|
char *output = tal_strjoin(cmd, tal_strsplit(cmd, msg, "\n", STR_NO_EMPTY), " ", STR_NO_TRAIL);
|
|
|
|
was_pending(command_fail(cmd, FUNDING_BROADCAST_FAIL,
|
|
|
|
"Error broadcasting funding transaction: %s", output));
|
|
|
|
|
|
|
|
/* Frees fc too */
|
|
|
|
tal_free(fc->uc);
|
|
|
|
|
|
|
|
/* Keep in state CHANNELD_AWAITING_LOCKIN until (manual) broadcast */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void funding_broadcast_success(struct channel *channel)
|
|
|
|
{
|
|
|
|
struct json_stream *response;
|
|
|
|
struct funding_channel *fc = channel->peer->uncommitted_channel->fc;
|
|
|
|
struct command *cmd = fc->cmd;
|
|
|
|
|
|
|
|
response = json_stream_success(cmd);
|
|
|
|
json_object_start(response, NULL);
|
2019-02-14 11:34:32 +01:00
|
|
|
json_add_string(response, "tx", fc->hextx);
|
2019-01-16 14:24:30 +01:00
|
|
|
json_add_txid(response, "txid", &channel->funding_txid);
|
|
|
|
json_add_string(response, "channel_id",
|
|
|
|
type_to_string(tmpctx, struct channel_id, &fc->cid));
|
|
|
|
json_object_end(response);
|
|
|
|
was_pending(command_success(cmd, response));
|
|
|
|
|
|
|
|
/* Frees fc too */
|
|
|
|
tal_free(fc->uc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void funding_broadcast_failed_or_success(struct channel *channel,
|
|
|
|
int exitstatus, const char *msg)
|
|
|
|
{
|
|
|
|
if (exitstatus == 0) {
|
|
|
|
funding_broadcast_success(channel);
|
|
|
|
} else {
|
|
|
|
funding_broadcast_failed(channel, exitstatus, msg);
|
|
|
|
}
|
2018-02-20 21:59:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void opening_funder_finished(struct subd *openingd, const u8 *resp,
|
|
|
|
const int *fds,
|
|
|
|
struct funding_channel *fc)
|
|
|
|
{
|
2019-01-16 14:24:30 +01:00
|
|
|
u8 *msg;
|
2018-02-20 21:59:04 +01:00
|
|
|
struct channel_info channel_info;
|
|
|
|
struct bitcoin_tx *fundingtx;
|
|
|
|
struct bitcoin_txid funding_txid, expected_txid;
|
|
|
|
struct pubkey changekey;
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature remote_commit_sig;
|
2018-02-20 21:59:04 +01:00
|
|
|
struct bitcoin_tx *remote_commit;
|
|
|
|
u16 funding_outnum;
|
|
|
|
u32 feerate;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat change;
|
2018-02-20 21:59:04 +01:00
|
|
|
struct channel *channel;
|
|
|
|
struct lightningd *ld = openingd->ld;
|
2019-05-01 00:58:09 +02:00
|
|
|
u8 *remote_upfront_shutdown_script;
|
2019-06-03 20:11:25 +02:00
|
|
|
struct per_peer_state *pps;
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
/* This is a new channel_info.their_config so set its ID to 0 */
|
|
|
|
channel_info.their_config.id = 0;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_opening_funder_reply(resp, resp,
|
2018-02-20 21:59:04 +01:00
|
|
|
&channel_info.their_config,
|
|
|
|
&remote_commit,
|
|
|
|
&remote_commit_sig,
|
2019-06-03 20:11:25 +02:00
|
|
|
&pps,
|
2018-02-20 21:59:04 +01:00
|
|
|
&channel_info.theirbase.revocation,
|
|
|
|
&channel_info.theirbase.payment,
|
|
|
|
&channel_info.theirbase.htlc,
|
|
|
|
&channel_info.theirbase.delayed_payment,
|
|
|
|
&channel_info.remote_per_commit,
|
|
|
|
&fc->uc->minimum_depth,
|
|
|
|
&channel_info.remote_fundingkey,
|
|
|
|
&expected_txid,
|
2018-07-31 06:34:42 +02:00
|
|
|
&feerate,
|
2019-05-01 00:58:09 +02:00
|
|
|
&fc->uc->our_config.channel_reserve,
|
|
|
|
&remote_upfront_shutdown_script)) {
|
2018-02-20 21:59:04 +01:00
|
|
|
log_broken(fc->uc->log,
|
|
|
|
"bad OPENING_FUNDER_REPLY %s",
|
|
|
|
tal_hex(resp, resp));
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_fail(fc->cmd, LIGHTNINGD,
|
|
|
|
"bad OPENING_FUNDER_REPLY %s",
|
|
|
|
tal_hex(fc->cmd, resp)));
|
2018-02-20 21:59:04 +01:00
|
|
|
goto failed;
|
|
|
|
}
|
2019-06-03 20:11:25 +02:00
|
|
|
per_peer_state_set_fds_arr(pps, fds);
|
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
log_debug(ld->log,
|
2018-03-15 05:30:39 +01:00
|
|
|
"%s", type_to_string(tmpctx, struct pubkey,
|
2018-02-20 21:59:04 +01:00
|
|
|
&channel_info.remote_per_commit));
|
|
|
|
|
|
|
|
/* Generate the funding tx. */
|
2019-06-05 07:29:05 +02:00
|
|
|
if (!amount_sat_eq(fc->wtx->change, AMOUNT_SAT(0))
|
2018-02-20 21:59:04 +01:00
|
|
|
&& !bip32_pubkey(ld->wallet->bip32_base,
|
2019-06-05 07:29:05 +02:00
|
|
|
&changekey, fc->wtx->change_key_index))
|
|
|
|
fatal("Error deriving change key %u", fc->wtx->change_key_index);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
fundingtx = funding_tx(tmpctx, &funding_outnum,
|
2019-06-05 07:29:05 +02:00
|
|
|
fc->wtx->utxos, fc->wtx->amount,
|
2018-07-23 04:23:02 +02:00
|
|
|
&fc->uc->local_funding_pubkey,
|
2018-02-20 21:59:04 +01:00
|
|
|
&channel_info.remote_fundingkey,
|
2019-06-05 07:29:05 +02:00
|
|
|
fc->wtx->change, &changekey,
|
2018-02-20 21:59:04 +01:00
|
|
|
ld->wallet->bip32_base);
|
|
|
|
|
|
|
|
log_debug(fc->uc->log, "Funding tx has %zi inputs, %zu outputs:",
|
2019-03-25 11:35:56 +01:00
|
|
|
fundingtx->wtx->num_inputs,
|
|
|
|
fundingtx->wtx->num_outputs);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2019-03-25 11:35:56 +01:00
|
|
|
for (size_t i = 0; i < fundingtx->wtx->num_inputs; i++) {
|
|
|
|
struct bitcoin_txid tmptxid;
|
|
|
|
bitcoin_tx_input_get_txid(fundingtx, i, &tmptxid);
|
2019-02-21 03:39:51 +01:00
|
|
|
log_debug(fc->uc->log, "%zi: %s (%s) %s\n",
|
|
|
|
i,
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
2019-06-05 07:29:05 +02:00
|
|
|
&fc->wtx->utxos[i]->amount),
|
|
|
|
fc->wtx->utxos[i]->is_p2sh ? "P2SH" : "SEGWIT",
|
2018-03-15 05:30:39 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_txid,
|
2019-03-25 11:35:56 +01:00
|
|
|
&tmptxid));
|
2018-02-20 21:59:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bitcoin_txid(fundingtx, &funding_txid);
|
|
|
|
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!bitcoin_txid_eq(&funding_txid, &expected_txid)) {
|
2018-02-20 21:59:04 +01:00
|
|
|
log_broken(fc->uc->log,
|
|
|
|
"Funding txid mismatch:"
|
2019-02-21 03:38:42 +01:00
|
|
|
" amount %s change %s"
|
2018-02-20 21:59:04 +01:00
|
|
|
" changeidx %u"
|
|
|
|
" localkey %s remotekey %s",
|
2019-02-21 03:38:42 +01:00
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
2019-06-05 07:29:05 +02:00
|
|
|
&fc->wtx->amount),
|
2019-02-21 03:38:42 +01:00
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
2019-06-05 07:29:05 +02:00
|
|
|
&fc->wtx->change),
|
|
|
|
fc->wtx->change_key_index,
|
2018-02-20 21:59:04 +01:00
|
|
|
type_to_string(fc, struct pubkey,
|
2018-07-23 04:23:02 +02:00
|
|
|
&fc->uc->local_funding_pubkey),
|
2018-02-20 21:59:04 +01:00
|
|
|
type_to_string(fc, struct pubkey,
|
|
|
|
&channel_info.remote_fundingkey));
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_fail(fc->cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"Funding txid mismatch:"
|
2019-02-21 03:38:42 +01:00
|
|
|
" amount %s change %s"
|
2018-12-16 05:53:06 +01:00
|
|
|
" changeidx %u"
|
|
|
|
" localkey %s remotekey %s",
|
2019-02-21 03:38:42 +01:00
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_sat,
|
2019-06-05 07:29:05 +02:00
|
|
|
&fc->wtx->amount),
|
2019-02-21 03:38:42 +01:00
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_sat,
|
2019-06-05 07:29:05 +02:00
|
|
|
&fc->wtx->change),
|
|
|
|
fc->wtx->change_key_index,
|
2018-12-16 05:53:06 +01:00
|
|
|
type_to_string(fc, struct pubkey,
|
|
|
|
&fc->uc->local_funding_pubkey),
|
|
|
|
type_to_string(fc, struct pubkey,
|
|
|
|
&channel_info.remote_fundingkey)));
|
2018-02-20 21:59:04 +01:00
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Steals fields from uc */
|
|
|
|
channel = wallet_commit_channel(ld, fc->uc,
|
|
|
|
remote_commit,
|
|
|
|
&remote_commit_sig,
|
|
|
|
&funding_txid,
|
|
|
|
funding_outnum,
|
2019-06-05 07:29:05 +02:00
|
|
|
fc->wtx->amount,
|
2019-02-21 04:45:55 +01:00
|
|
|
fc->push,
|
2018-02-20 21:59:04 +01:00
|
|
|
fc->channel_flags,
|
|
|
|
&channel_info,
|
2019-05-01 00:58:09 +02:00
|
|
|
feerate,
|
|
|
|
remote_upfront_shutdown_script);
|
2018-03-07 01:06:07 +01:00
|
|
|
if (!channel) {
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_fail(fc->cmd, LIGHTNINGD,
|
|
|
|
"Key generation failure"));
|
2018-03-07 01:06:07 +01:00
|
|
|
goto failed;
|
|
|
|
}
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
/* Get HSM to sign the funding tx. */
|
|
|
|
log_debug(channel->log, "Getting HSM to sign funding tx");
|
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
msg = towire_hsm_sign_funding(tmpctx, channel->funding,
|
2019-06-05 07:29:05 +02:00
|
|
|
fc->wtx->change,
|
|
|
|
fc->wtx->change_key_index,
|
2018-07-23 04:23:02 +02:00
|
|
|
&fc->uc->local_funding_pubkey,
|
2018-02-20 21:59:04 +01:00
|
|
|
&channel_info.remote_fundingkey,
|
2019-06-05 07:29:05 +02:00
|
|
|
fc->wtx->utxos);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
if (!wire_sync_write(ld->hsm_fd, take(msg)))
|
|
|
|
fatal("Could not write to HSM: %s", strerror(errno));
|
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
msg = wire_sync_read(fc, ld->hsm_fd);
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_hsm_sign_funding_reply(tmpctx, msg, &fundingtx))
|
2018-02-20 21:59:04 +01:00
|
|
|
fatal("HSM gave bad sign_funding_reply %s",
|
|
|
|
tal_hex(msg, resp));
|
|
|
|
|
|
|
|
/* Extract the change output and add it to the DB */
|
2019-02-21 04:45:55 +01:00
|
|
|
wallet_extract_owned_outputs(ld->wallet, fundingtx, NULL, &change);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2018-03-21 23:39:21 +01:00
|
|
|
/* Make sure we recognize our change output by its scriptpubkey in
|
|
|
|
* future. This assumes that we have only two outputs, may not be true
|
|
|
|
* if we add support for multifundchannel */
|
2019-03-25 11:35:56 +01:00
|
|
|
if (fundingtx->wtx->num_outputs == 2)
|
|
|
|
txfilter_add_scriptpubkey(ld->owned_txfilter, bitcoin_tx_output_get_script(tmpctx, fundingtx, !funding_outnum));
|
2018-03-21 23:39:21 +01:00
|
|
|
|
2019-01-16 14:24:30 +01:00
|
|
|
/* We need these to compose cmd's response in funding_broadcast_success */
|
2019-02-14 11:34:32 +01:00
|
|
|
fc->hextx = tal_hex(fc, linearize_tx(fc->cmd, fundingtx));
|
2019-01-16 14:24:30 +01:00
|
|
|
derive_channel_id(&fc->cid, &channel->funding_txid, funding_outnum);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2019-01-16 14:24:30 +01:00
|
|
|
/* Send it out and watch for confirms. */
|
|
|
|
broadcast_tx(ld->topology, channel, fundingtx, funding_broadcast_failed_or_success);
|
2018-02-20 21:59:04 +01:00
|
|
|
channel_watch_funding(ld, channel);
|
|
|
|
|
2019-01-16 14:24:30 +01:00
|
|
|
/* Mark consumed outputs as spent */
|
2019-06-05 07:29:05 +02:00
|
|
|
wallet_confirm_utxos(ld->wallet, fc->wtx->utxos);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2019-01-16 14:24:30 +01:00
|
|
|
/* Start normal channel daemon. */
|
2019-06-03 20:11:25 +02:00
|
|
|
peer_start_channeld(channel, pps, NULL, false);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
subd_release_channel(openingd, fc->uc);
|
2018-04-23 12:08:01 +02:00
|
|
|
fc->uc->openingd = NULL;
|
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
subd_release_channel(openingd, fc->uc);
|
2018-04-23 12:08:01 +02:00
|
|
|
fc->uc->openingd = NULL;
|
2018-02-20 21:59:04 +01:00
|
|
|
/* Frees fc too, and tmpctx */
|
|
|
|
tal_free(fc->uc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void opening_fundee_finished(struct subd *openingd,
|
|
|
|
const u8 *reply,
|
|
|
|
const int *fds,
|
|
|
|
struct uncommitted_channel *uc)
|
|
|
|
{
|
|
|
|
u8 *funding_signed;
|
|
|
|
struct channel_info channel_info;
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature remote_commit_sig;
|
2018-02-20 21:59:04 +01:00
|
|
|
struct bitcoin_tx *remote_commit;
|
|
|
|
struct lightningd *ld = openingd->ld;
|
|
|
|
struct bitcoin_txid funding_txid;
|
|
|
|
u16 funding_outnum;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat funding;
|
|
|
|
struct amount_msat push;
|
2018-02-20 21:59:04 +01:00
|
|
|
u32 feerate;
|
|
|
|
u8 channel_flags;
|
|
|
|
struct channel *channel;
|
2019-05-01 00:58:09 +02:00
|
|
|
u8 *remote_upfront_shutdown_script;
|
2019-06-03 20:11:25 +02:00
|
|
|
struct per_peer_state *pps;
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
log_debug(uc->log, "Got opening_fundee_finish_response");
|
|
|
|
|
|
|
|
/* This is a new channel_info.their_config, set its ID to 0 */
|
|
|
|
channel_info.their_config.id = 0;
|
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
if (!fromwire_opening_fundee(tmpctx, reply,
|
2018-02-20 21:59:04 +01:00
|
|
|
&channel_info.their_config,
|
|
|
|
&remote_commit,
|
|
|
|
&remote_commit_sig,
|
2019-06-03 20:11:25 +02:00
|
|
|
&pps,
|
2018-02-20 21:59:04 +01:00
|
|
|
&channel_info.theirbase.revocation,
|
|
|
|
&channel_info.theirbase.payment,
|
|
|
|
&channel_info.theirbase.htlc,
|
|
|
|
&channel_info.theirbase.delayed_payment,
|
|
|
|
&channel_info.remote_per_commit,
|
|
|
|
&channel_info.remote_fundingkey,
|
|
|
|
&funding_txid,
|
|
|
|
&funding_outnum,
|
2019-02-21 04:45:55 +01:00
|
|
|
&funding,
|
|
|
|
&push,
|
2018-02-20 21:59:04 +01:00
|
|
|
&channel_flags,
|
|
|
|
&feerate,
|
2018-07-31 06:34:42 +02:00
|
|
|
&funding_signed,
|
2019-05-01 00:58:09 +02:00
|
|
|
&uc->our_config.channel_reserve,
|
|
|
|
&remote_upfront_shutdown_script)) {
|
2018-02-20 21:59:04 +01:00
|
|
|
log_broken(uc->log, "bad OPENING_FUNDEE_REPLY %s",
|
|
|
|
tal_hex(reply, reply));
|
2018-08-09 02:25:29 +02:00
|
|
|
uncommitted_channel_disconnect(uc, "bad OPENING_FUNDEE_REPLY");
|
|
|
|
goto failed;
|
2018-02-20 21:59:04 +01:00
|
|
|
}
|
2019-06-03 20:11:25 +02:00
|
|
|
per_peer_state_set_fds_arr(pps, fds);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2018-08-09 02:25:29 +02:00
|
|
|
/* openingd should never accept them funding channel in this case. */
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
if (peer_active_channel(uc->peer)) {
|
2018-08-09 02:25:29 +02:00
|
|
|
log_broken(uc->log, "openingd accepted peer funding channel");
|
2018-08-09 02:25:29 +02:00
|
|
|
uncommitted_channel_disconnect(uc, "already have active channel");
|
|
|
|
goto failed;
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
/* Consumes uc */
|
|
|
|
channel = wallet_commit_channel(ld, uc,
|
|
|
|
remote_commit,
|
|
|
|
&remote_commit_sig,
|
|
|
|
&funding_txid,
|
|
|
|
funding_outnum,
|
2019-02-21 04:45:55 +01:00
|
|
|
funding,
|
|
|
|
push,
|
2018-02-20 21:59:04 +01:00
|
|
|
channel_flags,
|
|
|
|
&channel_info,
|
2019-05-01 00:58:09 +02:00
|
|
|
feerate,
|
|
|
|
remote_upfront_shutdown_script);
|
2018-03-07 01:06:07 +01:00
|
|
|
if (!channel) {
|
2018-08-09 02:25:29 +02:00
|
|
|
uncommitted_channel_disconnect(uc, "Commit channel failed");
|
|
|
|
goto failed;
|
2018-03-07 01:06:07 +01:00
|
|
|
}
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
log_debug(channel->log, "Watching funding tx %s",
|
|
|
|
type_to_string(reply, struct bitcoin_txid,
|
|
|
|
&channel->funding_txid));
|
|
|
|
|
|
|
|
channel_watch_funding(ld, channel);
|
|
|
|
|
|
|
|
/* On to normal operation! */
|
2019-06-03 20:11:25 +02:00
|
|
|
peer_start_channeld(channel, pps, funding_signed, false);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
subd_release_channel(openingd, uc);
|
2018-04-23 12:08:01 +02:00
|
|
|
uc->openingd = NULL;
|
2018-02-20 21:59:04 +01:00
|
|
|
tal_free(uc);
|
2018-08-09 02:25:29 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
close(fds[0]);
|
|
|
|
close(fds[1]);
|
2019-05-04 07:53:13 +02:00
|
|
|
close(fds[3]);
|
2018-08-09 02:25:29 +02:00
|
|
|
tal_free(uc);
|
2018-02-20 21:59:04 +01:00
|
|
|
}
|
|
|
|
|
2018-08-09 02:25:29 +02:00
|
|
|
static void opening_funder_failed(struct subd *openingd, const u8 *msg,
|
|
|
|
struct uncommitted_channel *uc)
|
|
|
|
{
|
|
|
|
char *desc;
|
|
|
|
|
|
|
|
if (!fromwire_opening_funder_failed(msg, msg, &desc)) {
|
|
|
|
log_broken(uc->log,
|
|
|
|
"bad OPENING_FUNDER_FAILED %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_fail(uc->fc->cmd, LIGHTNINGD,
|
|
|
|
"bad OPENING_FUNDER_FAILED %s",
|
|
|
|
tal_hex(uc->fc->cmd, msg)));
|
2018-08-09 02:25:29 +02:00
|
|
|
tal_free(uc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_fail(uc->fc->cmd, LIGHTNINGD, "%s", desc));
|
2018-08-09 02:25:29 +02:00
|
|
|
|
|
|
|
/* Clear uc->fc, so we can try again, and so we don't fail twice
|
|
|
|
* if they close. */
|
|
|
|
uc->fc = tal_free(uc->fc);
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
static void opening_channel_errmsg(struct uncommitted_channel *uc,
|
2019-06-03 20:11:25 +02:00
|
|
|
struct per_peer_state *pps,
|
2018-03-05 17:40:50 +01:00
|
|
|
const struct channel_id *channel_id UNUSED,
|
2018-02-20 21:59:04 +01:00
|
|
|
const char *desc,
|
2018-08-09 02:25:29 +02:00
|
|
|
const u8 *err_for_them UNUSED)
|
2018-02-20 21:59:04 +01:00
|
|
|
{
|
2019-05-04 07:53:12 +02:00
|
|
|
/* Close fds, if any. */
|
2019-06-03 20:11:25 +02:00
|
|
|
tal_free(pps);
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
uncommitted_channel_disconnect(uc, desc);
|
2018-02-20 21:59:04 +01:00
|
|
|
tal_free(uc);
|
|
|
|
}
|
|
|
|
|
2018-02-23 06:53:47 +01:00
|
|
|
/* There's nothing permanent in an unconfirmed transaction */
|
|
|
|
static void opening_channel_set_billboard(struct uncommitted_channel *uc,
|
|
|
|
bool perm UNUSED,
|
|
|
|
const char *happenings TAKES)
|
|
|
|
{
|
2018-02-23 06:53:47 +01:00
|
|
|
uc->transient_billboard = tal_free(uc->transient_billboard);
|
|
|
|
if (happenings)
|
|
|
|
uc->transient_billboard = tal_strdup(uc, happenings);
|
2018-02-23 06:53:47 +01:00
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
static void destroy_uncommitted_channel(struct uncommitted_channel *uc)
|
|
|
|
{
|
2018-04-23 12:08:01 +02:00
|
|
|
if (uc->openingd) {
|
|
|
|
struct subd *openingd = uc->openingd;
|
|
|
|
uc->openingd = NULL;
|
|
|
|
subd_release_channel(openingd, uc);
|
|
|
|
}
|
|
|
|
|
2018-08-02 08:49:55 +02:00
|
|
|
/* This is how shutdown_subdaemons tells us not to delete from db! */
|
|
|
|
if (!uc->peer->uncommitted_channel)
|
|
|
|
return;
|
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
uc->peer->uncommitted_channel = NULL;
|
|
|
|
|
2018-08-02 08:49:55 +02:00
|
|
|
maybe_delete_peer(uc->peer);
|
2018-02-20 21:59:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct uncommitted_channel *
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
new_uncommitted_channel(struct peer *peer)
|
2018-02-20 21:59:04 +01:00
|
|
|
{
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
struct lightningd *ld = peer->ld;
|
2018-02-20 21:59:04 +01:00
|
|
|
struct uncommitted_channel *uc = tal(ld, struct uncommitted_channel);
|
2019-02-18 03:43:29 +01:00
|
|
|
const char *idname;
|
2018-02-20 21:59:04 +01:00
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
uc->peer = peer;
|
|
|
|
assert(!peer->uncommitted_channel);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2018-02-23 06:53:47 +01:00
|
|
|
uc->transient_billboard = NULL;
|
2018-02-20 21:59:04 +01:00
|
|
|
uc->dbid = wallet_get_channel_dbid(ld->wallet);
|
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
idname = type_to_string(uc, struct node_id, &uc->peer->id);
|
2018-02-20 21:59:04 +01:00
|
|
|
uc->log = new_log(uc, uc->peer->log_book, "%s chan #%"PRIu64":",
|
|
|
|
idname, uc->dbid);
|
|
|
|
tal_free(idname);
|
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
uc->fc = NULL;
|
2018-02-20 21:59:04 +01:00
|
|
|
uc->our_config.id = 0;
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
get_channel_basepoints(ld, &uc->peer->id, uc->dbid,
|
|
|
|
&uc->local_basepoints, &uc->local_funding_pubkey);
|
2018-07-23 04:23:02 +02:00
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
uc->peer->uncommitted_channel = uc;
|
|
|
|
tal_add_destructor(uc, destroy_uncommitted_channel);
|
|
|
|
|
|
|
|
return uc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void channel_config(struct lightningd *ld,
|
|
|
|
struct channel_config *ours,
|
|
|
|
u32 *max_to_self_delay,
|
2019-02-21 04:45:54 +01:00
|
|
|
struct amount_msat *min_effective_htlc_capacity)
|
2018-02-20 21:59:04 +01:00
|
|
|
{
|
2019-04-15 22:48:12 +02:00
|
|
|
struct amount_msat dust_limit;
|
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
/* FIXME: depend on feerate. */
|
|
|
|
*max_to_self_delay = ld->config.locktime_max;
|
2019-04-03 10:20:38 +02:00
|
|
|
|
|
|
|
/* Take minimal effective capacity from config min_capacity_sat */
|
2019-04-15 22:48:08 +02:00
|
|
|
if (!amount_msat_from_sat_u64(min_effective_htlc_capacity,
|
|
|
|
ld->config.min_capacity_sat))
|
|
|
|
fatal("amount_msat overflow for config.min_capacity_sat");
|
2019-04-15 22:48:12 +02:00
|
|
|
/* Substract 2 * dust_limit, so fundchannel with min value is possible */
|
|
|
|
if (!amount_sat_to_msat(&dust_limit, get_chainparams(ld)->dust_limit))
|
|
|
|
fatal("amount_msat overflow for dustlimit");
|
|
|
|
if (!amount_msat_sub(min_effective_htlc_capacity,
|
|
|
|
*min_effective_htlc_capacity,
|
|
|
|
dust_limit))
|
|
|
|
*min_effective_htlc_capacity = AMOUNT_MSAT(0);
|
|
|
|
if (!amount_msat_sub(min_effective_htlc_capacity,
|
|
|
|
*min_effective_htlc_capacity,
|
|
|
|
dust_limit))
|
|
|
|
*min_effective_htlc_capacity = AMOUNT_MSAT(0);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The sending node SHOULD:
|
|
|
|
*...
|
|
|
|
* - set `dust_limit_satoshis` to a sufficient value to allow
|
|
|
|
* commitment transactions to propagate through the Bitcoin network.
|
2018-02-20 21:59:04 +01:00
|
|
|
*/
|
2019-02-22 20:38:56 +01:00
|
|
|
ours->dust_limit = get_chainparams(ld)->dust_limit;
|
2019-02-21 04:45:54 +01:00
|
|
|
ours->max_htlc_value_in_flight = AMOUNT_MSAT(UINT64_MAX);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
/* Don't care */
|
2019-02-21 04:45:54 +01:00
|
|
|
ours->htlc_minimum = AMOUNT_MSAT(0);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The sending node SHOULD:
|
|
|
|
* - set `to_self_delay` sufficient to ensure the sender can
|
|
|
|
* irreversibly spend a commitment transaction output, in case of
|
|
|
|
* misbehavior by the receiver.
|
2018-02-20 21:59:04 +01:00
|
|
|
*/
|
|
|
|
ours->to_self_delay = ld->config.locktime_blocks;
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The receiving node MUST fail the channel if:
|
|
|
|
*...
|
|
|
|
* - `max_accepted_htlcs` is greater than 483.
|
2018-02-20 21:59:04 +01:00
|
|
|
*/
|
|
|
|
ours->max_accepted_htlcs = 483;
|
|
|
|
|
|
|
|
/* This is filled in by lightning_openingd, for consistency. */
|
2019-02-21 04:45:54 +01:00
|
|
|
ours->channel_reserve = AMOUNT_SAT(UINT64_MAX);
|
2018-07-31 06:34:42 +02:00
|
|
|
}
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2019-05-21 00:45:20 +02:00
|
|
|
struct openchannel_hook_payload {
|
|
|
|
struct subd *openingd;
|
|
|
|
struct amount_sat funding_satoshis;
|
|
|
|
struct amount_msat push_msat;
|
|
|
|
struct amount_sat dust_limit_satoshis;
|
|
|
|
struct amount_msat max_htlc_value_in_flight_msat;
|
|
|
|
struct amount_sat channel_reserve_satoshis;
|
|
|
|
struct amount_msat htlc_minimum_msat;
|
|
|
|
u32 feerate_per_kw;
|
|
|
|
u16 to_self_delay;
|
|
|
|
u16 max_accepted_htlcs;
|
|
|
|
u8 channel_flags;
|
|
|
|
u8 *shutdown_scriptpubkey;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
openchannel_hook_serialize(struct openchannel_hook_payload *payload,
|
|
|
|
struct json_stream *stream)
|
|
|
|
{
|
|
|
|
struct uncommitted_channel *uc = payload->openingd->channel;
|
|
|
|
json_object_start(stream, "openchannel");
|
|
|
|
json_add_node_id(stream, "id", &uc->peer->id);
|
|
|
|
json_add_amount_sat_only(stream, "funding_satoshis",
|
|
|
|
payload->funding_satoshis);
|
|
|
|
json_add_amount_msat_only(stream, "push_msat", payload->push_msat);
|
|
|
|
json_add_amount_sat_only(stream, "dust_limit_satoshis",
|
|
|
|
payload->dust_limit_satoshis);
|
|
|
|
json_add_amount_msat_only(stream, "max_htlc_value_in_flight_msat",
|
|
|
|
payload->max_htlc_value_in_flight_msat);
|
|
|
|
json_add_amount_sat_only(stream, "channel_reserve_satoshis",
|
|
|
|
payload->channel_reserve_satoshis);
|
|
|
|
json_add_amount_msat_only(stream, "htlc_minimum_msat",
|
|
|
|
payload->htlc_minimum_msat);
|
|
|
|
json_add_num(stream, "feerate_per_kw", payload->feerate_per_kw);
|
|
|
|
json_add_num(stream, "to_self_delay", payload->to_self_delay);
|
|
|
|
json_add_num(stream, "max_accepted_htlcs", payload->max_accepted_htlcs);
|
|
|
|
json_add_num(stream, "channel_flags", payload->channel_flags);
|
|
|
|
if (tal_count(payload->shutdown_scriptpubkey) != 0)
|
2019-06-05 07:29:01 +02:00
|
|
|
json_add_hex_talarr(stream, "shutdown_scriptpubkey",
|
|
|
|
payload->shutdown_scriptpubkey);
|
2019-05-21 00:45:20 +02:00
|
|
|
json_object_end(stream); /* .openchannel */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* openingd dies? Remove openingd ptr from payload */
|
|
|
|
static void openchannel_payload_remove_openingd(struct subd *openingd,
|
|
|
|
struct openchannel_hook_payload *payload)
|
|
|
|
{
|
|
|
|
assert(payload->openingd == openingd);
|
|
|
|
payload->openingd = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void openchannel_hook_cb(struct openchannel_hook_payload *payload,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *toks)
|
|
|
|
{
|
|
|
|
struct subd *openingd = payload->openingd;
|
|
|
|
const char *errmsg = NULL;
|
|
|
|
|
|
|
|
/* We want to free this, whatever happens. */
|
|
|
|
tal_steal(tmpctx, payload);
|
|
|
|
|
|
|
|
/* If openingd went away, don't send it anything! */
|
|
|
|
if (!openingd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tal_del_destructor2(openingd, openchannel_payload_remove_openingd, payload);
|
|
|
|
|
|
|
|
/* If we had a hook, check what it says */
|
|
|
|
if (buffer) {
|
|
|
|
const jsmntok_t *t = json_get_member(buffer, toks, "result");
|
|
|
|
if (!t)
|
|
|
|
fatal("Plugin returned an invalid response to the"
|
|
|
|
" openchannel hook: %.*s",
|
|
|
|
toks[0].end - toks[0].start,
|
|
|
|
buffer + toks[0].start);
|
|
|
|
|
|
|
|
if (json_tok_streq(buffer, t, "reject")) {
|
|
|
|
t = json_get_member(buffer, toks, "error_message");
|
|
|
|
if (t)
|
|
|
|
errmsg = json_strdup(tmpctx, buffer, t);
|
|
|
|
else
|
|
|
|
errmsg = "";
|
|
|
|
log_debug(openingd->ld->log,
|
|
|
|
"openchannel_hook_cb says '%s'",
|
|
|
|
errmsg);
|
|
|
|
} else if (!json_tok_streq(buffer, t, "continue"))
|
|
|
|
fatal("Plugin returned an invalid result for the "
|
|
|
|
"openchannel hook: %.*s",
|
|
|
|
t->end - t->start, buffer + t->start);
|
|
|
|
}
|
|
|
|
|
|
|
|
subd_send_msg(openingd,
|
|
|
|
take(towire_opening_got_offer_reply(NULL, errmsg)));
|
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_PLUGIN_HOOK(openchannel,
|
|
|
|
openchannel_hook_cb,
|
|
|
|
struct openchannel_hook_payload *,
|
|
|
|
openchannel_hook_serialize,
|
|
|
|
struct openchannel_hook_payload *);
|
|
|
|
|
2019-05-20 07:05:24 +02:00
|
|
|
static void opening_got_offer(struct subd *openingd,
|
|
|
|
const u8 *msg,
|
|
|
|
struct uncommitted_channel *uc)
|
|
|
|
{
|
2019-05-21 00:45:20 +02:00
|
|
|
struct openchannel_hook_payload *payload;
|
2019-05-20 07:05:24 +02:00
|
|
|
|
|
|
|
/* Tell them they can't open, if we already have open channel. */
|
2019-05-21 00:45:20 +02:00
|
|
|
if (peer_active_channel(uc->peer)) {
|
|
|
|
subd_send_msg(openingd,
|
|
|
|
take(towire_opening_got_offer_reply(NULL,
|
|
|
|
"Already have active channel")));
|
|
|
|
return;
|
|
|
|
}
|
2019-05-20 07:05:24 +02:00
|
|
|
|
2019-05-21 00:45:20 +02:00
|
|
|
payload = tal(openingd->ld, struct openchannel_hook_payload);
|
|
|
|
payload->openingd = openingd;
|
|
|
|
if (!fromwire_opening_got_offer(payload, msg,
|
|
|
|
&payload->funding_satoshis,
|
|
|
|
&payload->push_msat,
|
|
|
|
&payload->dust_limit_satoshis,
|
|
|
|
&payload->max_htlc_value_in_flight_msat,
|
|
|
|
&payload->channel_reserve_satoshis,
|
|
|
|
&payload->htlc_minimum_msat,
|
|
|
|
&payload->feerate_per_kw,
|
|
|
|
&payload->to_self_delay,
|
|
|
|
&payload->max_accepted_htlcs,
|
|
|
|
&payload->channel_flags,
|
|
|
|
&payload->shutdown_scriptpubkey)) {
|
|
|
|
log_broken(openingd->log, "Malformed opening_got_offer %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
tal_free(openingd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tal_add_destructor2(openingd, openchannel_payload_remove_openingd, payload);
|
|
|
|
plugin_hook_call_openchannel(openingd->ld, payload, payload);
|
2019-05-20 07:05:24 +02:00
|
|
|
}
|
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
static unsigned int openingd_msg(struct subd *openingd,
|
|
|
|
const u8 *msg, const int *fds)
|
|
|
|
{
|
|
|
|
enum opening_wire_type t = fromwire_peektype(msg);
|
|
|
|
struct uncommitted_channel *uc = openingd->channel;
|
|
|
|
|
|
|
|
switch (t) {
|
|
|
|
case WIRE_OPENING_FUNDER_REPLY:
|
|
|
|
if (!uc->fc) {
|
|
|
|
log_broken(openingd->log, "Unexpected FUNDER_REPLY %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
tal_free(openingd);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-05-04 07:53:13 +02:00
|
|
|
if (tal_count(fds) != 3)
|
|
|
|
return 3;
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
opening_funder_finished(openingd, msg, fds, uc->fc);
|
|
|
|
return 0;
|
|
|
|
|
2018-08-09 02:25:29 +02:00
|
|
|
case WIRE_OPENING_FUNDER_FAILED:
|
|
|
|
if (!uc->fc) {
|
|
|
|
log_broken(openingd->log, "Unexpected FUNDER_FAILED %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
tal_free(openingd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
opening_funder_failed(openingd, msg, uc);
|
|
|
|
return 0;
|
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
case WIRE_OPENING_FUNDEE:
|
2019-05-04 07:53:13 +02:00
|
|
|
if (tal_count(fds) != 3)
|
|
|
|
return 3;
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
opening_fundee_finished(openingd, msg, fds, uc);
|
|
|
|
return 0;
|
|
|
|
|
2019-05-20 07:05:24 +02:00
|
|
|
case WIRE_OPENING_GOT_OFFER:
|
|
|
|
opening_got_offer(openingd, msg, uc);
|
|
|
|
return 0;
|
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
/* We send these! */
|
|
|
|
case WIRE_OPENING_INIT:
|
|
|
|
case WIRE_OPENING_FUNDER:
|
2019-05-20 07:05:24 +02:00
|
|
|
case WIRE_OPENING_GOT_OFFER_REPLY:
|
2018-11-22 03:17:29 +01:00
|
|
|
case WIRE_OPENING_DEV_MEMLEAK:
|
|
|
|
/* Replies never get here */
|
|
|
|
case WIRE_OPENING_DEV_MEMLEAK_REPLY:
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
log_broken(openingd->log, "Unexpected msg %s: %s",
|
|
|
|
opening_wire_type_name(t), tal_hex(tmpctx, msg));
|
|
|
|
tal_free(openingd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void peer_start_openingd(struct peer *peer,
|
2019-06-03 20:11:25 +02:00
|
|
|
struct per_peer_state *pps,
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
const u8 *send_msg)
|
2018-02-20 21:59:04 +01:00
|
|
|
{
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
int hsmfd;
|
2018-05-17 06:46:22 +02:00
|
|
|
u32 max_to_self_delay;
|
2019-02-21 04:45:54 +01:00
|
|
|
struct amount_msat min_effective_htlc_capacity;
|
2018-02-20 21:59:04 +01:00
|
|
|
struct uncommitted_channel *uc;
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
const u8 *msg;
|
2018-02-20 21:59:04 +01:00
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
assert(!peer->uncommitted_channel);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
uc = peer->uncommitted_channel = new_uncommitted_channel(peer);
|
2018-02-20 21:59:04 +01:00
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
hsmfd = hsm_get_client_fd(peer->ld, &uc->peer->id, uc->dbid,
|
2018-07-23 04:23:03 +02:00
|
|
|
HSM_CAP_COMMITMENT_POINT
|
|
|
|
| HSM_CAP_SIGN_REMOTE_TX);
|
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
uc->openingd = new_channel_subd(peer->ld,
|
|
|
|
"lightning_openingd",
|
|
|
|
uc, uc->log,
|
|
|
|
true, opening_wire_type_name,
|
|
|
|
openingd_msg,
|
2018-02-20 21:59:04 +01:00
|
|
|
opening_channel_errmsg,
|
2018-02-23 06:53:47 +01:00
|
|
|
opening_channel_set_billboard,
|
2019-06-03 20:11:25 +02:00
|
|
|
take(&pps->peer_fd),
|
|
|
|
take(&pps->gossip_fd),
|
|
|
|
take(&pps->gossip_store_fd),
|
2018-07-23 04:23:03 +02:00
|
|
|
take(&hsmfd), NULL);
|
2018-02-20 21:59:04 +01:00
|
|
|
if (!uc->openingd) {
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
uncommitted_channel_disconnect(uc,
|
|
|
|
tal_fmt(tmpctx,
|
|
|
|
"Running lightning_openingd: %s",
|
|
|
|
strerror(errno)));
|
|
|
|
return;
|
2018-02-20 21:59:04 +01:00
|
|
|
}
|
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
channel_config(peer->ld, &uc->our_config,
|
|
|
|
&max_to_self_delay,
|
2019-02-21 04:45:54 +01:00
|
|
|
&min_effective_htlc_capacity);
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The sender:
|
|
|
|
* - SHOULD set `minimum_depth` to a number of blocks it considers
|
|
|
|
* reasonable to avoid double-spending of the funding transaction.
|
2018-02-20 21:59:04 +01:00
|
|
|
*/
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
uc->minimum_depth = peer->ld->config.anchor_confirms;
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2018-09-08 13:37:29 +02:00
|
|
|
msg = towire_opening_init(NULL,
|
|
|
|
&get_chainparams(peer->ld)->genesis_blockhash,
|
2018-02-20 21:59:04 +01:00
|
|
|
&uc->our_config,
|
|
|
|
max_to_self_delay,
|
2019-02-21 04:45:55 +01:00
|
|
|
min_effective_htlc_capacity,
|
2019-06-03 20:11:25 +02:00
|
|
|
pps, &uc->local_basepoints,
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
&uc->local_funding_pubkey,
|
|
|
|
uc->minimum_depth,
|
2018-08-23 01:27:25 +02:00
|
|
|
feerate_min(peer->ld, NULL),
|
|
|
|
feerate_max(peer->ld, NULL),
|
2019-05-01 00:58:09 +02:00
|
|
|
peer->localfeatures,
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
send_msg);
|
2018-02-20 21:59:04 +01:00
|
|
|
subd_send_msg(uc->openingd, take(msg));
|
2018-04-23 12:08:01 +02:00
|
|
|
}
|
|
|
|
|
2018-03-21 00:06:04 +01:00
|
|
|
/**
|
|
|
|
* json_fund_channel - Entrypoint for funding a channel
|
|
|
|
*/
|
2018-12-16 05:52:06 +01:00
|
|
|
static struct command_result *json_fund_channel(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *obj UNNEEDED,
|
|
|
|
const jsmntok_t *params)
|
2018-02-20 21:59:04 +01:00
|
|
|
{
|
2018-12-16 05:50:06 +01:00
|
|
|
struct command_result *res;
|
2018-04-05 21:19:47 +02:00
|
|
|
struct funding_channel * fc = tal(cmd, struct funding_channel);
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id *id;
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
struct peer *peer;
|
|
|
|
struct channel *channel;
|
2019-02-21 20:34:49 +01:00
|
|
|
u32 *feerate_per_kw, *minconf, maxheight;
|
2018-12-08 00:38:41 +01:00
|
|
|
bool *announce_channel;
|
2018-02-20 21:59:04 +01:00
|
|
|
u8 *msg;
|
2019-02-21 03:38:42 +01:00
|
|
|
struct amount_sat max_funding_satoshi;
|
|
|
|
|
2019-02-21 04:45:54 +01:00
|
|
|
max_funding_satoshi = get_chainparams(cmd->ld)->max_funding;
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2018-04-05 21:19:47 +02:00
|
|
|
fc->cmd = cmd;
|
|
|
|
fc->uc = NULL;
|
2019-06-05 07:29:05 +02:00
|
|
|
fc->wtx = tal(fc, struct wallet_tx);
|
|
|
|
wtx_init(cmd, fc->wtx, max_funding_satoshi);
|
2018-07-20 03:14:02 +02:00
|
|
|
if (!param(fc->cmd, buffer, params,
|
2019-04-08 11:58:32 +02:00
|
|
|
p_req("id", param_node_id, &id),
|
2019-06-05 07:29:05 +02:00
|
|
|
p_req("satoshi", param_wtx, fc->wtx),
|
2018-12-16 05:50:06 +01:00
|
|
|
p_opt("feerate", param_feerate, &feerate_per_kw),
|
|
|
|
p_opt_def("announce", param_bool, &announce_channel, true),
|
2019-02-21 21:19:14 +01:00
|
|
|
p_opt_def("minconf", param_number, &minconf, 1),
|
2018-07-20 03:14:02 +02:00
|
|
|
NULL))
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_param_failed();
|
2018-07-20 03:14:02 +02:00
|
|
|
|
2018-08-28 22:46:32 +02:00
|
|
|
if (!feerate_per_kw) {
|
|
|
|
feerate_per_kw = tal(cmd, u32);
|
|
|
|
*feerate_per_kw = opening_feerate(cmd->ld->topology);
|
|
|
|
if (!*feerate_per_kw) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
|
|
|
"Cannot estimate fees");
|
2018-08-28 22:46:32 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-23 01:27:25 +02:00
|
|
|
|
2018-10-11 08:41:48 +02:00
|
|
|
if (*feerate_per_kw < feerate_floor()) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, LIGHTNINGD,
|
|
|
|
"Feerate below feerate floor");
|
2018-10-11 08:41:48 +02:00
|
|
|
}
|
|
|
|
|
2018-08-14 23:19:31 +02:00
|
|
|
peer = peer_by_id(cmd->ld, id);
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
if (!peer) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, LIGHTNINGD, "Unknown peer");
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
channel = peer_active_channel(peer);
|
|
|
|
if (channel) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, LIGHTNINGD, "Peer already %s",
|
|
|
|
channel_state_name(channel));
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!peer->uncommitted_channel) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, LIGHTNINGD, "Peer not connected");
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (peer->uncommitted_channel->fc) {
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_fail(cmd, LIGHTNINGD, "Already funding channel");
|
2018-02-20 21:59:04 +01:00
|
|
|
}
|
2018-04-05 21:19:47 +02:00
|
|
|
|
2018-02-20 21:59:04 +01:00
|
|
|
/* FIXME: Support push_msat? */
|
2019-02-21 04:45:55 +01:00
|
|
|
fc->push = AMOUNT_MSAT(0);
|
2018-02-20 21:59:04 +01:00
|
|
|
fc->channel_flags = OUR_CHANNEL_FLAGS;
|
2018-12-08 00:38:41 +01:00
|
|
|
if (!*announce_channel) {
|
|
|
|
fc->channel_flags &= ~CHANNEL_FLAGS_ANNOUNCE_CHANNEL;
|
|
|
|
log_info(peer->ld->log, "Will open private channel with node %s",
|
2019-04-08 11:58:32 +02:00
|
|
|
type_to_string(fc, struct node_id, id));
|
2018-12-08 00:38:41 +01:00
|
|
|
}
|
2018-02-20 21:59:04 +01:00
|
|
|
|
2019-02-21 20:34:49 +01:00
|
|
|
maxheight = minconf_to_maxheight(*minconf, cmd->ld);
|
2019-06-05 07:29:05 +02:00
|
|
|
res = wtx_select_utxos(fc->wtx, *feerate_per_kw,
|
2019-02-21 20:34:49 +01:00
|
|
|
BITCOIN_SCRIPTPUBKEY_P2WSH_LEN, maxheight);
|
2018-12-16 05:51:06 +01:00
|
|
|
if (res)
|
2018-12-16 05:52:06 +01:00
|
|
|
return res;
|
2018-03-21 00:06:04 +01:00
|
|
|
|
2019-06-05 07:29:05 +02:00
|
|
|
assert(!amount_sat_greater(fc->wtx->amount, max_funding_satoshi));
|
2018-02-20 21:59:04 +01:00
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
peer->uncommitted_channel->fc = tal_steal(peer->uncommitted_channel, fc);
|
|
|
|
fc->uc = peer->uncommitted_channel;
|
|
|
|
|
|
|
|
msg = towire_opening_funder(NULL,
|
2019-06-05 07:29:05 +02:00
|
|
|
fc->wtx->amount,
|
2019-02-21 04:45:55 +01:00
|
|
|
fc->push,
|
2018-08-28 22:46:32 +02:00
|
|
|
*feerate_per_kw,
|
2019-06-05 07:29:05 +02:00
|
|
|
fc->wtx->change,
|
|
|
|
fc->wtx->change_key_index,
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
fc->channel_flags,
|
2019-06-05 07:29:05 +02:00
|
|
|
fc->wtx->utxos,
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
cmd->ld->wallet->bip32_base);
|
2018-04-23 12:08:01 +02:00
|
|
|
|
openingd: take peer before we're opening, wait for explicit funding msg.
Prior to this, lightningd would hand uninteresting peers back to connectd,
which would then return it to lightningd if it sent a non-gossip msg,
or if lightningd asked it to release the peer.
Now connectd hands the peer to lightningd once we've done the init
handshake, which hands it off to openingd.
This is a deep structural change, so we do the minimum here and cleanup
in the following patches.
Lightningd:
1. Remove peer_nongossip handling from connect_control and peer_control.
2. Remove list of outstanding fundchannel command; it was only needed to
find the race between us asking connectd to release the peer and it
reconnecting.
3. We can no longer tell if the remote end has started trying to fund a
channel (until it has succeeded): it's very transitory anyway so not
worth fixing.
4. We now always have a struct peer, and allocate an uncommitted_channel
for it, though it may never be used if neither end funds a channel.
5. We start funding on messages for openingd: we can get a funder_reply
or a fundee, or an error in response to our request to fund a channel.
so we handle all of them.
6. A new peer_start_openingd() is called after connectd hands us a peer.
7. json_fund_channel just looks through local peers; there are none
hidden in connectd any more.
8. We sometimes start a new openingd just to send an error message.
Openingd:
1. We always have information we need to accept them funding a channel (in
the init message).
2. We have to listen for three fds: peer, gossip and master, so we opencode
the poll.
3. We have an explicit message to start trying to fund a channel.
4. We can be told to send a message in our init message.
Testing:
1. We don't handle some things gracefully yet, so two tests are disabled.
2. 'hand_back_peer .*: now local again' from connectd is no longer a message,
openingd says 'Handed peer, entering loop' once its managing it.
3. peer['state'] used to be set to 'GOSSIPING' (otherwise this field doesn't
exist; 'state' is now per-channel. It doesn't exist at all now.
4. Some tests now need to turn on IO logging in openingd, not connectd.
5. There's a gap between connecting on one node and having connectd on
the peer hand over the connection to openingd. Our tests sometimes
checked getpeers() on the peer, and didn't see anything, so line_graph
needed updating.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2018-08-08 16:10:58 +02:00
|
|
|
/* Openingd will either succeed, or fail, or tell us the other side
|
|
|
|
* funded first. */
|
|
|
|
subd_send_msg(peer->uncommitted_channel->openingd, take(msg));
|
2018-12-16 05:52:06 +01:00
|
|
|
return command_still_pending(cmd);
|
2018-02-20 21:59:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct json_command fund_channel_command = {
|
2019-05-22 16:08:16 +02:00
|
|
|
"fundchannel",
|
|
|
|
"channels",
|
|
|
|
json_fund_channel,
|
|
|
|
"Fund channel with {id} using {satoshi} (or 'all') satoshis, at optional "
|
|
|
|
"{feerate}. Only use outputs that have {minconf} confirmations."
|
2018-02-20 21:59:04 +01:00
|
|
|
};
|
|
|
|
AUTODATA(json_command, &fund_channel_command);
|
2018-11-22 03:17:29 +01:00
|
|
|
|
|
|
|
#if DEVELOPER
|
|
|
|
/* Indented to avoid include ordering check */
|
|
|
|
#include <lightningd/memdump.h>
|
|
|
|
|
2018-11-22 03:17:29 +01:00
|
|
|
static void opening_died_forget_memleak(struct subd *openingd,
|
|
|
|
struct command *cmd)
|
|
|
|
{
|
|
|
|
/* FIXME: We ignore the remaining openingds in this case. */
|
|
|
|
opening_memleak_done(cmd, NULL);
|
|
|
|
}
|
|
|
|
|
2018-11-22 03:17:29 +01:00
|
|
|
/* Mutual recursion */
|
|
|
|
static void opening_memleak_req_next(struct command *cmd, struct peer *prev);
|
|
|
|
static void opening_memleak_req_done(struct subd *openingd,
|
|
|
|
const u8 *msg, const int *fds UNUSED,
|
|
|
|
struct command *cmd)
|
|
|
|
{
|
|
|
|
bool found_leak;
|
|
|
|
struct uncommitted_channel *uc = openingd->channel;
|
|
|
|
|
2018-11-22 03:17:29 +01:00
|
|
|
tal_del_destructor2(openingd, opening_died_forget_memleak, cmd);
|
2018-11-22 03:17:29 +01:00
|
|
|
if (!fromwire_opening_dev_memleak_reply(msg, &found_leak)) {
|
2018-12-16 05:53:06 +01:00
|
|
|
was_pending(command_fail(cmd, LIGHTNINGD,
|
|
|
|
"Bad opening_dev_memleak"));
|
2018-11-22 03:17:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_leak) {
|
|
|
|
opening_memleak_done(cmd, openingd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
opening_memleak_req_next(cmd, uc->peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void opening_memleak_req_next(struct command *cmd, struct peer *prev)
|
|
|
|
{
|
|
|
|
struct peer *p;
|
|
|
|
|
|
|
|
list_for_each(&cmd->ld->peers, p, list) {
|
|
|
|
if (!p->uncommitted_channel)
|
|
|
|
continue;
|
|
|
|
if (p == prev) {
|
|
|
|
prev = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (prev != NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
subd_req(p,
|
|
|
|
p->uncommitted_channel->openingd,
|
|
|
|
take(towire_opening_dev_memleak(NULL)),
|
|
|
|
-1, 0, opening_memleak_req_done, cmd);
|
2018-11-22 03:17:29 +01:00
|
|
|
/* Just in case it dies before replying! */
|
|
|
|
tal_add_destructor2(p->uncommitted_channel->openingd,
|
|
|
|
opening_died_forget_memleak, cmd);
|
2018-11-22 03:17:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
opening_memleak_done(cmd, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void opening_dev_memleak(struct command *cmd)
|
|
|
|
{
|
|
|
|
opening_memleak_req_next(cmd, NULL);
|
|
|
|
}
|
|
|
|
#endif /* DEVELOPER */
|