Skip to content

Commit 49cefb0

Browse files
rustyrussellcdecker
authored andcommitted
EXPERIMENTAL: handle receiving quiescence request.
Changelog-EXPERIMENTAL: Protocol: we support the quiescence protocol from lightning/bolts#869 Signed-off-by: Rusty Russell <[email protected]>
1 parent 47d51fe commit 49cefb0

File tree

1 file changed

+114
-3
lines changed

1 file changed

+114
-3
lines changed

channeld/channeld.c

Lines changed: 114 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ struct peer {
159159
/* If master told us to send wrong_funding */
160160
struct bitcoin_outpoint *shutdown_wrong_funding;
161161

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+
162171
/* Information used for reestablishment. */
163172
bool last_was_revoke;
164173
struct changed_htlc *last_sent_commit;
@@ -273,6 +282,79 @@ static struct amount_msat advertized_htlc_max(const struct channel *channel)
273282
return lower_bound_msat;
274283
}
275284

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+
276358
/* Create and send channel_update to gossipd (and maybe peer) */
277359
static void send_channel_update(struct peer *peer, int disable_flag)
278360
{
@@ -952,6 +1034,12 @@ static bool want_fee_update(const struct peer *peer, u32 *target)
9521034
if (peer->channel->opener != LOCAL)
9531035
return false;
9541036

1037+
#if EXPERIMENTAL_FEATURES
1038+
/* No fee update while quiescing! */
1039+
if (peer->stfu)
1040+
return false;
1041+
#endif
1042+
9551043
max = approx_max_feerate(peer->channel);
9561044
val = peer->desired_feerate;
9571045

@@ -1408,6 +1496,9 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
14081496
send_revocation(peer,
14091497
&commit_sig, htlc_sigs, changed_htlcs, txs[0]);
14101498

1499+
/* We may now be quiescent on our side. */
1500+
maybe_send_stfu(peer);
1501+
14111502
/* This might have synced the feerates: if so, we may want to
14121503
* update */
14131504
if (want_fee_update(peer, NULL))
@@ -1537,6 +1628,9 @@ static void handle_peer_revoke_and_ack(struct peer *peer, const u8 *msg)
15371628
type_to_string(tmpctx, struct pubkey,
15381629
&peer->old_remote_per_commit));
15391630

1631+
/* We may now be quiescent on our side. */
1632+
maybe_send_stfu(peer);
1633+
15401634
start_commit_timer(peer);
15411635
}
15421636

@@ -1931,6 +2025,11 @@ static void peer_in(struct peer *peer, const u8 *msg)
19312025
handle_peer_shutdown(peer, msg);
19322026
return;
19332027

2028+
#if EXPERIMENTAL_FEATURES
2029+
case WIRE_STFU:
2030+
handle_stfu(peer, msg);
2031+
return;
2032+
#endif
19342033
case WIRE_INIT:
19352034
case WIRE_OPEN_CHANNEL:
19362035
case WIRE_ACCEPT_CHANNEL:
@@ -1949,9 +2048,6 @@ static void peer_in(struct peer *peer, const u8 *msg)
19492048
return;
19502049
case WIRE_INIT_RBF:
19512050
case WIRE_ACK_RBF:
1952-
#if EXPERIMENTAL_FEATURES
1953-
case WIRE_STFU:
1954-
#endif
19552051
break;
19562052

19572053
case WIRE_CHANNEL_REESTABLISH:
@@ -2972,18 +3068,28 @@ static void req_in(struct peer *peer, const u8 *msg)
29723068
handle_funding_depth(peer, msg);
29733069
return;
29743070
case WIRE_CHANNELD_OFFER_HTLC:
3071+
if (handle_master_request_later(peer, msg))
3072+
return;
29753073
handle_offer_htlc(peer, msg);
29763074
return;
29773075
case WIRE_CHANNELD_FEERATES:
3076+
if (handle_master_request_later(peer, msg))
3077+
return;
29783078
handle_feerates(peer, msg);
29793079
return;
29803080
case WIRE_CHANNELD_FULFILL_HTLC:
3081+
if (handle_master_request_later(peer, msg))
3082+
return;
29813083
handle_preimage(peer, msg);
29823084
return;
29833085
case WIRE_CHANNELD_FAIL_HTLC:
3086+
if (handle_master_request_later(peer, msg))
3087+
return;
29843088
handle_fail(peer, msg);
29853089
return;
29863090
case WIRE_CHANNELD_SPECIFIC_FEERATES:
3091+
if (handle_master_request_later(peer, msg))
3092+
return;
29873093
handle_specific_feerates(peer, msg);
29883094
return;
29893095
case WIRE_CHANNELD_SEND_SHUTDOWN:
@@ -3266,6 +3372,11 @@ int main(int argc, char *argv[])
32663372
/* We actually received it in the previous daemon, but near enough */
32673373
peer->last_recv = time_now();
32683374
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
32693380

32703381
/* We send these to HSM to get real signatures; don't have valgrind
32713382
* complain. */

0 commit comments

Comments
 (0)