From d04b7af56e1bbd6b084d46a02c57e39d107ab23f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 27 Sep 2017 06:32:48 +0930 Subject: [PATCH] onchain: add initial onchain state. We have a race where we start onchaind, but state is unchanged, so checks like peer_control.c's: peer_ready = (peer->owner && peer->state == CHANNELD_AWAITING_LOCKIN); if (!peer_ready) { log_unusual(peer->log, "Funding tx confirmed, but peer state %s %s", peer_state_name(peer->state), peer->owner ? peer->owner->name : "unowned"); } else { subd_send_msg(peer->owner, take(towire_channel_funding_locked(peer, peer->scid))); } Can send to the wrong daemon. Signed-off-by: Rusty Russell --- lightningd/peer_control.c | 8 ++++++-- lightningd/peer_state.h | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index fd1796ac7..64bbeccff 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -517,6 +517,7 @@ static bool peer_reconnected(struct lightningd *ld, get_gossip_fd_for_closingd_reconnect(ld, id, peer->unique_id, fd, cs); return true; + case FUNDING_SPEND_SEEN: case ONCHAIND_CHEATED: case ONCHAIND_THEIR_UNILATERAL: case ONCHAIND_OUR_UNILATERAL: @@ -1151,8 +1152,7 @@ static int handle_onchain_init_reply(struct peer *peer, const u8 *msg) return -1; } - /* We could come from almost any state. */ - peer_set_condition(peer, peer->state, state); + peer_set_condition(peer, FUNDING_SPEND_SEEN, state); /* Tell it about any preimages we know. */ onchaind_tell_fulfill(peer); @@ -1392,6 +1392,10 @@ static enum watch_result funding_spent(struct peer *peer, struct pubkey ourkey; peer_fail_permanent_str(peer, "Funding transaction spent"); + + /* We could come from almost any state. */ + peer_set_condition(peer, peer->state, FUNDING_SPEND_SEEN); + peer->owner = new_subd(peer->ld, peer->ld, "lightning_onchaind", peer, onchain_wire_type_name, diff --git a/lightningd/peer_state.h b/lightningd/peer_state.h index 4c557752a..f2d228626 100644 --- a/lightningd/peer_state.h +++ b/lightningd/peer_state.h @@ -26,6 +26,9 @@ enum peer_state { /* Waiting for onchain event. */ CLOSINGD_COMPLETE, + /* We've seen the funding spent, we're waiting for onchaind. */ + FUNDING_SPEND_SEEN, + /* Various onchain states. */ ONCHAIND_CHEATED, ONCHAIND_THEIR_UNILATERAL,