voting_schedule.c 6.4 KB

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