diff --git a/channeld/channel.c b/channeld/channel.c index 294471e21..cb7cf36c7 100644 --- a/channeld/channel.c +++ b/channeld/channel.c @@ -1941,53 +1941,48 @@ static void peer_reconnect(struct peer *peer) peer_billboard(true, "Reconnected, and reestablished."); } +/* Funding has locked in, and reached depth. */ static void handle_funding_locked(struct peer *peer, const u8 *msg) { - struct pubkey next_per_commit_point; + unsigned int depth; if (!fromwire_channel_funding_locked(msg, - &peer->short_channel_ids[LOCAL])) + &peer->short_channel_ids[LOCAL], + &depth)) master_badmsg(WIRE_CHANNEL_FUNDING_LOCKED, msg); /* Too late, we're shutting down! */ if (peer->shutdown_sent[LOCAL]) return; - per_commit_point(&peer->shaseed, - &next_per_commit_point, peer->next_index[LOCAL]); + if (!peer->funding_locked[LOCAL]) { + struct pubkey next_per_commit_point; - status_trace("funding_locked: sending commit index %"PRIu64": %s", - peer->next_index[LOCAL], - type_to_string(tmpctx, struct pubkey, &next_per_commit_point)); - msg = towire_funding_locked(NULL, - &peer->channel_id, &next_per_commit_point); - enqueue_peer_msg(peer, take(msg)); - peer->funding_locked[LOCAL] = true; + per_commit_point(&peer->shaseed, + &next_per_commit_point, + peer->next_index[LOCAL]); - billboard_update(peer); + status_trace("funding_locked: sending commit index %"PRIu64": %s", + peer->next_index[LOCAL], + type_to_string(tmpctx, struct pubkey, &next_per_commit_point)); + msg = towire_funding_locked(NULL, + &peer->channel_id, &next_per_commit_point); + enqueue_peer_msg(peer, take(msg)); + peer->funding_locked[LOCAL] = true; + } + + peer->announce_depth_reached = (depth >= ANNOUNCE_MIN_DEPTH); /* Send temporary or final announcements */ send_temporary_announcement(peer); send_announcement_signatures(peer); -} - -static void handle_funding_announce_depth(struct peer *peer) -{ - /* This can happen if we got told already at init time */ - if (peer->announce_depth_reached) - return; - - /* Too late, we're shutting down! */ - if (peer->shutdown_sent[LOCAL]) - return; - - 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]) + if (peer->have_sigs[REMOTE] && peer->have_sigs[LOCAL]) announce_channel(peer); + + billboard_update(peer); } static void handle_offer_htlc(struct peer *peer, const u8 *inmsg) @@ -2357,9 +2352,6 @@ static void req_in(struct peer *peer, const u8 *msg) case WIRE_CHANNEL_FUNDING_LOCKED: handle_funding_locked(peer, msg); return; - case WIRE_CHANNEL_FUNDING_ANNOUNCE_DEPTH: - handle_funding_announce_depth(peer); - return; case WIRE_CHANNEL_OFFER_HTLC: handle_offer_htlc(peer, msg); return; diff --git a/channeld/channel_wire.csv b/channeld/channel_wire.csv index d7ff88496..9c87eab4d 100644 --- a/channeld/channel_wire.csv +++ b/channeld/channel_wire.csv @@ -60,12 +60,10 @@ channel_init,,init_peer_pkt_len,u16 channel_init,,init_peer_pkt,init_peer_pkt_len*u8 channel_init,,reached_announce_depth,bool -# Tx is deep enough, go! +# master->channeld funding hit new depth >= lock depth channel_funding_locked,1002 channel_funding_locked,,short_channel_id,struct short_channel_id - -# Tell the channel that we may announce the channel's existence -channel_funding_announce_depth,1003 +channel_funding_locked,,depth,u32 # Tell channel to offer this htlc channel_offer_htlc,1004 diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 7d5905710..1fe983d82 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -148,7 +148,6 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds) /* And we never get these from channeld. */ case WIRE_CHANNEL_INIT: case WIRE_CHANNEL_FUNDING_LOCKED: - case WIRE_CHANNEL_FUNDING_ANNOUNCE_DEPTH: case WIRE_CHANNEL_OFFER_HTLC: case WIRE_CHANNEL_FULFILL_HTLC: case WIRE_CHANNEL_FAIL_HTLC: @@ -300,10 +299,12 @@ bool peer_start_channeld(struct channel *channel, bool channel_tell_funding_locked(struct lightningd *ld, struct channel *channel, - const struct bitcoin_txid *txid) + const struct bitcoin_txid *txid, + u32 depth) { - /* If not awaiting lockin, it doesn't care any more */ - if (channel->state != CHANNELD_AWAITING_LOCKIN) { + /* If not awaiting lockin/announce, it doesn't care any more */ + if (channel->state != CHANNELD_AWAITING_LOCKIN + && channel->state != CHANNELD_NORMAL) { log_debug(channel->log, "Funding tx confirmed, but peer in state %s", channel_state_name(channel)); @@ -317,9 +318,12 @@ bool channel_tell_funding_locked(struct lightningd *ld, } subd_send_msg(channel->owner, - take(towire_channel_funding_locked(NULL, channel->scid))); + take(towire_channel_funding_locked(NULL, channel->scid, + depth))); - if (channel->remote_funding_locked) + if (channel->remote_funding_locked + && channel->state == CHANNELD_AWAITING_LOCKIN) lockin_complete(channel); + return true; } diff --git a/lightningd/channel_control.h b/lightningd/channel_control.h index de24fe274..27c0a037a 100644 --- a/lightningd/channel_control.h +++ b/lightningd/channel_control.h @@ -17,6 +17,7 @@ bool peer_start_channeld(struct channel *channel, /* Returns true if subd told, otherwise false. */ bool channel_tell_funding_locked(struct lightningd *ld, struct channel *channel, - const struct bitcoin_txid *txid); + const struct bitcoin_txid *txid, + u32 depth); #endif /* LIGHTNING_LIGHTNINGD_CHANNEL_CONTROL_H */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 77b1ad087..1439995c8 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -611,28 +611,6 @@ send_error: subd_send_fd(ld->gossip, gossip_fd); } -static enum watch_result funding_announce_cb(struct channel *channel, - const struct bitcoin_txid *txid UNUSED, - unsigned int depth) -{ - if (depth < ANNOUNCE_MIN_DEPTH) { - return KEEP_WATCHING; - } - - if (!channel->owner || !streq(channel->owner->name, "lightning_channeld")) { - log_debug(channel->log, - "Funding tx announce ready, but channel state %s" - " owned by %s", - channel_state_name(channel), - channel->owner ? channel->owner->name : "none"); - return KEEP_WATCHING; - } - - subd_send_msg(channel->owner, - take(towire_channel_funding_announce_depth(channel))); - return DELETE_WATCH; -} - static enum watch_result funding_lockin_cb(struct channel *channel, const struct bitcoin_txid *txid, unsigned int depth) @@ -661,7 +639,8 @@ static enum watch_result funding_lockin_cb(struct channel *channel, wallet_channel_save(ld->wallet, channel); } - if (!channel_tell_funding_locked(ld, channel, txid)) + /* Try to tell subdaemon */ + if (!channel_tell_funding_locked(ld, channel, txid, depth)) return KEEP_WATCHING; /* BOLT #7: @@ -673,16 +652,10 @@ static enum watch_result funding_lockin_cb(struct channel *channel, if (!(channel->channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL)) return DELETE_WATCH; - /* 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(channel, txid, depth); - else - watch_txid(channel, ld->topology, channel, txid, - funding_announce_cb); + /* We keep telling it depth until we get to announce depth. */ + if (depth < ANNOUNCE_MIN_DEPTH) + return KEEP_WATCHING; + return DELETE_WATCH; } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 1ed45842f..d0d607f26 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -47,7 +47,8 @@ void broadcast_tx(struct chain_topology *topo UNNEEDED, /* Generated stub for channel_tell_funding_locked */ bool channel_tell_funding_locked(struct lightningd *ld UNNEEDED, struct channel *channel UNNEEDED, - const struct bitcoin_txid *txid UNNEEDED) + const struct bitcoin_txid *txid UNNEEDED, + u32 depth UNNEEDED) { fprintf(stderr, "channel_tell_funding_locked called!\n"); abort(); } /* Generated stub for command_fail */ void command_fail(struct command *cmd UNNEEDED, const char *fmt UNNEEDED, ...) @@ -363,9 +364,6 @@ void tell_gossipd_peer_is_important(struct lightningd *ld UNNEEDED, /* Generated stub for towire_channel_dev_reenable_commit */ u8 *towire_channel_dev_reenable_commit(const tal_t *ctx UNNEEDED) { fprintf(stderr, "towire_channel_dev_reenable_commit called!\n"); abort(); } -/* Generated stub for towire_channel_funding_announce_depth */ -u8 *towire_channel_funding_announce_depth(const tal_t *ctx UNNEEDED) -{ fprintf(stderr, "towire_channel_funding_announce_depth called!\n"); abort(); } /* Generated stub for towire_channel_send_shutdown */ u8 *towire_channel_send_shutdown(const tal_t *ctx UNNEEDED) { fprintf(stderr, "towire_channel_send_shutdown called!\n"); abort(); }