Skip to content

Commit

Permalink
channeld: simplify announce/locked-in callback,
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed May 19, 2018
1 parent 981ffb8 commit 3234722
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 78 deletions.
52 changes: 22 additions & 30 deletions channeld/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions channeld/channel_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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));
Expand All @@ -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;
}
3 changes: 2 additions & 1 deletion lightningd/channel_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
39 changes: 6 additions & 33 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,28 +611,6 @@ void peer_sent_nongossip(struct lightningd *ld,
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)
Expand Down Expand Up @@ -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:
Expand All @@ -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;
}

Expand Down
6 changes: 2 additions & 4 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)
Expand Down Expand Up @@ -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(); }
Expand Down

0 comments on commit 3234722

Please sign in to comment.