@@ -199,50 +199,6 @@ static void broadcast_new_gossip(struct lightningd *ld,
199199/* Recursion */
200200static void cupdate_timer_refresh (struct channel * channel );
201201
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-
246202static enum channel_gossip_state init_public_state (struct channel * channel ,
247203 const struct remote_announce_sigs * remote_sigs )
248204{
@@ -328,53 +284,91 @@ static void send_private_cupdate(struct channel *channel, bool even_if_redundant
328284 msg_to_peer (channel -> peer , cg -> cupdate );
329285}
330286
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 )
334290{
335- struct lightningd * ld = channel -> peer -> ld ;
336291 struct channel_gossip * cg = channel -> channel_gossip ;
337292 const u8 * cupdate ;
338293 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+ {
339316 bool enable , have_old ;
340317
341318 /* 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 );
344321
345322 if (!channel_state_can_add_htlc (channel -> state )) {
346323 /* If it's (no longer) usable, it's a simply courtesy
347324 * to disable */
348- enable = false;
325+ return false;
349326 } else if (channel -> owner ) {
350327 /* If it's live, it's enabled */
351- enable = true;
328+ return true;
352329 } else if (starting_up ) {
353330 /* If we are starting up, don't change it! */
354331 if (!have_old )
355332 /* Assume the best if we don't have an updated */
356333 enable = true;
334+ return enable ;
357335 } else {
358- enable = ok_if_disconnected ;
336+ return ok_if_disconnected ;
359337 }
338+ }
360339
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 ;
365347
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?" );
368350 return ;
351+ }
352+ due .ts .tv_sec = timestamp ;
353+ due .ts .tv_nsec = 0 ;
369354
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 );
374367}
375368
376369static void cupdate_timer_refresh (struct channel * channel )
377370{
371+ struct lightningd * ld = channel -> peer -> ld ;
378372 struct channel_gossip * cg = channel -> channel_gossip ;
379373
380374 /* 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)
385379
386380 /* Free old cupdate to force a new one to be generated */
387381 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 );
389386}
390387
391388static void stash_remote_announce_sigs (struct channel * channel ,
@@ -523,8 +520,13 @@ static void send_channel_announcement(struct channel *channel)
523520
524521 /* Send everyone our new channel announcement */
525522 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+
528530 /* And maybe our first node_announcement */
529531 channel_gossip_node_announce (ld );
530532}
@@ -638,7 +640,8 @@ void channel_gossip_update(struct channel *channel)
638640 announced :
639641 /* We don't penalize disconnected clients normally: we only
640642 * 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" );
642645 check_channel_gossip (channel );
643646 return ;
644647 }
@@ -795,6 +798,8 @@ void channel_gossip_notify_new_block(struct lightningd *ld,
795798void channel_gossip_update_from_gossipd (struct channel * channel ,
796799 const u8 * channel_update TAKES )
797800{
801+ struct channel_gossip * cg = channel -> channel_gossip ;
802+
798803 if (!channel -> channel_gossip ) {
799804 log_broken (channel -> log ,
800805 "gossipd gave channel_update for unsaved channel? update=%s" ,
@@ -828,10 +833,20 @@ void channel_gossip_update_from_gossipd(struct channel *channel,
828833 break ;
829834 }
830835
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+
831842 /* We don't set refresh timer if we're not ANNOUNCED, we're just saving updates
832843 * 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+ }
835850 check_channel_gossip (channel );
836851}
837852
@@ -953,7 +968,12 @@ const u8 *channel_gossip_update_for_error(const tal_t *ctx,
953968 case CGOSSIP_NEED_PEER_SIGS :
954969 return NULL ;
955970 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+ }
957977 check_channel_gossip (channel );
958978 return cg -> cupdate ;
959979 }
0 commit comments