From 2577ad87d56d3f9dddc516cd6fa5866a028d4c5f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 12 Sep 2019 09:52:12 +0930 Subject: [PATCH] gossipd: use gossip_time_now() everywhere. We've been slack, but it's going to be important for testing ratelimiting. And it currently has a minor memory leak. Signed-off-by: Rusty Russell --- gossipd/gossip_store.c | 2 +- gossipd/gossipd.c | 18 ++++++++++-------- gossipd/routing.c | 7 +++++-- gossipd/routing.h | 2 +- gossipd/test/run-crc32_of_update.c | 3 +++ gossipd/test/run-extended-info.c | 3 +++ 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/gossipd/gossip_store.c b/gossipd/gossip_store.c index f8132cd94..15d60c0cd 100644 --- a/gossipd/gossip_store.c +++ b/gossipd/gossip_store.c @@ -743,7 +743,7 @@ bool gossip_store_load(struct routing_state *rstate, struct gossip_store *gs) goto corrupt; /* If last timestamp is within 24 hours, say we're OK. */ - contents_ok = (last_timestamp >= time_now().ts.tv_sec - 24*3600); + contents_ok = (last_timestamp >= gossip_time_now(rstate).ts.tv_sec - 24*3600); goto out; badmsg: diff --git a/gossipd/gossipd.c b/gossipd/gossipd.c index 24c754368..cbf2acdde 100644 --- a/gossipd/gossipd.c +++ b/gossipd/gossipd.c @@ -405,15 +405,16 @@ static UNNEEDED bool encoding_end_external_type(u8 **encoded, u8 *type, size_t m } /*~ We have different levels of gossipiness, depending on our needs. */ -static u32 gossip_start(enum gossip_level gossip_level) +static u32 gossip_start(const struct routing_state *rstate, + enum gossip_level gossip_level) { switch (gossip_level) { case GOSSIP_HIGH: return 0; case GOSSIP_MEDIUM: - return time_now().ts.tv_sec - 24 * 3600; + return gossip_time_now(rstate).ts.tv_sec - 24 * 3600; case GOSSIP_LOW: - return time_now().ts.tv_sec; + return gossip_time_now(rstate).ts.tv_sec; case GOSSIP_NONE: return UINT32_MAX; } @@ -446,7 +447,8 @@ static void setup_gossip_range(struct peer *peer) /*~ We need to ask for something to start the gossip flowing. */ msg = towire_gossip_timestamp_filter(peer, &peer->daemon->chain_hash, - gossip_start(peer->gossip_level), + gossip_start(peer->daemon->rstate, + peer->gossip_level), UINT32_MAX); queue_peer_msg(peer, take(msg)); } @@ -484,7 +486,7 @@ static u8 *create_node_announcement(const tal_t *ctx, struct daemon *daemon, * (to prevent spam), so we only call this once we've announced a channel. */ static void send_node_announcement(struct daemon *daemon) { - u32 timestamp = time_now().ts.tv_sec; + u32 timestamp = gossip_time_now(daemon->rstate).ts.tv_sec; secp256k1_ecdsa_signature sig; u8 *msg, *nannounce, *err; struct node *self = get_node(daemon->rstate, &daemon->id); @@ -1644,7 +1646,7 @@ static void update_local_channel(struct daemon *daemon, { secp256k1_ecdsa_signature dummy_sig; u8 *update, *msg; - u32 timestamp = time_now().ts.tv_sec; + u32 timestamp = gossip_time_now(daemon->rstate).ts.tv_sec; u8 message_flags, channel_flags; /* So valgrind doesn't complain */ @@ -2289,7 +2291,7 @@ static void gossip_send_keepalive_update(struct daemon *daemon, */ static void gossip_refresh_network(struct daemon *daemon) { - u64 now = time_now().ts.tv_sec; + u64 now = gossip_time_now(daemon->rstate).ts.tv_sec; /* Anything below this highwater mark could be pruned if not refreshed */ s64 highwater = now - daemon->rstate->prune_timeout / 2; struct node *n; @@ -2453,7 +2455,7 @@ static struct io_plan *gossip_init(struct io_conn *conn, &daemon->id, update_channel_interval * 2, &daemon->peers, - dev_gossip_time); + take(dev_gossip_time)); /* Load stored gossip messages */ if (!gossip_store_load(daemon->rstate, daemon->rstate->gs)) diff --git a/gossipd/routing.c b/gossipd/routing.c index 911649266..61deed091 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -190,7 +190,7 @@ struct routing_state *new_routing_state(const tal_t *ctx, const struct node_id *local_id, u32 prune_timeout, struct list_head *peers, - const u32 *dev_gossip_time) + const u32 *dev_gossip_time TAKES) { struct routing_state *rstate = tal(ctx, struct routing_state); rstate->nodes = new_node_map(rstate); @@ -221,6 +221,9 @@ struct routing_state *new_routing_state(const tal_t *ctx, tal_add_destructor(rstate, destroy_routing_state); memleak_add_helper(rstate, memleak_help_routing_tables); + if (taken(dev_gossip_time)) + tal_free(dev_gossip_time); + return rstate; } @@ -1481,7 +1484,7 @@ bool routing_add_channel_announcement(struct routing_state *rstate, uc = tal(rstate, struct unupdated_channel); uc->channel_announce = tal_dup_arr(uc, u8, msg, tal_count(msg), 0); - uc->added = time_now(); + uc->added = gossip_time_now(rstate); uc->index = index; uc->sat = sat; uc->scid = scid; diff --git a/gossipd/routing.h b/gossipd/routing.h index 1ba810c19..29d6a888d 100644 --- a/gossipd/routing.h +++ b/gossipd/routing.h @@ -273,7 +273,7 @@ struct routing_state *new_routing_state(const tal_t *ctx, const struct node_id *local_id, u32 prune_timeout, struct list_head *peers, - const u32 *dev_gossip_time); + const u32 *dev_gossip_time TAKES); /** * Add a new bidirectional channel from id1 to id2 with the given diff --git a/gossipd/test/run-crc32_of_update.c b/gossipd/test/run-crc32_of_update.c index d6147f97d..900cbdb98 100644 --- a/gossipd/test/run-crc32_of_update.c +++ b/gossipd/test/run-crc32_of_update.c @@ -157,6 +157,9 @@ bool gossip_store_load(struct routing_state *rstate UNNEEDED, struct gossip_stor /* Generated stub for gossip_store_readonly_fd */ int gossip_store_readonly_fd(struct gossip_store *gs UNNEEDED) { fprintf(stderr, "gossip_store_readonly_fd called!\n"); abort(); } +/* Generated stub for gossip_time_now */ +struct timeabs gossip_time_now(const struct routing_state *rstate UNNEEDED) +{ fprintf(stderr, "gossip_time_now called!\n"); abort(); } /* Generated stub for got_pong */ const char *got_pong(const u8 *pong UNNEEDED, size_t *num_pings_outstanding UNNEEDED) { fprintf(stderr, "got_pong called!\n"); abort(); } diff --git a/gossipd/test/run-extended-info.c b/gossipd/test/run-extended-info.c index aa20cecb0..2542ddb22 100644 --- a/gossipd/test/run-extended-info.c +++ b/gossipd/test/run-extended-info.c @@ -180,6 +180,9 @@ bool gossip_store_load(struct routing_state *rstate UNNEEDED, struct gossip_stor /* Generated stub for gossip_store_readonly_fd */ int gossip_store_readonly_fd(struct gossip_store *gs UNNEEDED) { fprintf(stderr, "gossip_store_readonly_fd called!\n"); abort(); } +/* Generated stub for gossip_time_now */ +struct timeabs gossip_time_now(const struct routing_state *rstate UNNEEDED) +{ fprintf(stderr, "gossip_time_now called!\n"); abort(); } /* Generated stub for got_pong */ const char *got_pong(const u8 *pong UNNEEDED, size_t *num_pings_outstanding UNNEEDED) { fprintf(stderr, "got_pong called!\n"); abort(); }