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