voting_schedule.c 5.3 KB

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