voting_schedule.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. } else {
  71. interval = options->TestingV3AuthInitialVotingInterval;
  72. vote_delay = options->TestingV3AuthInitialVoteDelay;
  73. dist_delay = options->TestingV3AuthInitialDistDelay;
  74. }
  75. tor_assert(interval > 0);
  76. if (vote_delay + dist_delay > interval/2)
  77. vote_delay = dist_delay = interval / 4;
  78. start = new_voting_schedule->interval_starts =
  79. voting_schedule_get_start_of_next_interval(now,interval,
  80. options->TestingV3AuthVotingStartOffset);
  81. end = voting_schedule_get_start_of_next_interval(start+1, interval,
  82. options->TestingV3AuthVotingStartOffset);
  83. tor_assert(end > start);
  84. new_voting_schedule->fetch_missing_signatures = start - (dist_delay/2);
  85. new_voting_schedule->voting_ends = start - dist_delay;
  86. new_voting_schedule->fetch_missing_votes =
  87. start - dist_delay - (vote_delay/2);
  88. new_voting_schedule->voting_starts = start - dist_delay - vote_delay;
  89. {
  90. char tbuf[ISO_TIME_LEN+1];
  91. format_iso_time(tbuf, new_voting_schedule->interval_starts);
  92. tor_log(severity, LD_DIR,"Choosing expected valid-after time as %s: "
  93. "consensus_set=%d, interval=%d",
  94. tbuf, consensus?1:0, interval);
  95. }
  96. return new_voting_schedule;
  97. }
  98. #define voting_schedule_free(s) \
  99. FREE_AND_NULL(voting_schedule_t, voting_schedule_free_, (s))
  100. /** Frees a voting_schedule_t. This should be used instead of the generic
  101. * tor_free. */
  102. static void
  103. voting_schedule_free_(voting_schedule_t *voting_schedule_to_free)
  104. {
  105. if (!voting_schedule_to_free)
  106. return;
  107. tor_free(voting_schedule_to_free);
  108. }
  109. voting_schedule_t voting_schedule;
  110. /* Using the time <b>now</b>, return the next voting valid-after time. */
  111. time_t
  112. voting_schedule_get_next_valid_after_time(void)
  113. {
  114. /* This is a safe guard in order to make sure that the voting schedule
  115. * static object is at least initialized. Using this function with a zeroed
  116. * voting schedule can lead to bugs. */
  117. if (tor_mem_is_zero((const char *) &voting_schedule,
  118. sizeof(voting_schedule))) {
  119. voting_schedule_recalculate_timing(get_options(), time(NULL));
  120. voting_schedule.created_on_demand = 1;
  121. }
  122. return voting_schedule.interval_starts;
  123. }
  124. /** Set voting_schedule to hold the timing for the next vote we should be
  125. * doing. All type of tor do that because HS subsystem needs the timing as
  126. * well to function properly. */
  127. void
  128. voting_schedule_recalculate_timing(const or_options_t *options, time_t now)
  129. {
  130. voting_schedule_t *new_voting_schedule;
  131. /* get the new voting schedule */
  132. new_voting_schedule = get_voting_schedule(options, now, LOG_INFO);
  133. tor_assert(new_voting_schedule);
  134. /* Fill in the global static struct now */
  135. memcpy(&voting_schedule, new_voting_schedule, sizeof(voting_schedule));
  136. voting_schedule_free(new_voting_schedule);
  137. }