diff --git a/channeld/channel.c b/channeld/channel.c index 320312e7f..ee9a5acfc 100644 --- a/channeld/channel.c +++ b/channeld/channel.c @@ -133,6 +133,8 @@ struct peer { struct changed_htlc *last_sent_commit; u64 revocations_received; u8 channel_flags; + + bool announce_depth_reached; }; static u8 *create_channel_announcement(const tal_t *ctx, struct peer *peer); @@ -168,13 +170,25 @@ static void send_announcement_signatures(struct peer *peer) { /* First 2 + 256 byte are the signatures and msg type, skip them */ size_t offset = 258; - const tal_t *tmpctx = tal_tmpctx(peer); + const tal_t *tmpctx; struct sha256_double hash; - u8 *msg; - u8 *ca = create_channel_announcement(tmpctx, peer); - u8 *req = towire_hsm_cannouncement_sig_req(tmpctx, - &peer->channel->funding_pubkey[LOCAL], - ca); + u8 *msg, *ca, *req; + + /* BOLT #7: + * + * If sent, `announcement_signatures` messages MUST NOT be sent until + * `funding_locked` has been sent, and the funding transaction is has + * at least 6 confirmations. + */ + if (!(peer->announce_depth_reached && peer->funding_locked[LOCAL])) + return; + + tmpctx = tal_tmpctx(peer); + status_trace("Exchanging announcement signatures."); + ca = create_channel_announcement(tmpctx, peer); + req = towire_hsm_cannouncement_sig_req( + tmpctx, &peer->channel->funding_pubkey[LOCAL], ca); + if (!wire_sync_write(HSM_FD, req)) status_failed(WIRE_CHANNEL_HSM_FAILED, @@ -331,9 +345,20 @@ static struct io_plan *handle_peer_funding_locked(struct io_conn *conn, take(towire_channel_normal_operation(peer))); } + send_announcement_signatures(peer); + return peer_read_message(conn, &peer->pcs, peer_in); } +static void announce_channel(struct peer *peer) +{ + send_channel_announcement(peer); + send_channel_update(peer, false); + /* Tell the master that we just announced the channel, + * so it may announce the node */ + daemon_conn_send(&peer->master, take(towire_channel_announced(peer))); +} + static struct io_plan *handle_peer_announcement_signatures(struct io_conn *conn, struct peer *peer, const u8 *msg) @@ -362,13 +387,8 @@ static struct io_plan *handle_peer_announcement_signatures(struct io_conn *conn, peer->have_sigs[REMOTE] = true; /* We have the remote sigs, do we have the local ones as well? */ - if (peer->funding_locked[LOCAL] && peer->have_sigs[LOCAL]) { - send_channel_announcement(peer); - send_channel_update(peer, false); - /* Tell the master that we just announced the channel, - * so it may announce the node */ - daemon_conn_send(&peer->master, take(towire_channel_announced(msg))); - } + if (peer->funding_locked[LOCAL] && peer->have_sigs[LOCAL]) + announce_channel(peer); return peer_read_message(conn, &peer->pcs, peer_in); } @@ -1667,20 +1687,13 @@ static void handle_funding_locked(struct peer *peer, const u8 *msg) static void handle_funding_announce_depth(struct peer *peer, const u8 *msg) { - if (peer->channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL) { - status_trace("Exchanging announcement signatures."); - send_announcement_signatures(peer); - } + peer->announce_depth_reached = true; + send_announcement_signatures(peer); /* Only send the announcement and update if the other end gave * us its sig */ - if (peer->have_sigs[REMOTE]) { - send_channel_announcement(peer); - send_channel_update(peer, false); - /* Tell the master that we just announced the channel, - * so it may announce the node */ - daemon_conn_send(&peer->master, take(towire_channel_announced(msg))); - } + if (peer->have_sigs[REMOTE]) + announce_channel(peer); } static void handle_offer_htlc(struct peer *peer, const u8 *inmsg) @@ -2171,6 +2184,7 @@ int main(int argc, char *argv[]) timers_init(&peer->timers, time_mono()); peer->commit_timer = NULL; peer->have_sigs[LOCAL] = peer->have_sigs[REMOTE] = false; + peer->announce_depth_reached = false; peer->handle_master_reply = NULL; peer->master_reply_type = 0; msg_queue_init(&peer->master_deferred, peer); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index e145478cb..a2fae8855 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -1011,13 +1012,13 @@ static enum watch_result funding_announce_cb(struct peer *peer, if (depth < ANNOUNCE_MIN_DEPTH) { return KEEP_WATCHING; } - if (peer->state != CHANNELD_NORMAL || !peer->owner) { + + if (!peer->owner || !streq(peer->owner->name, "lightning_channeld")) { log_debug(peer->ld->log, - "Funding tx announce ready, but peer state %s %s", - peer_state_name(peer->state), - peer->owner ? peer->owner->name : "unowned"); + "Funding tx announce ready, but peer is not owned by channeld"); return KEEP_WATCHING; } + subd_send_msg(peer->owner, take(towire_channel_funding_announce_depth(peer))); return DELETE_WATCH; @@ -1350,20 +1351,16 @@ static enum watch_result funding_lockin_cb(struct peer *peer, if (!(peer->channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL)) return DELETE_WATCH; - /* BOLT #7: - * - * If sent, `announcement_signatures` messages MUST NOT be sent until - * `funding_locked` has been sent, and the funding transaction is has - * at least 6 confirmations. - */ - if (depth >= ANNOUNCE_MIN_DEPTH && peer_ready) { - subd_send_msg(peer->owner, - take(towire_channel_funding_announce_depth(peer))); - } else { - /* Worst case, we'll send next block. */ + /* Tell channeld that we have reached the announce_depth and + * that it may send the announcement_signatures upon receiving + * funding_locked, or right now if it already received it + * before. If we are at the right depth, call the callback + * directly, otherwise schedule a callback */ + if (depth >= ANNOUNCE_MIN_DEPTH) + funding_announce_cb(peer, tx, depth, NULL); + else watch_txid(peer, peer->ld->topology, peer, &txid, funding_announce_cb, NULL); - } return DELETE_WATCH; }