44
44
#include <lightningd/channel.h>
45
45
#include <lightningd/channel_control.h>
46
46
#include <lightningd/channel_gossip.h>
47
+ #include <lightningd/closed_channel.h>
47
48
#include <lightningd/closing_control.h>
48
49
#include <lightningd/connect_control.h>
49
50
#include <lightningd/dual_open_control.h>
@@ -1797,7 +1798,10 @@ void peer_connected(struct lightningd *ld, const u8 *msg)
1797
1798
plugin_hook_call_peer_connected (ld , cmd_id , hook_payload );
1798
1799
}
1799
1800
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 )
1801
1805
{
1802
1806
u8 * msg ;
1803
1807
struct secret last_remote_per_commit_secret ;
@@ -1810,30 +1814,42 @@ static void send_reestablish(struct lightningd *ld, struct channel *channel)
1810
1814
* - MUST set `your_last_per_commitment_secret` to the last
1811
1815
* `per_commitment_secret` it received
1812
1816
*/
1813
- num_revocations = revocations_received (& channel -> their_shachain . chain );
1817
+ num_revocations = revocations_received (their_shachain );
1814
1818
if (num_revocations == 0 )
1815
1819
memset (& last_remote_per_commit_secret , 0 ,
1816
1820
sizeof (last_remote_per_commit_secret ));
1817
- else if (!shachain_get_secret (& channel -> their_shachain . chain ,
1821
+ else if (!shachain_get_secret (their_shachain ,
1818
1822
num_revocations - 1 ,
1819
1823
& 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 );
1824
1827
return ;
1825
1828
}
1826
1829
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 ,
1829
1844
num_revocations ,
1830
1845
& 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 ,
1832
1848
/* No upgrade for you, since we're closed! */
1833
1849
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 ,
1837
1853
msg )));
1838
1854
}
1839
1855
@@ -1847,6 +1863,7 @@ void peer_spoke(struct lightningd *ld, const u8 *msg)
1847
1863
u16 msgtype ;
1848
1864
u64 connectd_counter ;
1849
1865
struct channel * channel ;
1866
+ struct closed_channel * closed_channel ;
1850
1867
struct channel_id channel_id ;
1851
1868
struct peer * peer ;
1852
1869
bool dual_fund ;
@@ -1885,7 +1902,9 @@ void peer_spoke(struct lightningd *ld, const u8 *msg)
1885
1902
"Trouble in paradise?" );
1886
1903
goto send_error ;
1887
1904
}
1888
- send_reestablish (ld , channel );
1905
+ send_reestablish (peer , & channel -> cid ,
1906
+ & channel -> their_shachain .chain ,
1907
+ channel -> next_index [LOCAL ]);
1889
1908
}
1890
1909
1891
1910
/* 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)
1978
1997
/* FIXME: Send informative error? */
1979
1998
close (other_fd );
1980
1999
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
+ }
1981
2014
}
1982
2015
1983
2016
/* Weird message? Log and reply with error. */
0 commit comments