@@ -346,6 +346,50 @@ static bool handle_master_request_later(struct peer *peer, const u8 *msg)
346346 }
347347 return false;
348348}
349+
350+ static bool channel_type_eq (const struct channel_type * a ,
351+ const struct channel_type * b )
352+ {
353+ return featurebits_eq (a -> features , b -> features );
354+ }
355+
356+ static bool match_type (const struct channel_type * desired ,
357+ const struct channel_type * current ,
358+ struct channel_type * * upgradable )
359+ {
360+ /* Missing fields are possible. */
361+ if (!desired || !current )
362+ return false;
363+
364+ if (channel_type_eq (desired , current ))
365+ return true;
366+
367+ for (size_t i = 0 ; i < tal_count (upgradable ); i ++ ) {
368+ if (channel_type_eq (desired , upgradable [i ]))
369+ return true;
370+ }
371+
372+ return false;
373+ }
374+
375+ static void set_channel_type (struct channel * channel ,
376+ const struct channel_type * type )
377+ {
378+ const struct channel_type * cur = channel_type (tmpctx , channel );
379+
380+ if (channel_type_eq (cur , type ))
381+ return ;
382+
383+ /* We only allow one upgrade at the moment, so that's it. */
384+ assert (!channel -> option_static_remotekey );
385+ assert (feature_offered (type -> features , OPT_STATIC_REMOTEKEY ));
386+
387+ /* Do upgrade, tell master. */
388+ channel -> option_static_remotekey = true;
389+ status_unusual ("Upgraded channel to [%s]" ,
390+ fmt_featurebits (tmpctx , type -> features ));
391+ wire_sync_write (MASTER_FD , take (towire_channeld_upgraded (NULL , true)));
392+ }
349393#else /* !EXPERIMENTAL_FEATURES */
350394static bool handle_master_request_later (struct peer * peer , const u8 * msg )
351395{
@@ -2477,7 +2521,8 @@ static void peer_reconnect(struct peer *peer,
24772521 & my_current_per_commitment_point , NULL );
24782522
24792523#if EXPERIMENTAL_FEATURES
2480- send_tlvs = tlv_channel_reestablish_tlvs_new (tmpctx );
2524+ /* Subtle: we free tmpctx below as we loop, so tal off peer */
2525+ send_tlvs = tlv_channel_reestablish_tlvs_new (peer );
24812526 /* BOLT-upgrade_protocol #2:
24822527 * A node sending `channel_reestablish`, if it supports upgrading channels:
24832528 * - MUST set `next_to_send` the commitment number of the next
@@ -2775,6 +2820,71 @@ static void peer_reconnect(struct peer *peer,
27752820 fmt_featurebits (tmpctx ,
27762821 recv_tlvs -> upgradable [i ]-> features ));
27772822 }
2823+
2824+ /* BOLT-upgrade_protocol #2:
2825+ *
2826+ * A node receiving `channel_reestablish`:
2827+ * - if it has to retransmit `commitment_signed` or `revoke_and_ack`:
2828+ * - MUST consider the channel feature change failed.
2829+ */
2830+ if (retransmit_commitment_signed || retransmit_revoke_and_ack ) {
2831+ status_debug ("No upgrade: we retransmitted" );
2832+ /* BOLT-upgrade_protocol #2:
2833+ *
2834+ * - if `next_to_send` is missing, or not equal to the
2835+ * `next_commitment_number` it sent:
2836+ * - MUST consider the channel feature change failed.
2837+ */
2838+ } else if (!recv_tlvs -> next_to_send ) {
2839+ status_debug ("No upgrade: no next_to_send received" );
2840+ } else if (* recv_tlvs -> next_to_send != peer -> next_index [LOCAL ]) {
2841+ status_debug ("No upgrade: they're retransmitting" );
2842+ /* BOLT-upgrade_protocol #2:
2843+ *
2844+ * - if updates are pending on either sides' commitment transaction:
2845+ * - MUST consider the channel feature change failed.
2846+ */
2847+ /* Note that we can have HTLCs we *want* to add or remove
2848+ * but haven't yet: thats OK! */
2849+ } else if (pending_updates (peer -> channel , LOCAL , true)
2850+ || pending_updates (peer -> channel , REMOTE , true)) {
2851+ status_debug ("No upgrade: pending changes" );
2852+ } else {
2853+ const struct tlv_channel_reestablish_tlvs * initr , * ninitr ;
2854+ const struct channel_type * type ;
2855+
2856+ if (peer -> channel -> opener == LOCAL ) {
2857+ initr = send_tlvs ;
2858+ ninitr = recv_tlvs ;
2859+ } else {
2860+ initr = recv_tlvs ;
2861+ ninitr = send_tlvs ;
2862+ }
2863+
2864+ /* BOLT-upgrade_protocol #2:
2865+ *
2866+ * - if `desired_type` matches `current_type` or any
2867+ * `upgradable` `upgrades`:
2868+ * - MUST consider the channel type to be `desired_type`.
2869+ * - otherwise:
2870+ * - MUST consider the channel feature change failed.
2871+ * - if there is a `current_type` field:
2872+ * - MUST consider the channel type to be `current_type`.
2873+ */
2874+ /* Note: returns NULL on missing fields, aka NULL */
2875+ if (match_type (initr -> desired_type ,
2876+ ninitr -> current_type , ninitr -> upgradable ))
2877+ type = initr -> desired_type ;
2878+ else if (ninitr -> current_type )
2879+ type = ninitr -> current_type ;
2880+ else
2881+ type = NULL ;
2882+
2883+ if (type )
2884+ set_channel_type (peer -> channel , type );
2885+ }
2886+ tal_free (send_tlvs );
2887+
27782888#endif /* EXPERIMENTAL_FEATURES */
27792889
27802890 /* Corner case: we didn't send shutdown before because update_add_htlc
@@ -3223,6 +3333,7 @@ static void req_in(struct peer *peer, const u8 *msg)
32233333 case WIRE_CHANNELD_DEV_MEMLEAK_REPLY :
32243334 case WIRE_CHANNELD_SEND_ERROR_REPLY :
32253335 case WIRE_CHANNELD_DEV_QUIESCE_REPLY :
3336+ case WIRE_CHANNELD_UPGRADED :
32263337 break ;
32273338 }
32283339
0 commit comments