1 /* This contains the code which actively seeks out gossip from peers */
2 #include <bitcoin/chainparams.h>
3 #include <ccan/array_size/array_size.h>
4 #include <ccan/asort/asort.h>
5 #include <ccan/tal/str/str.h>
6 #include <common/decode_array.h>
7 #include <common/pseudorand.h>
8 #include <common/random_select.h>
9 #include <common/status.h>
10 #include <common/timeout.h>
11 #include <gossipd/gossipd.h>
12 #include <gossipd/queries.h>
13 #include <gossipd/routing.h>
14 #include <gossipd/seeker.h>
15 
16 #define GOSSIP_SEEKER_INTERVAL(seeker) \
17 	DEV_FAST_GOSSIP((seeker)->daemon->rstate->dev_fast_gossip, 5, 60)
18 
19 enum seeker_state {
20 	/* Still streaming gossip from single peer. */
21 	STARTING_UP,
22 
23 	/* Probing: checking our startup really is finished. */
24 	PROBING_SCIDS,
25 
26 	/* Probing: check that we have node_announcements. */
27 	PROBING_NANNOUNCES,
28 
29 	/* Normal running. */
30 	NORMAL,
31 
32 	/* Asking a peer for unknown scids. */
33 	ASKING_FOR_UNKNOWN_SCIDS,
34 
35 	/* Asking a peer for stale scids. */
36 	ASKING_FOR_STALE_SCIDS,
37 };
38 
39 #if DEVELOPER
40 bool dev_suppress_gossip;
41 #endif
42 
43 /* Gossip we're seeking at the moment. */
44 struct seeker {
45 	struct daemon *daemon;
46 
47 	enum seeker_state state;
48 
49 	/* Timer which checks on progress every minute */
50 	struct oneshot *check_timer;
51 
52 	/* Channels we've heard about, but don't know (by scid). */
53 	UINTMAP(bool) unknown_scids;
54 
55 	/* Channels we've heard about newer timestamps for (by scid).  u8 is
56 	 * query_flags. */
57 	UINTMAP(u8 *) stale_scids;
58 
59 	/* Range of scid blocks we've probed. */
60 	size_t scid_probe_start, scid_probe_end;
61 
62 	/* During startup, we ask a single peer for gossip. */
63 	struct peer *random_peer_softref;
64 
65 	/* This checks progress of our random peer */
66 	size_t prev_gossip_count;
67 
68 	/* Array of scids for node announcements. */
69 	struct short_channel_id *nannounce_scids;
70 	u8 *nannounce_query_flags;
71 
72 	/* Are there any node_ids we didn't know?  Implies we're
73 	 * missing channels. */
74 	bool unknown_nodes;
75 
76 	/* Peers we've asked to stream us gossip */
77 	struct peer *gossiper_softref[5];
78 
79 	/* A peer that told us about unknown gossip. */
80 	struct peer *preferred_peer_softref;
81 
82 };
83 
84 /* Mutual recursion */
85 static void seeker_check(struct seeker *seeker);
86 static void probe_some_random_scids(struct seeker *seeker);
87 
begin_check_timer(struct seeker * seeker)88 static void begin_check_timer(struct seeker *seeker)
89 {
90 	const u32 polltime = GOSSIP_SEEKER_INTERVAL(seeker);
91 
92 	seeker->check_timer = new_reltimer(&seeker->daemon->timers,
93 					   seeker,
94 					   time_from_sec(polltime),
95 					   seeker_check, seeker);
96 }
97 
98 /* Set this peer as our random peer; return false if NULL. */
selected_peer(struct seeker * seeker,struct peer * peer)99 static bool selected_peer(struct seeker *seeker, struct peer *peer)
100 {
101 	if (!peer)
102 		return false;
103 
104 	set_softref(seeker, &seeker->random_peer_softref, peer);
105 
106 	/* Give it some grace in case we immediately hit timer */
107 	seeker->prev_gossip_count
108 		= peer->gossip_counter - GOSSIP_SEEKER_INTERVAL(seeker);
109 	return true;
110 }
111 
112 #define set_state(seeker, state, peer, ...)				\
113 	set_state_((seeker), (state), (peer), stringify(state), __VA_ARGS__)
114 
115 static void set_state_(struct seeker *seeker, enum seeker_state state,
116 		       struct peer *peer,
117 		       const char *statename, const char *fmt, ...)
118 PRINTF_FMT(5,6);
119 
set_state_(struct seeker * seeker,enum seeker_state state,struct peer * peer,const char * statename,const char * fmt,...)120 static void set_state_(struct seeker *seeker, enum seeker_state state,
121 		       struct peer *peer,
122 		       const char *statename, const char *fmt, ...)
123 {
124 	va_list ap;
125 	va_start(ap, fmt);
126 	status_peer_debug(peer ? &peer->id : NULL,
127 			  "seeker: state = %s %s",
128 			  statename, tal_vfmt(tmpctx, fmt, ap));
129 	va_end(ap);
130 	seeker->state = state;
131 	selected_peer(seeker, peer);
132 }
133 
new_seeker(struct daemon * daemon)134 struct seeker *new_seeker(struct daemon *daemon)
135 {
136 	struct seeker *seeker = tal(daemon, struct seeker);
137 
138 	seeker->daemon = daemon;
139 	uintmap_init(&seeker->unknown_scids);
140 	uintmap_init(&seeker->stale_scids);
141 	seeker->random_peer_softref = NULL;
142 	for (size_t i = 0; i < ARRAY_SIZE(seeker->gossiper_softref); i++)
143 		seeker->gossiper_softref[i] = NULL;
144 	seeker->preferred_peer_softref = NULL;
145 	seeker->unknown_nodes = false;
146 	set_state(seeker, STARTING_UP, NULL, "New seeker");
147 	begin_check_timer(seeker);
148 	return seeker;
149 }
150 
set_preferred_peer(struct seeker * seeker,struct peer * peer)151 static void set_preferred_peer(struct seeker *seeker, struct peer *peer)
152 {
153 	if (seeker->preferred_peer_softref
154 	    && seeker->preferred_peer_softref != peer) {
155 		clear_softref(seeker, &seeker->preferred_peer_softref);
156 		set_softref(seeker, &seeker->preferred_peer_softref, peer);
157 	}
158 }
159 
160 /* Get a random peer, but try our preferred peer first, if any.  This
161  * biasses us to the peer that told us of unexpected gossip. */
random_seeker(struct seeker * seeker,bool (* check_peer)(const struct peer * peer))162 static struct peer *random_seeker(struct seeker *seeker,
163 				  bool (*check_peer)(const struct peer *peer))
164 {
165 	struct peer *peer = seeker->preferred_peer_softref;
166 
167 	/* 80% chance of immediately choosing a peer who reported the missing
168 	 * stuff: they presumably can tell us more about it.  We don't
169 	 * *always* choose it because it could be simply spamming us with
170 	 * invalid announcements to get chosen, and we don't handle that case
171 	 * well yet. */
172 	if (peer && check_peer(peer) && pseudorand(5) != 0) {
173 		clear_softref(seeker, &seeker->random_peer_softref);
174 		return peer;
175 	}
176 
177 	return random_peer(seeker->daemon, check_peer);
178 }
179 
peer_made_progress(struct seeker * seeker)180 static bool peer_made_progress(struct seeker *seeker)
181 {
182 	const struct peer *peer = seeker->random_peer_softref;
183 
184 	/* Has it made progress (at least one valid update per second)?  If
185 	 * not, we assume it's finished, and if it hasn't, we'll end up
186 	 * querying backwards in next steps. */
187 	if (peer->gossip_counter
188 	    >= seeker->prev_gossip_count + GOSSIP_SEEKER_INTERVAL(seeker)) {
189 		seeker->prev_gossip_count = peer->gossip_counter;
190 		return true;
191 	}
192 
193 	return false;
194 }
195 
disable_gossip_stream(struct seeker * seeker,struct peer * peer)196 static void disable_gossip_stream(struct seeker *seeker, struct peer *peer)
197 {
198 	u8 *msg;
199 
200 	status_peer_debug(&peer->id, "seeker: disabling gossip");
201 
202 	/* This is allowed even if they don't understand it (odd) */
203 	msg = towire_gossip_timestamp_filter(NULL,
204 					     &chainparams->genesis_blockhash,
205 					     UINT32_MAX,
206 					     UINT32_MAX);
207 	queue_peer_msg(peer, take(msg));
208 }
209 
enable_gossip_stream(struct seeker * seeker,struct peer * peer)210 static void enable_gossip_stream(struct seeker *seeker, struct peer *peer)
211 {
212 	/* We seek some way back, to take into account propagation time */
213 	const u32 polltime = GOSSIP_SEEKER_INTERVAL(seeker) * 10;
214 	u32 start = seeker->daemon->rstate->last_timestamp;
215 	u8 *msg;
216 
217 #if DEVELOPER
218 	if (dev_suppress_gossip)
219 		return;
220 #endif
221 
222 	if (start > polltime)
223 		start -= polltime;
224 	else
225 		start = 0;
226 
227 	status_peer_debug(&peer->id, "seeker: starting gossip");
228 
229 	/* This is allowed even if they don't understand it (odd) */
230 	msg = towire_gossip_timestamp_filter(NULL,
231 					     &chainparams->genesis_blockhash,
232 					     start,
233 					     UINT32_MAX);
234 	queue_peer_msg(peer, take(msg));
235 }
236 
normal_gossip_start(struct seeker * seeker,struct peer * peer)237 static void normal_gossip_start(struct seeker *seeker, struct peer *peer)
238 {
239 	bool enable_stream = false;
240 
241 	/* Make this one of our streaming gossipers if we aren't full */
242 	for (size_t i = 0; i < ARRAY_SIZE(seeker->gossiper_softref); i++) {
243 		if (seeker->gossiper_softref[i] == NULL) {
244 			set_softref(seeker, &seeker->gossiper_softref[i], peer);
245 			enable_stream = true;
246 			break;
247 		}
248 	}
249 
250 	if (enable_stream)
251 		enable_gossip_stream(seeker, peer);
252 	else
253 		disable_gossip_stream(seeker, peer);
254 }
255 
256 /* Turn unknown_scids map into a flat array, removes from map. */
unknown_scids_remove(const tal_t * ctx,struct seeker * seeker)257 static struct short_channel_id *unknown_scids_remove(const tal_t *ctx,
258 						     struct seeker *seeker)
259 {
260 	struct short_channel_id *scids;
261 	/* Marshal into an array: we can fit 8000 comfortably. */
262 	size_t i, max = 8000;
263 	u64 scid;
264 
265 	scids = tal_arr(ctx, struct short_channel_id, max);
266 	i = 0;
267 	while (uintmap_first(&seeker->unknown_scids, &scid)) {
268 		scids[i].u64 = scid;
269 		(void)uintmap_del(&seeker->unknown_scids, scid);
270 		if (++i == max)
271 			break;
272 	}
273 	tal_resize(&scids, i);
274 	return scids;
275 }
276 
277 /* We have selected this peer to stream us startup gossip */
peer_gossip_startup(struct seeker * seeker,struct peer * peer)278 static void peer_gossip_startup(struct seeker *seeker, struct peer *peer)
279 {
280 	status_peer_debug(&peer->id, "seeker: chosen as startup peer");
281 	selected_peer(seeker, peer);
282 	normal_gossip_start(seeker, peer);
283 }
284 
peer_has_gossip_queries(const struct peer * peer)285 static bool peer_has_gossip_queries(const struct peer *peer)
286 {
287 	return peer->gossip_queries_feature;
288 }
289 
peer_can_take_range_query(const struct peer * peer)290 static bool peer_can_take_range_query(const struct peer *peer)
291 {
292 	return peer->gossip_queries_feature
293 		&& !peer->range_replies;
294 }
295 
peer_can_take_scid_query(const struct peer * peer)296 static bool peer_can_take_scid_query(const struct peer *peer)
297 {
298 	return peer->gossip_queries_feature
299 		&& !peer->scid_query_outstanding;
300 }
301 
scid_query_done(struct peer * peer,bool complete)302 static void scid_query_done(struct peer *peer, bool complete)
303 {
304 	struct seeker *seeker = peer->daemon->seeker;
305 
306 	/* Peer completed!  OK, start random scid probe in case we're
307 	 * still missing gossip. */
308 	probe_some_random_scids(seeker);
309 }
310 
311 /* Returns true if there were scids to seek. */
seek_any_unknown_scids(struct seeker * seeker)312 static bool seek_any_unknown_scids(struct seeker *seeker)
313 {
314 	struct peer *peer;
315 	struct short_channel_id *scids;
316 
317 	/* Nothing we need to know about? */
318 	if (uintmap_empty(&seeker->unknown_scids))
319 		return false;
320 
321 	/* No peers can answer?  Try again later. */
322 	peer = random_seeker(seeker, peer_can_take_scid_query);
323 	if (!peer)
324 		return false;
325 
326 	scids = unknown_scids_remove(tmpctx, seeker);
327 	set_state(seeker, ASKING_FOR_UNKNOWN_SCIDS, peer,
328 		  "Asking for %zu scids", tal_count(scids));
329 	if (!query_short_channel_ids(seeker->daemon, peer, scids, NULL,
330 				     scid_query_done))
331 		status_failed(STATUS_FAIL_INTERNAL_ERROR,
332 			      "seeker: quering %zu scids is too many?",
333 			      tal_count(scids));
334 	return true;
335 }
336 
337 /* Turns stale_scid_map into two arrays, and removes from map */
stale_scids_remove(const tal_t * ctx,struct seeker * seeker,u8 ** query_flags)338 static struct short_channel_id *stale_scids_remove(const tal_t *ctx,
339 						   struct seeker *seeker,
340 						   u8 **query_flags)
341 {
342 	struct short_channel_id *scids;
343 	const u8 *qf;
344 	/* We can fit 7000 comfortably (8 byte scid, 1 byte flag). */
345 	size_t i, max = 7000;
346 	u64 scid;
347 
348 	scids = tal_arr(ctx, struct short_channel_id, max);
349 	*query_flags = tal_arr(ctx, u8, max);
350 
351 	i = 0;
352 	while ((qf = uintmap_first(&seeker->stale_scids, &scid)) != NULL) {
353 		scids[i].u64 = scid;
354 		(*query_flags)[i] = *qf;
355 		uintmap_del(&seeker->stale_scids, scid);
356 		tal_free(qf);
357 		i++;
358 		if (i == max)
359 			break;
360 	}
361 	tal_resize(&scids, i);
362 	tal_resize(query_flags, i);
363 	return scids;
364 }
365 
seek_any_stale_scids(struct seeker * seeker)366 static bool seek_any_stale_scids(struct seeker *seeker)
367 {
368 	struct peer *peer;
369 	struct short_channel_id *scids;
370 	u8 *query_flags;
371 
372 	/* Nothing we need to know about? */
373 	if (uintmap_empty(&seeker->stale_scids))
374 		return false;
375 
376 	/* No peers can answer?  Try again later. */
377 	peer = random_seeker(seeker, peer_can_take_scid_query);
378 	if (!peer)
379 		return false;
380 
381 	/* This is best-effort, so this consumes them as well. */
382 	scids = stale_scids_remove(tmpctx, seeker, &query_flags);
383 	set_state(seeker, ASKING_FOR_STALE_SCIDS, peer,
384 		  "Asking for %zu scids", tal_count(scids));
385 
386 	if (!query_short_channel_ids(seeker->daemon, peer, scids, query_flags,
387 				     scid_query_done))
388 		status_failed(STATUS_FAIL_INTERNAL_ERROR,
389 			      "seeker: quering %zu scids is too many?",
390 			      tal_count(scids));
391 	return true;
392 }
393 
394 /* Returns true and sets first_blocknum and number_of_blocks if
395  * there's more to find. */
next_block_range(struct seeker * seeker,u32 prev_num_blocks,u32 * first_blocknum,u32 * number_of_blocks)396 static bool next_block_range(struct seeker *seeker,
397 			     u32 prev_num_blocks,
398 			     u32 *first_blocknum, u32 *number_of_blocks)
399 {
400 	const u32 current_height = seeker->daemon->current_blockheight;
401 
402 	/* We always try to get twice as many as last time. */
403 	*number_of_blocks = prev_num_blocks * 2;
404 
405 	if (seeker->scid_probe_start > 0) {
406 		/* Enlarge probe to cover prior blocks, but twice as many. */
407 		if (*number_of_blocks > seeker->scid_probe_start) {
408 			*number_of_blocks = seeker->scid_probe_start;
409 			*first_blocknum = 0;
410 		} else {
411 			*first_blocknum
412 				= seeker->scid_probe_start - *number_of_blocks;
413 		}
414 		seeker->scid_probe_start = *first_blocknum;
415 		return true;
416 	}
417 
418 	/* We allow 6 new blocks since we started; they should be empty anyway */
419 	if (seeker->scid_probe_end + 6 < current_height) {
420 		if (seeker->scid_probe_end + *number_of_blocks > current_height)
421 			*number_of_blocks
422 				= current_height - seeker->scid_probe_end;
423 		*first_blocknum = seeker->scid_probe_end + 1;
424 		seeker->scid_probe_end = *first_blocknum + *number_of_blocks - 1;
425 		return true;
426 	}
427 
428 	/* No more to find. */
429 	return false;
430 }
431 
cmp_scid(const struct short_channel_id * a,const struct short_channel_id * b,void * unused)432 static int cmp_scid(const struct short_channel_id *a,
433 		    const struct short_channel_id *b,
434 		    void *unused)
435 {
436 	if (a->u64 > b->u64)
437 		return 1;
438 	else if (a->u64 < b->u64)
439 		return -1;
440 	return 0;
441 }
442 
443 /* We can't ask for channels by node_id, so probe at random */
get_unannounced_nodes(const tal_t * ctx,struct routing_state * rstate,size_t max,struct short_channel_id ** scids,u8 ** query_flags)444 static bool get_unannounced_nodes(const tal_t *ctx,
445 				  struct routing_state *rstate,
446 				  size_t max,
447 				  struct short_channel_id **scids,
448 				  u8 **query_flags)
449 {
450 	size_t num = 0;
451 	u64 offset;
452 	double total_weight = 0.0;
453 
454 	/* Pick an example short_channel_id at random to query.  As a
455 	 * side-effect this gets the node. */
456 	*scids = tal_arr(ctx, struct short_channel_id, max);
457 
458 	/* FIXME: This is inefficient!  Reuse next_block_range here! */
459 	for (struct chan *c = uintmap_first(&rstate->chanmap, &offset);
460 	     c;
461 	     c = uintmap_after(&rstate->chanmap, &offset)) {
462 		/* Local-only?  Don't ask. */
463 		if (!is_chan_public(c))
464 			continue;
465 
466 		if (c->nodes[0]->bcast.index && c->nodes[1]->bcast.index)
467 			continue;
468 
469 		if (num < max) {
470 			(*scids)[num++] = c->scid;
471 		} else {
472 			/* Maybe replace one: approx. reservoir sampling */
473 			if (random_select(1.0, &total_weight))
474 				(*scids)[pseudorand(max)] = c->scid;
475 		}
476 	}
477 
478 	if (num == 0) {
479 		*scids = tal_free(*scids);
480 		return false;
481 	}
482 
483 	if (num < max)
484 		tal_resize(scids, num);
485 
486 	/* Sort them into order. */
487 	asort(*scids, num, cmp_scid, NULL);
488 
489 	/* Now get flags. */
490 	*query_flags = tal_arr(ctx, u8, num);
491 	for (size_t i = 0; i < tal_count(*scids); i++) {
492 		struct chan *c = get_channel(rstate, &(*scids)[i]);
493 
494 		(*query_flags)[i] = 0;
495 		if (!c->nodes[0]->bcast.index)
496 			(*query_flags)[i] |= SCID_QF_NODE1;
497 		if (!c->nodes[1]->bcast.index)
498 			(*query_flags)[i] |= SCID_QF_NODE2;
499 	}
500 	return true;
501 }
502 
503 /* Mutual recursion */
504 static void peer_gossip_probe_nannounces(struct seeker *seeker);
505 
nodeannounce_query_done(struct peer * peer,bool complete)506 static void nodeannounce_query_done(struct peer *peer, bool complete)
507 {
508 	struct seeker *seeker = peer->daemon->seeker;
509 	struct routing_state *rstate = seeker->daemon->rstate;
510 	size_t new_nannounce = 0, num_scids;
511 
512 	/* We might have given up on them, then they replied. */
513 	if (seeker->random_peer_softref != peer) {
514 		status_peer_debug(&peer->id, "seeker: belated reply: ignoring");
515 		return;
516 	}
517 
518 	clear_softref(seeker, &seeker->random_peer_softref);
519 
520 	num_scids = tal_count(seeker->nannounce_scids);
521 	for (size_t i = 0; i < num_scids; i++) {
522 		struct chan *c = get_channel(rstate,
523 					     &seeker->nannounce_scids[i]);
524 		/* Could have closed since we asked. */
525 		if (!c)
526 			continue;
527 		if ((seeker->nannounce_query_flags[i] & SCID_QF_NODE1)
528 		    && c->nodes[0]->bcast.index)
529 			new_nannounce++;
530 		if ((seeker->nannounce_query_flags[i] & SCID_QF_NODE2)
531 		    && c->nodes[1]->bcast.index)
532 			new_nannounce++;
533 	}
534 
535 	status_peer_debug(&peer->id,
536 			  "seeker: found %zu new node_announcements in %zu scids",
537 			  new_nannounce, num_scids);
538 
539 	seeker->nannounce_scids = tal_free(seeker->nannounce_scids);
540 	seeker->nannounce_query_flags = tal_free(seeker->nannounce_query_flags);
541 
542 	if (!new_nannounce) {
543 		set_state(seeker, NORMAL, NULL,
544 			  "No new node_announcements in %zu scids", num_scids);
545 		return;
546 	}
547 
548 	/* Since they told us about new announcements, keep asking them. */
549 	set_preferred_peer(seeker, peer);
550 
551 	/* Double every time.  We may skip a few, of course, since map
552 	 * is changing. */
553 	num_scids *= 2;
554 	/* Don't try to create a query larger than 64k */
555 	if (num_scids > 7000)
556 		num_scids = 7000;
557 
558 	if (!get_unannounced_nodes(seeker, seeker->daemon->rstate, num_scids,
559 				   &seeker->nannounce_scids,
560 				   &seeker->nannounce_query_flags)) {
561 		/* Nothing unknown at all?  Great, we're done */
562 		set_state(seeker, NORMAL, NULL, "No unannounced nodes");
563 		return;
564 	}
565 
566 	peer_gossip_probe_nannounces(seeker);
567 }
568 
569 /* Pick a peer, ask it for a few node announcements, to check. */
peer_gossip_probe_nannounces(struct seeker * seeker)570 static void peer_gossip_probe_nannounces(struct seeker *seeker)
571 {
572 	struct peer *peer;
573 
574 	peer = random_seeker(seeker, peer_can_take_scid_query);
575 	set_state(seeker, PROBING_NANNOUNCES, peer,
576 		  "Probing for %zu scids",
577 		  tal_count(seeker->nannounce_scids));
578 	if (!peer)
579 		return;
580 
581 	if (!query_short_channel_ids(seeker->daemon, peer,
582 				     seeker->nannounce_scids,
583 				     seeker->nannounce_query_flags,
584 				     nodeannounce_query_done))
585 		status_failed(STATUS_FAIL_INTERNAL_ERROR,
586 			      "seeker: quering %zu scids is too many?",
587 			      tal_count(seeker->nannounce_scids));
588 }
589 
590 /* They have update with this timestamp: do we want it? */
want_update(struct seeker * seeker,u32 timestamp,const struct half_chan * hc)591 static bool want_update(struct seeker *seeker,
592 			u32 timestamp, const struct half_chan *hc)
593 {
594 	if (!is_halfchan_defined(hc))
595 		return timestamp != 0;
596 
597 	if (timestamp <= hc->bcast.timestamp)
598 		return false;
599 
600 	return !would_ratelimit_cupdate(seeker->daemon->rstate, hc, timestamp);
601 }
602 
603 /* They gave us timestamps.  Do we want updated versions? */
check_timestamps(struct seeker * seeker,struct chan * c,const struct channel_update_timestamps * ts,struct peer * peer)604 static void check_timestamps(struct seeker *seeker,
605 			     struct chan *c,
606 			     const struct channel_update_timestamps *ts,
607 			     struct peer *peer)
608 {
609 	u8 *stale;
610 	u8 query_flag = 0;
611 
612 	/* BOLT #7:
613 	 * * `timestamp_node_id_1` is the timestamp of the `channel_update`
614 	 *    for `node_id_1`, or 0 if there was no `channel_update` from that
615 	 *    node.
616 	 * * `timestamp_node_id_2` is the timestamp of the `channel_update`
617 	 *    for `node_id_2`, or 0 if there was no `channel_update` from that
618 	 *    node.
619 	 */
620 	if (want_update(seeker, ts->timestamp_node_id_1, &c->half[0]))
621 		query_flag |= SCID_QF_UPDATE1;
622 	if (want_update(seeker, ts->timestamp_node_id_2, &c->half[1]))
623 		query_flag |= SCID_QF_UPDATE2;
624 
625 	if (!query_flag)
626 		return;
627 
628 	/* Add in flags if we're already getting it. */
629 	stale = uintmap_get(&seeker->stale_scids, c->scid.u64);
630 	if (!stale) {
631 		stale = talz(seeker, u8);
632 		uintmap_add(&seeker->stale_scids, c->scid.u64, stale);
633 		set_preferred_peer(seeker, peer);
634 	}
635 	*stale |= query_flag;
636 }
637 
process_scid_probe(struct peer * peer,u32 first_blocknum,u32 number_of_blocks,const struct range_query_reply * replies)638 static void process_scid_probe(struct peer *peer,
639 			       u32 first_blocknum, u32 number_of_blocks,
640 			       const struct range_query_reply *replies)
641 {
642 	struct seeker *seeker = peer->daemon->seeker;
643 	bool new_unknown_scids = false;
644 
645 	/* We might have given up on them, then they replied. */
646 	if (seeker->random_peer_softref != peer)
647 		return;
648 
649 	clear_softref(seeker, &seeker->random_peer_softref);
650 
651 	for (size_t i = 0; i < tal_count(replies); i++) {
652 		struct chan *c = get_channel(seeker->daemon->rstate,
653 					     &replies[i].scid);
654 		if (c) {
655 			check_timestamps(seeker, c, &replies[i].ts, peer);
656 			continue;
657 		}
658 
659 		new_unknown_scids |= add_unknown_scid(seeker, &replies[i].scid,
660 						      peer);
661 	}
662 
663 	/* No new unknown scids, or no more to ask?  We give some wiggle
664 	 * room in case blocks came in since we started. */
665 	if (new_unknown_scids
666 	    && next_block_range(seeker, number_of_blocks,
667 				&first_blocknum, &number_of_blocks)) {
668 		/* This must return a peer, since we have the current peer! */
669 		peer = random_seeker(seeker, peer_can_take_range_query);
670 		assert(peer);
671 		selected_peer(seeker, peer);
672 
673 		query_channel_range(seeker->daemon, peer,
674 				    first_blocknum, number_of_blocks,
675 				    QUERY_ADD_TIMESTAMPS,
676 				    process_scid_probe);
677 		return;
678 	}
679 
680 	/* Channel probe finished, try asking for 128 unannounced nodes. */
681 	if (!get_unannounced_nodes(seeker, seeker->daemon->rstate, 128,
682 				   &seeker->nannounce_scids,
683 				   &seeker->nannounce_query_flags)) {
684 		/* No unknown nodes.  Great! */
685 		set_state(seeker, NORMAL, NULL, "No unannounced nodes");
686 		return;
687 	}
688 
689 	peer_gossip_probe_nannounces(seeker);
690 }
691 
692 /* Pick a peer, ask it for a few scids, to check. */
peer_gossip_probe_scids(struct seeker * seeker)693 static void peer_gossip_probe_scids(struct seeker *seeker)
694 {
695 	struct peer *peer;
696 
697 	peer = random_seeker(seeker, peer_can_take_range_query);
698 	set_state(seeker, PROBING_SCIDS, peer,
699 		  "Seeking scids %zu - %zu",
700 		  seeker->scid_probe_start, seeker->scid_probe_end);
701 	if (!peer)
702 		return;
703 
704 	/* This calls process_scid_probe when we get the reply. */
705 	query_channel_range(seeker->daemon, peer,
706 			    seeker->scid_probe_start,
707 			    seeker->scid_probe_end - seeker->scid_probe_start + 1,
708 			    QUERY_ADD_TIMESTAMPS,
709 			    process_scid_probe);
710 }
711 
probe_random_scids(struct seeker * seeker,size_t num_blocks)712 static void probe_random_scids(struct seeker *seeker, size_t num_blocks)
713 {
714 	u32 avail_blocks;
715 
716 	/* Ignore early blocks (unless we're before, which would be weird) */
717 	if (seeker->daemon->current_blockheight
718 	    < chainparams->when_lightning_became_cool)
719 		avail_blocks = seeker->daemon->current_blockheight;
720 	else
721 		avail_blocks = seeker->daemon->current_blockheight
722 			- chainparams->when_lightning_became_cool;
723 
724 	if (avail_blocks < num_blocks) {
725 		seeker->scid_probe_start = 0;
726 		seeker->scid_probe_end = seeker->daemon->current_blockheight;
727 	} else {
728 		seeker->scid_probe_start
729 			= chainparams->when_lightning_became_cool
730 			+ pseudorand(avail_blocks - num_blocks);
731 		seeker->scid_probe_end
732 			= seeker->scid_probe_start + num_blocks - 1;
733 	}
734 
735 	seeker->nannounce_scids = NULL;
736 	peer_gossip_probe_scids(seeker);
737 }
738 
739 /* We usually get a channel per block, so these cover a fair bit of ground */
probe_some_random_scids(struct seeker * seeker)740 static void probe_some_random_scids(struct seeker *seeker)
741 {
742 	return probe_random_scids(seeker, 1024);
743 }
744 
probe_many_random_scids(struct seeker * seeker)745 static void probe_many_random_scids(struct seeker *seeker)
746 {
747 	return probe_random_scids(seeker, 10000);
748 }
749 
check_firstpeer(struct seeker * seeker)750 static void check_firstpeer(struct seeker *seeker)
751 {
752 	struct peer *peer = seeker->random_peer_softref, *p;
753 
754 	/* It might have died, pick another. */
755 	if (!peer) {
756 		peer = random_seeker(seeker, peer_has_gossip_queries);
757 		/* No peer?  Wait for a new one to join. */
758 		if (!peer) {
759 			status_debug("seeker: no peers, waiting");
760 			return;
761 		}
762 
763 		peer_gossip_startup(seeker, peer);
764 		return;
765 	}
766 
767 	/* If no progress, we assume it's finished, and if it hasn't,
768 	 * we'll end up querying backwards in next steps. */
769 	if (peer_made_progress(seeker))
770 		return;
771 
772 	/* Other peers can gossip now. */
773 	status_peer_debug(&peer->id, "seeker: startup peer finished");
774 	clear_softref(seeker, &seeker->random_peer_softref);
775 	list_for_each(&seeker->daemon->peers, p, list) {
776 		if (p == peer)
777 			continue;
778 
779 		normal_gossip_start(seeker, p);
780 	}
781 
782 	/* Ask a random peer for all channels, in case we're missing */
783 	seeker->scid_probe_start = chainparams->when_lightning_became_cool;
784 	seeker->scid_probe_end = seeker->daemon->current_blockheight;
785 	if (seeker->scid_probe_start > seeker->scid_probe_end)
786 		seeker->scid_probe_start = 0;
787 	peer_gossip_probe_scids(seeker);
788 }
789 
check_probe(struct seeker * seeker,void (* restart)(struct seeker * seeker))790 static void check_probe(struct seeker *seeker,
791 			void (*restart)(struct seeker *seeker))
792 {
793 	struct peer *peer = seeker->random_peer_softref;
794 
795 	/* It might have died, pick another. */
796 	if (!peer) {
797 		restart(seeker);
798 		return;
799 	}
800 
801 	/* Is peer making progress with responses? */
802 	if (peer_made_progress(seeker))
803 		return;
804 
805 	status_peer_debug(&peer->id,
806 			  "has only moved gossip %zu->%zu for probe, giving up on it",
807 			  seeker->prev_gossip_count, peer->gossip_counter);
808 	clear_softref(seeker, &seeker->random_peer_softref);
809 	restart(seeker);
810 }
811 
peer_is_not_gossipper(const struct peer * peer)812 static bool peer_is_not_gossipper(const struct peer *peer)
813 {
814 	const struct seeker *seeker = peer->daemon->seeker;
815 
816 	for (size_t i = 0; i < ARRAY_SIZE(seeker->gossiper_softref); i++) {
817 		if (seeker->gossiper_softref[i] == peer)
818 			return false;
819 	}
820 	return true;
821 }
822 
823 /* FIXME: We should look at gossip performance and replace the underperforming
824  * peers in preference. */
maybe_rotate_gossipers(struct seeker * seeker)825 static void maybe_rotate_gossipers(struct seeker *seeker)
826 {
827 	struct peer *peer;
828 	size_t i;
829 
830 	/* If all peers are gossiping, we're done */
831 	peer = random_seeker(seeker, peer_is_not_gossipper);
832 	if (!peer)
833 		return;
834 
835 	/* If we have a slot free, or ~ 1 per hour */
836 	for (i = 0; i < ARRAY_SIZE(seeker->gossiper_softref); i++) {
837 		if (!seeker->gossiper_softref[i]) {
838 			status_peer_debug(&peer->id, "seeker: filling slot %zu",
839 					  i);
840 			goto set_gossiper;
841 		}
842 		if (pseudorand(ARRAY_SIZE(seeker->gossiper_softref) * 60) == 0) {
843 			status_peer_debug(&peer->id,
844 					  "seeker: replacing slot %zu",
845 					  i);
846 			goto clear_and_set_gossiper;
847 		}
848 	}
849 	return;
850 
851 clear_and_set_gossiper:
852 	disable_gossip_stream(seeker, seeker->gossiper_softref[i]);
853 	clear_softref(seeker, &seeker->gossiper_softref[i]);
854 set_gossiper:
855 	set_softref(seeker, &seeker->gossiper_softref[i], peer);
856 	enable_gossip_stream(seeker, peer);
857 }
858 
seek_any_unknown_nodes(struct seeker * seeker)859 static bool seek_any_unknown_nodes(struct seeker *seeker)
860 {
861 	if (!seeker->unknown_nodes)
862 		return false;
863 
864 	seeker->unknown_nodes = false;
865 	probe_many_random_scids(seeker);
866 	return true;
867 }
868 
869 /* Periodic timer to see how our gossip is going. */
seeker_check(struct seeker * seeker)870 static void seeker_check(struct seeker *seeker)
871 {
872 #if DEVELOPER
873 	if (dev_suppress_gossip)
874 		goto out;
875 #endif
876 
877 	/* We don't do anything until we're synced. */
878 	if (seeker->daemon->current_blockheight == 0)
879 		goto out;
880 
881 	switch (seeker->state) {
882 	case STARTING_UP:
883 		check_firstpeer(seeker);
884 		break;
885 	case PROBING_SCIDS:
886 		check_probe(seeker, peer_gossip_probe_scids);
887 		break;
888 	case ASKING_FOR_UNKNOWN_SCIDS:
889 		check_probe(seeker, probe_many_random_scids);
890 		break;
891 	case ASKING_FOR_STALE_SCIDS:
892 		check_probe(seeker, probe_some_random_scids);
893 		break;
894 	case PROBING_NANNOUNCES:
895 		check_probe(seeker, peer_gossip_probe_nannounces);
896 		break;
897 	case NORMAL:
898 		maybe_rotate_gossipers(seeker);
899 		if (!seek_any_unknown_scids(seeker)
900 		    && !seek_any_stale_scids(seeker))
901 			seek_any_unknown_nodes(seeker);
902 		break;
903 	}
904 
905 out:
906 	begin_check_timer(seeker);
907 }
908 
909 /* We get this when we have a new peer. */
seeker_setup_peer_gossip(struct seeker * seeker,struct peer * peer)910 void seeker_setup_peer_gossip(struct seeker *seeker, struct peer *peer)
911 {
912 	/* Can't do anything useful with these peers. */
913 	if (!peer->gossip_queries_feature)
914 		return;
915 
916 #if DEVELOPER
917 	if (dev_suppress_gossip)
918 		return;
919 #endif
920 	/* Don't start gossiping until we're synced. */
921 	if (seeker->daemon->current_blockheight == 0)
922 		return;
923 
924 	switch (seeker->state) {
925 	case STARTING_UP:
926 		if (seeker->random_peer_softref == NULL)
927 			peer_gossip_startup(seeker, peer);
928 		/* Waiting for seeker_check to release us */
929 		return;
930 
931 	/* In these states, we set up peers to stream gossip normally */
932 	case PROBING_SCIDS:
933 	case PROBING_NANNOUNCES:
934 	case NORMAL:
935 	case ASKING_FOR_UNKNOWN_SCIDS:
936 	case ASKING_FOR_STALE_SCIDS:
937 		normal_gossip_start(seeker, peer);
938 		return;
939 	}
940 	abort();
941 }
942 
remove_unknown_scid(struct seeker * seeker,const struct short_channel_id * scid,bool found)943 bool remove_unknown_scid(struct seeker *seeker,
944 			 const struct short_channel_id *scid,
945 			 bool found /*FIXME: use this info!*/)
946 {
947 	return uintmap_del(&seeker->unknown_scids, scid->u64);
948 }
949 
add_unknown_scid(struct seeker * seeker,const struct short_channel_id * scid,struct peer * peer)950 bool add_unknown_scid(struct seeker *seeker,
951 		      const struct short_channel_id *scid,
952 		      struct peer *peer)
953 {
954 	/* Check we're not already getting this one. */
955 	if (!uintmap_add(&seeker->unknown_scids, scid->u64, true))
956 		return false;
957 
958 	set_preferred_peer(seeker, peer);
959 	return true;
960 }
961 
962 /* This peer told us about an update to an unknown channel.  Ask it for a
963  * channel_announcement. */
query_unknown_channel(struct daemon * daemon,struct peer * peer,const struct short_channel_id * id)964 void query_unknown_channel(struct daemon *daemon,
965 			   struct peer *peer,
966 			   const struct short_channel_id *id)
967 {
968 	/* Too many, or duplicate? */
969 	if (!add_unknown_scid(daemon->seeker, id, peer))
970 		return;
971 }
972 
973 /* This peer told us about an unknown node.  Start probing it. */
query_unknown_node(struct seeker * seeker,struct peer * peer)974 void query_unknown_node(struct seeker *seeker, struct peer *peer)
975 {
976 	seeker->unknown_nodes = true;
977 	set_preferred_peer(seeker, peer);
978 }
979