channeld: simplify announce/locked-in callback,

Just have a "new depth" callback, and let channeld do the right thing.

This makes the channeld paths a bit more straightforward.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2018-05-17 14:38:11 +09:30 committed by Christian Decker
parent 981ffb83f7
commit 323472225c
6 changed files with 44 additions and 78 deletions

View File

@ -1941,53 +1941,48 @@ static void peer_reconnect(struct peer *peer)
peer_billboard(true, "Reconnected, and reestablished."); peer_billboard(true, "Reconnected, and reestablished.");
} }
/* Funding has locked in, and reached depth. */
static void handle_funding_locked(struct peer *peer, const u8 *msg) 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, if (!fromwire_channel_funding_locked(msg,
&peer->short_channel_ids[LOCAL])) &peer->short_channel_ids[LOCAL],
&depth))
master_badmsg(WIRE_CHANNEL_FUNDING_LOCKED, msg); master_badmsg(WIRE_CHANNEL_FUNDING_LOCKED, msg);
/* Too late, we're shutting down! */ /* Too late, we're shutting down! */
if (peer->shutdown_sent[LOCAL]) if (peer->shutdown_sent[LOCAL])
return; return;
per_commit_point(&peer->shaseed, if (!peer->funding_locked[LOCAL]) {
&next_per_commit_point, peer->next_index[LOCAL]); struct pubkey next_per_commit_point;
status_trace("funding_locked: sending commit index %"PRIu64": %s", per_commit_point(&peer->shaseed,
peer->next_index[LOCAL], &next_per_commit_point,
type_to_string(tmpctx, struct pubkey, &next_per_commit_point)); peer->next_index[LOCAL]);
msg = towire_funding_locked(NULL,
&peer->channel_id, &next_per_commit_point);
enqueue_peer_msg(peer, take(msg));
peer->funding_locked[LOCAL] = true;
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 or final announcements */
send_temporary_announcement(peer); send_temporary_announcement(peer);
send_announcement_signatures(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 /* Only send the announcement and update if the other end gave
* us its sig */ * us its sig */
if (peer->have_sigs[REMOTE]) if (peer->have_sigs[REMOTE] && peer->have_sigs[LOCAL])
announce_channel(peer); announce_channel(peer);
billboard_update(peer);
} }
static void handle_offer_htlc(struct peer *peer, const u8 *inmsg) 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: case WIRE_CHANNEL_FUNDING_LOCKED:
handle_funding_locked(peer, msg); handle_funding_locked(peer, msg);
return; return;
case WIRE_CHANNEL_FUNDING_ANNOUNCE_DEPTH:
handle_funding_announce_depth(peer);
return;
case WIRE_CHANNEL_OFFER_HTLC: case WIRE_CHANNEL_OFFER_HTLC:
handle_offer_htlc(peer, msg); handle_offer_htlc(peer, msg);
return; return;

View File

@ -60,12 +60,10 @@ channel_init,,init_peer_pkt_len,u16
channel_init,,init_peer_pkt,init_peer_pkt_len*u8 channel_init,,init_peer_pkt,init_peer_pkt_len*u8
channel_init,,reached_announce_depth,bool 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,1002
channel_funding_locked,,short_channel_id,struct short_channel_id channel_funding_locked,,short_channel_id,struct short_channel_id
channel_funding_locked,,depth,u32
# Tell the channel that we may announce the channel's existence
channel_funding_announce_depth,1003
# Tell channel to offer this htlc # Tell channel to offer this htlc
channel_offer_htlc,1004 channel_offer_htlc,1004

1 #include <common/cryptomsg.h>
60 channel_init,,reached_announce_depth,bool
61 # Tx is deep enough, go! # master->channeld funding hit new depth >= lock depth
62 channel_funding_locked,1002
63 channel_funding_locked,,short_channel_id,struct short_channel_id
64 # Tell the channel that we may announce the channel's existence channel_funding_locked,,depth,u32
65 channel_funding_announce_depth,1003 # Tell channel to offer this htlc
66 # Tell channel to offer this htlc channel_offer_htlc,1004
channel_offer_htlc,1004
channel_offer_htlc,,amount_msat,u64
67 channel_offer_htlc,,cltv_expiry,u32 channel_offer_htlc,,amount_msat,u64
68 channel_offer_htlc,,payment_hash,struct sha256 channel_offer_htlc,,cltv_expiry,u32
69 channel_offer_htlc,,onion_routing_packet,1366*u8 channel_offer_htlc,,payment_hash,struct sha256

View File

