4444#include <lightningd/channel.h>
4545#include <lightningd/channel_control.h>
4646#include <lightningd/channel_gossip.h>
47+ #include <lightningd/closed_channel.h>
4748#include <lightningd/closing_control.h>
4849#include <lightningd/connect_control.h>
4950#include <lightningd/dual_open_control.h>
@@ -1797,7 +1798,10 @@ void peer_connected(struct lightningd *ld, const u8 *msg)
17971798 plugin_hook_call_peer_connected (ld , cmd_id , hook_payload );
17981799}
17991800
1800- static void send_reestablish (struct lightningd * ld , struct channel * channel )
1801+ static void send_reestablish (struct peer * peer ,
1802+ const struct channel_id * cid ,
1803+ const struct shachain * their_shachain ,
1804+ u64 local_next_index )
18011805{
18021806 u8 * msg ;
18031807 struct secret last_remote_per_commit_secret ;
@@ -1810,30 +1814,42 @@ static void send_reestablish(struct lightningd *ld, struct channel *channel)
18101814 * - MUST set `your_last_per_commitment_secret` to the last
18111815 * `per_commitment_secret` it received
18121816 */
1813- num_revocations = revocations_received (& channel -> their_shachain . chain );
1817+ num_revocations = revocations_received (their_shachain );
18141818 if (num_revocations == 0 )
18151819 memset (& last_remote_per_commit_secret , 0 ,
18161820 sizeof (last_remote_per_commit_secret ));
1817- else if (!shachain_get_secret (& channel -> their_shachain . chain ,
1821+ else if (!shachain_get_secret (their_shachain ,
18181822 num_revocations - 1 ,
18191823 & last_remote_per_commit_secret )) {
1820- channel_fail_permanent (channel ,
1821- REASON_LOCAL ,
1822- "Could not get revocation secret %" PRIu64 ,
1823- num_revocations - 1 );
1824+ log_peer_broken (peer -> ld -> log , & peer -> id ,
1825+ "%s: cannot get shachain secret %" PRIu64 " to send reestablish" ,
1826+ fmt_channel_id (tmpctx , cid ), num_revocations - 1 );
18241827 return ;
18251828 }
18261829
1827- msg = towire_channel_reestablish (tmpctx , & channel -> cid ,
1828- channel -> next_index [LOCAL ],
1830+ /* BOLT #2:
1831+ * The sending node:
1832+ * - MUST set `next_commitment_number` to the commitment number of the
1833+ * next `commitment_signed` it expects to receive.
1834+ * - MUST set `next_revocation_number` to the commitment number of the
1835+ * next `revoke_and_ack` message it expects to receive.
1836+ * - MUST set `my_current_per_commitment_point` to a valid point.
1837+ * - if `next_revocation_number` equals 0:
1838+ * - MUST set `your_last_per_commitment_secret` to all zeroes
1839+ * - otherwise:
1840+ * - MUST set `your_last_per_commitment_secret` to the last `per_commitment_secret` it received
1841+ */
1842+ msg = towire_channel_reestablish (tmpctx , cid ,
1843+ local_next_index ,
18291844 num_revocations ,
18301845 & last_remote_per_commit_secret ,
1831- & channel -> channel_info .remote_per_commit ,
1846+ /* Any valid point works, since static_remotekey */
1847+ & peer -> ld -> our_pubkey ,
18321848 /* No upgrade for you, since we're closed! */
18331849 NULL );
1834- subd_send_msg (ld -> connectd ,
1835- take (towire_connectd_peer_send_msg (NULL , & channel -> peer -> id ,
1836- channel -> peer -> connectd_counter ,
1850+ subd_send_msg (peer -> ld -> connectd ,
1851+ take (towire_connectd_peer_send_msg (NULL , & peer -> id ,
1852+ peer -> connectd_counter ,
18371853 msg )));
18381854}
18391855
@@ -1847,6 +1863,7 @@ void peer_spoke(struct lightningd *ld, const u8 *msg)
18471863 u16 msgtype ;
18481864 u64 connectd_counter ;
18491865 struct channel * channel ;
1866+ struct closed_channel * closed_channel ;
18501867 struct channel_id channel_id ;
18511868 struct peer * peer ;
18521869 bool dual_fund ;
@@ -1885,7 +1902,9 @@ void peer_spoke(struct lightningd *ld, const u8 *msg)
18851902 "Trouble in paradise?" );
18861903 goto send_error ;
18871904 }
1888- send_reestablish (ld , channel );
1905+ send_reestablish (peer , & channel -> cid ,
1906+ & channel -> their_shachain .chain ,
1907+ channel -> next_index [LOCAL ]);
18891908 }
18901909
18911910 /* If we have a canned error for this channel, send it now */
@@ -1978,6 +1997,20 @@ void peer_spoke(struct lightningd *ld, const u8 *msg)
19781997 /* FIXME: Send informative error? */
19791998 close (other_fd );
19801999 return ;
2000+
2001+ case WIRE_CHANNEL_REESTABLISH :
2002+ /* Maybe a previously closed channel? */
2003+ closed_channel = closed_channel_map_get (peer -> ld -> closed_channels , & channel_id );
2004+ if (closed_channel && closed_channel -> their_shachain ) {
2005+ send_reestablish (peer , & closed_channel -> cid ,
2006+ closed_channel -> their_shachain ,
2007+ closed_channel -> next_index [LOCAL ]);
2008+ log_peer_info (ld -> log , & peer -> id , "Responded to reestablish for long-closed channel %s" ,
2009+ fmt_channel_id (tmpctx , & channel_id ));
2010+ error = towire_errorfmt (tmpctx , & channel_id ,
2011+ "Channel is closed and forgotten" );
2012+ goto send_error ;
2013+ }
19812014 }
19822015
19832016 /* Weird message? Log and reply with error. */
0 commit comments