gossipd: de-duplicate code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2021-07-11 16:29:42 +09:30 committed by neil saitug
parent 30c57f9d35
commit 87239dc691
1 changed files with 14 additions and 65 deletions

View File

@ -211,11 +211,15 @@ static void sign_and_send_nannounce(struct daemon *daemon,
tal_hex(tmpctx, err));
}
/* Mutual recursion via timer */
static void update_own_node_announcement_after_startup(struct daemon *daemon);
/* This routine created a `node_announcement` for our node, and hands it to
* the routing.c code like any other `node_announcement`. Such announcements
* are only accepted if there is an announced channel associated with that node
* (to prevent spam), so we only call this once we've announced a channel. */
static void update_own_node_announcement(struct daemon *daemon)
static void update_own_node_announcement(struct daemon *daemon, bool startup)
{
u32 timestamp = gossip_time_now(daemon->rstate).ts.tv_sec;
u8 *nannounce;
@ -235,62 +239,6 @@ static void update_own_node_announcement(struct daemon *daemon)
timestamp,
daemon->rates);
/* If it's the same as the previous, nothing to do. */
if (self && self->bcast.index) {
u32 next;
if (!nannounce_different(daemon->rstate->gs, self, nannounce,
NULL))
return;
/* BOLT #7:
*
* The origin node:
* - MUST set `timestamp` to be greater than that of any
* previous `node_announcement` it has previously created.
*/
/* We do better: never send them within more than 5 minutes. */
next = self->bcast.timestamp
+ GOSSIP_MIN_INTERVAL(daemon->rstate->dev_fast_gossip);
if (timestamp < next) {
status_debug("node_announcement: delaying %u secs",
next - timestamp);
daemon->node_announce_timer
= new_reltimer(&daemon->timers,
daemon,
time_from_sec(next - timestamp),
update_own_node_announcement,
daemon);
return;
}
}
sign_and_send_nannounce(daemon, nannounce, timestamp);
}
static void update_own_node_announce_startup(struct daemon *daemon)
{
u32 timestamp = gossip_time_now(daemon->rstate).ts.tv_sec;
u8 *nannounce;
struct node *self = get_node(daemon->rstate, &daemon->id);
/* Discard existing timer. */
daemon->node_announce_timer = tal_free(daemon->node_announce_timer);
/* If we ever use set-based propagation, ensuring the toggle the lower
* bit in consecutive timestamps makes it more robust. */
if (self && self->bcast.index
&& (timestamp & 1) == (self->bcast.timestamp & 1))
timestamp++;
/* Make unsigned announcement. */
nannounce = create_node_announcement(tmpctx, daemon, NULL,
timestamp,
daemon->rates);
/* If it's the same as the previous, nothing to do. */
if (self && self->bcast.index) {
u32 next;
@ -301,7 +249,7 @@ static void update_own_node_announce_startup(struct daemon *daemon)
return;
/* Missing liquidity_ad, maybe we'll get plugin callback */
if (only_missing_tlv) {
if (startup && only_missing_tlv) {
u32 delay = GOSSIP_NANN_STARTUP_DELAY(daemon->rstate->dev_fast_gossip);
status_debug("node_announcement: delaying"
" %u secs at start", delay);
@ -310,11 +258,10 @@ static void update_own_node_announce_startup(struct daemon *daemon)
= new_reltimer(&daemon->timers,
daemon,
time_from_sec(delay),
update_own_node_announcement,
update_own_node_announcement_after_startup,
daemon);
return;
}
/* BOLT #7:
*
* The origin node:
@ -333,7 +280,7 @@ static void update_own_node_announce_startup(struct daemon *daemon)
= new_reltimer(&daemon->timers,
daemon,
time_from_sec(next - timestamp),
update_own_node_announcement,
update_own_node_announcement_after_startup,
daemon);
return;
}
@ -342,6 +289,11 @@ static void update_own_node_announce_startup(struct daemon *daemon)
sign_and_send_nannounce(daemon, nannounce, timestamp);
}
static void update_own_node_announcement_after_startup(struct daemon *daemon)
{
update_own_node_announcement(daemon, false);
}
/* Should we announce our own node? Called at strategic places. */
void maybe_send_own_node_announce(struct daemon *daemon, bool startup)
{
@ -352,10 +304,7 @@ void maybe_send_own_node_announce(struct daemon *daemon, bool startup)
if (!daemon->rstate->local_channel_announced)
return;
if (startup)
update_own_node_announce_startup(daemon);
else
update_own_node_announcement(daemon);
update_own_node_announcement(daemon, startup);
}
/* Our timer callbacks take a single argument, so we marshall everything