]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Move one voting schedule fn into networkstatus.c
authorNick Mathewson <nickm@torproject.org>
Mon, 24 Feb 2020 15:04:01 +0000 (10:04 -0500)
committerNick Mathewson <nickm@torproject.org>
Mon, 24 Feb 2020 15:04:01 +0000 (10:04 -0500)
The 'voting_schdule_get_start_of_next_interval' function isn't
actually dirauth-specific.

src/feature/dirauth/voting_schedule.c
src/feature/dirauth/voting_schedule.h
src/feature/nodelist/networkstatus.c
src/feature/nodelist/networkstatus.h
src/test/test_voting_schedule.c

index 712addfa75444d10c84e0456868f4bacdaf19945..d797a934058d84871e963088304a608da6b95253 100644 (file)
  * Vote scheduling
  * ===== */
 
-/** Return the start of the next interval of size <b>interval</b> (in
- * seconds) after <b>now</b>, plus <b>offset</b>. Midnight always
- * starts a fresh interval, and if the last interval of a day would be
- * truncated to less than half its size, it is rolled into the
- * previous interval. */
-time_t
-voting_schedule_get_start_of_next_interval(time_t now, int interval,
-                                           int offset)
-{
-  struct tm tm;
-  time_t midnight_today=0;
-  time_t midnight_tomorrow;
-  time_t next;
-
-  tor_gmtime_r(&now, &tm);
-  tm.tm_hour = 0;
-  tm.tm_min = 0;
-  tm.tm_sec = 0;
-
-  if (tor_timegm(&tm, &midnight_today) < 0) {
-    // LCOV_EXCL_START
-    log_warn(LD_BUG, "Ran into an invalid time when trying to find midnight.");
-    // LCOV_EXCL_STOP
-  }
-  midnight_tomorrow = midnight_today + (24*60*60);
-
-  next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
-
-  /* Intervals never cross midnight. */
-  if (next > midnight_tomorrow)
-    next = midnight_tomorrow;
-
-  /* If the interval would only last half as long as it's supposed to, then
-   * skip over to the next day. */
-  if (next + interval/2 > midnight_tomorrow)
-    next = midnight_tomorrow;
-
-  next += offset;
-  if (next - interval > now)
-    next -= interval;
-
-  return next;
-}
-
 /* Populate and return a new voting_schedule_t that can be used to schedule
  * voting. The object is allocated on the heap and it's the responsibility of
  * the caller to free it. Can't fail. */
@@ -190,4 +146,3 @@ voting_schedule_recalculate_timing(const or_options_t *options, time_t now)
   memcpy(&voting_schedule, new_voting_schedule, sizeof(voting_schedule));
   voting_schedule_free(new_voting_schedule);
 }
-
index e4c62100873a93075b627729a6696a475052d783..a3bb9cfd1dbeb16651162f537e845c3ac97ed315 100644 (file)
@@ -56,9 +56,6 @@ extern voting_schedule_t voting_schedule;
 void voting_schedule_recalculate_timing(const or_options_t *options,
                                         time_t now);
 
-time_t voting_schedule_get_start_of_next_interval(time_t now,
-                                                  int interval,
-                                                  int offset);
 time_t voting_schedule_get_next_valid_after_time(void);
 
 #endif /* !defined(TOR_VOTING_SCHEDULE_H) */
index d7864fa38a1c6453eaf3b3b91ae67201f8d16a97..6755de1a8199e3b5818bebcf28736277405260be 100644 (file)
@@ -2765,3 +2765,47 @@ networkstatus_free_all(void)
     }
   }
 }
+
+/** Return the start of the next interval of size <b>interval</b> (in
+ * seconds) after <b>now</b>, plus <b>offset</b>. Midnight always
+ * starts a fresh interval, and if the last interval of a day would be
+ * truncated to less than half its size, it is rolled into the
+ * previous interval. */
+time_t
+voting_schedule_get_start_of_next_interval(time_t now, int interval,
+                                           int offset)
+{
+  struct tm tm;
+  time_t midnight_today=0;
+  time_t midnight_tomorrow;
+  time_t next;
+
+  tor_gmtime_r(&now, &tm);
+  tm.tm_hour = 0;
+  tm.tm_min = 0;
+  tm.tm_sec = 0;
+
+  if (tor_timegm(&tm, &midnight_today) < 0) {
+    // LCOV_EXCL_START
+    log_warn(LD_BUG, "Ran into an invalid time when trying to find midnight.");
+    // LCOV_EXCL_STOP
+  }
+  midnight_tomorrow = midnight_today + (24*60*60);
+
+  next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
+
+  /* Intervals never cross midnight. */
+  if (next > midnight_tomorrow)
+    next = midnight_tomorrow;
+
+  /* If the interval would only last half as long as it's supposed to, then
+   * skip over to the next day. */
+  if (next + interval/2 > midnight_tomorrow)
+    next = midnight_tomorrow;
+
+  next += offset;
+  if (next - interval > now)
+    next -= interval;
+
+  return next;
+}
index 5e8c8a9e57b38b923e870b95c0105a5461506dfa..c376bdd37143baeb7cc4ee2ee7fe488b4c93b54e 100644 (file)
@@ -153,6 +153,9 @@ void vote_routerstatus_free_(vote_routerstatus_t *rs);
 void set_routerstatus_from_routerinfo(routerstatus_t *rs,
                                       const node_t *node,
                                       const routerinfo_t *ri);
+time_t voting_schedule_get_start_of_next_interval(time_t now,
+                                                  int interval,
+                                                  int offset);
 
 #ifdef NETWORKSTATUS_PRIVATE
 #ifdef TOR_UNIT_TESTS
index 843965568dd40929cf92387b6a2ccf841c9a1b67..51e2b9b1847d7150d37a6cf2499cb3b9d6088b04 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "core/or/or.h"
 #include "feature/dirauth/voting_schedule.h"
+#include "feature/nodelist/networkstatus.h"
 
 #include "test/test.h"
 
@@ -61,4 +62,3 @@ struct testcase_t voting_schedule_tests[] = {
   VS(interval_start, 0),
   END_OF_TESTCASES
 };
-