Skip to content

Commit

Permalink
gossipd: de-duplicate code.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and niftynei committed Jul 15, 2021
1 parent 2629d64 commit e4db4dc
Showing 1 changed file with 14 additions and 65 deletions.
79 changes: 14 additions & 65 deletions gossipd/gossip_generation.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,66 +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)
{
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;

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)
static void update_own_node_announcement(struct daemon *daemon, bool startup)
{
u32 timestamp = gossip_time_now(daemon->rstate).ts.tv_sec;
u8 *nannounce;
Expand All @@ -290,7 +239,6 @@ static void update_own_node_announce_startup(struct daemon *daemon)
timestamp,
daemon->rates);


/* If it's the same as the previous, nothing to do. */
if (self && self->bcast.index) {
u32 next;
Expand All @@ -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);
Expand All @@ -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:
Expand All @@ -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;
}
Expand All @@ -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)
{
Expand All @@ -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
Expand Down

0 comments on commit e4db4dc

Please sign in to comment.