@ -148,7 +148,6 @@ static unsigned channel_msg(struct subd *sd, const u8 *msg, const int *fds)
/* And we never get these from channeld. */ /* And we never get these from channeld. */
case WIRE_CHANNEL_INIT: case WIRE_CHANNEL_INIT:
case WIRE_CHANNEL_FUNDING_LOCKED: case WIRE_CHANNEL_FUNDING_LOCKED:
case WIRE_CHANNEL_FUNDING_ANNOUNCE_DEPTH:
case WIRE_CHANNEL_OFFER_HTLC: case WIRE_CHANNEL_OFFER_HTLC:
case WIRE_CHANNEL_FULFILL_HTLC: case WIRE_CHANNEL_FULFILL_HTLC:
case WIRE_CHANNEL_FAIL_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, bool channel_tell_funding_locked(struct lightningd *ld,
struct channel *channel, 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 not awaiting lockin/announce, it doesn't care any more */
if (channel->state != CHANNELD_AWAITING_LOCKIN) { if (channel->state != CHANNELD_AWAITING_LOCKIN
&& channel->state != CHANNELD_NORMAL) {
log_debug(channel->log, log_debug(channel->log,
"Funding tx confirmed, but peer in state %s", "Funding tx confirmed, but peer in state %s",
channel_state_name(channel)); channel_state_name(channel));
@ -317,9 +318,12 @@ bool channel_tell_funding_locked(struct lightningd *ld,
} }
subd_send_msg(channel->owner, 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); lockin_complete(channel);
return true; return true;
} }

View File

@ -17,6 +17,7 @@ bool peer_start_channeld(struct channel *channel,
/* Returns true if subd told, otherwise false. */ /* Returns true if subd told, otherwise false. */
bool channel_tell_funding_locked(struct lightningd *ld, bool channel_tell_funding_locked(struct lightningd *ld,
struct channel *channel, struct channel *channel,
const struct bitcoin_txid *txid); const struct bitcoin_txid *txid,
u32 depth);
#endif /* LIGHTNING_LIGHTNINGD_CHANNEL_CONTROL_H */ #endif /* LIGHTNING_LIGHTNINGD_CHANNEL_CONTROL_H */

View File

@ -611,28 +611,6 @@ send_error:
subd_send_fd(ld->gossip, gossip_fd); 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, static enum watch_result funding_lockin_cb(struct channel *channel,
const struct bitcoin_txid *txid, const struct bitcoin_txid *txid,
unsigned int depth) unsigned int depth)
@ -661,7 +639,8 @@ static enum watch_result funding_lockin_cb(struct channel *channel,
wallet_channel_save(ld->wallet, 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; return KEEP_WATCHING;
/* BOLT #7: /* BOLT #7:
@ -673,16 +652,10 @@ static enum watch_result funding_lockin_cb(struct channel *channel,
if (!(channel->channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL)) if (!(channel->channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL))
return DELETE_WATCH; return DELETE_WATCH;
/* Tell channeld that we have reached the announce_depth and /* We keep telling it depth until we get to announce depth. */
* that it may send the announcement_signatures upon receiving if (depth < ANNOUNCE_MIN_DEPTH)
* funding_locked, or right now if it already received it return KEEP_WATCHING;
* 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);
return DELETE_WATCH; return DELETE_WATCH;
} }

View File

@ -47,7 +47,8 @@ void broadcast_tx(struct chain_topology *topo UNNEEDED,
/* Generated stub for channel_tell_funding_locked */ /* Generated stub for channel_tell_funding_locked */
bool channel_tell_funding_locked(struct lightningd *ld UNNEEDED, bool channel_tell_funding_locked(struct lightningd *ld UNNEEDED,
struct channel *channel 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(); } { fprintf(stderr, "channel_tell_funding_locked called!\n"); abort(); }
/* Generated stub for command_fail */ /* Generated stub for command_fail */
void command_fail(struct command *cmd UNNEEDED, const char *fmt UNNEEDED, ...) 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 */ /* Generated stub for towire_channel_dev_reenable_commit */
u8 *towire_channel_dev_reenable_commit(const tal_t *ctx UNNEEDED) u8 *towire_channel_dev_reenable_commit(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "towire_channel_dev_reenable_commit called!\n"); abort(); } { 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 */ /* Generated stub for towire_channel_send_shutdown */
u8 *towire_channel_send_shutdown(const tal_t *ctx UNNEEDED) u8 *towire_channel_send_shutdown(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "towire_channel_send_shutdown called!\n"); abort(); } { fprintf(stderr, "towire_channel_send_shutdown called!\n"); abort(); }