voting_schedule.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Copyright (c) 2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file voting_schedule.c
  5. * \brief This file contains functions that are from the directory authority
  6. * subsystem related to voting specifically but used by many part of
  7. * tor. The full feature is built as part of the dirauth module.
  8. **/
  9. #define VOTING_SCHEDULE_PRIVATE
  10. #include "voting_schedule.h"
  11. #include "or.h"
  12. #include "config.h"
  13. #include "networkstatus.h"
  14. /* =====
  15. * Vote scheduling
  16. * ===== */
  17. /** Return the start of the next interval of size <b>interval</b> (in
  18. * seconds) after <b>now</b>, plus <b>offset</b>. Midnight always
  19. * starts a fresh interval, and if the last interval of a day would be
  20. * truncated to less than half its size, it is rolled into the
  21. * previous interval. */
  22. time_t
  23. voting_schedule_get_start_of_next_interval(time_t now, int interval,
  24. int offset)
  25. {
  26. struct tm tm;
  27. time_t midnight_today=0;
  28. time_t midnight_tomorrow;
  29. time_t next;
  30. tor_gmtime_r(&now, &tm);
  31. tm.tm_hour = 0;
  32. tm.tm_min = 0;
  33. tm.tm_sec = 0;
  34. if (tor_timegm(&tm, &midnight_today) < 0) {
  35. // LCOV_EXCL_START
  36. log_warn(LD_BUG, "Ran into an invalid time when trying to find midnight.");
  37. // LCOV_EXCL_STOP
  38. }
  39. midnight_tomorrow = midnight_today + (24*60*60);
  40. next = midnight_today + ((now-midnight_today)/interval + 1)*interval;
  41. /* Intervals never cross midnight. */
  42. if (next > midnight_tomorrow)
  43. next = midnight_tomorrow;
  44. /* If the interval would only last half as long as it's supposed to, then
  45. * skip over to the next day. */
  46. if (next + interval/2 > midnight_tomorrow)
  47. next = midnight_tomorrow;
  48. next += offset;
  49. if (next - interval > now)
  50. next -= interval;
  51. return next;
  52. }
  53. /* Populate and return a new voting_schedule_t that can be used to schedule
  54. * voting. The object is allocated on the heap and it's the responsibility of
  55. * the caller to free it. Can't fail. */
  56. static voting_schedule_t *
  57. get_voting_schedule(const or_options_t *options, time_t now, int severity)
  58. {
  59. int interval, vote_delay, dist_delay;
  60. time_t start;
  61. time_t end;
  62. networkstatus_t *consensus;
  63. voting_schedule_t *new_voting_schedule;
  64. new_voting_schedule = tor_malloc_zero(sizeof(voting_schedule_t));
  65. consensus = networkstatus_get_live_consensus(now);
  66. if (consensus) {
  67. interval = (int)( consensus->fresh_until - consensus->valid_after );
  68. vote_delay = consensus->vote_seconds;
  69. dist_delay = consensus->dist_seconds;
  70. /* Note down the consensus valid after, so that we detect outdated voting
  71. * schedules in case of skewed clocks etc. */
  72. new_voting_schedule->live_consensus_valid_after = consensus->valid_after;
  73. } else {
  74. interval = options->TestingV3AuthInitialVotingInterval;
  75. vote_delay = options->TestingV3AuthInitialVoteDelay;
  76. dist_delay = options->TestingV3AuthInitialDistDelay;
  77. }
  78. tor_assert(interval > 0);
  79. if (vote_delay + dist_delay > interval/2)
  80. vote_delay = dist_delay = interval / 4;
  81. start = new_voting_schedule->interval_starts =
  82. voting_schedule_get_start_of_next_interval(now,interval,
  83. options->TestingV3AuthVotingStartOffset);
  84. end = voting_schedule_get_start_of_next_interval(start+1, interval,
  85. options->TestingV3AuthVotingStartOffset);
  86. tor_assert(end > start);
  87. new_voting_schedule->fetch_missing_signatures = start - (dist_delay/2);
  88. new_voting_schedule->voting_ends = start - dist_delay;
  89. new_voting_schedule->fetch_missing_votes =
  90. start - dist_delay - (vote_delay/2);
  91. new_voting_schedule->voting_starts = start - dist_delay - vote_delay;
  92. {
  93. char tbuf[ISO_TIME_LEN+1];
  94. format_iso_time(tbuf, new_voting_schedule->interval_starts);
  95. tor_log(severity, LD_DIR,"Choosing expected valid-after time as %s: "
  96. "consensus_set=%d, interval=%d",
  97. tbuf, consensus?1:0, interval);
  98. }
  99. return new_voting_schedule;
  100. }
  101. #define voting_schedule_free(s) \
  102. FREE_AND_NULL(voting_schedule_t, voting_schedule_free_, (s))
  103. /** Frees a voting_schedule_t. This should be used instead of the generic
  104. * tor_free. */
  105. static void
  106. voting_schedule_free_(voting_schedule_t *voting_schedule_to_free)
  107. {
  108. if (!voting_schedule_to_free)
  109. return;
  110. tor_free(voting_schedule_to_free);
  111. }
  112. voting_schedule_t voting_schedule;
  113. /* Using the time <b>now</b>, return the next voting valid-after time. */
  114. time_t
  115. voting_schedule_get_next_valid_after_time(void)
  116. {
  117. time_t now = approx_time();
  118. bool need_to_recalculate_voting_schedule = false;
  119. /* This is a safe guard in order to make sure that the voting schedule
  120. * static object is at least initialized. Using this function with a zeroed
  121. * voting schedule can lead to bugs. */
  122. if (tor_mem_is_zero((const char *) &voting_schedule,
  123. sizeof(voting_schedule))) {
  124. need_to_recalculate_voting_schedule = true;
  125. goto done; /* no need for next check if we have to recalculate anyway */
  126. }
  127. /* Also make sure we are not using an outdated voting schedule. If we have a
  128. * newer consensus, make sure we recalculate the voting schedule. */
  129. const networkstatus_t *ns = networkstatus_get_live_consensus(now);
  130. if (ns && ns->valid_after != voting_schedule.live_consensus_valid_after) {
  131. log_info(LD_DIR, "Voting schedule is outdated: recalculating (%d/%d)",
  132. (int) ns->valid_after,
  133. (int) voting_schedule.live_consensus_valid_after);
  134. need_to_recalculate_voting_schedule = true;
  135. }
  136. done:
  137. if (need_to_recalculate_voting_schedule) {
  138. voting_schedule_recalculate_timing(get_options(), now);
  139. voting_schedule.created_on_demand = 1;
  140. }
  141. return voting_schedule.interval_starts;
  142. }
  143. /** Set voting_schedule to hold the timing for the next vote we should be
  144. * doing. All type of tor do that because HS subsystem needs the timing as
  145. * well to function properly. */
  146. void
  147. voting_schedule_recalculate_timing(const or_options_t *options, time_t now)
  148. {
  149. voting_schedule_t *new_voting_schedule;
  150. /* get the new voting schedule */
  151. new_voting_schedule = get_voting_schedule(options, now, LOG_INFO);
  152. tor_assert(new_voting_schedule);
  153. /* Fill in the global static struct now */
  154. memcpy(&voting_schedule, new_voting_schedule, sizeof(voting_schedule));
  155. voting_schedule_free(new_voting_schedule);
  156. }