@@ -1721,6 +1721,46 @@ void peer_connected(struct lightningd *ld, const u8 *msg)
17211721 plugin_hook_call_peer_connected (ld , cmd_id , hook_payload );
17221722}
17231723
1724+ static void send_reestablish (struct lightningd * ld , struct channel * channel )
1725+ {
1726+ u8 * msg ;
1727+ struct secret last_remote_per_commit_secret ;
1728+ u64 num_revocations ;
1729+
1730+ /* BOLT #2:
1731+ * - if `next_revocation_number` equals 0:
1732+ * - MUST set `your_last_per_commitment_secret` to all zeroes
1733+ * - otherwise:
1734+ * - MUST set `your_last_per_commitment_secret` to the last
1735+ * `per_commitment_secret` it received
1736+ */
1737+ num_revocations = revocations_received (& channel -> their_shachain .chain );
1738+ if (num_revocations == 0 )
1739+ memset (& last_remote_per_commit_secret , 0 ,
1740+ sizeof (last_remote_per_commit_secret ));
1741+ else if (!shachain_get_secret (& channel -> their_shachain .chain ,
1742+ num_revocations - 1 ,
1743+ & last_remote_per_commit_secret )) {
1744+ channel_fail_permanent (channel ,
1745+ REASON_LOCAL ,
1746+ "Could not get revocation secret %" PRIu64 ,
1747+ num_revocations - 1 );
1748+ return ;
1749+ }
1750+
1751+ msg = towire_channel_reestablish (tmpctx , & channel -> cid ,
1752+ channel -> next_index [LOCAL ],
1753+ num_revocations ,
1754+ & last_remote_per_commit_secret ,
1755+ & channel -> channel_info .remote_per_commit ,
1756+ /* No upgrade for you, since we're closed! */
1757+ NULL );
1758+ subd_send_msg (ld -> connectd ,
1759+ take (towire_connectd_peer_send_msg (NULL , & channel -> peer -> id ,
1760+ channel -> peer -> connectd_counter ,
1761+ msg )));
1762+ }
1763+
17241764/* connectd tells us a peer has a message and we've not already attached
17251765 * a subd. Normally this is a race, but it happens for real when opening
17261766 * a new channel, or referring to a channel we no longer want to talk to
@@ -1749,6 +1789,11 @@ void peer_spoke(struct lightningd *ld, const u8 *msg)
17491789 /* Do we know what channel they're talking about? */
17501790 channel = find_channel_by_id (peer , & channel_id );
17511791 if (channel ) {
1792+ /* In this case, we'll send an error below, but send reestablish reply first
1793+ * in case they lost their state and need it */
1794+ if (msgtype == WIRE_CHANNEL_REESTABLISH && channel_state_closed (channel -> state ))
1795+ send_reestablish (ld , channel );
1796+
17521797 /* If we have a canned error for this channel, send it now */
17531798 if (channel -> error ) {
17541799 error = channel -> error ;
@@ -1790,26 +1835,6 @@ void peer_spoke(struct lightningd *ld, const u8 *msg)
17901835 return ;
17911836 }
17921837
1793- if (msgtype == WIRE_CHANNEL_REESTABLISH ) {
1794- log_debug (channel -> log ,
1795- "Reestablish on %s channel: using channeld to reply" ,
1796- channel_state_name (channel ));
1797- if (socketpair (AF_LOCAL , SOCK_STREAM , 0 , fds ) != 0 ) {
1798- log_broken (channel -> log ,
1799- "Failed to create socketpair: %s" ,
1800- strerror (errno ));
1801- error = towire_warningfmt (tmpctx , & channel -> cid ,
1802- "Trouble in paradise?" );
1803- goto send_error ;
1804- }
1805- if (peer_start_channeld (channel , new_peer_fd (tmpctx , fds [0 ]), NULL , true, true)) {
1806- goto tell_connectd ;
1807- }
1808- /* FIXME: Send informative error? */
1809- close (fds [1 ]);
1810- return ;
1811- }
1812-
18131838 /* Send generic error. */
18141839 error = towire_errorfmt (tmpctx , & channel_id ,
18151840 "channel in state %s" ,
0 commit comments