diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index fafc7e03f..edf8b4f4e 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1019,26 +1019,6 @@ static enum watch_result funding_announce_cb(struct peer *peer, return DELETE_WATCH; } -static void peer_onchain_finished(struct subd *subd, int status) -{ - /* Moved on? Eg. reorg, and it has a new onchaind. */ - if (!subd->peer || subd->peer->owner != subd) - return; - - /* Unlink peer from us, so it doesn't try to free us in destroy_peer */ - subd->peer->owner = NULL; - - if (status != 0) { - log_broken(subd->peer->log, "onchaind died status %i", status); - return; - } - - /* FIXME: Remove peer from db. */ - log_info(subd->peer->log, "onchaind complete, forgetting peer"); - - /* Peer is gone. */ - tal_free(subd->peer); -} /* We dump all the known preimages when onchaind starts up. */ static void onchaind_tell_fulfill(struct peer *peer) @@ -1239,6 +1219,19 @@ static int handle_onchain_htlc_timeout(struct peer *peer, const u8 *msg) return 0; } +static int handle_irrevocably_resolved(struct peer *peer, const u8 *msg) +{ + /* FIXME: Remove peer from db. */ + log_info(peer->log, "onchaind complete, forgetting peer"); + + /* Peer is gone: don't free sd yet though; it will exit. */ + peer->owner->peer = NULL; + peer->owner = NULL; + + tal_free(peer); + return 0; +} + static int onchain_msg(struct subd *sd, const u8 *msg, const int *fds) { enum onchain_wire_type t = fromwire_peektype(msg); @@ -1262,6 +1255,9 @@ static int onchain_msg(struct subd *sd, const u8 *msg, const int *fds) case WIRE_ONCHAIN_HTLC_TIMEOUT: return handle_onchain_htlc_timeout(sd->peer, msg); + case WIRE_ONCHAIN_ALL_IRREVOCABLY_RESOLVED: + return handle_irrevocably_resolved(sd->peer, msg); + /* We send these, not receive them */ case WIRE_ONCHAIN_INIT: case WIRE_ONCHAIN_SPENT: @@ -1338,7 +1334,7 @@ static enum watch_result funding_spent(struct peer *peer, "lightning_onchaind", peer, onchain_wire_type_name, onchain_msg, - NULL, peer_onchain_finished, + NULL, peer_owner_finished, NULL, NULL); if (!peer->owner) { diff --git a/onchaind/onchain.c b/onchaind/onchain.c index 24c5f7099..c6425a479 100644 --- a/onchaind/onchain.c +++ b/onchaind/onchain.c @@ -934,6 +934,9 @@ static void wait_for_resolved(struct tracked_output **outs) master_badmsg(-1, msg); tal_free(msg); } + + wire_sync_write(REQ_FD, + take(towire_onchain_all_irrevocably_resolved(outs))); } static void set_state(enum peer_state state) diff --git a/onchaind/onchain_wire.csv b/onchaind/onchain_wire.csv index 86907ee66..97a4c6ceb 100644 --- a/onchaind/onchain_wire.csv +++ b/onchaind/onchain_wire.csv @@ -78,3 +78,6 @@ onchain_missing_htlc_output,,htlc,struct htlc_stub # onchaind->master: this HTLC has timed out (after reasonable_depth) onchain_htlc_timeout,5010 onchain_htlc_timeout,,htlc,struct htlc_stub + +# onchaind->master: this peer can be forgotten +onchain_all_irrevocably_resolved,5011