@@ -199,50 +199,6 @@ static void broadcast_new_gossip(struct lightningd *ld,
199
199
/* Recursion */
200
200
static void cupdate_timer_refresh (struct channel * channel );
201
201
202
- static void set_public_cupdate (struct channel * channel ,
203
- const u8 * cupdate TAKES ,
204
- bool refresh_later )
205
- {
206
- struct lightningd * ld = channel -> peer -> ld ;
207
- struct channel_gossip * cg = channel -> channel_gossip ;
208
- u32 timestamp ;
209
- bool enabled ;
210
- struct timeabs now , due ;
211
-
212
- if (!channel_update_details (cupdate , & timestamp , & enabled )) {
213
- log_broken (channel -> log , "Invalid channel_update %s: ignoring" ,
214
- tal_hex (tmpctx , cupdate ));
215
- if (taken (cupdate ))
216
- tal_free (cupdate );
217
- return ;
218
- }
219
-
220
- tal_free (cg -> cupdate );
221
- cg -> cupdate = tal_dup_talarr (cg , u8 , cupdate );
222
-
223
- cg -> refresh_timer = tal_free (cg -> refresh_timer );
224
-
225
- /* If enabled, we refresh, based on old timestamp */
226
- if (!enabled || !refresh_later )
227
- return ;
228
-
229
- due .ts .tv_sec = timestamp ;
230
- due .ts .tv_nsec = 0 ;
231
- due = timeabs_add (due ,
232
- time_from_sec (GOSSIP_PRUNE_INTERVAL (ld -> dev_fast_gossip_prune )
233
- - GOSSIP_BEFORE_DEADLINE (ld -> dev_fast_gossip_prune )));
234
-
235
- /* In case it's passed, timer should be zero */
236
- now = time_now ();
237
- if (time_after (now , due ))
238
- due = now ;
239
-
240
- cg -> refresh_timer = new_reltimer (ld -> timers , cg ,
241
- time_between (due , now ),
242
- cupdate_timer_refresh ,
243
- channel );
244
- }
245
-
246
202
static enum channel_gossip_state init_public_state (struct channel * channel ,
247
203
const struct remote_announce_sigs * remote_sigs )
248
204
{
@@ -328,53 +284,91 @@ static void send_private_cupdate(struct channel *channel, bool even_if_redundant
328
284
msg_to_peer (channel -> peer , cg -> cupdate );
329
285
}
330
286
331
- /* Send gossipd a channel_update, if not redundant . */
332
- static void broadcast_public_cupdate ( struct channel * channel ,
333
- bool ok_if_disconnected )
287
+ /* Sets channel->channel_gossip->cupdate. Returns true if it changed . */
288
+ static bool update_channel_update ( const struct channel * channel ,
289
+ bool enable )
334
290
{
335
- struct lightningd * ld = channel -> peer -> ld ;
336
291
struct channel_gossip * cg = channel -> channel_gossip ;
337
292
const u8 * cupdate ;
338
293
u32 old_timestamp ;
294
+ bool have_old ;
295
+
296
+ /* If we have no previous channel_update, this fails */
297
+ have_old = channel_update_details (cg -> cupdate , & old_timestamp , NULL );
298
+ cupdate = unsigned_channel_update (tmpctx , channel , * channel -> scid ,
299
+ have_old ? & old_timestamp : NULL ,
300
+ true,
301
+ enable );
302
+
303
+ /* Suppress redundant ones */
304
+ if (cg -> cupdate && channel_update_same (cg -> cupdate , cupdate ))
305
+ return false;
306
+
307
+ tal_free (cg -> cupdate );
308
+ cg -> cupdate = sign_update (cg , channel -> peer -> ld , cupdate );
309
+ return true;
310
+ }
311
+
312
+ /* Using default logic, should this channel be enabled? */
313
+ static bool channel_should_enable (const struct channel * channel ,
314
+ bool ok_if_disconnected )
315
+ {
339
316
bool enable , have_old ;
340
317
341
318
/* If we have no previous channel_update, this fails */
342
- have_old = channel_update_details (cg -> cupdate ,
343
- & old_timestamp , & enable );
319
+ have_old = channel_update_details (channel -> channel_gossip -> cupdate ,
320
+ NULL , & enable );
344
321
345
322
if (!channel_state_can_add_htlc (channel -> state )) {
346
323
/* If it's (no longer) usable, it's a simply courtesy
347
324
* to disable */
348
- enable = false;
325
+ return false;
349
326
} else if (channel -> owner ) {
350
327
/* If it's live, it's enabled */
351
- enable = true;
328
+ return true;
352
329
} else if (starting_up ) {
353
330
/* If we are starting up, don't change it! */
354
331
if (!have_old )
355
332
/* Assume the best if we don't have an updated */
356
333
enable = true;
334
+ return enable ;
357
335
} else {
358
- enable = ok_if_disconnected ;
336
+ return ok_if_disconnected ;
359
337
}
338
+ }
360
339
361
- cupdate = unsigned_channel_update (tmpctx , channel , * channel -> scid ,
362
- have_old ? & old_timestamp : NULL ,
363
- true,
364
- enable );
340
+ /* Based on existing update, schedule next refresh */
341
+ static void arm_refresh_timer (struct channel * channel )
342
+ {
343
+ struct lightningd * ld = channel -> peer -> ld ;
344
+ struct channel_gossip * cg = channel -> channel_gossip ;
345
+ struct timeabs now = time_now (), due ;
346
+ u32 timestamp ;
365
347
366
- /* Suppress redundant ones */
367
- if ( cg -> cupdate && channel_update_same ( cg -> cupdate , cupdate ))
348
+ if (! channel_update_details ( cg -> cupdate , & timestamp , NULL )) {
349
+ log_broken ( channel -> log , "Missing channel_update for refresh?" );
368
350
return ;
351
+ }
352
+ due .ts .tv_sec = timestamp ;
353
+ due .ts .tv_nsec = 0 ;
369
354
370
- set_public_cupdate (channel ,
371
- take (sign_update (NULL , channel -> peer -> ld , cupdate )),
372
- true);
373
- broadcast_new_gossip (ld , cg -> cupdate , NULL , "channel update" );
355
+ due = timeabs_add (due ,
356
+ time_from_sec (GOSSIP_PRUNE_INTERVAL (ld -> dev_fast_gossip_prune )
357
+ - GOSSIP_BEFORE_DEADLINE (ld -> dev_fast_gossip_prune )));
358
+
359
+ /* In case it's passed, timer should be zero */
360
+ if (time_after (now , due ))
361
+ due = now ;
362
+
363
+ cg -> refresh_timer = new_reltimer (ld -> timers , cg ,
364
+ time_between (due , now ),
365
+ cupdate_timer_refresh ,
366
+ channel );
374
367
}
375
368
376
369
static void cupdate_timer_refresh (struct channel * channel )
377
370
{
371
+ struct lightningd * ld = channel -> peer -> ld ;
378
372
struct channel_gossip * cg = channel -> channel_gossip ;
379
373
380
374
/* Don't try to free this again if set_public_cupdate called later */
@@ -385,7 +379,10 @@ static void cupdate_timer_refresh(struct channel *channel)
385
379
386
380
/* Free old cupdate to force a new one to be generated */
387
381
cg -> cupdate = tal_free (cg -> cupdate );
388
- broadcast_public_cupdate (channel , true);
382
+ update_channel_update (channel , channel_should_enable (channel , true));
383
+
384
+ broadcast_new_gossip (ld , cg -> cupdate , NULL , "channel update" );
385
+ arm_refresh_timer (channel );
389
386
}
390
387
391
388
static void stash_remote_announce_sigs (struct channel * channel ,
@@ -523,8 +520,13 @@ static void send_channel_announcement(struct channel *channel)
523
520
524
521
/* Send everyone our new channel announcement */
525
522
broadcast_new_gossip (ld , ca , & channel -> funding_sats , "channel announcement" );
526
- /* We can also send our first public channel_update now */
527
- broadcast_public_cupdate (channel , true);
523
+
524
+ /* Any private cupdate will be different from this, so will force a refresh. */
525
+ update_channel_update (channel , channel_should_enable (channel , true));
526
+
527
+ broadcast_new_gossip (ld , cg -> cupdate , NULL , "channel update" );
528
+ arm_refresh_timer (channel );
529
+
528
530
/* And maybe our first node_announcement */
529
531
channel_gossip_node_announce (ld );
530
532
}
@@ -638,7 +640,8 @@ void channel_gossip_update(struct channel *channel)
638
640
announced :
639
641
/* We don't penalize disconnected clients normally: we only
640
642
* do that if we actually try to send an htlc through */
641
- broadcast_public_cupdate (channel , true);
643
+ update_channel_update (channel , true);
644
+ broadcast_new_gossip (ld , cg -> cupdate , NULL , "channel update" );
642
645
check_channel_gossip (channel );
643
646
return ;
644
647
}
@@ -795,6 +798,8 @@ void channel_gossip_notify_new_block(struct lightningd *ld,
795
798
void channel_gossip_update_from_gossipd (struct channel * channel ,
796
799
const u8 * channel_update TAKES )
797
800
{
801
+ struct channel_gossip * cg = channel -> channel_gossip ;
802
+
798
803
if (!channel -> channel_gossip ) {
799
804
log_broken (channel -> log ,
800
805
"gossipd gave channel_update for unsaved channel? update=%s" ,
@@ -828,10 +833,20 @@ void channel_gossip_update_from_gossipd(struct channel *channel,
828
833
break ;
829
834
}
830
835
836
+ /* In case we generated one before gossipd told us? */
837
+ if (cg -> cupdate ) {
838
+ tal_free (cg -> cupdate );
839
+ cg -> refresh_timer = tal_free (cg -> refresh_timer );
840
+ }
841
+
831
842
/* We don't set refresh timer if we're not ANNOUNCED, we're just saving updates
832
843
* for later! */
833
- set_public_cupdate (channel , channel_update ,
834
- channel -> channel_gossip -> state == CGOSSIP_ANNOUNCED );
844
+ cg -> cupdate = tal_dup_talarr (cg , u8 , channel_update );
845
+ if (cg -> state == CGOSSIP_ANNOUNCED ) {
846
+ broadcast_new_gossip (channel -> peer -> ld ,
847
+ channel_update , NULL , "channel update" );
848
+ arm_refresh_timer (channel );
849
+ }
835
850
check_channel_gossip (channel );
836
851
}
837
852
@@ -953,7 +968,12 @@ const u8 *channel_gossip_update_for_error(const tal_t *ctx,
953
968
case CGOSSIP_NEED_PEER_SIGS :
954
969
return NULL ;
955
970
case CGOSSIP_ANNOUNCED :
956
- broadcast_public_cupdate (channel , false);
971
+ /* At this point we actually disable disconnected peers. */
972
+ if (update_channel_update (channel , channel_should_enable (channel , false))) {
973
+ broadcast_new_gossip (channel -> peer -> ld ,
974
+ cg -> cupdate , NULL ,
975
+ "channel update" );
976
+ }
957
977
check_channel_gossip (channel );
958
978
return cg -> cupdate ;
959
979
}
0 commit comments