2017-11-28 23:50:14 +01:00
|
|
|
/* Main channel operation daemon: runs from funding_locked to shutdown_complete.
|
|
|
|
*
|
|
|
|
* We're fairly synchronous: our main loop looks for gossip, master or
|
|
|
|
* peer requests and services them synchronously.
|
|
|
|
*
|
|
|
|
* The exceptions are:
|
|
|
|
* 1. When we've asked the master something: in that case, we queue
|
|
|
|
* non-response packets for later processing while we await the reply.
|
|
|
|
* 2. We queue and send non-blocking responses to peers: if both peers were
|
|
|
|
* reading and writing synchronously we could deadlock if we hit buffer
|
|
|
|
* limits, unlikely as that is.
|
|
|
|
*/
|
2018-11-21 11:36:21 +01:00
|
|
|
#include <bitcoin/chainparams.h>
|
2017-03-07 02:26:12 +01:00
|
|
|
#include <bitcoin/privkey.h>
|
|
|
|
#include <bitcoin/script.h>
|
2017-06-20 07:49:03 +02:00
|
|
|
#include <ccan/cast/cast.h>
|
2017-03-10 14:17:23 +01:00
|
|
|
#include <ccan/container_of/container_of.h>
|
2017-03-07 02:26:12 +01:00
|
|
|
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
|
2017-06-20 08:11:03 +02:00
|
|
|
#include <ccan/crypto/shachain/shachain.h>
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
#include <ccan/err/err.h>
|
2017-03-07 02:26:12 +01:00
|
|
|
#include <ccan/fdpass/fdpass.h>
|
2017-04-01 12:58:30 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
2017-03-11 21:49:52 +01:00
|
|
|
#include <ccan/take/take.h>
|
2017-03-29 13:01:15 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2017-03-11 21:49:52 +01:00
|
|
|
#include <ccan/time/time.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <channeld/commit_tx.h>
|
|
|
|
#include <channeld/full_channel.h>
|
|
|
|
#include <channeld/gen_channel_wire.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/crypto_sync.h>
|
2017-09-26 06:57:31 +02:00
|
|
|
#include <common/dev_disconnect.h>
|
2018-12-10 02:03:42 +01:00
|
|
|
#include <common/features.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/htlc_tx.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/key_derive.h>
|
2018-11-22 03:17:29 +01:00
|
|
|
#include <common/memleak.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/msg_queue.h>
|
2019-04-08 11:58:32 +02:00
|
|
|
#include <common/node_id.h>
|
2018-02-23 06:53:47 +01:00
|
|
|
#include <common/peer_billboard.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/peer_failed.h>
|
|
|
|
#include <common/ping.h>
|
2018-01-31 03:53:42 +01:00
|
|
|
#include <common/read_peer_msg.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/sphinx.h>
|
|
|
|
#include <common/status.h>
|
2018-01-08 11:01:09 +01:00
|
|
|
#include <common/subdaemon.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/timeout.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <common/version.h>
|
2017-12-19 01:02:28 +01:00
|
|
|
#include <common/wire_error.h>
|
2017-03-07 02:26:12 +01:00
|
|
|
#include <errno.h>
|
2017-09-08 12:23:57 +02:00
|
|
|
#include <fcntl.h>
|
2018-11-13 05:03:51 +01:00
|
|
|
#include <gossipd/gen_gossip_peerd_wire.h>
|
2018-05-17 07:08:11 +02:00
|
|
|
#include <gossipd/gossip_constants.h>
|
2018-09-20 05:06:42 +02:00
|
|
|
#include <hsmd/gen_hsm_wire.h>
|
2017-03-07 02:26:12 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <secp256k1.h>
|
|
|
|
#include <stdio.h>
|
2017-03-29 13:01:15 +02:00
|
|
|
#include <wire/gen_onion_wire.h>
|
2017-06-20 08:11:03 +02:00
|
|
|
#include <wire/peer_wire.h>
|
2017-03-07 02:26:12 +01:00
|
|
|
#include <wire/wire.h>
|
|
|
|
#include <wire/wire_io.h>
|
|
|
|
#include <wire/wire_sync.h>
|
|
|
|
|
2019-05-04 07:53:13 +02:00
|
|
|
/* stdin == requests, 3 == peer, 4 = gossip, 5 = gossip_store, 6 = HSM */
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
#define MASTER_FD STDIN_FILENO
|
2017-03-07 02:26:12 +01:00
|
|
|
#define PEER_FD 3
|
2017-03-19 21:32:44 +01:00
|
|
|
#define GOSSIP_FD 4
|
2019-05-04 07:53:13 +02:00
|
|
|
#define GOSSIP_STORE_FD 5
|
|
|
|
#define HSM_FD 6
|
2017-03-07 02:26:12 +01:00
|
|
|
|
|
|
|
struct peer {
|
2017-11-28 23:50:14 +01:00
|
|
|
struct crypto_state cs;
|
2017-03-07 02:26:12 +01:00
|
|
|
bool funding_locked[NUM_SIDES];
|
2017-06-27 04:55:06 +02:00
|
|
|
u64 next_index[NUM_SIDES];
|
2017-03-07 02:26:12 +01:00
|
|
|
|
2018-12-10 02:03:42 +01:00
|
|
|
/* Features peer supports. */
|
|
|
|
u8 *localfeatures;
|
|
|
|
|
2017-11-21 06:26:59 +01:00
|
|
|
/* Tolerable amounts for feerate (only relevant for fundee). */
|
|
|
|
u32 feerate_min, feerate_max;
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
/* Local next per-commit point. */
|
|
|
|
struct pubkey next_local_per_commit;
|
|
|
|
|
2017-06-20 08:10:03 +02:00
|
|
|
/* Remote's current per-commit point. */
|
|
|
|
struct pubkey remote_per_commit;
|
|
|
|
|
|
|
|
/* Remotes's last per-commitment point: we keep this to check
|
|
|
|
* revoke_and_ack's `per_commitment_secret` is correct. */
|
|
|
|
struct pubkey old_remote_per_commit;
|
|
|
|
|
2017-03-07 02:26:12 +01:00
|
|
|
/* Their sig for current commit. */
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature their_commit_sig;
|
2017-03-07 02:26:12 +01:00
|
|
|
|
2017-03-29 13:01:15 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A sending node:
|
|
|
|
*...
|
|
|
|
* - for the first HTLC it offers:
|
|
|
|
* - MUST set `id` to 0.
|
2017-03-29 13:01:15 +02:00
|
|
|
*/
|
|
|
|
u64 htlc_id;
|
|
|
|
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid chain_hash;
|
2017-03-07 02:26:12 +01:00
|
|
|
struct channel_id channel_id;
|
|
|
|
struct channel *channel;
|
|
|
|
|
2017-11-28 23:52:29 +01:00
|
|
|
/* Messages from master / gossipd: we queue them since we
|
|
|
|
* might be waiting for a specific reply. */
|
2018-10-25 01:46:08 +02:00
|
|
|
struct msg_queue *from_master, *from_gossipd;
|
2017-06-20 07:49:03 +02:00
|
|
|
|
2017-04-01 12:28:39 +02:00
|
|
|
struct timers timers;
|
|
|
|
struct oneshot *commit_timer;
|
2018-01-26 01:19:46 +01:00
|
|
|
u64 commit_timer_attempts;
|
2017-04-01 12:28:39 +02:00
|
|
|
u32 commit_msec;
|
|
|
|
|
2018-08-09 04:53:17 +02:00
|
|
|
/* Are we expecting a pong? */
|
|
|
|
bool expecting_pong;
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2017-11-21 06:26:59 +01:00
|
|
|
/* The feerate we want. */
|
|
|
|
u32 desired_feerate;
|
|
|
|
|
2017-03-11 21:49:52 +01:00
|
|
|
/* Announcement related information */
|
2019-04-08 11:58:32 +02:00
|
|
|
struct node_id node_ids[NUM_SIDES];
|
2017-03-11 21:49:52 +01:00
|
|
|
struct short_channel_id short_channel_ids[NUM_SIDES];
|
|
|
|
secp256k1_ecdsa_signature announcement_node_sigs[NUM_SIDES];
|
|
|
|
secp256k1_ecdsa_signature announcement_bitcoin_sigs[NUM_SIDES];
|
2017-04-04 13:24:47 +02:00
|
|
|
bool have_sigs[NUM_SIDES];
|
2017-03-22 16:46:48 +01:00
|
|
|
|
|
|
|
/* Which direction of the channel do we control? */
|
|
|
|
u16 channel_direction;
|
2017-05-01 18:25:20 +02:00
|
|
|
|
|
|
|
/* CLTV delta to announce to peers */
|
2017-10-11 06:32:15 +02:00
|
|
|
u16 cltv_delta;
|
2017-05-01 18:25:20 +02:00
|
|
|
u32 fee_base;
|
|
|
|
u32 fee_per_satoshi;
|
2017-06-20 08:08:03 +02:00
|
|
|
|
2018-03-07 01:06:07 +01:00
|
|
|
/* The scriptpubkey to use for shutting down. */
|
|
|
|
u8 *final_scriptpubkey;
|
|
|
|
|
|
|
|
/* If master told us to shut down */
|
|
|
|
bool send_shutdown;
|
|
|
|
/* Has shutdown been sent by each side? */
|
2017-07-04 02:47:32 +02:00
|
|
|
bool shutdown_sent[NUM_SIDES];
|
2017-06-26 03:16:43 +02:00
|
|
|
|
2017-06-20 08:11:03 +02:00
|
|
|
/* Information used for reestablishment. */
|
|
|
|
bool last_was_revoke;
|
|
|
|
struct changed_htlc *last_sent_commit;
|
|
|
|
u64 revocations_received;
|
2017-06-29 16:02:37 +02:00
|
|
|
u8 channel_flags;
|
2017-09-04 05:41:34 +02:00
|
|
|
|
|
|
|
bool announce_depth_reached;
|
2018-05-21 06:32:37 +02:00
|
|
|
bool channel_local_active;
|
2017-12-11 04:33:16 +01:00
|
|
|
|
2018-01-04 12:40:21 +01:00
|
|
|
/* Make sure timestamps move forward. */
|
|
|
|
u32 last_update_timestamp;
|
2018-08-09 04:53:18 +02:00
|
|
|
|
|
|
|
/* Make sure peer is live. */
|
|
|
|
struct timeabs last_recv;
|
2019-02-26 17:57:19 +01:00
|
|
|
|
|
|
|
/* Additional confirmations need for local lockin. */
|
|
|
|
u32 depth_togo;
|
2019-04-16 06:00:15 +02:00
|
|
|
|
2019-05-01 02:47:11 +02:00
|
|
|
/* Non-empty if they specified a fixed shutdown script */
|
|
|
|
u8 *remote_upfront_shutdown_script;
|
|
|
|
|
2019-04-16 06:00:15 +02:00
|
|
|
/* Empty commitments. Spec violation, but a minor one. */
|
|
|
|
u64 last_empty_commitment;
|
2017-03-07 02:26:12 +01:00
|
|
|
};
|
|
|
|
|
2017-04-03 03:03:38 +02:00
|
|
|
static u8 *create_channel_announcement(const tal_t *ctx, struct peer *peer);
|
2017-06-20 07:50:03 +02:00
|
|
|
static void start_commit_timer(struct peer *peer);
|
|
|
|
|
2018-02-23 06:53:47 +01:00
|
|
|
static void billboard_update(const struct peer *peer)
|
|
|
|
{
|
|
|
|
const char *funding_status, *announce_status, *shutdown_status;
|
|
|
|
|
|
|
|
if (peer->funding_locked[LOCAL] && peer->funding_locked[REMOTE])
|
|
|
|
funding_status = "Funding transaction locked.";
|
|
|
|
else if (!peer->funding_locked[LOCAL] && !peer->funding_locked[REMOTE])
|
2019-02-26 17:57:19 +01:00
|
|
|
funding_status = tal_fmt(tmpctx,
|
|
|
|
"Funding needs %d confirmations to reach lockin.",
|
|
|
|
peer->depth_togo);
|
2018-02-23 06:53:47 +01:00
|
|
|
else if (peer->funding_locked[LOCAL] && !peer->funding_locked[REMOTE])
|
|
|
|
funding_status = "We've confirmed funding, they haven't yet.";
|
|
|
|
else if (!peer->funding_locked[LOCAL] && peer->funding_locked[REMOTE])
|
|
|
|
funding_status = "They've confirmed funding, we haven't yet.";
|
|
|
|
|
|
|
|
if (peer->have_sigs[LOCAL] && peer->have_sigs[REMOTE])
|
|
|
|
announce_status = " Channel announced.";
|
|
|
|
else if (peer->have_sigs[LOCAL] && !peer->have_sigs[REMOTE])
|
|
|
|
announce_status = " Waiting for their announcement signatures.";
|
|
|
|
else if (!peer->have_sigs[LOCAL] && peer->have_sigs[REMOTE])
|
|
|
|
announce_status = " They need our announcement signatures.";
|
|
|
|
else if (!peer->have_sigs[LOCAL] && !peer->have_sigs[REMOTE])
|
|
|
|
announce_status = "";
|
|
|
|
|
|
|
|
if (!peer->shutdown_sent[LOCAL] && !peer->shutdown_sent[REMOTE])
|
|
|
|
shutdown_status = "";
|
|
|
|
else if (!peer->shutdown_sent[LOCAL] && peer->shutdown_sent[REMOTE])
|
|
|
|
shutdown_status = " We've send shutdown, waiting for theirs";
|
|
|
|
else if (peer->shutdown_sent[LOCAL] && !peer->shutdown_sent[REMOTE])
|
|
|
|
shutdown_status = " They've sent shutdown, waiting for ours";
|
|
|
|
else if (peer->shutdown_sent[LOCAL] && peer->shutdown_sent[REMOTE]) {
|
|
|
|
size_t num_htlcs = num_channel_htlcs(peer->channel);
|
|
|
|
if (num_htlcs)
|
|
|
|
shutdown_status = tal_fmt(tmpctx,
|
|
|
|
" Shutdown messages exchanged,"
|
|
|
|
" waiting for %zu HTLCs to complete.",
|
|
|
|
num_htlcs);
|
|
|
|
else
|
|
|
|
shutdown_status = tal_fmt(tmpctx,
|
|
|
|
" Shutdown messages exchanged.");
|
|
|
|
}
|
|
|
|
peer_billboard(false, "%s%s%s", funding_status,
|
|
|
|
announce_status, shutdown_status);
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
static const u8 *hsm_req(const tal_t *ctx, const u8 *req TAKES)
|
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
int type = fromwire_peektype(req);
|
|
|
|
|
|
|
|
if (!wire_sync_write(HSM_FD, req))
|
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
|
|
|
"Writing %s to HSM: %s",
|
2018-09-20 05:06:42 +02:00
|
|
|
hsm_wire_type_name(type),
|
2018-07-23 04:23:03 +02:00
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
msg = wire_sync_read(ctx, HSM_FD);
|
|
|
|
if (!msg)
|
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
|
|
|
"Reading resp to %s: %s",
|
2018-09-20 05:06:42 +02:00
|
|
|
hsm_wire_type_name(type),
|
2018-07-23 04:23:03 +02:00
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2018-10-16 02:35:08 +02:00
|
|
|
/*
|
|
|
|
* The maximum msat that this node will accept for an htlc.
|
|
|
|
* It's flagged as an optional field in `channel_update`.
|
|
|
|
*
|
|
|
|
* We advertise the maximum value possible, defined as the smaller
|
|
|
|
* of the remote's maximum in-flight HTLC or the total channel
|
|
|
|
* capacity minus the cumulative reserve.
|
|
|
|
* FIXME: does this need fuzz?
|
|
|
|
*/
|
2019-02-21 04:45:55 +01:00
|
|
|
static struct amount_msat advertised_htlc_max(const struct channel *channel)
|
2018-10-16 02:35:08 +02:00
|
|
|
{
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat cumulative_reserve, lower_bound;
|
2019-02-21 04:45:54 +01:00
|
|
|
struct amount_msat lower_bound_msat;
|
2018-11-21 11:36:21 +01:00
|
|
|
|
2019-02-21 04:45:54 +01:00
|
|
|
/* This shouldn't fail */
|
|
|
|
if (!amount_sat_add(&cumulative_reserve,
|
|
|
|
channel->config[LOCAL].channel_reserve,
|
|
|
|
channel->config[REMOTE].channel_reserve)) {
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"reserve overflow: local %s + remote %s",
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
|
|
|
&channel->config[LOCAL].channel_reserve),
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
|
|
|
&channel->config[REMOTE].channel_reserve));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This shouldn't fail either */
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_sat_sub(&lower_bound, channel->funding, cumulative_reserve)) {
|
2019-02-21 04:45:54 +01:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"funding %s - cumulative_reserve %s?",
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
2019-02-21 04:45:55 +01:00
|
|
|
&channel->funding),
|
2019-02-21 04:45:54 +01:00
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
|
|
|
&cumulative_reserve));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!amount_sat_to_msat(&lower_bound_msat, lower_bound)) {
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"lower_bound %s invalid?",
|
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
|
|
|
&lower_bound));
|
|
|
|
}
|
2019-04-05 16:46:09 +02:00
|
|
|
|
2019-02-21 04:45:54 +01:00
|
|
|
if (amount_msat_greater(lower_bound_msat,
|
|
|
|
channel->chainparams->max_payment))
|
2019-04-05 16:46:09 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* The origin node:
|
|
|
|
* ...
|
|
|
|
* - if the `htlc_maximum_msat` field is present:
|
|
|
|
* ...
|
|
|
|
* - for channels with `chain_hash` identifying the Bitcoin blockchain:
|
|
|
|
* - MUST set this to less than 2^32.
|
|
|
|
*/
|
2019-02-21 04:45:54 +01:00
|
|
|
lower_bound_msat = channel->chainparams->max_payment;
|
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
return lower_bound_msat;
|
2018-10-16 02:35:08 +02:00
|
|
|
}
|
|
|
|
|
2018-05-17 07:08:11 +02:00
|
|
|
/* Create and send channel_update to gossipd (and maybe peer) */
|
2018-05-21 06:35:40 +02:00
|
|
|
static void send_channel_update(struct peer *peer, int disable_flag)
|
2018-05-17 07:08:11 +02:00
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
|
|
|
|
assert(disable_flag == 0 || disable_flag == ROUTING_FLAGS_DISABLED);
|
|
|
|
|
2018-05-21 06:32:37 +02:00
|
|
|
/* Only send an update if we told gossipd */
|
|
|
|
if (!peer->channel_local_active)
|
2018-05-17 07:08:11 +02:00
|
|
|
return;
|
|
|
|
|
2018-05-21 06:35:40 +02:00
|
|
|
assert(peer->short_channel_ids[LOCAL].u64);
|
|
|
|
|
2018-11-13 05:03:51 +01:00
|
|
|
msg = towire_gossipd_local_channel_update(NULL,
|
|
|
|
&peer->short_channel_ids[LOCAL],
|
|
|
|
disable_flag
|
|
|
|
== ROUTING_FLAGS_DISABLED,
|
|
|
|
peer->cltv_delta,
|
2019-02-21 04:45:55 +01:00
|
|
|
peer->channel->config[REMOTE].htlc_minimum,
|
2018-11-13 05:03:51 +01:00
|
|
|
peer->fee_base,
|
|
|
|
peer->fee_per_satoshi,
|
2019-02-21 04:45:55 +01:00
|
|
|
advertised_htlc_max(peer->channel));
|
2018-05-17 07:08:11 +02:00
|
|
|
wire_sync_write(GOSSIP_FD, take(msg));
|
|
|
|
}
|
|
|
|
|
2018-05-03 16:25:07 +02:00
|
|
|
/**
|
|
|
|
* Add a channel locally and send a channel update to the peer
|
|
|
|
*
|
|
|
|
* Send a local_add_channel message to gossipd in order to make the channel
|
|
|
|
* usable locally, and also tell our peer about our parameters via a
|
|
|
|
* channel_update message. The peer may accept the update and use the contained
|
|
|
|
* information to route incoming payments through the channel. The
|
|
|
|
* channel_update is not preceeded by a channel_announcement and won't make much
|
|
|
|
* sense to other nodes, so we don't tell gossipd about it.
|
|
|
|
*/
|
2018-05-21 06:32:37 +02:00
|
|
|
static void make_channel_local_active(struct peer *peer)
|
2017-12-15 15:47:58 +01:00
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
|
2018-05-17 07:09:59 +02:00
|
|
|
/* Tell gossipd about local channel. */
|
2018-11-13 05:03:51 +01:00
|
|
|
msg = towire_gossipd_local_add_channel(NULL,
|
|
|
|
&peer->short_channel_ids[LOCAL],
|
|
|
|
&peer->node_ids[REMOTE],
|
2019-02-21 04:45:55 +01:00
|
|
|
peer->channel->funding);
|
2018-05-17 07:09:59 +02:00
|
|
|
wire_sync_write(GOSSIP_FD, take(msg));
|
2018-05-10 16:00:05 +02:00
|
|
|
|
2018-05-17 07:09:59 +02:00
|
|
|
/* Tell gossipd and the other side what parameters we expect should
|
|
|
|
* they route through us */
|
2018-05-21 06:35:40 +02:00
|
|
|
send_channel_update(peer, 0);
|
2017-12-15 15:47:58 +01:00
|
|
|
}
|
|
|
|
|
2017-03-11 21:49:52 +01:00
|
|
|
static void send_announcement_signatures(struct peer *peer)
|
|
|
|
{
|
2017-04-03 03:03:38 +02:00
|
|
|
/* First 2 + 256 byte are the signatures and msg type, skip them */
|
|
|
|
size_t offset = 258;
|
|
|
|
struct sha256_double hash;
|
2018-07-23 04:23:03 +02:00
|
|
|
const u8 *msg, *ca, *req;
|
2019-04-08 11:58:32 +02:00
|
|
|
struct pubkey mykey;
|
2017-09-04 05:41:34 +02:00
|
|
|
|
|
|
|
status_trace("Exchanging announcement signatures.");
|
|
|
|
ca = create_channel_announcement(tmpctx, peer);
|
2018-07-09 13:17:59 +02:00
|
|
|
req = towire_hsm_cannouncement_sig_req(tmpctx, ca);
|
2017-04-03 03:03:38 +02:00
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
msg = hsm_req(tmpctx, req);
|
|
|
|
if (!fromwire_hsm_cannouncement_sig_reply(msg,
|
2018-07-09 13:17:59 +02:00
|
|
|
&peer->announcement_node_sigs[LOCAL],
|
|
|
|
&peer->announcement_bitcoin_sigs[LOCAL]))
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
|
|
|
"Reading cannouncement_sig_resp: %s",
|
|
|
|
strerror(errno));
|
2017-04-03 03:03:38 +02:00
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
/* Double-check that HSM gave valid signatures. */
|
2018-07-28 08:00:16 +02:00
|
|
|
sha256_double(&hash, ca + offset, tal_count(ca) - offset);
|
2019-04-08 11:58:32 +02:00
|
|
|
if (!pubkey_from_node_id(&mykey, &peer->node_ids[LOCAL]))
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Could not convert my id '%s' to pubkey",
|
|
|
|
type_to_string(tmpctx, struct node_id,
|
|
|
|
&peer->node_ids[LOCAL]));
|
2017-04-03 03:03:38 +02:00
|
|
|
if (!check_signed_hash(&hash, &peer->announcement_node_sigs[LOCAL],
|
2019-04-08 11:58:32 +02:00
|
|
|
&mykey)) {
|
2017-04-03 03:03:38 +02:00
|
|
|
/* It's ok to fail here, the channel announcement is
|
|
|
|
* unique, unlike the channel update which may have
|
|
|
|
* been replaced in the meantime. */
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
2018-07-09 13:17:59 +02:00
|
|
|
"HSM returned an invalid node signature");
|
2017-04-03 03:03:38 +02:00
|
|
|
}
|
2017-03-11 21:49:52 +01:00
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
if (!check_signed_hash(&hash, &peer->announcement_bitcoin_sigs[LOCAL],
|
|
|
|
&peer->channel->funding_pubkey[LOCAL])) {
|
|
|
|
/* It's ok to fail here, the channel announcement is
|
|
|
|
* unique, unlike the channel update which may have
|
|
|
|
* been replaced in the meantime. */
|
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
|
|
|
"HSM returned an invalid bitcoin signature");
|
|
|
|
}
|
2017-04-03 05:34:52 +02:00
|
|
|
|
2017-04-03 03:03:38 +02:00
|
|
|
msg = towire_announcement_signatures(
|
2018-03-15 07:10:22 +01:00
|
|
|
NULL, &peer->channel_id, &peer->short_channel_ids[LOCAL],
|
2017-04-03 03:03:38 +02:00
|
|
|
&peer->announcement_node_sigs[LOCAL],
|
|
|
|
&peer->announcement_bitcoin_sigs[LOCAL]);
|
2018-08-02 08:49:55 +02:00
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
2017-03-11 21:49:52 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 03:03:38 +02:00
|
|
|
/* Tentatively create a channel_announcement, possibly with invalid
|
|
|
|
* signatures. The signatures need to be collected first, by asking
|
|
|
|
* the HSM and by exchanging announcement_signature messages. */
|
|
|
|
static u8 *create_channel_announcement(const tal_t *ctx, struct peer *peer)
|
2017-03-21 21:21:17 +01:00
|
|
|
{
|
|
|
|
int first, second;
|
2017-04-03 03:03:38 +02:00
|
|
|
u8 *cannounce, *features = tal_arr(ctx, u8, 0);
|
2017-03-21 21:21:17 +01:00
|
|
|
|
2017-03-22 16:46:48 +01:00
|
|
|
if (peer->channel_direction == 0) {
|
2017-03-11 21:49:52 +01:00
|
|
|
first = LOCAL;
|
|
|
|
second = REMOTE;
|
|
|
|
} else {
|
|
|
|
first = REMOTE;
|
|
|
|
second = LOCAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cannounce = towire_channel_announcement(
|
2017-04-03 03:03:38 +02:00
|
|
|
ctx, &peer->announcement_node_sigs[first],
|
2017-03-11 21:49:52 +01:00
|
|
|
&peer->announcement_node_sigs[second],
|
|
|
|
&peer->announcement_bitcoin_sigs[first],
|
|
|
|
&peer->announcement_bitcoin_sigs[second],
|
2017-08-03 03:45:04 +02:00
|
|
|
features,
|
2017-08-22 07:25:01 +02:00
|
|
|
&peer->chain_hash,
|
2019-04-08 11:58:44 +02:00
|
|
|
&peer->short_channel_ids[LOCAL],
|
|
|
|
&peer->node_ids[first],
|
|
|
|
&peer->node_ids[second],
|
|
|
|
&peer->channel->funding_pubkey[first],
|
2017-08-03 03:45:04 +02:00
|
|
|
&peer->channel->funding_pubkey[second]);
|
2017-04-03 03:03:38 +02:00
|
|
|
tal_free(features);
|
|
|
|
return cannounce;
|
|
|
|
}
|
2017-03-11 21:49:52 +01:00
|
|
|
|
2018-05-17 07:09:40 +02:00
|
|
|
/* Once we have both, we'd better make sure we agree what they are! */
|
|
|
|
static void check_short_ids_match(struct peer *peer)
|
|
|
|
{
|
|
|
|
assert(peer->have_sigs[LOCAL]);
|
|
|
|
assert(peer->have_sigs[REMOTE]);
|
|
|
|
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!short_channel_id_eq(&peer->short_channel_ids[LOCAL],
|
|
|
|
&peer->short_channel_ids[REMOTE]))
|
2018-05-17 07:09:40 +02:00
|
|
|
peer_failed(&peer->cs,
|
|
|
|
&peer->channel_id,
|
|
|
|
"We disagree on short_channel_ids:"
|
|
|
|
" I have %s, you say %s",
|
|
|
|
type_to_string(peer, struct short_channel_id,
|
|
|
|
&peer->short_channel_ids[LOCAL]),
|
|
|
|
type_to_string(peer, struct short_channel_id,
|
|
|
|
&peer->short_channel_ids[REMOTE]));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void announce_channel(struct peer *peer)
|
|
|
|
{
|
2018-05-21 06:35:40 +02:00
|
|
|
u8 *cannounce;
|
2018-05-17 07:09:40 +02:00
|
|
|
|
|
|
|
check_short_ids_match(peer);
|
|
|
|
|
|
|
|
cannounce = create_channel_announcement(tmpctx, peer);
|
|
|
|
|
|
|
|
wire_sync_write(GOSSIP_FD, cannounce);
|
2018-05-21 06:35:40 +02:00
|
|
|
send_channel_update(peer, 0);
|
2018-05-17 07:09:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void channel_announcement_negotiate(struct peer *peer)
|
|
|
|
{
|
|
|
|
/* Don't do any announcement work if we're shutting down */
|
|
|
|
if (peer->shutdown_sent[LOCAL])
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Can't do anything until funding is locked. */
|
|
|
|
if (!peer->funding_locked[LOCAL] || !peer->funding_locked[REMOTE])
|
|
|
|
return;
|
|
|
|
|
2018-05-21 06:32:37 +02:00
|
|
|
if (!peer->channel_local_active) {
|
|
|
|
peer->channel_local_active = true;
|
|
|
|
make_channel_local_active(peer);
|
|
|
|
}
|
2018-12-08 00:38:41 +01:00
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* A node:
|
2019-01-14 03:22:05 +01:00
|
|
|
* - if the `open_channel` message has the `announce_channel` bit set AND a `shutdown` message has not been sent:
|
2018-12-08 00:38:41 +01:00
|
|
|
* - MUST send the `announcement_signatures` message.
|
2019-01-14 03:22:05 +01:00
|
|
|
* - MUST NOT send `announcement_signatures` messages until `funding_locked`
|
|
|
|
* has been sent and received AND the funding transaction has at least six confirmations.
|
2018-12-08 00:38:41 +01:00
|
|
|
* - otherwise:
|
|
|
|
* - MUST NOT send the `announcement_signatures` message.
|
|
|
|
*/
|
|
|
|
if (!(peer->channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL))
|
|
|
|
return;
|
|
|
|
|
2018-05-17 07:09:40 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2019-01-14 03:22:05 +01:00
|
|
|
* - MUST NOT send `announcement_signatures` messages until `funding_locked`
|
|
|
|
* has been sent and received AND the funding transaction has at least six confirmations.
|
|
|
|
*/
|
2018-05-18 07:49:15 +02:00
|
|
|
if (peer->announce_depth_reached && !peer->have_sigs[LOCAL]) {
|
2018-05-17 07:09:40 +02:00
|
|
|
send_announcement_signatures(peer);
|
|
|
|
peer->have_sigs[LOCAL] = true;
|
|
|
|
billboard_update(peer);
|
|
|
|
}
|
|
|
|
|
2018-05-18 07:49:15 +02:00
|
|
|
/* If we've completed the signature exchange, we can send a real
|
|
|
|
* announcement, otherwise we send a temporary one */
|
2018-05-17 07:09:40 +02:00
|
|
|
if (peer->have_sigs[LOCAL] && peer->have_sigs[REMOTE])
|
|
|
|
announce_channel(peer);
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void handle_peer_funding_locked(struct peer *peer, const u8 *msg)
|
2017-03-07 02:26:12 +01:00
|
|
|
{
|
|
|
|
struct channel_id chanid;
|
2017-04-01 12:26:07 +02:00
|
|
|
|
2017-06-20 08:11:03 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A node:
|
|
|
|
*...
|
|
|
|
* - upon reconnection:
|
|
|
|
* - MUST ignore any redundant `funding_locked` it receives.
|
2017-06-20 08:11:03 +02:00
|
|
|
*/
|
|
|
|
if (peer->funding_locked[REMOTE])
|
2017-11-28 23:50:14 +01:00
|
|
|
return;
|
2017-06-20 08:11:03 +02:00
|
|
|
|
2018-05-07 09:04:38 +02:00
|
|
|
/* Too late, we're shutting down! */
|
|
|
|
if (peer->shutdown_sent[LOCAL])
|
|
|
|
return;
|
|
|
|
|
2017-06-20 08:10:03 +02:00
|
|
|
peer->old_remote_per_commit = peer->remote_per_commit;
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_funding_locked(msg, &chanid,
|
2017-06-20 08:10:03 +02:00
|
|
|
&peer->remote_per_commit))
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&peer->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&peer->channel_id,
|
2017-09-12 06:55:52 +02:00
|
|
|
"Bad funding_locked %s", tal_hex(msg, msg));
|
2017-04-01 12:26:07 +02:00
|
|
|
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!channel_id_eq(&chanid, &peer->channel_id))
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&peer->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&peer->channel_id,
|
2017-09-12 06:55:52 +02:00
|
|
|
"Wrong channel id in %s (expected %s)",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, msg),
|
2017-09-12 06:55:52 +02:00
|
|
|
type_to_string(msg, struct channel_id,
|
|
|
|
&peer->channel_id));
|
2017-04-01 12:26:07 +02:00
|
|
|
|
|
|
|
peer->funding_locked[REMOTE] = true;
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
wire_sync_write(MASTER_FD,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_channel_got_funding_locked(NULL,
|
2017-06-20 08:10:03 +02:00
|
|
|
&peer->remote_per_commit)));
|
2017-03-07 02:26:12 +01:00
|
|
|
|
2018-05-17 07:09:40 +02:00
|
|
|
channel_announcement_negotiate(peer);
|
2018-02-23 06:53:47 +01:00
|
|
|
billboard_update(peer);
|
2017-09-04 05:41:34 +02:00
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void handle_peer_announcement_signatures(struct peer *peer, const u8 *msg)
|
2017-04-01 12:26:07 +02:00
|
|
|
{
|
|
|
|
struct channel_id chanid;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_announcement_signatures(msg,
|
2017-04-01 12:26:07 +02:00
|
|
|
&chanid,
|
|
|
|
&peer->short_channel_ids[REMOTE],
|
|
|
|
&peer->announcement_node_sigs[REMOTE],
|
|
|
|
&peer->announcement_bitcoin_sigs[REMOTE]))
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&peer->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&peer->channel_id,
|
2017-09-12 06:55:52 +02:00
|
|
|
"Bad announcement_signatures %s",
|
|
|
|
tal_hex(msg, msg));
|
2017-04-01 12:26:07 +02:00
|
|
|
|
|
|
|
/* Make sure we agree on the channel ids */
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!channel_id_eq(&chanid, &peer->channel_id)) {
|
2018-04-26 06:51:01 +02:00
|
|
|
peer_failed(&peer->cs,
|
2018-02-19 02:06:14 +01:00
|
|
|
&peer->channel_id,
|
2018-01-10 03:43:21 +01:00
|
|
|
"Wrong channel_id: expected %s, got %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct channel_id,
|
2018-01-10 03:43:21 +01:00
|
|
|
&peer->channel_id),
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct channel_id, &chanid));
|
2017-04-01 12:26:07 +02:00
|
|
|
}
|
|
|
|
|
2017-04-04 13:24:47 +02:00
|
|
|
peer->have_sigs[REMOTE] = true;
|
2018-02-23 06:53:47 +01:00
|
|
|
billboard_update(peer);
|
2017-04-04 13:24:47 +02:00
|
|
|
|
2018-05-17 07:09:40 +02:00
|
|
|
channel_announcement_negotiate(peer);
|
2017-04-01 12:26:07 +02:00
|
|
|
}
|
|
|
|
|
2019-01-08 01:20:50 +01:00
|
|
|
static struct secret *get_shared_secret(const tal_t *ctx,
|
2019-01-08 01:52:13 +01:00
|
|
|
const struct htlc *htlc,
|
2019-01-08 01:53:25 +01:00
|
|
|
enum onion_type *why_bad,
|
|
|
|
struct sha256 *next_onion_sha)
|
2017-11-28 06:03:13 +01:00
|
|
|
{
|
|
|
|
struct onionpacket *op;
|
2019-01-08 01:20:50 +01:00
|
|
|
struct secret *secret = tal(ctx, struct secret);
|
2018-07-23 04:23:03 +02:00
|
|
|
const u8 *msg;
|
2019-01-08 01:52:13 +01:00
|
|
|
struct route_step *rs;
|
2017-11-28 06:03:13 +01:00
|
|
|
|
|
|
|
/* We unwrap the onion now. */
|
2019-01-08 01:21:50 +01:00
|
|
|
op = parse_onionpacket(tmpctx, htlc->routing, TOTAL_PACKET_SIZE,
|
2019-01-08 01:52:13 +01:00
|
|
|
why_bad);
|
2019-01-08 01:20:50 +01:00
|
|
|
if (!op)
|
|
|
|
return tal_free(secret);
|
2017-11-28 06:03:13 +01:00
|
|
|
|
|
|
|
/* Because wire takes struct pubkey. */
|
2019-01-08 01:53:26 +01:00
|
|
|
msg = hsm_req(tmpctx, towire_hsm_ecdh_req(tmpctx, &op->ephemeralkey));
|
2019-01-08 01:20:50 +01:00
|
|
|
if (!fromwire_hsm_ecdh_resp(msg, secret))
|
2017-11-28 06:03:13 +01:00
|
|
|
status_failed(STATUS_FAIL_HSM_IO, "Reading ecdh response");
|
2018-01-17 21:29:49 +01:00
|
|
|
|
2019-01-08 01:52:13 +01:00
|
|
|
/* We make sure we can parse onion packet, so we know if shared secret
|
|
|
|
* is actually valid (this checks hmac). */
|
|
|
|
rs = process_onionpacket(tmpctx, op, secret->data,
|
|
|
|
htlc->rhash.u.u8,
|
|
|
|
sizeof(htlc->rhash));
|
|
|
|
if (!rs) {
|
|
|
|
*why_bad = WIRE_INVALID_ONION_HMAC;
|
|
|
|
return tal_free(secret);
|
|
|
|
}
|
|
|
|
|
2019-01-08 01:53:25 +01:00
|
|
|
/* Calculate sha256 we'll hand to next peer, in case they complain. */
|
|
|
|
msg = serialize_onionpacket(tmpctx, rs->next);
|
|
|
|
sha256(next_onion_sha, msg, tal_bytelen(msg));
|
|
|
|
|
2019-01-08 01:20:50 +01:00
|
|
|
return secret;
|
2017-11-28 06:03:13 +01:00
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void handle_peer_add_htlc(struct peer *peer, const u8 *msg)
|
2017-04-01 12:26:07 +02:00
|
|
|
{
|
|
|
|
struct channel_id channel_id;
|
|
|
|
u64 id;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat amount;
|
2017-04-01 12:26:07 +02:00
|
|
|
u32 cltv_expiry;
|
|
|
|
struct sha256 payment_hash;
|
2017-04-28 16:28:53 +02:00
|
|
|
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
|
2017-04-01 12:26:07 +02:00
|
|
|
enum channel_add_err add_err;
|
2017-11-28 06:03:13 +01:00
|
|
|
struct htlc *htlc;
|
2017-04-01 12:26:07 +02:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!fromwire_update_add_htlc(msg, &channel_id, &id, &amount,
|
2017-06-06 01:49:10 +02:00
|
|
|
&payment_hash, &cltv_expiry,
|
2017-04-01 12:26:07 +02:00
|
|
|
onion_routing_packet))
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 12:26:07 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad peer_add_htlc %s", tal_hex(msg, msg));
|
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
add_err = channel_add_htlc(peer->channel, REMOTE, id, amount,
|
2017-04-01 12:26:07 +02:00
|
|
|
cltv_expiry, &payment_hash,
|
2017-11-28 06:03:13 +01:00
|
|
|
onion_routing_packet, &htlc);
|
2017-04-01 12:26:07 +02:00
|
|
|
if (add_err != CHANNEL_ERR_ADD_OK)
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 12:26:07 +02:00
|
|
|
&peer->channel_id,
|
2018-02-19 02:06:14 +01:00
|
|
|
"Bad peer_add_htlc: %s",
|
|
|
|
channel_add_err_name(add_err));
|
2017-11-28 06:03:13 +01:00
|
|
|
|
|
|
|
/* If this is wrong, we don't complain yet; when it's confirmed we'll
|
|
|
|
* send it to the master which handles all HTLC failures. */
|
2019-01-08 01:52:13 +01:00
|
|
|
htlc->shared_secret = get_shared_secret(htlc, htlc,
|
2019-01-08 01:53:25 +01:00
|
|
|
&htlc->why_bad_onion,
|
|
|
|
&htlc->next_onion_sha);
|
2017-04-01 12:26:07 +02:00
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void handle_peer_feechange(struct peer *peer, const u8 *msg)
|
2017-11-21 06:26:59 +01:00
|
|
|
{
|
|
|
|
struct channel_id channel_id;
|
|
|
|
u32 feerate;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_update_fee(msg, &channel_id, &feerate)) {
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-11-21 06:26:59 +01:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad update_fee %s", tal_hex(msg, msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A receiving node:
|
|
|
|
*...
|
|
|
|
* - if the sender is not responsible for paying the Bitcoin fee:
|
|
|
|
* - MUST fail the channel.
|
2017-11-21 06:26:59 +01:00
|
|
|
*/
|
|
|
|
if (peer->channel->funder != REMOTE)
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-11-21 06:26:59 +01:00
|
|
|
&peer->channel_id,
|
|
|
|
"update_fee from non-funder?");
|
|
|
|
|
2018-01-16 10:24:46 +01:00
|
|
|
status_trace("update_fee %u, range %u-%u",
|
|
|
|
feerate, peer->feerate_min, peer->feerate_max);
|
|
|
|
|
2017-11-21 06:26:59 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A receiving node:
|
|
|
|
* - if the `update_fee` is too low for timely processing, OR is
|
|
|
|
* unreasonably large:
|
|
|
|
* - SHOULD fail the channel.
|
2017-11-21 06:26:59 +01:00
|
|
|
*/
|
|
|
|
if (feerate < peer->feerate_min || feerate > peer->feerate_max)
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-11-21 06:26:59 +01:00
|
|
|
&peer->channel_id,
|
|
|
|
"update_fee %u outside range %u-%u",
|
|
|
|
feerate, peer->feerate_min, peer->feerate_max);
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - if the sender cannot afford the new fee rate on the receiving
|
|
|
|
* node's current commitment transaction:
|
|
|
|
* - SHOULD fail the channel,
|
|
|
|
* - but MAY delay this check until the `update_fee` is committed.
|
2017-11-21 06:26:59 +01:00
|
|
|
*/
|
|
|
|
if (!channel_update_feerate(peer->channel, feerate))
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-11-21 06:26:59 +01:00
|
|
|
&peer->channel_id,
|
|
|
|
"update_fee %u unaffordable",
|
|
|
|
feerate);
|
|
|
|
|
2017-11-21 06:26:59 +01:00
|
|
|
status_trace("peer updated fee to %u", feerate);
|
2017-11-21 06:26:59 +01:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
static struct changed_htlc *changed_htlc_arr(const tal_t *ctx,
|
|
|
|
const struct htlc **changed_htlcs)
|
2017-04-01 12:28:39 +02:00
|
|
|
{
|
2017-06-20 07:50:03 +02:00
|
|
|
struct changed_htlc *changed;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
changed = tal_arr(ctx, struct changed_htlc, tal_count(changed_htlcs));
|
|
|
|
for (i = 0; i < tal_count(changed_htlcs); i++) {
|
|
|
|
changed[i].id = changed_htlcs[i]->id;
|
|
|
|
changed[i].newstate = changed_htlcs[i]->state;
|
|
|
|
}
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u8 *sending_commitsig_msg(const tal_t *ctx,
|
|
|
|
u64 remote_commit_index,
|
2017-11-21 06:26:59 +01:00
|
|
|
u32 remote_feerate,
|
2017-06-20 08:08:03 +02:00
|
|
|
const struct htlc **changed_htlcs,
|
2018-12-03 00:15:06 +01:00
|
|
|
const struct bitcoin_signature *commit_sig,
|
2017-06-20 08:08:03 +02:00
|
|
|
const secp256k1_ecdsa_signature *htlc_sigs)
|
2017-06-20 07:50:03 +02:00
|
|
|
{
|
|
|
|
struct changed_htlc *changed;
|
2017-04-01 12:28:39 +02:00
|
|
|
u8 *msg;
|
2017-06-20 07:50:03 +02:00
|
|
|
|
|
|
|
/* We tell master what (of our) HTLCs peer will now be
|
|
|
|
* committed to. */
|
|
|
|
changed = changed_htlc_arr(tmpctx, changed_htlcs);
|
|
|
|
msg = towire_channel_sending_commitsig(ctx, remote_commit_index,
|
2017-11-21 06:26:59 +01:00
|
|
|
remote_feerate,
|
2017-06-20 08:08:03 +02:00
|
|
|
changed, commit_sig, htlc_sigs);
|
2017-06-20 07:50:03 +02:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2017-07-04 02:47:32 +02:00
|
|
|
static bool shutdown_complete(const struct peer *peer)
|
|
|
|
{
|
|
|
|
return peer->shutdown_sent[LOCAL]
|
|
|
|
&& peer->shutdown_sent[REMOTE]
|
2018-02-23 06:53:47 +01:00
|
|
|
&& num_channel_htlcs(peer->channel) == 0
|
2018-01-19 02:37:55 +01:00
|
|
|
/* We could be awaiting revoke-and-ack for a feechange */
|
|
|
|
&& peer->revocations_received == peer->next_index[REMOTE] - 1;
|
|
|
|
|
2017-07-04 02:47:32 +02:00
|
|
|
}
|
|
|
|
|
2017-06-26 03:16:43 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A sending node:
|
|
|
|
*...
|
|
|
|
* - if there are updates pending on the receiving node's commitment
|
|
|
|
* transaction:
|
|
|
|
* - MUST NOT send a `shutdown`.
|
2017-06-26 03:16:43 +02:00
|
|
|
*/
|
|
|
|
/* So we only call this after reestablish or immediately after sending commit */
|
|
|
|
static void maybe_send_shutdown(struct peer *peer)
|
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
|
2018-03-07 01:06:07 +01:00
|
|
|
if (!peer->send_shutdown)
|
2017-06-26 03:16:43 +02:00
|
|
|
return;
|
|
|
|
|
2018-01-21 12:01:18 +01:00
|
|
|
/* Send a disable channel_update so others don't try to route
|
|
|
|
* over us */
|
2018-05-21 06:35:40 +02:00
|
|
|
send_channel_update(peer, ROUTING_FLAGS_DISABLED);
|
2018-01-21 12:01:18 +01:00
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_shutdown(NULL, &peer->channel_id, peer->final_scriptpubkey);
|
2018-08-02 08:49:55 +02:00
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
2018-03-07 01:06:07 +01:00
|
|
|
peer->send_shutdown = false;
|
2017-07-04 02:47:32 +02:00
|
|
|
peer->shutdown_sent[LOCAL] = true;
|
2018-02-23 06:53:47 +01:00
|
|
|
billboard_update(peer);
|
2017-06-26 03:16:43 +02:00
|
|
|
}
|
|
|
|
|
2017-11-28 23:52:29 +01:00
|
|
|
/* This queues other traffic from the fd until we get reply. */
|
|
|
|
static u8 *wait_sync_reply(const tal_t *ctx,
|
|
|
|
const u8 *msg,
|
|
|
|
int replytype,
|
|
|
|
int fd,
|
|
|
|
struct msg_queue *queue,
|
|
|
|
const char *who)
|
2017-06-20 07:50:03 +02:00
|
|
|
{
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
u8 *reply;
|
2017-06-20 08:08:03 +02:00
|
|
|
|
2017-11-28 23:52:29 +01:00
|
|
|
status_trace("Sending %s %u", who, fromwire_peektype(msg));
|
2017-06-20 08:20:03 +02:00
|
|
|
|
2017-11-28 23:52:29 +01:00
|
|
|
if (!wire_sync_write(fd, msg))
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-11-28 23:52:29 +01:00
|
|
|
"Could not set sync write to %s: %s",
|
|
|
|
who, strerror(errno));
|
2017-06-20 08:08:03 +02:00
|
|
|
|
2017-11-28 23:52:29 +01:00
|
|
|
status_trace("... , awaiting %u", replytype);
|
2017-06-26 03:16:43 +02:00
|
|
|
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
for (;;) {
|
2017-11-28 23:52:29 +01:00
|
|
|
reply = wire_sync_read(ctx, fd);
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
if (!reply)
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-11-28 23:52:29 +01:00
|
|
|
"Could not set sync read from %s: %s",
|
|
|
|
who, strerror(errno));
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
if (fromwire_peektype(reply) == replytype) {
|
|
|
|
status_trace("Got it!");
|
|
|
|
break;
|
|
|
|
}
|
2017-06-20 08:08:03 +02:00
|
|
|
|
2017-11-28 23:52:29 +01:00
|
|
|
status_trace("Nope, got %u instead", fromwire_peektype(reply));
|
|
|
|
msg_enqueue(queue, take(reply));
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
}
|
2017-06-20 08:08:03 +02:00
|
|
|
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
return reply;
|
2017-06-20 08:08:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-28 23:52:29 +01:00
|
|
|
static u8 *master_wait_sync_reply(const tal_t *ctx,
|
|
|
|
struct peer *peer, const u8 *msg,
|
|
|
|
enum channel_wire_type replytype)
|
|
|
|
{
|
|
|
|
return wait_sync_reply(ctx, msg, replytype,
|
2018-10-25 01:46:08 +02:00
|
|
|
MASTER_FD, peer->from_master, "master");
|
2017-11-28 23:52:29 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 04:17:35 +01:00
|
|
|
static u8 *gossipd_wait_sync_reply(const tal_t *ctx,
|
2017-11-28 23:52:29 +01:00
|
|
|
struct peer *peer, const u8 *msg,
|
2018-11-13 05:03:51 +01:00
|
|
|
enum gossip_peerd_wire_type replytype)
|
2017-11-28 23:52:29 +01:00
|
|
|
{
|
|
|
|
return wait_sync_reply(ctx, msg, replytype,
|
2018-10-25 01:46:08 +02:00
|
|
|
GOSSIP_FD, peer->from_gossipd, "gossipd");
|
2017-11-28 23:52:29 +01:00
|
|
|
}
|
|
|
|
|
2018-07-02 06:14:43 +02:00
|
|
|
static u8 *foreign_channel_update(const tal_t *ctx,
|
|
|
|
struct peer *peer,
|
|
|
|
const struct short_channel_id *scid)
|
|
|
|
{
|
2018-07-30 13:16:20 +02:00
|
|
|
u8 *msg, *update, *channel_update;
|
2018-07-02 06:14:43 +02:00
|
|
|
|
2018-11-13 05:03:51 +01:00
|
|
|
msg = towire_gossipd_get_update(NULL, scid);
|
2018-07-02 06:14:43 +02:00
|
|
|
msg = gossipd_wait_sync_reply(tmpctx, peer, take(msg),
|
2018-11-13 05:03:51 +01:00
|
|
|
WIRE_GOSSIPD_GET_UPDATE_REPLY);
|
|
|
|
if (!fromwire_gossipd_get_update_reply(ctx, msg, &update))
|
2018-07-02 06:14:43 +02:00
|
|
|
status_failed(STATUS_FAIL_GOSSIP_IO,
|
|
|
|
"Invalid update reply");
|
2018-07-30 13:16:20 +02:00
|
|
|
|
|
|
|
/* Strip the type from the channel_update. Due to the specification
|
|
|
|
* being underspecified, some implementations skipped the type
|
|
|
|
* prefix. Since we are in the minority we adapt (See #1730 and
|
|
|
|
* lightningnetwork/lnd#1599 for details). */
|
|
|
|
if (update && fromwire_peektype(update) == WIRE_CHANNEL_UPDATE) {
|
|
|
|
assert(tal_bytelen(update) > 2);
|
|
|
|
channel_update = tal_arr(ctx, u8, 0);
|
|
|
|
towire(&channel_update, update + 2, tal_bytelen(update) - 2);
|
|
|
|
tal_free(update);
|
|
|
|
return channel_update;
|
|
|
|
} else {
|
|
|
|
return update;
|
|
|
|
}
|
2018-07-02 06:14:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static u8 *make_failmsg(const tal_t *ctx,
|
|
|
|
struct peer *peer,
|
|
|
|
const struct htlc *htlc,
|
|
|
|
enum onion_type failcode,
|
2019-01-08 01:53:13 +01:00
|
|
|
const struct short_channel_id *scid,
|
|
|
|
const struct sha256 *sha256)
|
2018-07-02 06:14:43 +02:00
|
|
|
{
|
|
|
|
u8 *msg, *channel_update = NULL;
|
|
|
|
u32 cltv_expiry = abs_locktime_to_blocks(&htlc->expiry);
|
|
|
|
|
|
|
|
switch (failcode) {
|
|
|
|
case WIRE_INVALID_REALM:
|
|
|
|
msg = towire_invalid_realm(ctx);
|
|
|
|
goto done;
|
|
|
|
case WIRE_TEMPORARY_NODE_FAILURE:
|
|
|
|
msg = towire_temporary_node_failure(ctx);
|
|
|
|
goto done;
|
|
|
|
case WIRE_PERMANENT_NODE_FAILURE:
|
|
|
|
msg = towire_permanent_node_failure(ctx);
|
|
|
|
goto done;
|
|
|
|
case WIRE_REQUIRED_NODE_FEATURE_MISSING:
|
|
|
|
msg = towire_required_node_feature_missing(ctx);
|
|
|
|
goto done;
|
|
|
|
case WIRE_TEMPORARY_CHANNEL_FAILURE:
|
|
|
|
channel_update = foreign_channel_update(ctx, peer, scid);
|
|
|
|
msg = towire_temporary_channel_failure(ctx, channel_update);
|
|
|
|
goto done;
|
|
|
|
case WIRE_CHANNEL_DISABLED:
|
|
|
|
msg = towire_channel_disabled(ctx);
|
|
|
|
goto done;
|
|
|
|
case WIRE_PERMANENT_CHANNEL_FAILURE:
|
|
|
|
msg = towire_permanent_channel_failure(ctx);
|
|
|
|
goto done;
|
|
|
|
case WIRE_REQUIRED_CHANNEL_FEATURE_MISSING:
|
|
|
|
msg = towire_required_channel_feature_missing(ctx);
|
|
|
|
goto done;
|
|
|
|
case WIRE_UNKNOWN_NEXT_PEER:
|
|
|
|
msg = towire_unknown_next_peer(ctx);
|
|
|
|
goto done;
|
|
|
|
case WIRE_AMOUNT_BELOW_MINIMUM:
|
|
|
|
channel_update = foreign_channel_update(ctx, peer, scid);
|
2019-02-21 04:45:55 +01:00
|
|
|
msg = towire_amount_below_minimum(ctx, htlc->amount,
|
2018-07-02 06:14:43 +02:00
|
|
|
channel_update);
|
|
|
|
goto done;
|
|
|
|
case WIRE_FEE_INSUFFICIENT:
|
|
|
|
channel_update = foreign_channel_update(ctx, peer, scid);
|
2019-02-21 04:45:55 +01:00
|
|
|
msg = towire_fee_insufficient(ctx, htlc->amount,
|
2018-07-02 06:14:43 +02:00
|
|
|
channel_update);
|
|
|
|
goto done;
|
|
|
|
case WIRE_INCORRECT_CLTV_EXPIRY:
|
|
|
|
channel_update = foreign_channel_update(ctx, peer, scid);
|
|
|
|
msg = towire_incorrect_cltv_expiry(ctx, cltv_expiry,
|
|
|
|
channel_update);
|
|
|
|
goto done;
|
|
|
|
case WIRE_EXPIRY_TOO_SOON:
|
|
|
|
channel_update = foreign_channel_update(ctx, peer, scid);
|
|
|
|
msg = towire_expiry_too_soon(ctx, channel_update);
|
|
|
|
goto done;
|
|
|
|
case WIRE_EXPIRY_TOO_FAR:
|
|
|
|
msg = towire_expiry_too_far(ctx);
|
|
|
|
goto done;
|
2019-01-14 03:26:12 +01:00
|
|
|
case WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS:
|
|
|
|
msg = towire_incorrect_or_unknown_payment_details(
|
2019-02-21 04:45:55 +01:00
|
|
|
ctx, htlc->amount);
|
2018-07-02 06:14:43 +02:00
|
|
|
goto done;
|
|
|
|
case WIRE_FINAL_EXPIRY_TOO_SOON:
|
|
|
|
msg = towire_final_expiry_too_soon(ctx);
|
|
|
|
goto done;
|
|
|
|
case WIRE_FINAL_INCORRECT_CLTV_EXPIRY:
|
|
|
|
msg = towire_final_incorrect_cltv_expiry(ctx, cltv_expiry);
|
|
|
|
goto done;
|
|
|
|
case WIRE_FINAL_INCORRECT_HTLC_AMOUNT:
|
2019-02-21 04:45:55 +01:00
|
|
|
msg = towire_final_incorrect_htlc_amount(ctx, htlc->amount);
|
2018-07-02 06:14:43 +02:00
|
|
|
goto done;
|
|
|
|
case WIRE_INVALID_ONION_VERSION:
|
2019-01-08 01:53:13 +01:00
|
|
|
msg = towire_invalid_onion_version(ctx, sha256);
|
|
|
|
goto done;
|
2018-07-02 06:14:43 +02:00
|
|
|
case WIRE_INVALID_ONION_HMAC:
|
2019-01-08 01:53:13 +01:00
|
|
|
msg = towire_invalid_onion_hmac(ctx, sha256);
|
|
|
|
goto done;
|
2018-07-02 06:14:43 +02:00
|
|
|
case WIRE_INVALID_ONION_KEY:
|
2019-01-08 01:53:13 +01:00
|
|
|
msg = towire_invalid_onion_key(ctx, sha256);
|
|
|
|
goto done;
|
2018-07-02 06:14:43 +02:00
|
|
|
}
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Asked to create failmsg %u (%s)",
|
|
|
|
failcode, onion_type_name(failcode));
|
|
|
|
|
|
|
|
done:
|
|
|
|
tal_free(channel_update);
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:14:53 +01:00
|
|
|
/* Returns HTLC sigs, sets commit_sig */
|
|
|
|
static secp256k1_ecdsa_signature *calc_commitsigs(const tal_t *ctx,
|
|
|
|
const struct peer *peer,
|
|
|
|
u64 commit_index,
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature *commit_sig)
|
2017-06-20 08:08:03 +02:00
|
|
|
{
|
2017-04-01 12:28:39 +02:00
|
|
|
size_t i;
|
|
|
|
struct bitcoin_tx **txs;
|
|
|
|
const u8 **wscripts;
|
|
|
|
const struct htlc **htlc_map;
|
2018-07-23 04:23:03 +02:00
|
|
|
struct pubkey local_htlckey;
|
|
|
|
const u8 *msg;
|
2018-12-03 00:14:53 +01:00
|
|
|
secp256k1_ecdsa_signature *htlc_sigs;
|
2017-06-20 08:10:03 +02:00
|
|
|
|
2017-04-01 12:28:39 +02:00
|
|
|
txs = channel_txs(tmpctx, &htlc_map, &wscripts, peer->channel,
|
2017-06-20 08:10:03 +02:00
|
|
|
&peer->remote_per_commit,
|
2017-06-20 08:11:03 +02:00
|
|
|
commit_index,
|
2017-06-20 08:10:03 +02:00
|
|
|
REMOTE);
|
2017-04-01 12:28:39 +02:00
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
msg = towire_hsm_sign_remote_commitment_tx(NULL, txs[0],
|
|
|
|
&peer->channel->funding_pubkey[REMOTE],
|
2019-03-22 14:01:16 +01:00
|
|
|
*txs[0]->input_amounts[0]);
|
2018-07-23 04:23:03 +02:00
|
|
|
|
|
|
|
msg = hsm_req(tmpctx, take(msg));
|
2018-12-03 00:14:53 +01:00
|
|
|
if (!fromwire_hsm_sign_tx_reply(msg, commit_sig))
|
2018-07-23 04:23:03 +02:00
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
|
|
|
"Reading sign_remote_commitment_tx reply: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
2017-04-01 12:28:39 +02:00
|
|
|
|
2017-06-20 08:10:03 +02:00
|
|
|
status_trace("Creating commit_sig signature %"PRIu64" %s for tx %s wscript %s key %s",
|
2017-06-20 08:11:03 +02:00
|
|
|
commit_index,
|
2018-12-03 00:15:06 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_signature,
|
2018-12-03 00:14:53 +01:00
|
|
|
commit_sig),
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_tx, txs[0]),
|
|
|
|
tal_hex(tmpctx, wscripts[0]),
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2017-04-01 12:28:39 +02:00
|
|
|
&peer->channel->funding_pubkey[LOCAL]));
|
2017-06-20 07:50:03 +02:00
|
|
|
dump_htlcs(peer->channel, "Sending commit_sig");
|
2017-04-01 12:28:39 +02:00
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
if (!derive_simple_key(&peer->channel->basepoints[LOCAL].htlc,
|
|
|
|
&peer->remote_per_commit,
|
|
|
|
&local_htlckey))
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Deriving local_htlckey");
|
|
|
|
|
2017-04-01 12:28:39 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A sending node:
|
|
|
|
*...
|
|
|
|
* - MUST include one `htlc_signature` for every HTLC transaction
|
2019-04-01 05:25:45 +02:00
|
|
|
* corresponding to the ordering of the commitment transaction
|
2017-04-01 12:28:39 +02:00
|
|
|
*/
|
2018-12-03 00:14:53 +01:00
|
|
|
htlc_sigs = tal_arr(ctx, secp256k1_ecdsa_signature, tal_count(txs) - 1);
|
2017-04-01 12:28:39 +02:00
|
|
|
|
2018-12-03 00:14:53 +01:00
|
|
|
for (i = 0; i < tal_count(htlc_sigs); i++) {
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature sig;
|
2018-07-23 04:23:03 +02:00
|
|
|
msg = towire_hsm_sign_remote_htlc_tx(NULL, txs[i + 1],
|
|
|
|
wscripts[i + 1],
|
2019-03-22 14:01:16 +01:00
|
|
|
*txs[i+1]->input_amounts[0],
|
2018-07-23 04:23:03 +02:00
|
|
|
&peer->remote_per_commit);
|
|
|
|
|
|
|
|
msg = hsm_req(tmpctx, take(msg));
|
2018-12-03 00:15:06 +01:00
|
|
|
if (!fromwire_hsm_sign_tx_reply(msg, &sig))
|
2018-07-23 04:23:03 +02:00
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
|
|
|
"Bad sign_remote_htlc_tx reply: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
|
2018-12-03 00:15:06 +01:00
|
|
|
htlc_sigs[i] = sig.s;
|
2017-04-01 12:28:39 +02:00
|
|
|
status_trace("Creating HTLC signature %s for tx %s wscript %s key %s",
|
2018-12-03 00:15:06 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_signature,
|
|
|
|
&sig),
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct bitcoin_tx, txs[1+i]),
|
|
|
|
tal_hex(tmpctx, wscripts[1+i]),
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2017-11-15 07:19:39 +01:00
|
|
|
&local_htlckey));
|
2017-04-01 12:28:39 +02:00
|
|
|
assert(check_tx_sig(txs[1+i], 0, NULL, wscripts[1+i],
|
2017-11-15 07:19:39 +01:00
|
|
|
&local_htlckey,
|
2018-12-03 00:15:06 +01:00
|
|
|
&sig));
|
2017-04-01 12:28:39 +02:00
|
|
|
}
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-12-03 00:14:53 +01:00
|
|
|
return htlc_sigs;
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 04:53:18 +02:00
|
|
|
/* Have we received something from peer recently? */
|
|
|
|
static bool peer_recently_active(struct peer *peer)
|
|
|
|
{
|
|
|
|
return time_less(time_between(time_now(), peer->last_recv),
|
|
|
|
time_from_sec(30));
|
|
|
|
}
|
|
|
|
|
2018-08-09 04:53:17 +02:00
|
|
|
static void maybe_send_ping(struct peer *peer)
|
|
|
|
{
|
|
|
|
/* Already have a ping in flight? */
|
|
|
|
if (peer->expecting_pong)
|
|
|
|
return;
|
|
|
|
|
2018-08-09 04:53:18 +02:00
|
|
|
if (peer_recently_active(peer))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Send a ping to try to elicit a receive. */
|
2018-08-09 04:53:17 +02:00
|
|
|
sync_crypto_write_no_delay(&peer->cs, PEER_FD,
|
|
|
|
take(make_ping(NULL, 1, 0)));
|
|
|
|
peer->expecting_pong = true;
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
static void send_commit(struct peer *peer)
|
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
const struct htlc **changed_htlcs;
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature commit_sig;
|
|
|
|
secp256k1_ecdsa_signature *htlc_sigs;
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2017-09-26 06:57:31 +02:00
|
|
|
/* Hack to suppress all commit sends if dev_disconnect says to */
|
|
|
|
if (dev_suppress_commit) {
|
|
|
|
peer->commit_timer = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif
|
2017-09-26 06:57:31 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* FIXME: Document this requirement in BOLT 2! */
|
|
|
|
/* We can't send two commits in a row. */
|
2017-12-02 06:55:21 +01:00
|
|
|
if (peer->revocations_received != peer->next_index[REMOTE] - 1) {
|
|
|
|
assert(peer->revocations_received
|
|
|
|
== peer->next_index[REMOTE] - 2);
|
2018-01-26 01:19:46 +01:00
|
|
|
peer->commit_timer_attempts++;
|
|
|
|
/* Only report this in extreme cases */
|
|
|
|
if (peer->commit_timer_attempts % 100 == 0)
|
|
|
|
status_trace("Can't send commit:"
|
|
|
|
" waiting for revoke_and_ack with %"
|
|
|
|
PRIu64" attempts",
|
|
|
|
peer->commit_timer_attempts);
|
2017-06-20 07:50:03 +02:00
|
|
|
/* Mark this as done and try again. */
|
|
|
|
peer->commit_timer = NULL;
|
|
|
|
start_commit_timer(peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-02 01:46:18 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* - if no HTLCs remain in either commitment transaction:
|
|
|
|
* - MUST NOT send any `update` message after a `shutdown`.
|
|
|
|
*/
|
2018-02-23 06:53:47 +01:00
|
|
|
if (peer->shutdown_sent[LOCAL] && !num_channel_htlcs(peer->channel)) {
|
2018-02-02 01:46:18 +01:00
|
|
|
status_trace("Can't send commit: final shutdown phase");
|
|
|
|
|
|
|
|
peer->commit_timer = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-09 04:53:18 +02:00
|
|
|
/* If we haven't received a packet for > 30 seconds, delay. */
|
|
|
|
if (!peer_recently_active(peer)) {
|
|
|
|
/* Mark this as done and try again. */
|
|
|
|
peer->commit_timer = NULL;
|
|
|
|
start_commit_timer(peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-21 06:26:59 +01:00
|
|
|
/* If we wanted to update fees, do it now. */
|
|
|
|
if (peer->channel->funder == LOCAL
|
|
|
|
&& peer->desired_feerate != channel_feerate(peer->channel, REMOTE)) {
|
|
|
|
u8 *msg;
|
|
|
|
u32 feerate, max = approx_max_feerate(peer->channel);
|
|
|
|
|
|
|
|
feerate = peer->desired_feerate;
|
|
|
|
|
|
|
|
/* FIXME: We should avoid adding HTLCs until we can meet this
|
|
|
|
* feerate! */
|
|
|
|
if (feerate > max)
|
|
|
|
feerate = max;
|
|
|
|
|
|
|
|
if (!channel_update_feerate(peer->channel, feerate))
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Could not afford feerate %u"
|
|
|
|
" (vs max %u)",
|
|
|
|
feerate, max);
|
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_update_fee(NULL, &peer->channel_id, feerate);
|
2018-08-02 08:49:55 +02:00
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
2017-11-21 06:26:59 +01:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A sending node:
|
|
|
|
* - MUST NOT send a `commitment_signed` message that does not include
|
|
|
|
* any updates.
|
2017-06-20 07:50:03 +02:00
|
|
|
*/
|
|
|
|
changed_htlcs = tal_arr(tmpctx, const struct htlc *, 0);
|
|
|
|
if (!channel_sending_commit(peer->channel, &changed_htlcs)) {
|
|
|
|
status_trace("Can't send commit: nothing to send");
|
|
|
|
|
2017-06-26 03:16:43 +02:00
|
|
|
/* Covers the case where we've just been told to shutdown. */
|
|
|
|
maybe_send_shutdown(peer);
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
peer->commit_timer = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:14:53 +01:00
|
|
|
htlc_sigs = calc_commitsigs(tmpctx, peer, peer->next_index[REMOTE],
|
|
|
|
&commit_sig);
|
2017-06-20 08:08:03 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
status_trace("Telling master we're about to commit...");
|
2017-06-20 08:04:03 +02:00
|
|
|
/* Tell master to save this next commit to database, then wait. */
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = sending_commitsig_msg(NULL, peer->next_index[REMOTE],
|
2017-11-21 06:26:59 +01:00
|
|
|
channel_feerate(peer->channel, REMOTE),
|
2017-06-20 08:08:03 +02:00
|
|
|
changed_htlcs,
|
2018-12-03 00:14:53 +01:00
|
|
|
&commit_sig,
|
|
|
|
htlc_sigs);
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
/* Message is empty; receiving it is the point. */
|
|
|
|
master_wait_sync_reply(tmpctx, peer, take(msg),
|
|
|
|
WIRE_CHANNEL_SENDING_COMMITSIG_REPLY);
|
|
|
|
|
|
|
|
status_trace("Sending commit_sig with %zu htlc sigs",
|
2018-12-03 00:14:53 +01:00
|
|
|
tal_count(htlc_sigs));
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
|
|
|
|
peer->next_index[REMOTE]++;
|
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_commitment_signed(NULL, &peer->channel_id,
|
2018-12-03 00:15:06 +01:00
|
|
|
&commit_sig.s,
|
2018-12-03 00:14:53 +01:00
|
|
|
htlc_sigs);
|
2018-08-09 02:31:30 +02:00
|
|
|
sync_crypto_write_no_delay(&peer->cs, PEER_FD, take(msg));
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
|
|
|
|
maybe_send_shutdown(peer);
|
|
|
|
|
|
|
|
/* Timer now considered expired, you can add a new one. */
|
|
|
|
peer->commit_timer = NULL;
|
|
|
|
start_commit_timer(peer);
|
2017-04-01 12:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void start_commit_timer(struct peer *peer)
|
|
|
|
{
|
2018-08-09 04:53:17 +02:00
|
|
|
/* We should send a ping now if we need a liveness check. */
|
|
|
|
maybe_send_ping(peer);
|
|
|
|
|
2017-04-01 12:28:39 +02:00
|
|
|
/* Already armed? */
|
2018-01-26 01:19:46 +01:00
|
|
|
if (peer->commit_timer)
|
2017-04-01 12:28:39 +02:00
|
|
|
return;
|
|
|
|
|
2018-01-26 01:19:46 +01:00
|
|
|
peer->commit_timer_attempts = 0;
|
2017-04-01 12:28:39 +02:00
|
|
|
peer->commit_timer = new_reltimer(&peer->timers, peer,
|
|
|
|
time_from_msec(peer->commit_msec),
|
|
|
|
send_commit, peer);
|
|
|
|
}
|
|
|
|
|
2018-08-17 06:15:39 +02:00
|
|
|
/* If old_secret is NULL, we don't care, otherwise it is filled in. */
|
|
|
|
static void get_per_commitment_point(u64 index, struct pubkey *point,
|
|
|
|
struct secret *old_secret)
|
2017-06-20 07:50:03 +02:00
|
|
|
{
|
2018-08-17 06:15:39 +02:00
|
|
|
struct secret *s;
|
2018-07-23 04:23:03 +02:00
|
|
|
const u8 *msg;
|
2017-06-20 08:11:03 +02:00
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
msg = hsm_req(tmpctx,
|
2018-08-17 06:15:39 +02:00
|
|
|
take(towire_hsm_get_per_commitment_point(NULL, index)));
|
2017-06-20 08:10:03 +02:00
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
if (!fromwire_hsm_get_per_commitment_point_reply(tmpctx, msg,
|
|
|
|
point,
|
2018-08-17 06:15:39 +02:00
|
|
|
&s))
|
2018-07-23 04:23:03 +02:00
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
|
|
|
"Bad per_commitment_point reply %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-08-17 06:15:39 +02:00
|
|
|
if (old_secret) {
|
|
|
|
if (!s)
|
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
|
|
|
"No secret in per_commitment_point_reply %"
|
|
|
|
PRIu64,
|
|
|
|
index);
|
|
|
|
*old_secret = *s;
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2018-08-17 06:15:39 +02:00
|
|
|
/* revoke_index == current index - 1 (usually; not for retransmission) */
|
|
|
|
static u8 *make_revocation_msg(const struct peer *peer, u64 revoke_index,
|
|
|
|
struct pubkey *point)
|
|
|
|
{
|
|
|
|
struct secret old_commit_secret;
|
|
|
|
|
|
|
|
get_per_commitment_point(revoke_index+2, point, &old_commit_secret);
|
|
|
|
|
|
|
|
return towire_revoke_and_ack(peer, &peer->channel_id, &old_commit_secret,
|
2018-07-23 04:23:03 +02:00
|
|
|
point);
|
2017-06-20 08:11:03 +02:00
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void send_revocation(struct peer *peer)
|
2017-06-20 08:11:03 +02:00
|
|
|
{
|
2018-07-23 04:23:03 +02:00
|
|
|
/* Revoke previous commit, get new point. */
|
|
|
|
u8 *msg = make_revocation_msg(peer, peer->next_index[LOCAL]-1,
|
|
|
|
&peer->next_local_per_commit);
|
2017-06-20 08:11:03 +02:00
|
|
|
|
2017-06-27 04:55:06 +02:00
|
|
|
/* From now on we apply changes to the next commitment */
|
2017-06-27 04:55:06 +02:00
|
|
|
peer->next_index[LOCAL]++;
|
2017-06-20 08:11:03 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* If this queues more changes on the other end, send commit. */
|
|
|
|
if (channel_sending_revoke_and_ack(peer->channel)) {
|
|
|
|
status_trace("revoke_and_ack made pending: commit timer");
|
|
|
|
start_commit_timer(peer);
|
|
|
|
}
|
|
|
|
|
2018-08-09 02:31:30 +02:00
|
|
|
sync_crypto_write_no_delay(&peer->cs, PEER_FD, take(msg));
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static u8 *got_commitsig_msg(const tal_t *ctx,
|
|
|
|
u64 local_commit_index,
|
2017-11-21 06:26:59 +01:00
|
|
|
u32 local_feerate,
|
2018-12-03 00:15:06 +01:00
|
|
|
const struct bitcoin_signature *commit_sig,
|
2017-06-20 07:50:03 +02:00
|
|
|
const secp256k1_ecdsa_signature *htlc_sigs,
|
2017-08-18 06:43:52 +02:00
|
|
|
const struct htlc **changed_htlcs,
|
|
|
|
const struct bitcoin_tx *committx)
|
2017-06-20 07:50:03 +02:00
|
|
|
{
|
|
|
|
struct changed_htlc *changed;
|
|
|
|
struct fulfilled_htlc *fulfilled;
|
2018-02-08 02:24:46 +01:00
|
|
|
const struct failed_htlc **failed;
|
2017-06-20 07:50:03 +02:00
|
|
|
struct added_htlc *added;
|
2017-06-20 07:54:03 +02:00
|
|
|
struct secret *shared_secret;
|
2017-06-20 07:50:03 +02:00
|
|
|
u8 *msg;
|
|
|
|
|
|
|
|
changed = tal_arr(tmpctx, struct changed_htlc, 0);
|
|
|
|
added = tal_arr(tmpctx, struct added_htlc, 0);
|
2017-06-20 07:54:03 +02:00
|
|
|
shared_secret = tal_arr(tmpctx, struct secret, 0);
|
2018-02-08 02:24:46 +01:00
|
|
|
failed = tal_arr(tmpctx, const struct failed_htlc *, 0);
|
2017-06-20 07:50:03 +02:00
|
|
|
fulfilled = tal_arr(tmpctx, struct fulfilled_htlc, 0);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < tal_count(changed_htlcs); i++) {
|
|
|
|
const struct htlc *htlc = changed_htlcs[i];
|
|
|
|
if (htlc->state == RCVD_ADD_COMMIT) {
|
2019-01-15 04:51:27 +01:00
|
|
|
struct added_htlc a;
|
|
|
|
struct secret s;
|
|
|
|
|
|
|
|
a.id = htlc->id;
|
2019-02-21 04:45:55 +01:00
|
|
|
a.amount = htlc->amount;
|
2019-01-15 04:51:27 +01:00
|
|
|
a.payment_hash = htlc->rhash;
|
|
|
|
a.cltv_expiry = abs_locktime_to_blocks(&htlc->expiry);
|
|
|
|
memcpy(a.onion_routing_packet,
|
2017-06-20 07:50:03 +02:00
|
|
|
htlc->routing,
|
2019-01-15 04:51:27 +01:00
|
|
|
sizeof(a.onion_routing_packet));
|
2019-01-08 01:20:50 +01:00
|
|
|
/* Invalid shared secret gets set to all-zero: our
|
|
|
|
* code generator can't make arrays of optional values */
|
|
|
|
if (!htlc->shared_secret)
|
2019-01-15 04:51:27 +01:00
|
|
|
memset(&s, 0, sizeof(s));
|
2019-01-08 01:20:50 +01:00
|
|
|
else
|
2019-01-15 04:51:27 +01:00
|
|
|
s = *htlc->shared_secret;
|
|
|
|
tal_arr_expand(&added, a);
|
|
|
|
tal_arr_expand(&shared_secret, s);
|
2017-06-20 07:50:03 +02:00
|
|
|
} else if (htlc->state == RCVD_REMOVE_COMMIT) {
|
|
|
|
if (htlc->r) {
|
2019-01-15 04:51:27 +01:00
|
|
|
struct fulfilled_htlc f;
|
2019-01-08 01:53:25 +01:00
|
|
|
assert(!htlc->fail && !htlc->failcode);
|
2019-01-15 04:51:27 +01:00
|
|
|
f.id = htlc->id;
|
|
|
|
f.payment_preimage = *htlc->r;
|
|
|
|
tal_arr_expand(&fulfilled, f);
|
2017-06-20 07:50:03 +02:00
|
|
|
} else {
|
2018-09-27 02:19:24 +02:00
|
|
|
struct failed_htlc *f;
|
2019-01-08 01:53:25 +01:00
|
|
|
assert(htlc->fail || htlc->failcode);
|
2018-09-27 02:19:24 +02:00
|
|
|
f = tal(failed, struct failed_htlc);
|
|
|
|
f->id = htlc->id;
|
|
|
|
f->failcode = htlc->failcode;
|
|
|
|
f->failreason = cast_const(u8 *, htlc->fail);
|
|
|
|
f->scid = cast_const(struct short_channel_id *,
|
2018-07-26 23:23:37 +02:00
|
|
|
htlc->failed_scid);
|
2019-01-15 04:51:27 +01:00
|
|
|
tal_arr_expand(&failed, f);
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-01-15 04:51:27 +01:00
|
|
|
struct changed_htlc c;
|
2017-06-20 07:50:03 +02:00
|
|
|
assert(htlc->state == RCVD_REMOVE_ACK_COMMIT
|
|
|
|
|| htlc->state == RCVD_ADD_ACK_COMMIT);
|
|
|
|
|
2019-01-15 04:51:27 +01:00
|
|
|
c.id = htlc->id;
|
|
|
|
c.newstate = htlc->state;
|
|
|
|
tal_arr_expand(&changed, c);
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = towire_channel_got_commitsig(ctx, local_commit_index,
|
2017-11-21 06:26:59 +01:00
|
|
|
local_feerate,
|
2017-06-20 07:50:03 +02:00
|
|
|
commit_sig,
|
|
|
|
htlc_sigs,
|
|
|
|
added,
|
2017-06-20 07:54:03 +02:00
|
|
|
shared_secret,
|
2017-06-20 07:50:03 +02:00
|
|
|
fulfilled,
|
|
|
|
failed,
|
2017-08-18 06:43:52 +02:00
|
|
|
changed,
|
|
|
|
committx);
|
2017-06-20 07:50:03 +02:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
|
2017-04-01 12:28:39 +02:00
|
|
|
{
|
|
|
|
struct channel_id channel_id;
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature commit_sig;
|
|
|
|
secp256k1_ecdsa_signature *htlc_sigs;
|
2018-07-23 04:23:03 +02:00
|
|
|
struct pubkey remote_htlckey;
|
2017-04-01 12:28:39 +02:00
|
|
|
struct bitcoin_tx **txs;
|
2017-06-20 07:41:03 +02:00
|
|
|
const struct htlc **htlc_map, **changed_htlcs;
|
2017-04-01 12:28:39 +02:00
|
|
|
const u8 **wscripts;
|
|
|
|
size_t i;
|
|
|
|
|
2017-06-20 07:41:03 +02:00
|
|
|
changed_htlcs = tal_arr(msg, const struct htlc *, 0);
|
|
|
|
if (!channel_rcvd_commit(peer->channel, &changed_htlcs)) {
|
2017-04-01 12:28:39 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A sending node:
|
|
|
|
* - MUST NOT send a `commitment_signed` message that does not
|
|
|
|
* include any updates.
|
2017-04-01 12:28:39 +02:00
|
|
|
*/
|
2019-04-16 06:00:15 +02:00
|
|
|
status_trace("Oh hi LND! Empty commitment at #%"PRIu64,
|
|
|
|
peer->next_index[LOCAL]);
|
|
|
|
if (peer->last_empty_commitment == peer->next_index[LOCAL] - 1)
|
|
|
|
peer_failed(&peer->cs,
|
|
|
|
&peer->channel_id,
|
|
|
|
"commit_sig with no changes (again!)");
|
|
|
|
peer->last_empty_commitment = peer->next_index[LOCAL];
|
2017-04-01 12:28:39 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 06:26:25 +01:00
|
|
|
/* We were supposed to check this was affordable as we go. */
|
|
|
|
if (peer->channel->funder == REMOTE)
|
|
|
|
assert(can_funder_afford_feerate(peer->channel,
|
|
|
|
peer->channel->view[LOCAL]
|
|
|
|
.feerate_per_kw));
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_commitment_signed(tmpctx, msg,
|
2018-12-03 00:15:06 +01:00
|
|
|
&channel_id, &commit_sig.s, &htlc_sigs))
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 12:28:39 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad commit_sig %s", tal_hex(msg, msg));
|
2018-12-03 00:15:06 +01:00
|
|
|
/* SIGHASH_ALL is implied. */
|
|
|
|
commit_sig.sighash_type = SIGHASH_ALL;
|
2017-04-01 12:28:39 +02:00
|
|
|
|
|
|
|
txs = channel_txs(tmpctx, &htlc_map, &wscripts, peer->channel,
|
2018-07-23 04:23:03 +02:00
|
|
|
&peer->next_local_per_commit,
|
|
|
|
peer->next_index[LOCAL], LOCAL);
|
2017-04-01 12:28:39 +02:00
|
|
|
|
2017-11-15 07:19:39 +01:00
|
|
|
if (!derive_simple_key(&peer->channel->basepoints[REMOTE].htlc,
|
2018-07-23 04:23:03 +02:00
|
|
|
&peer->next_local_per_commit, &remote_htlckey))
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-11-15 07:19:39 +01:00
|
|
|
"Deriving remote_htlckey");
|
2017-06-20 08:10:03 +02:00
|
|
|
status_trace("Derived key %s from basepoint %s, point %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &remote_htlckey),
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2017-11-15 07:19:39 +01:00
|
|
|
&peer->channel->basepoints[REMOTE].htlc),
|
2018-07-23 04:23:03 +02:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
&peer->next_local_per_commit));
|
2017-04-01 12:28:39 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A receiving node:
|
|
|
|
* - once all pending updates are applied:
|
|
|
|
* - if `signature` is not valid for its local commitment transaction:
|
|
|
|
* - MUST fail the channel.
|
2017-04-01 12:28:39 +02:00
|
|
|
*/
|
|
|
|
if (!check_tx_sig(txs[0], 0, NULL, wscripts[0],
|
2017-06-20 07:50:03 +02:00
|
|
|
&peer->channel->funding_pubkey[REMOTE], &commit_sig)) {
|
|
|
|
dump_htlcs(peer->channel, "receiving commit_sig");
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 12:28:39 +02:00
|
|
|
&peer->channel_id,
|
2018-08-22 02:09:57 +02:00
|
|
|
"Bad commit_sig signature %"PRIu64" %s for tx %s wscript %s key %s feerate %u",
|
2017-06-27 04:55:06 +02:00
|
|
|
peer->next_index[LOCAL],
|
2018-12-03 00:15:06 +01:00
|
|
|
type_to_string(msg, struct bitcoin_signature,
|
2017-04-01 12:28:39 +02:00
|
|
|
&commit_sig),
|
|
|
|
type_to_string(msg, struct bitcoin_tx, txs[0]),
|
|
|
|
tal_hex(msg, wscripts[0]),
|
|
|
|
type_to_string(msg, struct pubkey,
|
|
|
|
&peer->channel->funding_pubkey
|
2018-08-22 02:09:57 +02:00
|
|
|
[REMOTE]),
|
|
|
|
peer->channel->view[LOCAL].feerate_per_kw);
|
2017-06-20 07:50:03 +02:00
|
|
|
}
|
2017-04-01 12:28:39 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A receiving node:
|
|
|
|
*...
|
|
|
|
* - if `num_htlcs` is not equal to the number of HTLC outputs in the
|
|
|
|
* local commitment transaction:
|
|
|
|
* - MUST fail the channel.
|
2017-04-01 12:28:39 +02:00
|
|
|
*/
|
|
|
|
if (tal_count(htlc_sigs) != tal_count(txs) - 1)
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 12:28:39 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Expected %zu htlc sigs, not %zu",
|
|
|
|
tal_count(txs) - 1, tal_count(htlc_sigs));
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - if any `htlc_signature` is not valid for the corresponding HTLC
|
|
|
|
* transaction:
|
|
|
|
* - MUST fail the channel.
|
2017-04-01 12:28:39 +02:00
|
|
|
*/
|
|
|
|
for (i = 0; i < tal_count(htlc_sigs); i++) {
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature sig;
|
|
|
|
|
|
|
|
/* SIGHASH_ALL is implied. */
|
|
|
|
sig.s = htlc_sigs[i];
|
|
|
|
sig.sighash_type = SIGHASH_ALL;
|
|
|
|
|
2017-04-01 12:28:39 +02:00
|
|
|
if (!check_tx_sig(txs[1+i], 0, NULL, wscripts[1+i],
|
2018-12-03 00:15:06 +01:00
|
|
|
&remote_htlckey, &sig))
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 12:28:39 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad commit_sig signature %s for htlc %s wscript %s key %s",
|
2018-12-03 00:15:06 +01:00
|
|
|
type_to_string(msg, struct bitcoin_signature, &sig),
|
2017-04-01 12:28:39 +02:00
|
|
|
type_to_string(msg, struct bitcoin_tx, txs[1+i]),
|
|
|
|
tal_hex(msg, wscripts[1+i]),
|
2017-11-15 07:19:39 +01:00
|
|
|
type_to_string(msg, struct pubkey,
|
|
|
|
&remote_htlckey));
|
2017-04-01 12:28:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
status_trace("Received commit_sig with %zu htlc sigs",
|
|
|
|
tal_count(htlc_sigs));
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* Tell master daemon, then wait for ack. */
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = got_commitsig_msg(NULL, peer->next_index[LOCAL],
|
2017-11-21 06:26:59 +01:00
|
|
|
channel_feerate(peer->channel, LOCAL),
|
|
|
|
&commit_sig, htlc_sigs, changed_htlcs, txs[0]);
|
2017-04-01 12:58:30 +02:00
|
|
|
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
master_wait_sync_reply(tmpctx, peer, take(msg),
|
|
|
|
WIRE_CHANNEL_GOT_COMMITSIG_REPLY);
|
2017-11-28 23:50:14 +01:00
|
|
|
return send_revocation(peer);
|
2017-04-01 12:28:39 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
static u8 *got_revoke_msg(const tal_t *ctx, u64 revoke_num,
|
2018-07-09 13:17:58 +02:00
|
|
|
const struct secret *per_commitment_secret,
|
2017-06-20 07:55:03 +02:00
|
|
|
const struct pubkey *next_per_commit_point,
|
2018-08-22 02:09:57 +02:00
|
|
|
const struct htlc **changed_htlcs,
|
|
|
|
u32 feerate)
|
2017-06-20 07:50:03 +02:00
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
struct changed_htlc *changed = tal_arr(tmpctx, struct changed_htlc, 0);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < tal_count(changed_htlcs); i++) {
|
2019-01-15 04:51:27 +01:00
|
|
|
struct changed_htlc c;
|
2017-06-20 07:50:03 +02:00
|
|
|
const struct htlc *htlc = changed_htlcs[i];
|
2017-06-20 07:54:03 +02:00
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
status_trace("HTLC %"PRIu64"[%s] => %s",
|
|
|
|
htlc->id, side_to_str(htlc_owner(htlc)),
|
|
|
|
htlc_state_name(htlc->state));
|
|
|
|
|
2019-01-15 04:51:27 +01:00
|
|
|
c.id = changed_htlcs[i]->id;
|
|
|
|
c.newstate = changed_htlcs[i]->state;
|
|
|
|
tal_arr_expand(&changed, c);
|
2017-04-01 12:58:30 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
msg = towire_channel_got_revoke(ctx, revoke_num, per_commitment_secret,
|
2018-08-22 02:09:57 +02:00
|
|
|
next_per_commit_point, feerate, changed);
|
2017-06-20 07:50:03 +02:00
|
|
|
return msg;
|
|
|
|
}
|
2017-04-01 12:58:30 +02:00
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void handle_peer_revoke_and_ack(struct peer *peer, const u8 *msg)
|
2017-04-01 12:58:30 +02:00
|
|
|
{
|
2018-07-09 13:17:58 +02:00
|
|
|
struct secret old_commit_secret;
|
2017-04-01 12:58:30 +02:00
|
|
|
struct privkey privkey;
|
|
|
|
struct channel_id channel_id;
|
|
|
|
struct pubkey per_commit_point, next_per_commit;
|
2017-06-20 07:41:03 +02:00
|
|
|
const struct htlc **changed_htlcs = tal_arr(msg, const struct htlc *, 0);
|
2017-04-01 12:58:30 +02:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_revoke_and_ack(msg, &channel_id, &old_commit_secret,
|
2017-04-01 12:58:30 +02:00
|
|
|
&next_per_commit)) {
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 12:58:30 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad revoke_and_ack %s", tal_hex(msg, msg));
|
|
|
|
}
|
|
|
|
|
2017-12-02 06:55:21 +01:00
|
|
|
if (peer->revocations_received != peer->next_index[REMOTE] - 2) {
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-12-02 06:55:21 +01:00
|
|
|
&peer->channel_id,
|
|
|
|
"Unexpected revoke_and_ack");
|
|
|
|
}
|
2017-11-20 07:09:29 +01:00
|
|
|
|
2017-04-01 12:58:30 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A receiving node:
|
|
|
|
* - if `per_commitment_secret` does not generate the previous
|
|
|
|
* `per_commitment_point`:
|
|
|
|
* - MUST fail the channel.
|
2017-04-01 12:58:30 +02:00
|
|
|
*/
|
|
|
|
memcpy(&privkey, &old_commit_secret, sizeof(privkey));
|
|
|
|
if (!pubkey_from_privkey(&privkey, &per_commit_point)) {
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 12:58:30 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad privkey %s",
|
|
|
|
type_to_string(msg, struct privkey, &privkey));
|
|
|
|
}
|
2017-06-20 08:10:03 +02:00
|
|
|
if (!pubkey_eq(&per_commit_point, &peer->old_remote_per_commit)) {
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 12:58:30 +02:00
|
|
|
&peer->channel_id,
|
2017-06-20 08:11:03 +02:00
|
|
|
"Wrong privkey %s for %"PRIu64" %s",
|
2017-04-01 12:58:30 +02:00
|
|
|
type_to_string(msg, struct privkey, &privkey),
|
2017-06-27 04:55:06 +02:00
|
|
|
peer->next_index[LOCAL]-2,
|
2017-04-01 12:58:30 +02:00
|
|
|
type_to_string(msg, struct pubkey,
|
2017-06-20 08:10:03 +02:00
|
|
|
&peer->old_remote_per_commit));
|
2017-04-01 12:58:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We start timer even if this returns false: we might have delayed
|
|
|
|
* commit because we were waiting for this! */
|
2017-06-20 07:41:03 +02:00
|
|
|
if (channel_rcvd_revoke_and_ack(peer->channel, &changed_htlcs))
|
2017-04-01 12:58:30 +02:00
|
|
|
status_trace("Commits outstanding after recv revoke_and_ack");
|
|
|
|
else
|
|
|
|
status_trace("No commits outstanding after recv revoke_and_ack");
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* Tell master about things this locks in, wait for response */
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = got_revoke_msg(NULL, peer->revocations_received++,
|
2017-06-20 07:55:03 +02:00
|
|
|
&old_commit_secret, &next_per_commit,
|
2018-08-22 02:09:57 +02:00
|
|
|
changed_htlcs,
|
|
|
|
channel_feerate(peer->channel, LOCAL));
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
master_wait_sync_reply(tmpctx, peer, take(msg),
|
|
|
|
WIRE_CHANNEL_GOT_REVOKE_REPLY);
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2017-06-20 08:10:03 +02:00
|
|
|
peer->old_remote_per_commit = peer->remote_per_commit;
|
|
|
|
peer->remote_per_commit = next_per_commit;
|
2017-06-20 08:11:03 +02:00
|
|
|
status_trace("revoke_and_ack %s: remote_per_commit = %s, old_remote_per_commit = %s",
|
|
|
|
side_to_str(peer->channel->funder),
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2017-06-20 08:11:03 +02:00
|
|
|
&peer->remote_per_commit),
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2017-06-20 08:11:03 +02:00
|
|
|
&peer->old_remote_per_commit));
|
2017-06-20 07:51:03 +02:00
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
start_commit_timer(peer);
|
2017-04-01 12:58:30 +02:00
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void handle_peer_fulfill_htlc(struct peer *peer, const u8 *msg)
|
2017-04-01 13:01:13 +02:00
|
|
|
{
|
|
|
|
struct channel_id channel_id;
|
|
|
|
u64 id;
|
|
|
|
struct preimage preimage;
|
|
|
|
enum channel_remove_err e;
|
2018-07-02 06:30:12 +02:00
|
|
|
struct htlc *h;
|
2017-04-01 13:01:13 +02:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_update_fulfill_htlc(msg, &channel_id,
|
2017-04-01 13:01:13 +02:00
|
|
|
&id, &preimage)) {
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 13:01:13 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad update_fulfill_htlc %s", tal_hex(msg, msg));
|
|
|
|
}
|
|
|
|
|
2018-07-02 06:30:12 +02:00
|
|
|
e = channel_fulfill_htlc(peer->channel, LOCAL, id, &preimage, &h);
|
2017-04-01 13:01:13 +02:00
|
|
|
switch (e) {
|
|
|
|
case CHANNEL_ERR_REMOVE_OK:
|
2017-06-20 07:50:03 +02:00
|
|
|
/* FIXME: We could send preimages to master immediately. */
|
2017-04-01 13:01:13 +02:00
|
|
|
start_commit_timer(peer);
|
2017-11-28 23:50:14 +01:00
|
|
|
return;
|
2017-04-01 13:01:13 +02:00
|
|
|
/* These shouldn't happen, because any offered HTLC (which would give
|
|
|
|
* us the preimage) should have timed out long before. If we
|
|
|
|
* were to get preimages from other sources, this could happen. */
|
|
|
|
case CHANNEL_ERR_NO_SUCH_ID:
|
|
|
|
case CHANNEL_ERR_ALREADY_FULFILLED:
|
|
|
|
case CHANNEL_ERR_HTLC_UNCOMMITTED:
|
|
|
|
case CHANNEL_ERR_HTLC_NOT_IRREVOCABLE:
|
|
|
|
case CHANNEL_ERR_BAD_PREIMAGE:
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 13:01:13 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad update_fulfill_htlc: failed to fulfill %"
|
2018-02-19 02:06:14 +01:00
|
|
|
PRIu64 " error %s", id, channel_remove_err_name(e));
|
2017-04-01 13:01:13 +02:00
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void handle_peer_fail_htlc(struct peer *peer, const u8 *msg)
|
2017-04-01 13:01:13 +02:00
|
|
|
{
|
|
|
|
struct channel_id channel_id;
|
|
|
|
u64 id;
|
|
|
|
enum channel_remove_err e;
|
|
|
|
u8 *reason;
|
2017-06-20 07:50:03 +02:00
|
|
|
struct htlc *htlc;
|
2017-04-01 13:01:13 +02:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_update_fail_htlc(msg, msg,
|
2017-04-01 13:01:13 +02:00
|
|
|
&channel_id, &id, &reason)) {
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 13:01:13 +02:00
|
|
|
&peer->channel_id,
|
2018-07-26 23:23:37 +02:00
|
|
|
"Bad update_fail_htlc %s", tal_hex(msg, msg));
|
2017-04-01 13:01:13 +02:00
|
|
|
}
|
|
|
|
|
2018-07-02 06:29:30 +02:00
|
|
|
e = channel_fail_htlc(peer->channel, LOCAL, id, &htlc);
|
2017-04-01 13:01:13 +02:00
|
|
|
switch (e) {
|
|
|
|
case CHANNEL_ERR_REMOVE_OK:
|
2017-06-20 07:50:03 +02:00
|
|
|
/* Save reason for when we tell master. */
|
|
|
|
htlc->fail = tal_steal(htlc, reason);
|
2017-04-01 13:01:13 +02:00
|
|
|
start_commit_timer(peer);
|
2017-11-28 23:50:14 +01:00
|
|
|
return;
|
2017-04-01 13:01:13 +02:00
|
|
|
case CHANNEL_ERR_NO_SUCH_ID:
|
|
|
|
case CHANNEL_ERR_ALREADY_FULFILLED:
|
|
|
|
case CHANNEL_ERR_HTLC_UNCOMMITTED:
|
|
|
|
case CHANNEL_ERR_HTLC_NOT_IRREVOCABLE:
|
|
|
|
case CHANNEL_ERR_BAD_PREIMAGE:
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 13:01:13 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad update_fail_htlc: failed to remove %"
|
2018-02-19 02:06:14 +01:00
|
|
|
PRIu64 " error %s", id,
|
|
|
|
channel_remove_err_name(e));
|
2017-04-01 13:01:13 +02:00
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void handle_peer_fail_malformed_htlc(struct peer *peer, const u8 *msg)
|
2017-05-02 07:26:29 +02:00
|
|
|
{
|
|
|
|
struct channel_id channel_id;
|
|
|
|
u64 id;
|
|
|
|
enum channel_remove_err e;
|
|
|
|
struct sha256 sha256_of_onion;
|
2017-06-20 07:50:03 +02:00
|
|
|
u16 failure_code;
|
|
|
|
struct htlc *htlc;
|
2017-05-02 07:26:29 +02:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_update_fail_malformed_htlc(msg, &channel_id, &id,
|
2017-06-20 07:50:03 +02:00
|
|
|
&sha256_of_onion,
|
|
|
|
&failure_code)) {
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-05-02 07:26:29 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad update_fail_malformed_htlc %s",
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:50:03 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - if the `BADONION` bit in `failure_code` is not set for
|
|
|
|
* `update_fail_malformed_htlc`:
|
|
|
|
* - MUST fail the channel.
|
2017-06-20 07:50:03 +02:00
|
|
|
*/
|
2019-01-08 01:53:26 +01:00
|
|
|
if (!(failure_code & BADONION)) {
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-06-20 07:50:03 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad update_fail_malformed_htlc failure code %u",
|
|
|
|
failure_code);
|
|
|
|
}
|
2019-01-08 01:53:26 +01:00
|
|
|
|
|
|
|
/* We only handle these cases in make_failmsg, so convert any
|
|
|
|
* (future?) unknown one. */
|
|
|
|
if (failure_code != WIRE_INVALID_ONION_VERSION
|
|
|
|
&& failure_code != WIRE_INVALID_ONION_HMAC
|
|
|
|
&& failure_code != WIRE_INVALID_ONION_KEY) {
|
|
|
|
status_unusual("Unknown update_fail_malformed_htlc code %u:"
|
|
|
|
" sending temporary_channel_failure",
|
|
|
|
failure_code);
|
|
|
|
failure_code = WIRE_TEMPORARY_CHANNEL_FAILURE;
|
|
|
|
}
|
2017-06-20 07:50:03 +02:00
|
|
|
|
2017-11-28 06:03:09 +01:00
|
|
|
e = channel_fail_htlc(peer->channel, LOCAL, id, &htlc);
|
2017-05-02 07:26:29 +02:00
|
|
|
switch (e) {
|
|
|
|
case CHANNEL_ERR_REMOVE_OK:
|
2017-06-20 07:50:03 +02:00
|
|
|
/* FIXME: Do this! */
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - if the `sha256_of_onion` in `update_fail_malformed_htlc`
|
|
|
|
* doesn't match the onion it sent:
|
|
|
|
* - MAY retry or choose an alternate error response.
|
2017-06-20 07:50:03 +02:00
|
|
|
*/
|
|
|
|
|
2019-01-08 01:53:25 +01:00
|
|
|
/* This is the only case where we set failcode for a non-local
|
|
|
|
* failure; in a way, it is, since we have to report it. */
|
|
|
|
htlc->failcode = failure_code;
|
2017-05-02 07:26:29 +02:00
|
|
|
start_commit_timer(peer);
|
2017-11-28 23:50:14 +01:00
|
|
|
return;
|
2017-05-02 07:26:29 +02:00
|
|
|
case CHANNEL_ERR_NO_SUCH_ID:
|
|
|
|
case CHANNEL_ERR_ALREADY_FULFILLED:
|
|
|
|
case CHANNEL_ERR_HTLC_UNCOMMITTED:
|
|
|
|
case CHANNEL_ERR_HTLC_NOT_IRREVOCABLE:
|
|
|
|
case CHANNEL_ERR_BAD_PREIMAGE:
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-05-02 07:26:29 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad update_fail_malformed_htlc: failed to remove %"
|
2018-02-19 02:06:14 +01:00
|
|
|
PRIu64 " error %s", id, channel_remove_err_name(e));
|
2017-05-02 07:26:29 +02:00
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void handle_peer_shutdown(struct peer *peer, const u8 *shutdown)
|
2017-06-26 03:16:43 +02:00
|
|
|
{
|
|
|
|
struct channel_id channel_id;
|
2018-05-17 07:08:11 +02:00
|
|
|
u8 *scriptpubkey;
|
2018-01-21 12:05:34 +01:00
|
|
|
|
2018-05-17 07:08:11 +02:00
|
|
|
/* Disable the channel. */
|
2018-05-21 06:35:40 +02:00
|
|
|
send_channel_update(peer, ROUTING_FLAGS_DISABLED);
|
2017-06-26 03:16:43 +02:00
|
|
|
|
2018-11-22 03:17:29 +01:00
|
|
|
if (!fromwire_shutdown(tmpctx, shutdown, &channel_id, &scriptpubkey))
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-09-12 06:55:52 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Bad shutdown %s", tal_hex(peer, shutdown));
|
2017-06-26 03:16:43 +02:00
|
|
|
|
2019-05-01 02:47:11 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* - if both nodes advertised the `option_upfront_shutdown_script`
|
|
|
|
* feature, and the receiving node received a non-zero-length
|
|
|
|
* `shutdown_scriptpubkey` in `open_channel` or `accept_channel`, and
|
|
|
|
* that `shutdown_scriptpubkey` is not equal to `scriptpubkey`:
|
|
|
|
* - MUST fail the connection.
|
|
|
|
*/
|
|
|
|
/* openingd only sets this if feature was negotiated at opening. */
|
|
|
|
if (tal_count(peer->remote_upfront_shutdown_script)
|
|
|
|
&& !memeq(scriptpubkey, tal_count(scriptpubkey),
|
|
|
|
peer->remote_upfront_shutdown_script,
|
|
|
|
tal_count(peer->remote_upfront_shutdown_script)))
|
|
|
|
peer_failed(&peer->cs,
|
|
|
|
&peer->channel_id,
|
|
|
|
"scriptpubkey %s is not as agreed upfront (%s)",
|
|
|
|
tal_hex(peer, scriptpubkey),
|
|
|
|
tal_hex(peer, peer->remote_upfront_shutdown_script));
|
|
|
|
|
2018-03-07 01:06:07 +01:00
|
|
|
/* Tell master: we don't have to wait because on reconnect other end
|
|
|
|
* will re-send anyway. */
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
wire_sync_write(MASTER_FD,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_channel_got_shutdown(NULL, scriptpubkey)));
|
2017-07-04 02:47:32 +02:00
|
|
|
|
|
|
|
peer->shutdown_sent[REMOTE] = true;
|
2018-03-07 01:06:07 +01:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A receiving node:
|
|
|
|
* ...
|
|
|
|
* - once there are no outstanding updates on the peer, UNLESS
|
|
|
|
* it has already sent a `shutdown`:
|
|
|
|
* - MUST reply to a `shutdown` message with a `shutdown`
|
|
|
|
*/
|
|
|
|
if (!peer->shutdown_sent[LOCAL]) {
|
|
|
|
peer->send_shutdown = true;
|
|
|
|
start_commit_timer(peer);
|
|
|
|
}
|
2018-02-23 06:53:47 +01:00
|
|
|
billboard_update(peer);
|
2017-06-26 03:16:43 +02:00
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void peer_in(struct peer *peer, const u8 *msg)
|
2017-04-01 12:26:07 +02:00
|
|
|
{
|
|
|
|
enum wire_type type = fromwire_peektype(msg);
|
|
|
|
|
2018-08-09 04:53:18 +02:00
|
|
|
peer->last_recv = time_now();
|
|
|
|
|
2018-08-09 04:53:17 +02:00
|
|
|
/* Catch our own ping replies. */
|
|
|
|
if (type == WIRE_PONG && peer->expecting_pong) {
|
|
|
|
peer->expecting_pong = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-04 07:53:13 +02:00
|
|
|
if (handle_peer_gossip_or_error(PEER_FD, GOSSIP_FD, GOSSIP_STORE_FD,
|
2018-08-02 08:49:55 +02:00
|
|
|
&peer->cs,
|
|
|
|
&peer->channel_id, msg))
|
|
|
|
return;
|
|
|
|
|
2017-04-01 12:26:07 +02:00
|
|
|
/* Must get funding_locked before almost anything. */
|
|
|
|
if (!peer->funding_locked[REMOTE]) {
|
2018-04-03 09:08:53 +02:00
|
|
|
if (type != WIRE_FUNDING_LOCKED
|
|
|
|
&& type != WIRE_PONG
|
2018-10-12 08:01:40 +02:00
|
|
|
&& type != WIRE_SHUTDOWN
|
2018-11-22 06:19:19 +01:00
|
|
|
/* lnd sends these early; it's harmless. */
|
|
|
|
&& type != WIRE_UPDATE_FEE
|
2018-10-12 08:01:40 +02:00
|
|
|
&& type != WIRE_ANNOUNCEMENT_SIGNATURES) {
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 12:26:07 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"%s (%u) before funding locked",
|
|
|
|
wire_type_name(type), type);
|
2017-03-11 21:49:52 +01:00
|
|
|
}
|
2017-04-01 12:26:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case WIRE_FUNDING_LOCKED:
|
2017-11-28 23:50:14 +01:00
|
|
|
handle_peer_funding_locked(peer, msg);
|
|
|
|
return;
|
2017-04-01 12:26:07 +02:00
|
|
|
case WIRE_ANNOUNCEMENT_SIGNATURES:
|
2017-11-28 23:50:14 +01:00
|
|
|
handle_peer_announcement_signatures(peer, msg);
|
|
|
|
return;
|
2017-04-01 12:26:07 +02:00
|
|
|
case WIRE_UPDATE_ADD_HTLC:
|
2017-11-28 23:50:14 +01:00
|
|
|
handle_peer_add_htlc(peer, msg);
|
|
|
|
return;
|
2017-04-01 12:28:39 +02:00
|
|
|
case WIRE_COMMITMENT_SIGNED:
|
2017-11-28 23:50:14 +01:00
|
|
|
handle_peer_commit_sig(peer, msg);
|
|
|
|
return;
|
2017-11-21 06:26:59 +01:00
|
|
|
case WIRE_UPDATE_FEE:
|
2017-11-28 23:50:14 +01:00
|
|
|
handle_peer_feechange(peer, msg);
|
|
|
|
return;
|
2017-04-01 12:58:30 +02:00
|
|
|
case WIRE_REVOKE_AND_ACK:
|
2017-11-28 23:50:14 +01:00
|
|
|
handle_peer_revoke_and_ack(peer, msg);
|
|
|
|
return;
|
2017-04-01 13:01:13 +02:00
|
|
|
case WIRE_UPDATE_FULFILL_HTLC:
|
2017-11-28 23:50:14 +01:00
|
|
|
handle_peer_fulfill_htlc(peer, msg);
|
|
|
|
return;
|
2017-04-01 13:01:13 +02:00
|
|
|
case WIRE_UPDATE_FAIL_HTLC:
|
2017-11-28 23:50:14 +01:00
|
|
|
handle_peer_fail_htlc(peer, msg);
|
|
|
|
return;
|
2017-05-02 07:26:29 +02:00
|
|
|
case WIRE_UPDATE_FAIL_MALFORMED_HTLC:
|
2017-11-28 23:50:14 +01:00
|
|
|
handle_peer_fail_malformed_htlc(peer, msg);
|
|
|
|
return;
|
2017-06-26 03:16:43 +02:00
|
|
|
case WIRE_SHUTDOWN:
|
2017-11-28 23:50:14 +01:00
|
|
|
handle_peer_shutdown(peer, msg);
|
|
|
|
return;
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2017-12-19 01:02:28 +01:00
|
|
|
case WIRE_INIT:
|
2017-04-01 12:26:07 +02:00
|
|
|
case WIRE_OPEN_CHANNEL:
|
|
|
|
case WIRE_ACCEPT_CHANNEL:
|
|
|
|
case WIRE_FUNDING_CREATED:
|
|
|
|
case WIRE_FUNDING_SIGNED:
|
2017-06-20 07:40:03 +02:00
|
|
|
case WIRE_CHANNEL_REESTABLISH:
|
2017-04-01 12:26:07 +02:00
|
|
|
case WIRE_CLOSING_SIGNED:
|
2017-11-28 23:50:14 +01:00
|
|
|
break;
|
2018-01-31 03:53:42 +01:00
|
|
|
|
2018-08-02 08:49:55 +02:00
|
|
|
/* These are all swallowed by handle_peer_gossip_or_error */
|
2018-01-31 03:53:42 +01:00
|
|
|
case WIRE_CHANNEL_ANNOUNCEMENT:
|
|
|
|
case WIRE_CHANNEL_UPDATE:
|
|
|
|
case WIRE_NODE_ANNOUNCEMENT:
|
2018-06-04 06:18:25 +02:00
|
|
|
case WIRE_QUERY_SHORT_CHANNEL_IDS:
|
|
|
|
case WIRE_QUERY_CHANNEL_RANGE:
|
|
|
|
case WIRE_REPLY_CHANNEL_RANGE:
|
|
|
|
case WIRE_GOSSIP_TIMESTAMP_FILTER:
|
|
|
|
case WIRE_REPLY_SHORT_CHANNEL_IDS_END:
|
2018-01-31 03:53:42 +01:00
|
|
|
case WIRE_PING:
|
2018-07-24 02:26:43 +02:00
|
|
|
case WIRE_PONG:
|
2018-01-31 03:53:42 +01:00
|
|
|
case WIRE_ERROR:
|
|
|
|
abort();
|
2017-03-10 15:11:54 +01:00
|
|
|
}
|
2017-03-07 02:26:12 +01:00
|
|
|
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-04-01 12:26:07 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Peer sent unknown message %u (%s)",
|
|
|
|
type, wire_type_name(type));
|
2017-03-07 02:26:12 +01:00
|
|
|
}
|
|
|
|
|
2017-06-20 08:11:03 +02:00
|
|
|
static void resend_revoke(struct peer *peer)
|
|
|
|
{
|
2018-07-23 04:23:03 +02:00
|
|
|
struct pubkey point;
|
2017-06-27 04:55:06 +02:00
|
|
|
/* Current commit is peer->next_index[LOCAL]-1, revoke prior */
|
2018-07-23 04:23:03 +02:00
|
|
|
u8 *msg = make_revocation_msg(peer, peer->next_index[LOCAL]-2, &point);
|
2018-08-02 08:49:55 +02:00
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
2017-06-20 08:11:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 08:22:03 +02:00
|
|
|
static void send_fail_or_fulfill(struct peer *peer, const struct htlc *h)
|
|
|
|
{
|
|
|
|
u8 *msg;
|
2018-07-02 06:30:12 +02:00
|
|
|
|
2019-01-08 01:53:13 +01:00
|
|
|
/* Note that if h->shared_secret is NULL, it means that we knew
|
|
|
|
* this HTLC was invalid, but we still needed to hand it to lightningd
|
|
|
|
* for the db, etc. So in that case, we use our own saved failcode.
|
|
|
|
*
|
|
|
|
* This also lets us distinguish between "we can't decode onion" and
|
|
|
|
* "next hop said it can't decode onion". That second case is the
|
|
|
|
* only case where we use a failcode for a non-local error. */
|
|
|
|
/* Malformed: use special reply since we can't onion. */
|
|
|
|
if (!h->shared_secret) {
|
2017-06-20 08:22:03 +02:00
|
|
|
struct sha256 sha256_of_onion;
|
2018-07-28 08:00:16 +02:00
|
|
|
sha256(&sha256_of_onion, h->routing, tal_count(h->routing));
|
2017-06-20 08:22:03 +02:00
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_update_fail_malformed_htlc(NULL, &peer->channel_id,
|
2017-06-20 08:22:03 +02:00
|
|
|
h->id, &sha256_of_onion,
|
2019-01-08 01:53:13 +01:00
|
|
|
h->why_bad_onion);
|
2018-07-02 06:30:12 +02:00
|
|
|
} else if (h->failcode || h->fail) {
|
|
|
|
const u8 *onion;
|
|
|
|
if (h->failcode) {
|
|
|
|
/* Local failure, make a message. */
|
|
|
|
u8 *failmsg = make_failmsg(tmpctx, peer, h, h->failcode,
|
2019-01-08 01:53:25 +01:00
|
|
|
h->failed_scid,
|
|
|
|
&h->next_onion_sha);
|
2018-07-02 06:30:12 +02:00
|
|
|
onion = create_onionreply(tmpctx, h->shared_secret,
|
|
|
|
failmsg);
|
|
|
|
} else /* Remote failure, just forward. */
|
|
|
|
onion = h->fail;
|
|
|
|
|
|
|
|
/* Now we wrap, just before sending out. */
|
|
|
|
msg = towire_update_fail_htlc(peer, &peer->channel_id, h->id,
|
|
|
|
wrap_onionreply(tmpctx,
|
|
|
|
h->shared_secret,
|
|
|
|
onion));
|
2017-06-20 08:22:03 +02:00
|
|
|
} else if (h->r) {
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_update_fulfill_htlc(NULL, &peer->channel_id, h->id,
|
2017-06-20 08:22:03 +02:00
|
|
|
h->r);
|
|
|
|
} else
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-06-20 08:22:03 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"HTLC %"PRIu64" state %s not failed/fulfilled",
|
|
|
|
h->id, htlc_state_name(h->state));
|
2018-08-02 08:49:55 +02:00
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
2017-06-20 08:22:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 08:11:03 +02:00
|
|
|
static void resend_commitment(struct peer *peer, const struct changed_htlc *last)
|
|
|
|
{
|
|
|
|
size_t i;
|
2018-12-03 00:15:06 +01:00
|
|
|
struct bitcoin_signature commit_sig;
|
|
|
|
secp256k1_ecdsa_signature *htlc_sigs;
|
2017-06-20 08:11:03 +02:00
|
|
|
u8 *msg;
|
|
|
|
|
2018-08-22 02:09:57 +02:00
|
|
|
status_trace("Retransmitting commitment, feerate LOCAL=%u REMOTE=%u",
|
|
|
|
channel_feerate(peer->channel, LOCAL),
|
|
|
|
channel_feerate(peer->channel, REMOTE));
|
|
|
|
|
2017-06-20 08:11:03 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - if `next_local_commitment_number` is equal to the commitment
|
|
|
|
* number of the last `commitment_signed` message the receiving node
|
|
|
|
* has sent:
|
|
|
|
* - MUST reuse the same commitment number for its next
|
|
|
|
* `commitment_signed`.
|
2017-06-20 08:11:03 +02:00
|
|
|
*/
|
|
|
|
/* In our case, we consider ourselves already committed to this, so
|
|
|
|
* retransmission is simplest. */
|
|
|
|
for (i = 0; i < tal_count(last); i++) {
|
|
|
|
const struct htlc *h;
|
|
|
|
|
|
|
|
h = channel_get_htlc(peer->channel,
|
|
|
|
htlc_state_owner(last[i].newstate),
|
|
|
|
last[i].id);
|
|
|
|
|
|
|
|
/* I think this can happen if we actually received revoke_and_ack
|
|
|
|
* then they asked for a retransmit */
|
|
|
|
if (!h)
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-06-20 08:11:03 +02:00
|
|
|
&peer->channel_id,
|
|
|
|
"Can't find HTLC %"PRIu64" to resend",
|
|
|
|
last[i].id);
|
|
|
|
|
|
|
|
if (h->state == SENT_ADD_COMMIT) {
|
2018-08-02 08:49:55 +02:00
|
|
|
u8 *msg = towire_update_add_htlc(NULL, &peer->channel_id,
|
2019-02-21 04:45:55 +01:00
|
|
|
h->id, h->amount,
|
2017-06-20 08:11:03 +02:00
|
|
|
&h->rhash,
|
|
|
|
abs_locktime_to_blocks(
|
|
|
|
&h->expiry),
|
|
|
|
h->routing);
|
2018-08-02 08:49:55 +02:00
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
2017-06-20 08:11:03 +02:00
|
|
|
} else if (h->state == SENT_REMOVE_COMMIT) {
|
2017-06-20 08:22:03 +02:00
|
|
|
send_fail_or_fulfill(peer, h);
|
2017-06-20 08:11:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 06:26:59 +01:00
|
|
|
/* Make sure they have the correct fee. */
|
|
|
|
if (peer->channel->funder == LOCAL) {
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_update_fee(NULL, &peer->channel_id,
|
2017-11-21 06:26:59 +01:00
|
|
|
channel_feerate(peer->channel, REMOTE));
|
2018-08-02 08:49:55 +02:00
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
2017-11-21 06:26:59 +01:00
|
|
|
}
|
|
|
|
|
2017-06-20 08:11:03 +02:00
|
|
|
/* Re-send the commitment_signed itself. */
|
2018-12-03 00:14:53 +01:00
|
|
|
htlc_sigs = calc_commitsigs(tmpctx, peer, peer->next_index[REMOTE]-1,
|
|
|
|
&commit_sig);
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_commitment_signed(NULL, &peer->channel_id,
|
2018-12-03 00:15:06 +01:00
|
|
|
&commit_sig.s, htlc_sigs);
|
2018-08-02 08:49:55 +02:00
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
2017-12-01 01:19:37 +01:00
|
|
|
|
2018-06-15 03:12:23 +02:00
|
|
|
/* If we have already received the revocation for the previous, the
|
|
|
|
* other side shouldn't be asking for a retransmit! */
|
|
|
|
if (peer->revocations_received != peer->next_index[REMOTE] - 2)
|
|
|
|
status_unusual("Retransmitted commitment_signed %"PRIu64
|
|
|
|
" but they already send revocation %"PRIu64"?",
|
|
|
|
peer->next_index[REMOTE]-1,
|
|
|
|
peer->revocations_received);
|
2017-06-20 08:11:03 +02:00
|
|
|
}
|
|
|
|
|
2018-08-17 07:06:36 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A receiving node:
|
|
|
|
* - if it supports `option_data_loss_protect`, AND the
|
|
|
|
* `option_data_loss_protect` fields are present:
|
|
|
|
* - if `next_remote_revocation_number` is greater than expected above,
|
|
|
|
* AND `your_last_per_commitment_secret` is correct for that
|
|
|
|
* `next_remote_revocation_number` minus 1:
|
|
|
|
*/
|
|
|
|
static void check_future_dataloss_fields(struct peer *peer,
|
|
|
|
u64 next_remote_revocation_number,
|
|
|
|
const struct secret *last_local_per_commit_secret,
|
|
|
|
const struct pubkey *remote_current_per_commitment_point)
|
|
|
|
{
|
|
|
|
const u8 *msg;
|
|
|
|
bool correct;
|
|
|
|
|
|
|
|
assert(next_remote_revocation_number > peer->next_index[LOCAL] - 1);
|
|
|
|
|
|
|
|
msg = towire_hsm_check_future_secret(NULL,
|
|
|
|
next_remote_revocation_number - 1,
|
|
|
|
last_local_per_commit_secret);
|
|
|
|
msg = hsm_req(tmpctx, take(msg));
|
|
|
|
if (!fromwire_hsm_check_future_secret_reply(msg, &correct))
|
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
|
|
|
"Bad hsm_check_future_secret_reply: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
|
|
|
|
if (!correct)
|
|
|
|
peer_failed(&peer->cs,
|
|
|
|
&peer->channel_id,
|
|
|
|
"bad future last_local_per_commit_secret: %"PRIu64
|
|
|
|
" vs %"PRIu64,
|
|
|
|
next_remote_revocation_number,
|
|
|
|
peer->next_index[LOCAL] - 1);
|
|
|
|
|
|
|
|
/* Oh shit, they really are from the future! */
|
|
|
|
peer_billboard(true, "They have future commitment number %"PRIu64
|
|
|
|
" vs our %"PRIu64". We must wait for them to close!",
|
|
|
|
next_remote_revocation_number,
|
|
|
|
peer->next_index[LOCAL] - 1);
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
* - MUST NOT broadcast its commitment transaction.
|
|
|
|
* - SHOULD fail the channel.
|
|
|
|
* - SHOULD store `my_current_per_commitment_point` to
|
|
|
|
* retrieve funds should the sending node broadcast its
|
|
|
|
* commitment transaction on-chain.
|
|
|
|
*/
|
|
|
|
wire_sync_write(MASTER_FD,
|
|
|
|
take(towire_channel_fail_fallen_behind(NULL,
|
|
|
|
remote_current_per_commitment_point)));
|
|
|
|
|
|
|
|
/* We have to send them an error to trigger dropping to chain. */
|
2018-08-23 03:08:48 +02:00
|
|
|
peer_failed(&peer->cs, &peer->channel_id, "Awaiting unilateral close");
|
2018-08-17 07:06:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* A receiving node:
|
|
|
|
* - if it supports `option_data_loss_protect`, AND the
|
|
|
|
* `option_data_loss_protect` fields are present:
|
|
|
|
*...
|
|
|
|
* - otherwise (`your_last_per_commitment_secret` or
|
|
|
|
* `my_current_per_commitment_point` do not match the expected values):
|
|
|
|
* - SHOULD fail the channel.
|
|
|
|
*/
|
|
|
|
static void check_current_dataloss_fields(struct peer *peer,
|
|
|
|
u64 next_remote_revocation_number,
|
2019-01-18 02:21:19 +01:00
|
|
|
u64 next_local_commitment_number,
|
2018-08-17 07:06:36 +02:00
|
|
|
const struct secret *last_local_per_commit_secret,
|
|
|
|
const struct pubkey *remote_current_per_commitment_point)
|
|
|
|
{
|
|
|
|
struct secret old_commit_secret;
|
|
|
|
|
|
|
|
/* By the time we're called, we've ensured this is a valid revocation
|
|
|
|
* number. */
|
|
|
|
assert(next_remote_revocation_number == peer->next_index[LOCAL] - 2
|
|
|
|
|| next_remote_revocation_number == peer->next_index[LOCAL] - 1);
|
|
|
|
|
2019-01-18 02:21:19 +01:00
|
|
|
/* By the time we're called, we've ensured we're within 1 of
|
|
|
|
* their commitment chain */
|
|
|
|
assert(next_local_commitment_number == peer->next_index[REMOTE] ||
|
|
|
|
next_local_commitment_number == peer->next_index[REMOTE] - 1);
|
|
|
|
|
2018-08-17 07:06:36 +02:00
|
|
|
if (!last_local_per_commit_secret)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
* - if `next_remote_revocation_number` equals 0:
|
|
|
|
* - MUST set `your_last_per_commitment_secret` to all zeroes
|
|
|
|
*/
|
|
|
|
|
|
|
|
status_trace("next_remote_revocation_number = %"PRIu64,
|
|
|
|
next_remote_revocation_number);
|
|
|
|
if (next_remote_revocation_number == 0)
|
|
|
|
memset(&old_commit_secret, 0, sizeof(old_commit_secret));
|
|
|
|
else {
|
|
|
|
struct pubkey unused;
|
|
|
|
/* This gets previous revocation number, since asking for
|
|
|
|
* commitment point N gives secret for N-2 */
|
|
|
|
get_per_commitment_point(next_remote_revocation_number+1,
|
|
|
|
&unused, &old_commit_secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!secret_eq_consttime(&old_commit_secret,
|
|
|
|
last_local_per_commit_secret))
|
|
|
|
peer_failed(&peer->cs,
|
|
|
|
&peer->channel_id,
|
|
|
|
"bad reestablish: your_last_per_commitment_secret %"PRIu64
|
|
|
|
": %s should be %s",
|
|
|
|
next_remote_revocation_number,
|
|
|
|
type_to_string(tmpctx, struct secret,
|
|
|
|
last_local_per_commit_secret),
|
|
|
|
type_to_string(tmpctx, struct secret,
|
|
|
|
&old_commit_secret));
|
|
|
|
|
2019-01-18 02:21:19 +01:00
|
|
|
status_trace("Reestablish, comparing commitments. Remote's next local commitment number"
|
|
|
|
" is %"PRIu64". Our next remote is %"PRIu64" with %"PRIu64
|
|
|
|
" revocations received",
|
|
|
|
next_local_commitment_number,
|
|
|
|
peer->next_index[REMOTE],
|
|
|
|
peer->revocations_received);
|
|
|
|
|
|
|
|
/* Either they haven't received our commitment yet, or we're up to date */
|
|
|
|
if (next_local_commitment_number == peer->revocations_received + 1) {
|
2018-08-17 07:06:36 +02:00
|
|
|
if (!pubkey_eq(remote_current_per_commitment_point,
|
2019-01-18 02:21:19 +01:00
|
|
|
&peer->old_remote_per_commit)) {
|
2018-08-17 07:06:36 +02:00
|
|
|
peer_failed(&peer->cs,
|
|
|
|
&peer->channel_id,
|
2019-01-18 02:21:19 +01:00
|
|
|
"bad reestablish: remote's "
|
|
|
|
"my_current_per_commitment_point %"PRIu64
|
|
|
|
"is %s; expected %s (new is %s).",
|
|
|
|
next_local_commitment_number - 1,
|
2018-08-17 07:06:36 +02:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
remote_current_per_commitment_point),
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
&peer->old_remote_per_commit),
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
&peer->remote_per_commit));
|
|
|
|
}
|
2019-01-18 02:21:19 +01:00
|
|
|
} else {
|
|
|
|
/* We've sent a commit sig but haven't gotten a revoke+ack back */
|
|
|
|
if (!pubkey_eq(remote_current_per_commitment_point,
|
|
|
|
&peer->remote_per_commit)) {
|
|
|
|
peer_failed(&peer->cs,
|
|
|
|
&peer->channel_id,
|
|
|
|
"bad reestablish: remote's "
|
|
|
|
"my_current_per_commitment_point %"PRIu64
|
|
|
|
"is %s; expected %s (old is %s).",
|
|
|
|
next_local_commitment_number - 1,
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
remote_current_per_commitment_point),
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
&peer->remote_per_commit),
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
&peer->old_remote_per_commit));
|
|
|
|
}
|
|
|
|
}
|
2018-08-17 07:06:36 +02:00
|
|
|
|
|
|
|
status_trace("option_data_loss_protect: fields are correct");
|
|
|
|
}
|
|
|
|
|
2019-04-11 07:37:24 +02:00
|
|
|
/* Older LND sometimes sends funding_locked before reestablish! */
|
|
|
|
/* ... or announcement_signatures. Sigh, let's handle whatever they send. */
|
|
|
|
static bool capture_premature_msg(const u8 ***shit_lnd_says, const u8 *msg)
|
|
|
|
{
|
|
|
|
if (fromwire_peektype(msg) == WIRE_CHANNEL_REESTABLISH)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Don't allow infinite memory consumption. */
|
|
|
|
if (tal_count(*shit_lnd_says) > 10)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
status_trace("Stashing early %s msg!",
|
|
|
|
wire_type_name(fromwire_peektype(msg)));
|
|
|
|
|
|
|
|
tal_arr_expand(shit_lnd_says, tal_steal(*shit_lnd_says, msg));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-17 06:16:34 +02:00
|
|
|
static void peer_reconnect(struct peer *peer,
|
|
|
|
const struct secret *last_remote_per_commit_secret)
|
2017-06-20 08:11:03 +02:00
|
|
|
{
|
|
|
|
struct channel_id channel_id;
|
2017-06-27 04:55:06 +02:00
|
|
|
/* Note: BOLT #2 uses these names, which are sender-relative! */
|
|
|
|
u64 next_local_commitment_number, next_remote_revocation_number;
|
2018-08-17 07:06:36 +02:00
|
|
|
bool retransmit_revoke_and_ack, retransmit_commitment_signed;
|
2017-06-20 08:22:03 +02:00
|
|
|
struct htlc_map_iter it;
|
|
|
|
const struct htlc *htlc;
|
2018-05-17 07:08:11 +02:00
|
|
|
u8 *msg;
|
2018-08-17 07:06:36 +02:00
|
|
|
struct pubkey my_current_per_commitment_point,
|
2018-12-10 02:03:42 +01:00
|
|
|
remote_current_per_commitment_point;
|
|
|
|
struct secret last_local_per_commitment_secret;
|
|
|
|
bool dataloss_protect;
|
2019-04-11 07:37:24 +02:00
|
|
|
const u8 **premature_msgs = tal_arr(peer, const u8 *, 0);
|
2018-12-10 02:03:42 +01:00
|
|
|
|
|
|
|
dataloss_protect = local_feature_negotiated(peer->localfeatures,
|
|
|
|
LOCAL_DATA_LOSS_PROTECT);
|
2018-08-17 06:16:34 +02:00
|
|
|
|
2019-01-18 02:21:19 +01:00
|
|
|
/* Our current per-commitment point is the commitment point in the last
|
|
|
|
* received signed commitment */
|
|
|
|
get_per_commitment_point(peer->next_index[LOCAL] - 1,
|
2018-08-17 06:16:34 +02:00
|
|
|
&my_current_per_commitment_point, NULL);
|
2017-06-27 04:55:06 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - upon reconnection:
|
|
|
|
* - if a channel is in an error state:
|
|
|
|
* - SHOULD retransmit the error packet and ignore any other packets for
|
|
|
|
* that channel.
|
|
|
|
* - otherwise:
|
|
|
|
* - MUST transmit `channel_reestablish` for each channel.
|
|
|
|
* - MUST wait to receive the other node's `channel_reestablish`
|
|
|
|
* message before sending any other messages for that channel.
|
2017-06-27 04:55:06 +02:00
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The sending node:
|
|
|
|
* - MUST set `next_local_commitment_number` to the commitment number
|
|
|
|
* of the next `commitment_signed` it expects to receive.
|
|
|
|
* - MUST set `next_remote_revocation_number` to the commitment number
|
|
|
|
* of the next `revoke_and_ack` message it expects to receive.
|
2018-08-17 06:16:34 +02:00
|
|
|
* - if it supports `option_data_loss_protect`:
|
|
|
|
* - if `next_remote_revocation_number` equals 0:
|
|
|
|
* - MUST set `your_last_per_commitment_secret` to all zeroes
|
|
|
|
* - otherwise:
|
|
|
|
* - MUST set `your_last_per_commitment_secret` to the last
|
|
|
|
* `per_commitment_secret` it received
|
2017-06-27 04:55:06 +02:00
|
|
|
*/
|
2018-12-10 02:03:42 +01:00
|
|
|
if (dataloss_protect) {
|
|
|
|
msg = towire_channel_reestablish_option_data_loss_protect
|
|
|
|
(NULL, &peer->channel_id,
|
|
|
|
peer->next_index[LOCAL],
|
|
|
|
peer->revocations_received,
|
|
|
|
last_remote_per_commit_secret,
|
|
|
|
&my_current_per_commitment_point);
|
|
|
|
} else {
|
|
|
|
msg = towire_channel_reestablish
|
|
|
|
(NULL, &peer->channel_id,
|
|
|
|
peer->next_index[LOCAL],
|
|
|
|
peer->revocations_received);
|
|
|
|
}
|
|
|
|
|
2018-08-02 08:49:55 +02:00
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
2017-06-27 04:55:06 +02:00
|
|
|
|
2018-02-23 06:53:47 +01:00
|
|
|
peer_billboard(false, "Sent reestablish, waiting for theirs");
|
|
|
|
|
2018-07-24 08:18:59 +02:00
|
|
|
/* Read until they say something interesting (don't forward
|
2018-08-02 08:49:55 +02:00
|
|
|
* gossip *to* them yet: we might try sending channel_update
|
2018-07-24 08:18:59 +02:00
|
|
|
* before we've reestablished channel). */
|
2018-08-02 08:49:55 +02:00
|
|
|
do {
|
2018-03-15 05:30:37 +01:00
|
|
|
clean_tmpctx();
|
2018-11-22 03:17:29 +01:00
|
|
|
msg = sync_crypto_read(tmpctx, &peer->cs, PEER_FD);
|
2019-05-04 07:53:13 +02:00
|
|
|
} while (handle_peer_gossip_or_error(PEER_FD, GOSSIP_FD, GOSSIP_STORE_FD,
|
|
|
|
&peer->cs, &peer->channel_id, msg)
|
2019-04-11 07:37:24 +02:00
|
|
|
|| capture_premature_msg(&premature_msgs, msg));
|
2018-01-14 05:25:50 +01:00
|
|
|
|
2018-12-10 02:03:42 +01:00
|
|
|
if (dataloss_protect) {
|
|
|
|
if (!fromwire_channel_reestablish_option_data_loss_protect(msg,
|
2018-08-17 07:06:36 +02:00
|
|
|
&channel_id,
|
|
|
|
&next_local_commitment_number,
|
|
|
|
&next_remote_revocation_number,
|
2018-12-10 02:03:42 +01:00
|
|
|
&last_local_per_commitment_secret,
|
|
|
|
&remote_current_per_commitment_point)) {
|
|
|
|
peer_failed(&peer->cs,
|
|
|
|
&peer->channel_id,
|
|
|
|
"bad reestablish dataloss msg: %s %s",
|
|
|
|
wire_type_name(fromwire_peektype(msg)),
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-17 07:06:36 +02:00
|
|
|
if (!fromwire_channel_reestablish(msg, &channel_id,
|
2017-06-27 04:55:06 +02:00
|
|
|
&next_local_commitment_number,
|
|
|
|
&next_remote_revocation_number)) {
|
2018-08-17 07:06:36 +02:00
|
|
|
peer_failed(&peer->cs,
|
|
|
|
&peer->channel_id,
|
|
|
|
"bad reestablish msg: %s %s",
|
|
|
|
wire_type_name(fromwire_peektype(msg)),
|
|
|
|
tal_hex(msg, msg));
|
|
|
|
}
|
2017-06-20 08:11:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 04:55:06 +02:00
|
|
|
status_trace("Got reestablish commit=%"PRIu64" revoke=%"PRIu64,
|
|
|
|
next_local_commitment_number,
|
|
|
|
next_remote_revocation_number);
|
|
|
|
|
2017-06-20 08:11:03 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - if `next_local_commitment_number` is 1 in both the
|
|
|
|
* `channel_reestablish` it sent and received:
|
|
|
|
* - MUST retransmit `funding_locked`.
|
|
|
|
* - otherwise:
|
|
|
|
* - MUST NOT retransmit `funding_locked`.
|
2017-06-20 08:11:03 +02:00
|
|
|
*/
|
2017-06-27 04:55:06 +02:00
|
|
|
if (peer->funding_locked[LOCAL]
|
|
|
|
&& peer->next_index[LOCAL] == 1
|
|
|
|
&& next_local_commitment_number == 1) {
|
2017-06-20 08:11:03 +02:00
|
|
|
u8 *msg;
|
|
|
|
|
|
|
|
/* Contains per commit point #1, for first post-opening commit */
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_funding_locked(NULL,
|
2017-06-20 08:11:03 +02:00
|
|
|
&peer->channel_id,
|
2018-07-23 04:23:03 +02:00
|
|
|
&peer->next_local_per_commit);
|
2018-08-02 08:49:55 +02:00
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
2017-06-20 08:11:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 04:55:06 +02:00
|
|
|
/* Note: next_index is the index of the current commit we're working
|
2017-06-27 04:55:06 +02:00
|
|
|
* on, but BOLT #2 refers to the *last* commit index, so we -1 where
|
|
|
|
* required. */
|
2017-06-20 08:11:03 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-08-16 09:03:05 +02:00
|
|
|
* - if `next_remote_revocation_number` is equal to the commitment
|
|
|
|
* number of the last `revoke_and_ack` the receiving node sent, AND
|
|
|
|
* the receiving node hasn't already received a `closing_signed`:
|
|
|
|
* - MUST re-send the `revoke_and_ack`.
|
|
|
|
* - otherwise:
|
|
|
|
* - if `next_remote_revocation_number` is not equal to 1 greater
|
|
|
|
* than the commitment number of the last `revoke_and_ack` the
|
|
|
|
* receiving node has sent:
|
|
|
|
* - SHOULD fail the channel.
|
|
|
|
* - if it has not sent `revoke_and_ack`, AND
|
2019-04-01 05:25:45 +02:00
|
|
|
* `next_remote_revocation_number` is not equal to 0:
|
2018-08-16 09:03:05 +02:00
|
|
|
* - SHOULD fail the channel.
|
2017-06-20 08:11:03 +02:00
|
|
|
*/
|
2017-06-27 04:55:06 +02:00
|
|
|
if (next_remote_revocation_number == peer->next_index[LOCAL] - 2) {
|
2017-06-27 04:55:06 +02:00
|
|
|
/* Don't try to retransmit revocation index -1! */
|
2017-06-27 04:55:06 +02:00
|
|
|
if (peer->next_index[LOCAL] < 2) {
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2018-02-08 02:25:12 +01:00
|
|
|
&peer->channel_id,
|
|
|
|
"bad reestablish revocation_number: %"
|
|
|
|
PRIu64,
|
|
|
|
next_remote_revocation_number);
|
2017-06-20 08:11:03 +02:00
|
|
|
}
|
|
|
|
retransmit_revoke_and_ack = true;
|
2018-08-17 07:06:36 +02:00
|
|
|
} else if (next_remote_revocation_number < peer->next_index[LOCAL] - 1) {
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2018-02-08 02:25:12 +01:00
|
|
|
&peer->channel_id,
|
|
|
|
"bad reestablish revocation_number: %"PRIu64
|
|
|
|
" vs %"PRIu64,
|
|
|
|
next_remote_revocation_number,
|
|
|
|
peer->next_index[LOCAL]);
|
2018-08-17 07:06:36 +02:00
|
|
|
} else if (next_remote_revocation_number > peer->next_index[LOCAL] - 1) {
|
2018-12-10 02:03:42 +01:00
|
|
|
if (!dataloss_protect)
|
|
|
|
/* They don't support option_data_loss_protect, we
|
|
|
|
* fail it due to unexpected number */
|
|
|
|
peer_failed(&peer->cs,
|
|
|
|
&peer->channel_id,
|
|
|
|
"bad reestablish revocation_number: %"PRIu64
|
|
|
|
" vs %"PRIu64,
|
|
|
|
next_remote_revocation_number,
|
|
|
|
peer->next_index[LOCAL] - 1);
|
|
|
|
|
2018-08-17 07:06:36 +02:00
|
|
|
/* Remote claims it's ahead of us: can it prove it?
|
|
|
|
* Does not return. */
|
|
|
|
check_future_dataloss_fields(peer,
|
|
|
|
next_remote_revocation_number,
|
2018-12-10 02:03:42 +01:00
|
|
|
&last_local_per_commitment_secret,
|
|
|
|
&remote_current_per_commitment_point);
|
2018-08-17 07:06:36 +02:00
|
|
|
} else
|
|
|
|
retransmit_revoke_and_ack = false;
|
2017-06-20 08:11:03 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - if `next_local_commitment_number` is equal to the commitment
|
|
|
|
* number of the last `commitment_signed` message the receiving node
|
|
|
|
* has sent:
|
|
|
|
* - MUST reuse the same commitment number for its next
|
|
|
|
* `commitment_signed`.
|
2017-06-20 08:11:03 +02:00
|
|
|
*/
|
2017-06-27 04:55:06 +02:00
|
|
|
if (next_local_commitment_number == peer->next_index[REMOTE] - 1) {
|
2017-06-20 08:11:03 +02:00
|
|
|
/* We completed opening, we don't re-transmit that one! */
|
2017-06-27 04:55:06 +02:00
|
|
|
if (next_local_commitment_number == 0)
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2018-02-08 02:25:12 +01:00
|
|
|
&peer->channel_id,
|
|
|
|
"bad reestablish commitment_number: %"
|
|
|
|
PRIu64,
|
|
|
|
next_local_commitment_number);
|
2017-06-20 08:11:03 +02:00
|
|
|
|
2018-08-17 07:06:36 +02:00
|
|
|
retransmit_commitment_signed = true;
|
2017-06-27 04:55:06 +02:00
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - otherwise:
|
|
|
|
* - if `next_local_commitment_number` is not 1 greater than the
|
|
|
|
* commitment number of the last `commitment_signed` message the
|
|
|
|
* receiving node has sent:
|
|
|
|
* - SHOULD fail the channel.
|
2017-06-27 04:55:06 +02:00
|
|
|
*/
|
2017-06-27 04:55:06 +02:00
|
|
|
} else if (next_local_commitment_number != peer->next_index[REMOTE])
|
2018-02-19 02:06:14 +01:00
|
|
|
peer_failed(&peer->cs,
|
2017-06-20 08:11:03 +02:00
|
|
|
&peer->channel_id,
|
2017-06-27 04:55:06 +02:00
|
|
|
"bad reestablish commitment_number: %"PRIu64
|
2017-06-20 08:11:03 +02:00
|
|
|
" vs %"PRIu64,
|
2017-06-27 04:55:06 +02:00
|
|
|
next_local_commitment_number,
|
2017-06-27 04:55:06 +02:00
|
|
|
peer->next_index[REMOTE]);
|
2018-08-17 07:06:36 +02:00
|
|
|
else
|
|
|
|
retransmit_commitment_signed = false;
|
|
|
|
|
2018-08-17 07:06:36 +02:00
|
|
|
/* After we checked basic sanity, we check dataloss fields if any */
|
2018-12-10 02:03:42 +01:00
|
|
|
if (dataloss_protect)
|
|
|
|
check_current_dataloss_fields(peer,
|
|
|
|
next_remote_revocation_number,
|
2019-01-18 02:21:19 +01:00
|
|
|
next_local_commitment_number,
|
2018-12-10 02:03:42 +01:00
|
|
|
&last_local_per_commitment_secret,
|
|
|
|
&remote_current_per_commitment_point);
|
2018-08-17 07:06:36 +02:00
|
|
|
|
2018-08-17 07:06:36 +02:00
|
|
|
/* We have to re-send in the same order we sent originally:
|
|
|
|
* revoke_and_ack (usually) alters our next commitment. */
|
|
|
|
if (retransmit_revoke_and_ack && !peer->last_was_revoke)
|
|
|
|
resend_revoke(peer);
|
|
|
|
|
|
|
|
if (retransmit_commitment_signed)
|
|
|
|
resend_commitment(peer, peer->last_sent_commit);
|
2017-06-20 08:11:03 +02:00
|
|
|
|
|
|
|
/* This covers the case where we sent revoke after commit. */
|
|
|
|
if (retransmit_revoke_and_ack && peer->last_was_revoke)
|
|
|
|
resend_revoke(peer);
|
|
|
|
|
2017-06-26 03:16:43 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - upon reconnection:
|
|
|
|
* - if it has sent a previous `shutdown`:
|
|
|
|
* - MUST retransmit `shutdown`.
|
2017-06-26 03:16:43 +02:00
|
|
|
*/
|
2018-06-17 12:13:44 +02:00
|
|
|
/* (If we had sent `closing_signed`, we'd be in closingd). */
|
2017-06-26 03:16:43 +02:00
|
|
|
maybe_send_shutdown(peer);
|
|
|
|
|
2017-07-04 02:47:32 +02:00
|
|
|
/* Corner case: we didn't send shutdown before because update_add_htlc
|
|
|
|
* pending, but now they're cleared by restart, and we're actually
|
|
|
|
* complete. In that case, their `shutdown` will trigger us. */
|
|
|
|
|
2017-06-20 08:11:03 +02:00
|
|
|
/* Start commit timer: if we sent revoke we might need it. */
|
|
|
|
start_commit_timer(peer);
|
|
|
|
|
2017-06-20 08:22:03 +02:00
|
|
|
/* Now, re-send any that we're supposed to be failing. */
|
2017-08-28 18:02:01 +02:00
|
|
|
for (htlc = htlc_map_first(peer->channel->htlcs, &it);
|
2017-06-20 08:22:03 +02:00
|
|
|
htlc;
|
2017-08-28 18:02:01 +02:00
|
|
|
htlc = htlc_map_next(peer->channel->htlcs, &it)) {
|
2017-06-20 08:22:03 +02:00
|
|
|
if (htlc->state == SENT_REMOVE_HTLC)
|
|
|
|
send_fail_or_fulfill(peer, htlc);
|
|
|
|
}
|
2017-11-17 14:18:27 +01:00
|
|
|
|
2017-11-21 06:26:59 +01:00
|
|
|
/* Corner case: we will get upset with them if they send
|
|
|
|
* commitment_signed with no changes. But it could be that we sent a
|
|
|
|
* feechange, they acked, and now they want to commit it; we can't
|
|
|
|
* even tell by seeing if fees are different (short of saving full fee
|
|
|
|
* state in database) since it could be a tiny feechange, or two
|
|
|
|
* feechanges which cancelled out. */
|
|
|
|
if (peer->channel->funder == LOCAL)
|
|
|
|
peer->channel->changes_pending[LOCAL] = true;
|
2018-02-23 06:53:47 +01:00
|
|
|
|
|
|
|
peer_billboard(true, "Reconnected, and reestablished.");
|
2019-02-20 12:16:59 +01:00
|
|
|
|
2019-04-11 07:37:24 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
* - upon reconnection:
|
|
|
|
*...
|
|
|
|
* - MUST transmit `channel_reestablish` for each channel.
|
|
|
|
* - MUST wait to receive the other node's `channel_reestablish`
|
|
|
|
* message before sending any other messages for that channel.
|
|
|
|
*/
|
|
|
|
/* LND doesn't wait. */
|
|
|
|
for (size_t i = 0; i < tal_count(premature_msgs); i++)
|
|
|
|
peer_in(peer, premature_msgs[i]);
|
|
|
|
tal_free(premature_msgs);
|
2017-06-20 08:11:03 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 17:57:19 +01:00
|
|
|
/* ignores the funding_depth unless depth >= minimum_depth
|
|
|
|
* (except to update billboard, and set peer->depth_togo). */
|
|
|
|
static void handle_funding_depth(struct peer *peer, const u8 *msg)
|
2017-03-29 13:01:15 +02:00
|
|
|
{
|
2019-02-26 17:57:19 +01:00
|
|
|
u32 depth;
|
|
|
|
struct short_channel_id *scid;
|
2017-06-20 08:10:03 +02:00
|
|
|
|
2019-02-26 17:57:19 +01:00
|
|
|
if (!fromwire_channel_funding_depth(tmpctx,
|
|
|
|
msg,
|
|
|
|
&scid,
|
|
|
|
&depth))
|
|
|
|
master_badmsg(WIRE_CHANNEL_FUNDING_DEPTH, msg);
|
2017-03-29 13:01:15 +02:00
|
|
|
|
2018-05-07 09:04:38 +02:00
|
|
|
/* Too late, we're shutting down! */
|
|
|
|
if (peer->shutdown_sent[LOCAL])
|
|
|
|
return;
|
|
|
|
|
2019-02-26 17:57:19 +01:00
|
|
|
if (depth < peer->channel->minimum_depth) {
|
|
|
|
peer->depth_togo = peer->channel->minimum_depth - depth;
|
2018-05-17 07:08:11 +02:00
|
|
|
|
2019-02-26 17:57:19 +01:00
|
|
|
} else {
|
|
|
|
peer->depth_togo = 0;
|
2017-12-15 15:47:58 +01:00
|
|
|
|
2019-02-26 17:57:19 +01:00
|
|
|
assert(scid);
|
|
|
|
peer->short_channel_ids[LOCAL] = *scid;
|
|
|
|
|
|
|
|
if (!peer->funding_locked[LOCAL]) {
|
|
|
|
|
|
|
|
status_trace("funding_locked: sending commit index %"PRIu64": %s",
|
|
|
|
peer->next_index[LOCAL],
|
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
&peer->next_local_per_commit));
|
|
|
|
|
|
|
|
msg = towire_funding_locked(NULL,
|
|
|
|
&peer->channel_id,
|
|
|
|
&peer->next_local_per_commit);
|
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
|
|
|
|
|
|
|
peer->funding_locked[LOCAL] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer->announce_depth_reached = (depth >= ANNOUNCE_MIN_DEPTH);
|
|
|
|
|
|
|
|
/* Send temporary or final announcements */
|
|
|
|
channel_announcement_negotiate(peer);
|
|
|
|
}
|
2018-05-17 07:08:11 +02:00
|
|
|
|
|
|
|
billboard_update(peer);
|
2017-03-29 13:01:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
|
|
|
|
{
|
|
|
|
u8 *msg;
|
2017-06-06 01:49:10 +02:00
|
|
|
u32 cltv_expiry;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat amount;
|
2017-03-29 13:01:15 +02:00
|
|
|
struct sha256 payment_hash;
|
2017-04-01 12:58:30 +02:00
|
|
|
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
|
2017-04-27 08:08:50 +02:00
|
|
|
enum channel_add_err e;
|
2017-03-29 13:01:15 +02:00
|
|
|
enum onion_type failcode;
|
2018-07-28 08:00:16 +02:00
|
|
|
/* Subtle: must be tal object since we marshal using tal_bytelen() */
|
2017-03-29 13:01:15 +02:00
|
|
|
const char *failmsg;
|
|
|
|
|
|
|
|
if (!peer->funding_locked[LOCAL] || !peer->funding_locked[REMOTE])
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO,
|
|
|
|
"funding not locked for offer_htlc");
|
2017-03-29 13:01:15 +02:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!fromwire_channel_offer_htlc(inmsg, &amount,
|
2017-03-29 13:01:15 +02:00
|
|
|
&cltv_expiry, &payment_hash,
|
|
|
|
onion_routing_packet))
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(WIRE_CHANNEL_OFFER_HTLC, inmsg);
|
2017-03-29 13:01:15 +02:00
|
|
|
|
2017-04-27 08:08:50 +02:00
|
|
|
e = channel_add_htlc(peer->channel, LOCAL, peer->htlc_id,
|
2019-02-21 04:45:55 +01:00
|
|
|
amount, cltv_expiry, &payment_hash,
|
2017-11-28 06:03:09 +01:00
|
|
|
onion_routing_packet, NULL);
|
2019-02-21 04:45:55 +01:00
|
|
|
status_trace("Adding HTLC %"PRIu64" amount=%s cltv=%u gave %s",
|
|
|
|
peer->htlc_id,
|
|
|
|
type_to_string(tmpctx, struct amount_msat, &amount),
|
|
|
|
cltv_expiry,
|
2018-02-19 02:06:14 +01:00
|
|
|
channel_add_err_name(e));
|
2017-04-27 08:08:50 +02:00
|
|
|
|
|
|
|
switch (e) {
|
2017-03-29 13:01:15 +02:00
|
|
|
case CHANNEL_ERR_ADD_OK:
|
|
|
|
/* Tell the peer. */
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_update_add_htlc(NULL, &peer->channel_id,
|
2019-02-21 04:45:55 +01:00
|
|
|
peer->htlc_id, amount,
|
2017-06-06 01:49:10 +02:00
|
|
|
&payment_hash, cltv_expiry,
|
2017-03-29 13:01:15 +02:00
|
|
|
onion_routing_packet);
|
2018-08-02 08:49:55 +02:00
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
2017-03-29 13:01:15 +02:00
|
|
|
start_commit_timer(peer);
|
|
|
|
/* Tell the master. */
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_channel_offer_htlc_reply(NULL, peer->htlc_id,
|
2017-03-29 13:01:15 +02:00
|
|
|
0, NULL);
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
wire_sync_write(MASTER_FD, take(msg));
|
2017-03-29 13:01:15 +02:00
|
|
|
peer->htlc_id++;
|
|
|
|
return;
|
|
|
|
case CHANNEL_ERR_INVALID_EXPIRY:
|
|
|
|
failcode = WIRE_INCORRECT_CLTV_EXPIRY;
|
|
|
|
failmsg = tal_fmt(inmsg, "Invalid cltv_expiry %u", cltv_expiry);
|
|
|
|
goto failed;
|
|
|
|
case CHANNEL_ERR_DUPLICATE:
|
|
|
|
case CHANNEL_ERR_DUPLICATE_ID_DIFFERENT:
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO,
|
2017-03-29 13:01:15 +02:00
|
|
|
"Duplicate HTLC %"PRIu64, peer->htlc_id);
|
|
|
|
|
|
|
|
/* FIXME: Fuzz the boundaries a bit to avoid probing? */
|
|
|
|
case CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED:
|
|
|
|
failcode = WIRE_TEMPORARY_CHANNEL_FAILURE;
|
|
|
|
failmsg = tal_fmt(inmsg, "Maximum value exceeded");
|
|
|
|
goto failed;
|
|
|
|
case CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED:
|
|
|
|
failcode = WIRE_TEMPORARY_CHANNEL_FAILURE;
|
|
|
|
failmsg = tal_fmt(inmsg, "Capacity exceeded");
|
|
|
|
goto failed;
|
|
|
|
case CHANNEL_ERR_HTLC_BELOW_MINIMUM:
|
|
|
|
failcode = WIRE_AMOUNT_BELOW_MINIMUM;
|
2019-02-21 04:45:54 +01:00
|
|
|
failmsg = tal_fmt(inmsg, "HTLC too small (%s minimum)",
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct amount_msat,
|
|
|
|
&peer->channel->config[REMOTE].htlc_minimum));
|
2017-03-29 13:01:15 +02:00
|
|
|
goto failed;
|
|
|
|
case CHANNEL_ERR_TOO_MANY_HTLCS:
|
|
|
|
failcode = WIRE_TEMPORARY_CHANNEL_FAILURE;
|
|
|
|
failmsg = tal_fmt(inmsg, "Too many HTLCs");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
/* Shouldn't return anything else! */
|
|
|
|
abort();
|
|
|
|
|
|
|
|
failed:
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_channel_offer_htlc_reply(NULL, 0, failcode, (u8*)failmsg);
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
wire_sync_write(MASTER_FD, take(msg));
|
2017-03-29 13:01:15 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 06:26:59 +01:00
|
|
|
static void handle_feerates(struct peer *peer, const u8 *inmsg)
|
|
|
|
{
|
2017-12-01 03:06:37 +01:00
|
|
|
u32 feerate;
|
2017-11-21 06:26:59 +01:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_channel_feerates(inmsg, &feerate,
|
2017-12-01 03:06:37 +01:00
|
|
|
&peer->feerate_min,
|
|
|
|
&peer->feerate_max))
|
2017-11-21 06:26:59 +01:00
|
|
|
master_badmsg(WIRE_CHANNEL_FEERATES, inmsg);
|
|
|
|
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The node _responsible_ for paying the Bitcoin fee:
|
|
|
|
* - SHOULD send `update_fee` to ensure the current fee rate is
|
|
|
|
* sufficient (by a significant margin) for timely processing of the
|
|
|
|
* commitment transaction.
|
|
|
|
*/
|
2017-11-21 06:26:59 +01:00
|
|
|
if (peer->channel->funder == LOCAL) {
|
|
|
|
peer->desired_feerate = feerate;
|
|
|
|
start_commit_timer(peer);
|
|
|
|
} else {
|
|
|
|
/* BOLT #2:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* The node _not responsible_ for paying the Bitcoin fee:
|
|
|
|
* - MUST NOT send `update_fee`.
|
2017-11-21 06:26:59 +01:00
|
|
|
*/
|
|
|
|
/* FIXME: We could drop to chain if fees are too low, but
|
|
|
|
* that's fraught too. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 21:29:39 +01:00
|
|
|
static void handle_specific_feerates(struct peer *peer, const u8 *inmsg)
|
|
|
|
{
|
|
|
|
u32 base_old = peer->fee_base;
|
|
|
|
u32 per_satoshi_old = peer->fee_per_satoshi;
|
|
|
|
|
|
|
|
if (!fromwire_channel_specific_feerates(inmsg,
|
|
|
|
&peer->fee_base,
|
|
|
|
&peer->fee_per_satoshi))
|
|
|
|
master_badmsg(WIRE_CHANNEL_SPECIFIC_FEERATES, inmsg);
|
|
|
|
|
|
|
|
/* only send channel updates if values actually changed */
|
|
|
|
if (peer->fee_base != base_old || peer->fee_per_satoshi != per_satoshi_old)
|
|
|
|
send_channel_update(peer, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-29 13:01:15 +02:00
|
|
|
static void handle_preimage(struct peer *peer, const u8 *inmsg)
|
|
|
|
{
|
2018-07-26 23:26:37 +02:00
|
|
|
struct fulfilled_htlc fulfilled_htlc;
|
2018-07-02 06:30:12 +02:00
|
|
|
struct htlc *h;
|
2017-03-29 13:01:15 +02:00
|
|
|
|
2018-07-26 23:26:37 +02:00
|
|
|
if (!fromwire_channel_fulfill_htlc(inmsg, &fulfilled_htlc))
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(WIRE_CHANNEL_FULFILL_HTLC, inmsg);
|
2017-03-29 13:01:15 +02:00
|
|
|
|
2018-07-26 23:26:37 +02:00
|
|
|
switch (channel_fulfill_htlc(peer->channel, REMOTE,
|
|
|
|
fulfilled_htlc.id,
|
|
|
|
&fulfilled_htlc.payment_preimage,
|
|
|
|
&h)) {
|
2017-03-29 13:01:15 +02:00
|
|
|
case CHANNEL_ERR_REMOVE_OK:
|
2018-07-02 06:30:12 +02:00
|
|
|
send_fail_or_fulfill(peer, h);
|
2017-03-29 13:01:15 +02:00
|
|
|
start_commit_timer(peer);
|
|
|
|
return;
|
|
|
|
/* These shouldn't happen, because any offered HTLC (which would give
|
|
|
|
* us the preimage) should have timed out long before. If we
|
|
|
|
* were to get preimages from other sources, this could happen. */
|
|
|
|
case CHANNEL_ERR_NO_SUCH_ID:
|
|
|
|
case CHANNEL_ERR_ALREADY_FULFILLED:
|
|
|
|
case CHANNEL_ERR_HTLC_UNCOMMITTED:
|
|
|
|
case CHANNEL_ERR_HTLC_NOT_IRREVOCABLE:
|
|
|
|
case CHANNEL_ERR_BAD_PREIMAGE:
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO,
|
2018-07-26 23:26:37 +02:00
|
|
|
"HTLC %"PRIu64" preimage failed",
|
|
|
|
fulfilled_htlc.id);
|
2017-03-29 13:01:15 +02:00
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_fail(struct peer *peer, const u8 *inmsg)
|
|
|
|
{
|
2018-07-26 23:26:37 +02:00
|
|
|
struct failed_htlc *failed_htlc;
|
2017-04-01 13:01:13 +02:00
|
|
|
enum channel_remove_err e;
|
2017-11-28 06:03:09 +01:00
|
|
|
struct htlc *h;
|
2017-03-29 13:01:15 +02:00
|
|
|
|
2018-07-26 23:26:37 +02:00
|
|
|
if (!fromwire_channel_fail_htlc(inmsg, inmsg, &failed_htlc))
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(WIRE_CHANNEL_FAIL_HTLC, inmsg);
|
2017-03-29 13:01:15 +02:00
|
|
|
|
2018-07-26 23:26:37 +02:00
|
|
|
e = channel_fail_htlc(peer->channel, REMOTE, failed_htlc->id, &h);
|
2017-04-01 13:01:13 +02:00
|
|
|
switch (e) {
|
2017-03-29 13:01:15 +02:00
|
|
|
case CHANNEL_ERR_REMOVE_OK:
|
2018-07-26 23:26:37 +02:00
|
|
|
h->failcode = failed_htlc->failcode;
|
|
|
|
h->fail = tal_steal(h, failed_htlc->failreason);
|
|
|
|
h->failed_scid = tal_steal(h, failed_htlc->scid);
|
2018-07-02 06:30:12 +02:00
|
|
|
send_fail_or_fulfill(peer, h);
|
2017-03-29 13:01:15 +02:00
|
|
|
start_commit_timer(peer);
|
|
|
|
return;
|
|
|
|
case CHANNEL_ERR_NO_SUCH_ID:
|
|
|
|
case CHANNEL_ERR_ALREADY_FULFILLED:
|
|
|
|
case CHANNEL_ERR_HTLC_UNCOMMITTED:
|
|
|
|
case CHANNEL_ERR_HTLC_NOT_IRREVOCABLE:
|
|
|
|
case CHANNEL_ERR_BAD_PREIMAGE:
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO,
|
2018-07-26 23:26:37 +02:00
|
|
|
"HTLC %"PRIu64" removal failed: %s",
|
|
|
|
failed_htlc->id,
|
2018-02-19 02:06:14 +01:00
|
|
|
channel_remove_err_name(e));
|
2017-03-29 13:01:15 +02:00
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2017-06-26 03:16:43 +02:00
|
|
|
static void handle_shutdown_cmd(struct peer *peer, const u8 *inmsg)
|
|
|
|
{
|
2018-03-07 01:06:07 +01:00
|
|
|
if (!fromwire_channel_send_shutdown(inmsg))
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(WIRE_CHANNEL_SEND_SHUTDOWN, inmsg);
|
2017-06-26 03:16:43 +02:00
|
|
|
|
2018-03-07 01:06:07 +01:00
|
|
|
/* We can't send this until commit (if any) is done, so start timer. */
|
|
|
|
peer->send_shutdown = true;
|
2017-06-26 03:16:43 +02:00
|
|
|
start_commit_timer(peer);
|
|
|
|
}
|
|
|
|
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2017-09-26 06:57:31 +02:00
|
|
|
static void handle_dev_reenable_commit(struct peer *peer)
|
|
|
|
{
|
|
|
|
dev_suppress_commit = false;
|
|
|
|
start_commit_timer(peer);
|
|
|
|
status_trace("dev_reenable_commit");
|
|
|
|
wire_sync_write(MASTER_FD,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_channel_dev_reenable_commit_reply(NULL)));
|
2017-09-26 06:57:31 +02:00
|
|
|
}
|
2018-11-22 03:17:29 +01:00
|
|
|
|
|
|
|
static void handle_dev_memleak(struct peer *peer, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct htable *memtable;
|
|
|
|
bool found_leak;
|
|
|
|
|
|
|
|
memtable = memleak_enter_allocations(tmpctx, msg, msg);
|
|
|
|
|
|
|
|
/* Now delete peer and things it has pointers to. */
|
|
|
|
memleak_remove_referenced(memtable, peer);
|
|
|
|
memleak_remove_htable(memtable, &peer->channel->htlcs->raw);
|
|
|
|
|
|
|
|
found_leak = dump_memleak(memtable);
|
|
|
|
wire_sync_write(MASTER_FD,
|
|
|
|
take(towire_channel_dev_memleak_reply(NULL,
|
|
|
|
found_leak)));
|
|
|
|
}
|
|
|
|
#endif /* DEVELOPER */
|
2017-09-26 06:57:31 +02:00
|
|
|
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
static void req_in(struct peer *peer, const u8 *msg)
|
2017-03-19 21:32:44 +01:00
|
|
|
{
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
enum channel_wire_type t = fromwire_peektype(msg);
|
2017-06-20 07:49:03 +02:00
|
|
|
|
2017-06-20 08:00:03 +02:00
|
|
|
switch (t) {
|
2019-02-26 17:57:19 +01:00
|
|
|
case WIRE_CHANNEL_FUNDING_DEPTH:
|
|
|
|
handle_funding_depth(peer, msg);
|
2017-11-28 23:50:14 +01:00
|
|
|
return;
|
2017-06-20 08:00:03 +02:00
|
|
|
case WIRE_CHANNEL_OFFER_HTLC:
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
handle_offer_htlc(peer, msg);
|
2017-11-28 23:50:14 +01:00
|
|
|
return;
|
2017-11-21 06:26:59 +01:00
|
|
|
case WIRE_CHANNEL_FEERATES:
|
|
|
|
handle_feerates(peer, msg);
|
2017-11-28 23:50:14 +01:00
|
|
|
return;
|
2017-06-20 08:00:03 +02:00
|
|
|
case WIRE_CHANNEL_FULFILL_HTLC:
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
handle_preimage(peer, msg);
|
2017-11-28 23:50:14 +01:00
|
|
|
return;
|
2017-06-20 08:00:03 +02:00
|
|
|
case WIRE_CHANNEL_FAIL_HTLC:
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
handle_fail(peer, msg);
|
2017-11-28 23:50:14 +01:00
|
|
|
return;
|
2019-03-09 21:29:39 +01:00
|
|
|
case WIRE_CHANNEL_SPECIFIC_FEERATES:
|
|
|
|
handle_specific_feerates(peer, msg);
|
|
|
|
return;
|
2017-06-26 03:16:43 +02:00
|
|
|
case WIRE_CHANNEL_SEND_SHUTDOWN:
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
handle_shutdown_cmd(peer, msg);
|
2017-11-28 23:50:14 +01:00
|
|
|
return;
|
2017-10-24 04:06:14 +02:00
|
|
|
#if DEVELOPER
|
2018-11-22 03:17:29 +01:00
|
|
|
case WIRE_CHANNEL_DEV_REENABLE_COMMIT:
|
2017-09-26 06:57:31 +02:00
|
|
|
handle_dev_reenable_commit(peer);
|
2017-11-28 23:50:14 +01:00
|
|
|
return;
|
2018-11-22 03:17:29 +01:00
|
|
|
case WIRE_CHANNEL_DEV_MEMLEAK:
|
|
|
|
handle_dev_memleak(peer, msg);
|
|
|
|
return;
|
|
|
|
#else
|
|
|
|
case WIRE_CHANNEL_DEV_REENABLE_COMMIT:
|
|
|
|
case WIRE_CHANNEL_DEV_MEMLEAK:
|
2017-10-24 04:06:14 +02:00
|
|
|
#endif /* DEVELOPER */
|
2017-06-20 08:00:03 +02:00
|
|
|
case WIRE_CHANNEL_INIT:
|
|
|
|
case WIRE_CHANNEL_OFFER_HTLC_REPLY:
|
|
|
|
case WIRE_CHANNEL_SENDING_COMMITSIG:
|
|
|
|
case WIRE_CHANNEL_GOT_COMMITSIG:
|
|
|
|
case WIRE_CHANNEL_GOT_REVOKE:
|
|
|
|
case WIRE_CHANNEL_SENDING_COMMITSIG_REPLY:
|
|
|
|
case WIRE_CHANNEL_GOT_COMMITSIG_REPLY:
|
|
|
|
case WIRE_CHANNEL_GOT_REVOKE_REPLY:
|
|
|
|
case WIRE_CHANNEL_GOT_FUNDING_LOCKED:
|
2017-06-26 03:16:43 +02:00
|
|
|
case WIRE_CHANNEL_GOT_SHUTDOWN:
|
2017-07-04 02:47:32 +02:00
|
|
|
case WIRE_CHANNEL_SHUTDOWN_COMPLETE:
|
2017-09-26 06:57:31 +02:00
|
|
|
case WIRE_CHANNEL_DEV_REENABLE_COMMIT_REPLY:
|
2018-08-17 07:06:35 +02:00
|
|
|
case WIRE_CHANNEL_FAIL_FALLEN_BEHIND:
|
2018-11-22 03:17:29 +01:00
|
|
|
case WIRE_CHANNEL_DEV_MEMLEAK_REPLY:
|
2017-06-20 08:00:03 +02:00
|
|
|
break;
|
2017-03-29 13:01:15 +02:00
|
|
|
}
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(-1, msg);
|
2017-07-03 04:01:21 +02:00
|
|
|
}
|
|
|
|
|
2018-01-17 21:29:49 +01:00
|
|
|
static void init_shared_secrets(struct channel *channel,
|
|
|
|
const struct added_htlc *htlcs,
|
|
|
|
const enum htlc_state *hstates)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(htlcs); i++) {
|
|
|
|
struct htlc *htlc;
|
|
|
|
|
|
|
|
/* We only derive this for HTLCs *they* added. */
|
|
|
|
if (htlc_state_owner(hstates[i]) != REMOTE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
htlc = channel_get_htlc(channel, REMOTE, htlcs[i].id);
|
2019-01-08 01:52:13 +01:00
|
|
|
htlc->shared_secret = get_shared_secret(htlc, htlc,
|
2019-01-08 01:53:25 +01:00
|
|
|
&htlc->why_bad_onion,
|
|
|
|
&htlc->next_onion_sha);
|
2018-01-17 21:29:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 03:40:52 +02:00
|
|
|
/* We do this synchronously. */
|
|
|
|
static void init_channel(struct peer *peer)
|
|
|
|
{
|
|
|
|
struct basepoints points[NUM_SIDES];
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat funding;
|
2017-07-03 03:40:52 +02:00
|
|
|
u16 funding_txout;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat local_msat;
|
2017-07-03 03:40:52 +02:00
|
|
|
struct pubkey funding_pubkey[NUM_SIDES];
|
2018-11-21 04:13:03 +01:00
|
|
|
struct channel_config conf[NUM_SIDES];
|
2017-12-18 07:41:52 +01:00
|
|
|
struct bitcoin_txid funding_txid;
|
2017-07-04 02:45:28 +02:00
|
|
|
enum side funder;
|
2017-07-03 03:40:52 +02:00
|
|
|
enum htlc_state *hstates;
|
|
|
|
struct fulfilled_htlc *fulfilled;
|
|
|
|
enum side *fulfilled_sides;
|
2018-02-08 02:24:46 +01:00
|
|
|
struct failed_htlc **failed;
|
2017-07-03 03:40:52 +02:00
|
|
|
enum side *failed_sides;
|
|
|
|
struct added_htlc *htlcs;
|
|
|
|
bool reconnected;
|
|
|
|
u8 *funding_signed;
|
2018-07-23 04:23:03 +02:00
|
|
|
const u8 *msg;
|
2017-11-21 04:44:35 +01:00
|
|
|
u32 feerate_per_kw[NUM_SIDES];
|
2019-02-26 17:57:19 +01:00
|
|
|
u32 minimum_depth;
|
2018-08-17 06:16:34 +02:00
|
|
|
struct secret last_remote_per_commit_secret;
|
2017-07-03 03:40:52 +02:00
|
|
|
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
assert(!(fcntl(MASTER_FD, F_GETFL) & O_NONBLOCK));
|
2017-09-08 12:23:57 +02:00
|
|
|
|
2017-09-09 03:32:59 +02:00
|
|
|
status_setup_sync(MASTER_FD);
|
|
|
|
|
2018-11-22 03:17:29 +01:00
|
|
|
msg = wire_sync_read(tmpctx, MASTER_FD);
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_channel_init(peer, msg,
|
2017-08-22 07:05:37 +02:00
|
|
|
&peer->chain_hash,
|
2017-07-03 03:40:52 +02:00
|
|
|
&funding_txid, &funding_txout,
|
2019-02-21 04:45:55 +01:00
|
|
|
&funding,
|
2019-02-26 17:57:19 +01:00
|
|
|
&minimum_depth,
|
2018-11-21 04:13:03 +01:00
|
|
|
&conf[LOCAL], &conf[REMOTE],
|
2017-11-21 04:44:35 +01:00
|
|
|
feerate_per_kw,
|
2017-11-21 06:26:59 +01:00
|
|
|
&peer->feerate_min, &peer->feerate_max,
|
2017-07-03 03:40:52 +02:00
|
|
|
&peer->their_commit_sig,
|
2017-11-28 23:50:14 +01:00
|
|
|
&peer->cs,
|
2017-07-03 03:40:52 +02:00
|
|
|
&funding_pubkey[REMOTE],
|
2018-07-09 13:17:59 +02:00
|
|
|
&points[REMOTE],
|
2017-07-03 03:40:52 +02:00
|
|
|
&peer->remote_per_commit,
|
|
|
|
&peer->old_remote_per_commit,
|
2017-07-04 02:45:28 +02:00
|
|
|
&funder,
|
2017-07-03 03:40:52 +02:00
|
|
|
&peer->fee_base,
|
|
|
|
&peer->fee_per_satoshi,
|
2019-02-21 04:45:55 +01:00
|
|
|
&local_msat,
|
2018-07-23 04:23:03 +02:00
|
|
|
&points[LOCAL],
|
|
|
|
&funding_pubkey[LOCAL],
|
2017-07-03 03:40:52 +02:00
|
|
|
&peer->node_ids[LOCAL],
|
|
|
|
&peer->node_ids[REMOTE],
|
|
|
|
&peer->commit_msec,
|
2017-10-11 06:32:15 +02:00
|
|
|
&peer->cltv_delta,
|
2017-07-03 03:40:52 +02:00
|
|
|
&peer->last_was_revoke,
|
|
|
|
&peer->last_sent_commit,
|
|
|
|
&peer->next_index[LOCAL],
|
|
|
|
&peer->next_index[REMOTE],
|
|
|
|
&peer->revocations_received,
|
|
|
|
&peer->htlc_id,
|
|
|
|
&htlcs,
|
|
|
|
&hstates,
|
|
|
|
&fulfilled,
|
|
|
|
&fulfilled_sides,
|
|
|
|
&failed,
|
|
|
|
&failed_sides,
|
|
|
|
&peer->funding_locked[LOCAL],
|
|
|
|
&peer->funding_locked[REMOTE],
|
|
|
|
&peer->short_channel_ids[LOCAL],
|
|
|
|
&reconnected,
|
2018-03-07 01:06:07 +01:00
|
|
|
&peer->send_shutdown,
|
2017-07-04 02:47:32 +02:00
|
|
|
&peer->shutdown_sent[REMOTE],
|
2018-03-07 01:06:07 +01:00
|
|
|
&peer->final_scriptpubkey,
|
2017-07-03 03:40:52 +02:00
|
|
|
&peer->channel_flags,
|
2018-04-26 06:51:02 +02:00
|
|
|
&funding_signed,
|
2018-08-17 06:16:34 +02:00
|
|
|
&peer->announce_depth_reached,
|
2018-12-10 02:03:42 +01:00
|
|
|
&last_remote_per_commit_secret,
|
2019-05-01 02:47:11 +02:00
|
|
|
&peer->localfeatures,
|
|
|
|
&peer->remote_upfront_shutdown_script)) {
|
2019-02-26 17:57:19 +01:00
|
|
|
master_badmsg(WIRE_CHANNEL_INIT, msg);
|
|
|
|
}
|
2017-07-03 03:40:52 +02:00
|
|
|
|
|
|
|
status_trace("init %s: remote_per_commit = %s, old_remote_per_commit = %s"
|
|
|
|
" next_idx_local = %"PRIu64
|
|
|
|
" next_idx_remote = %"PRIu64
|
2017-11-21 06:26:59 +01:00
|
|
|
" revocations_received = %"PRIu64
|
|
|
|
" feerates %u/%u (range %u-%u)",
|
2017-07-04 02:45:28 +02:00
|
|
|
side_to_str(funder),
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2017-07-03 03:40:52 +02:00
|
|
|
&peer->remote_per_commit),
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2017-07-03 03:40:52 +02:00
|
|
|
&peer->old_remote_per_commit),
|
|
|
|
peer->next_index[LOCAL], peer->next_index[REMOTE],
|
2017-11-21 06:26:59 +01:00
|
|
|
peer->revocations_received,
|
|
|
|
feerate_per_kw[LOCAL], feerate_per_kw[REMOTE],
|
|
|
|
peer->feerate_min, peer->feerate_max);
|
2017-07-03 03:40:52 +02:00
|
|
|
|
|
|
|
/* First commit is used for opening: if we've sent 0, we're on
|
|
|
|
* index 1. */
|
|
|
|
assert(peer->next_index[LOCAL] > 0);
|
|
|
|
assert(peer->next_index[REMOTE] > 0);
|
|
|
|
|
2018-08-17 06:15:39 +02:00
|
|
|
get_per_commitment_point(peer->next_index[LOCAL],
|
|
|
|
&peer->next_local_per_commit, NULL);
|
2018-07-23 04:23:03 +02:00
|
|
|
|
2017-07-03 03:40:52 +02:00
|
|
|
/* channel_id is set from funding txout */
|
|
|
|
derive_channel_id(&peer->channel_id, &funding_txid, funding_txout);
|
|
|
|
|
2018-09-06 18:44:47 +02:00
|
|
|
peer->channel = new_full_channel(peer,
|
|
|
|
&peer->chain_hash,
|
2019-02-26 17:57:19 +01:00
|
|
|
&funding_txid,
|
|
|
|
funding_txout,
|
|
|
|
minimum_depth,
|
2019-02-21 04:45:55 +01:00
|
|
|
funding,
|
|
|
|
local_msat,
|
2018-02-11 12:02:51 +01:00
|
|
|
feerate_per_kw,
|
2018-11-21 04:13:03 +01:00
|
|
|
&conf[LOCAL], &conf[REMOTE],
|
2018-02-11 12:02:51 +01:00
|
|
|
&points[LOCAL], &points[REMOTE],
|
|
|
|
&funding_pubkey[LOCAL],
|
|
|
|
&funding_pubkey[REMOTE],
|
|
|
|
funder);
|
2017-07-03 03:40:52 +02:00
|
|
|
|
|
|
|
if (!channel_force_htlcs(peer->channel, htlcs, hstates,
|
|
|
|
fulfilled, fulfilled_sides,
|
2018-02-08 02:24:46 +01:00
|
|
|
cast_const2(const struct failed_htlc **,
|
|
|
|
failed),
|
|
|
|
failed_sides))
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-07-03 03:40:52 +02:00
|
|
|
"Could not restore HTLCs");
|
|
|
|
|
2018-01-17 21:29:49 +01:00
|
|
|
/* We derive shared secrets for each remote HTLC, so we can
|
|
|
|
* create error packet if necessary. */
|
|
|
|
init_shared_secrets(peer->channel, htlcs, hstates);
|
|
|
|
|
2018-11-22 03:17:29 +01:00
|
|
|
/* We don't need these any more, so free them. */
|
|
|
|
tal_free(htlcs);
|
|
|
|
tal_free(hstates);
|
|
|
|
tal_free(fulfilled);
|
|
|
|
tal_free(fulfilled_sides);
|
|
|
|
tal_free(failed);
|
|
|
|
tal_free(failed_sides);
|
|
|
|
|
2019-04-08 11:58:32 +02:00
|
|
|
peer->channel_direction = node_id_idx(&peer->node_ids[LOCAL],
|
|
|
|
&peer->node_ids[REMOTE]);
|
2017-07-03 03:40:52 +02:00
|
|
|
|
2017-11-21 06:26:59 +01:00
|
|
|
/* Default desired feerate is the feerate we set for them last. */
|
|
|
|
if (peer->channel->funder == LOCAL)
|
|
|
|
peer->desired_feerate = feerate_per_kw[REMOTE];
|
|
|
|
|
2019-02-26 17:57:19 +01:00
|
|
|
/* from now we need keep watch over WIRE_CHANNEL_FUNDING_DEPTH */
|
|
|
|
peer->depth_togo = minimum_depth;
|
|
|
|
|
2017-07-03 03:40:52 +02:00
|
|
|
/* OK, now we can process peer messages. */
|
|
|
|
if (reconnected)
|
2018-08-17 06:16:34 +02:00
|
|
|
peer_reconnect(peer, &last_remote_per_commit_secret);
|
2017-07-03 03:40:52 +02:00
|
|
|
|
|
|
|
/* If we have a funding_signed message, send that immediately */
|
2018-08-02 08:49:55 +02:00
|
|
|
if (funding_signed)
|
|
|
|
sync_crypto_write(&peer->cs, PEER_FD, take(funding_signed));
|
2017-07-03 03:40:52 +02:00
|
|
|
|
2018-07-23 04:22:36 +02:00
|
|
|
/* Reenable channel */
|
2018-05-17 07:09:40 +02:00
|
|
|
channel_announcement_negotiate(peer);
|
2018-04-26 06:51:02 +02:00
|
|
|
|
2018-02-23 06:53:47 +01:00
|
|
|
billboard_update(peer);
|
2017-07-03 03:40:52 +02:00
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
static void send_shutdown_complete(struct peer *peer)
|
|
|
|
{
|
2017-07-04 02:47:32 +02:00
|
|
|
/* Now we can tell master shutdown is complete. */
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
wire_sync_write(MASTER_FD,
|
2018-04-26 06:51:01 +02:00
|
|
|
take(towire_channel_shutdown_complete(NULL, &peer->cs)));
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
fdpass_send(MASTER_FD, PEER_FD);
|
|
|
|
fdpass_send(MASTER_FD, GOSSIP_FD);
|
2019-05-04 07:53:13 +02:00
|
|
|
fdpass_send(MASTER_FD, GOSSIP_STORE_FD);
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
close(MASTER_FD);
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:32:44 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-04-25 12:55:34 +02:00
|
|
|
setup_locale();
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
int i, nfds;
|
|
|
|
fd_set fds_in, fds_out;
|
|
|
|
struct peer *peer;
|
|
|
|
|
2018-01-08 11:01:09 +01:00
|
|
|
subdaemon_setup(argc, argv);
|
2017-03-19 21:32:44 +01:00
|
|
|
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
peer = tal(NULL, struct peer);
|
2018-08-09 04:53:17 +02:00
|
|
|
peer->expecting_pong = false;
|
2017-04-01 12:28:39 +02:00
|
|
|
timers_init(&peer->timers, time_mono());
|
|
|
|
peer->commit_timer = NULL;
|
2017-04-04 13:24:47 +02:00
|
|
|
peer->have_sigs[LOCAL] = peer->have_sigs[REMOTE] = false;
|
2017-09-04 05:41:34 +02:00
|
|
|
peer->announce_depth_reached = false;
|
2018-05-21 06:32:37 +02:00
|
|
|
peer->channel_local_active = false;
|
2018-10-25 01:46:08 +02:00
|
|
|
peer->from_master = msg_queue_new(peer);
|
|
|
|
peer->from_gossipd = msg_queue_new(peer);
|
2017-07-04 02:47:32 +02:00
|
|
|
peer->shutdown_sent[LOCAL] = false;
|
2018-01-04 12:40:21 +01:00
|
|
|
peer->last_update_timestamp = 0;
|
2018-08-09 04:53:18 +02:00
|
|
|
/* We actually received it in the previous daemon, but near enough */
|
|
|
|
peer->last_recv = time_now();
|
2019-04-16 06:00:15 +02:00
|
|
|
peer->last_empty_commitment = 0;
|
2017-04-12 08:11:15 +02:00
|
|
|
|
|
|
|
/* We send these to HSM to get real signatures; don't have valgrind
|
|
|
|
* complain. */
|
|
|
|
for (i = 0; i < NUM_SIDES; i++) {
|
|
|
|
memset(&peer->announcement_node_sigs[i], 0,
|
|
|
|
sizeof(peer->announcement_node_sigs[i]));
|
|
|
|
memset(&peer->announcement_bitcoin_sigs[i], 0,
|
|
|
|
sizeof(peer->announcement_bitcoin_sigs[i]));
|
|
|
|
}
|
|
|
|
|
2017-06-20 08:00:03 +02:00
|
|
|
/* Read init_channel message sync. */
|
|
|
|
init_channel(peer);
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
FD_ZERO(&fds_in);
|
|
|
|
FD_SET(MASTER_FD, &fds_in);
|
|
|
|
FD_SET(PEER_FD, &fds_in);
|
|
|
|
FD_SET(GOSSIP_FD, &fds_in);
|
|
|
|
|
|
|
|
FD_ZERO(&fds_out);
|
|
|
|
FD_SET(PEER_FD, &fds_out);
|
|
|
|
nfds = GOSSIP_FD+1;
|
|
|
|
|
|
|
|
while (!shutdown_complete(peer)) {
|
|
|
|
struct timemono first;
|
2018-08-02 08:49:55 +02:00
|
|
|
fd_set rfds = fds_in;
|
2017-11-28 23:50:14 +01:00
|
|
|
struct timeval timeout, *tptr;
|
|
|
|
struct timer *expired;
|
|
|
|
const u8 *msg;
|
|
|
|
struct timemono now = time_mono();
|
|
|
|
|
2018-03-15 05:30:37 +01:00
|
|
|
/* Free any temporary allocations */
|
|
|
|
clean_tmpctx();
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
/* For simplicity, we process one event at a time. */
|
2018-10-25 01:46:08 +02:00
|
|
|
msg = msg_dequeue(peer->from_master);
|
2017-11-28 23:50:14 +01:00
|
|
|
if (msg) {
|
|
|
|
status_trace("Now dealing with deferred %s",
|
|
|
|
channel_wire_type_name(
|
|
|
|
fromwire_peektype(msg)));
|
|
|
|
req_in(peer, msg);
|
2018-02-06 14:52:34 +01:00
|
|
|
tal_free(msg);
|
2017-11-28 23:50:14 +01:00
|
|
|
continue;
|
|
|
|
}
|
channeld: fix sync write to master.
We hit:
assert(!peer->handle_master_reply);
#4 0x000055bba3b030a0 in master_sync_reply (peer=0x55bba41c0030,
msg=0x55bba41c6a80 "", replytype=WIRE_CHANNEL_GOT_COMMITSIG_REPLY,
handle=0x55bba3b041cf <handle_reply_wake_peer>) at channeld/channel.c:518
#5 0x000055bba3b049bc in handle_peer_commit_sig (conn=0x55bba41c10d0,
peer=0x55bba41c0030, msg=0x55bba41c6a80 "") at channeld/channel.c:959
#6 0x000055bba3b05c69 in peer_in (conn=0x55bba41c10d0, peer=0x55bba41c0030,
msg=0x55bba41c67c0 "") at channeld/channel.c:1339
#7 0x000055bba3b123eb in peer_decrypt_body (conn=0x55bba41c10d0,
pcs=0x55bba41c0030) at common/cryptomsg.c:155
#8 0x000055bba3b2c63b in next_plan (conn=0x55bba41c10d0, plan=0x55bba41c1100)
at ccan/ccan/io/io.c:59
We got a commit_sig from the peer while waiting for the master to
reply to acknowledge the commitsig we want to send
(handle_sending_commitsig_reply).
The fix is to go always talk to the master synchronous, and not try to
process anything but messages from the master daemon. This avoids the
whole class of problems.
There's a fairly simple way to do this, as ccan/io lets you override
its poll call: we process any outstanding master requests there, or
add the master fd to the pollfds array.
Fixes: #266
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-09-08 13:08:15 +02:00
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
expired = timers_expire(&peer->timers, now);
|
|
|
|
if (expired) {
|
|
|
|
timer_expired(peer, expired);
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-01 12:28:39 +02:00
|
|
|
|
2018-10-25 01:46:08 +02:00
|
|
|
msg = msg_dequeue(peer->from_gossipd);
|
2017-11-28 23:52:29 +01:00
|
|
|
if (msg) {
|
|
|
|
status_trace("Now dealing with deferred gossip %u",
|
|
|
|
fromwire_peektype(msg));
|
2018-08-02 08:49:56 +02:00
|
|
|
handle_gossip_msg(PEER_FD, &peer->cs, take(msg));
|
2017-11-28 23:52:29 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:50:14 +01:00
|
|
|
if (timer_earliest(&peer->timers, &first)) {
|
|
|
|
timeout = timespec_to_timeval(
|
|
|
|
timemono_between(first, now).ts);
|
|
|
|
tptr = &timeout;
|
|
|
|
} else
|
|
|
|
tptr = NULL;
|
|
|
|
|
2018-08-02 08:49:55 +02:00
|
|
|
if (select(nfds, &rfds, NULL, NULL, tptr) < 0) {
|
2018-02-05 05:09:16 +01:00
|
|
|
/* Signals OK, eg. SIGUSR1 */
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
2017-11-28 23:50:14 +01:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"select failed: %s", strerror(errno));
|
2018-02-05 05:09:16 +01:00
|
|
|
}
|
2017-11-28 23:50:14 +01:00
|
|
|
|
|
|
|
if (FD_ISSET(MASTER_FD, &rfds)) {
|
2018-07-26 07:21:40 +02:00
|
|
|
msg = wire_sync_read(tmpctx, MASTER_FD);
|
2017-11-28 23:50:14 +01:00
|
|
|
|
|
|
|
if (!msg)
|
|
|
|
status_failed(STATUS_FAIL_MASTER_IO,
|
|
|
|
"Can't read command: %s",
|
|
|
|
strerror(errno));
|
|
|
|
req_in(peer, msg);
|
2019-01-26 13:55:07 +01:00
|
|
|
} else if (FD_ISSET(PEER_FD, &rfds)) {
|
|
|
|
/* This could take forever, but who cares? */
|
|
|
|
msg = sync_crypto_read(tmpctx, &peer->cs, PEER_FD);
|
|
|
|
peer_in(peer, msg);
|
2017-11-28 23:50:14 +01:00
|
|
|
} else if (FD_ISSET(GOSSIP_FD, &rfds)) {
|
2018-07-26 07:21:40 +02:00
|
|
|
msg = wire_sync_read(tmpctx, GOSSIP_FD);
|
2018-04-26 06:51:01 +02:00
|
|
|
/* Gossipd hangs up on us to kill us when a new
|
|
|
|
* connection comes in. */
|
2017-11-28 23:50:14 +01:00
|
|
|
if (!msg)
|
2018-06-22 01:55:57 +02:00
|
|
|
peer_failed_connection_lost();
|
2018-08-02 08:49:56 +02:00
|
|
|
handle_gossip_msg(PEER_FD, &peer->cs, take(msg));
|
2018-07-26 07:21:40 +02:00
|
|
|
}
|
2017-04-01 12:28:39 +02:00
|
|
|
}
|
|
|
|
|
2017-07-04 02:47:32 +02:00
|
|
|
/* We only exit when shutdown is complete. */
|
|
|
|
assert(shutdown_complete(peer));
|
|
|
|
send_shutdown_complete(peer);
|
2018-03-29 04:06:45 +02:00
|
|
|
daemon_shutdown();
|
2017-03-07 02:26:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|