@@ -159,6 +159,15 @@ struct peer {
159
159
/* If master told us to send wrong_funding */
160
160
struct bitcoin_outpoint * shutdown_wrong_funding ;
161
161
162
+ #if EXPERIMENTAL_FEATURES
163
+ /* Do we want quiescence? */
164
+ bool stfu ;
165
+ /* Has stfu been sent by each side? */
166
+ bool stfu_sent [NUM_SIDES ];
167
+ /* Updates master asked, which we've deferred while quiescing */
168
+ struct msg_queue * update_queue ;
169
+ #endif
170
+
162
171
/* Information used for reestablishment. */
163
172
bool last_was_revoke ;
164
173
struct changed_htlc * last_sent_commit ;
@@ -273,6 +282,79 @@ static struct amount_msat advertized_htlc_max(const struct channel *channel)
273
282
return lower_bound_msat ;
274
283
}
275
284
285
+ #if EXPERIMENTAL_FEATURES
286
+ static void maybe_send_stfu (struct peer * peer )
287
+ {
288
+ if (!peer -> stfu )
289
+ return ;
290
+
291
+ if (!peer -> stfu_sent [LOCAL ] && !pending_updates (peer -> channel , LOCAL )) {
292
+ u8 * msg = towire_stfu (NULL , & peer -> channel_id );
293
+ sync_crypto_write (peer -> pps , take (msg ));
294
+ peer -> stfu_sent [LOCAL ] = true;
295
+ }
296
+
297
+ /* FIXME: We're finished, do something! */
298
+ if (peer -> stfu_sent [LOCAL ] && peer -> stfu_sent [REMOTE ])
299
+ status_unusual ("STFU complete: we are quiescent" );
300
+ }
301
+
302
+ static void handle_stfu (struct peer * peer , const u8 * stfu )
303
+ {
304
+ struct channel_id channel_id ;
305
+
306
+ if (!fromwire_stfu (stfu , & channel_id ))
307
+ peer_failed_warn (peer -> pps , & peer -> channel_id ,
308
+ "Bad stfu %s" , tal_hex (peer , stfu ));
309
+
310
+ if (!channel_id_eq (& channel_id , & peer -> channel_id )) {
311
+ peer_failed_err (peer -> pps , & channel_id ,
312
+ "Wrong stfu channel_id: expected %s, got %s" ,
313
+ type_to_string (tmpctx , struct channel_id ,
314
+ & peer -> channel_id ),
315
+ type_to_string (tmpctx , struct channel_id ,
316
+ & channel_id ));
317
+ }
318
+
319
+ /* Sanity check */
320
+ if (pending_updates (peer -> channel , REMOTE ))
321
+ peer_failed_warn (peer -> pps , & peer -> channel_id ,
322
+ "STFU but you still have updates pending?" );
323
+
324
+ /* BOLT-quiescent #2:
325
+ * The receiver of `stfu`:
326
+ * - if it has sent `stfu` then:
327
+ * - MUST now consider the channel to be quiescent
328
+ * - otherwise:
329
+ * - SHOULD NOT send any more update messages.
330
+ * - MUST reply with `stfu` once it can do so.
331
+ */
332
+ peer -> stfu = true;
333
+ peer -> stfu_sent [REMOTE ] = true;
334
+
335
+ maybe_send_stfu (peer );
336
+ }
337
+
338
+ /* Returns true if we queued this for later handling (steals if true) */
339
+ static bool handle_master_request_later (struct peer * peer , const u8 * msg )
340
+ {
341
+ if (peer -> stfu ) {
342
+ msg_enqueue (peer -> update_queue , take (msg ));
343
+ return true;
344
+ }
345
+ return false;
346
+ }
347
+ #else /* !EXPERIMENTAL_FEATURES */
348
+ static bool handle_master_request_later (struct peer * peer , const u8 * msg )
349
+ {
350
+ return false;
351
+ }
352
+
353
+ static void maybe_send_stfu (struct peer * peer )
354
+ {
355
+ }
356
+ #endif
357
+
276
358
/* Create and send channel_update to gossipd (and maybe peer) */
277
359
static void send_channel_update (struct peer * peer , int disable_flag )
278
360
{
@@ -952,6 +1034,12 @@ static bool want_fee_update(const struct peer *peer, u32 *target)
952
1034
if (peer -> channel -> opener != LOCAL )
953
1035
return false;
954
1036
1037
+ #if EXPERIMENTAL_FEATURES
1038
+ /* No fee update while quiescing! */
1039
+ if (peer -> stfu )
1040
+ return false;
1041
+ #endif
1042
+
955
1043
max = approx_max_feerate (peer -> channel );
956
1044
val = peer -> desired_feerate ;
957
1045
@@ -1408,6 +1496,9 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
1408
1496
send_revocation (peer ,
1409
1497
& commit_sig , htlc_sigs , changed_htlcs , txs [0 ]);
1410
1498
1499
+ /* We may now be quiescent on our side. */
1500
+ maybe_send_stfu (peer );
1501
+
1411
1502
/* This might have synced the feerates: if so, we may want to
1412
1503
* update */
1413
1504
if (want_fee_update (peer , NULL ))
@@ -1537,6 +1628,9 @@ static void handle_peer_revoke_and_ack(struct peer *peer, const u8 *msg)
1537
1628
type_to_string (tmpctx , struct pubkey ,
1538
1629
& peer -> old_remote_per_commit ));
1539
1630
1631
+ /* We may now be quiescent on our side. */
1632
+ maybe_send_stfu (peer );
1633
+
1540
1634
start_commit_timer (peer );
1541
1635
}
1542
1636
@@ -1931,6 +2025,11 @@ static void peer_in(struct peer *peer, const u8 *msg)
1931
2025
handle_peer_shutdown (peer , msg );
1932
2026
return ;
1933
2027
2028
+ #if EXPERIMENTAL_FEATURES
2029
+ case WIRE_STFU :
2030
+ handle_stfu (peer , msg );
2031
+ return ;
2032
+ #endif
1934
2033
case WIRE_INIT :
1935
2034
case WIRE_OPEN_CHANNEL :
1936
2035
case WIRE_ACCEPT_CHANNEL :
@@ -1949,9 +2048,6 @@ static void peer_in(struct peer *peer, const u8 *msg)
1949
2048
return ;
1950
2049
case WIRE_INIT_RBF :
1951
2050
case WIRE_ACK_RBF :
1952
- #if EXPERIMENTAL_FEATURES
1953
- case WIRE_STFU :
1954
- #endif
1955
2051
break ;
1956
2052
1957
2053
case WIRE_CHANNEL_REESTABLISH :
@@ -2972,18 +3068,28 @@ static void req_in(struct peer *peer, const u8 *msg)
2972
3068
handle_funding_depth (peer , msg );
2973
3069
return ;
2974
3070
case WIRE_CHANNELD_OFFER_HTLC :
3071
+ if (handle_master_request_later (peer , msg ))
3072
+ return ;
2975
3073
handle_offer_htlc (peer , msg );
2976
3074
return ;
2977
3075
case WIRE_CHANNELD_FEERATES :
3076
+ if (handle_master_request_later (peer , msg ))
3077
+ return ;
2978
3078
handle_feerates (peer , msg );
2979
3079
return ;
2980
3080
case WIRE_CHANNELD_FULFILL_HTLC :
3081
+ if (handle_master_request_later (peer , msg ))
3082
+ return ;
2981
3083
handle_preimage (peer , msg );
2982
3084
return ;
2983
3085
case WIRE_CHANNELD_FAIL_HTLC :
3086
+ if (handle_master_request_later (peer , msg ))
3087
+ return ;
2984
3088
handle_fail (peer , msg );
2985
3089
return ;
2986
3090
case WIRE_CHANNELD_SPECIFIC_FEERATES :
3091
+ if (handle_master_request_later (peer , msg ))
3092
+ return ;
2987
3093
handle_specific_feerates (peer , msg );
2988
3094
return ;
2989
3095
case WIRE_CHANNELD_SEND_SHUTDOWN :
@@ -3266,6 +3372,11 @@ int main(int argc, char *argv[])
3266
3372
/* We actually received it in the previous daemon, but near enough */
3267
3373
peer -> last_recv = time_now ();
3268
3374
peer -> last_empty_commitment = 0 ;
3375
+ #if EXPERIMENTAL_FEATURES
3376
+ peer -> stfu = false;
3377
+ peer -> stfu_sent [LOCAL ] = peer -> stfu_sent [REMOTE ] = false;
3378
+ peer -> update_queue = msg_queue_new (peer );
3379
+ #endif
3269
3380
3270
3381
/* We send these to HSM to get real signatures; don't have valgrind
3271
3382
* complain. */
0 commit comments