Tor  0.4.6.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 Compute information about our voting schedule as a directory
7  * authority.
8  **/
9 
11 
12 #include "core/or/or.h"
13 #include "app/config/config.h"
15 
17 
18 /* =====
19  * Vote scheduling
20  * ===== */
21 
22 /* Populate and return a new voting_schedule_t that can be used to schedule
23  * voting. The object is allocated on the heap and it's the responsibility of
24  * the caller to free it. Can't fail. */
25 static voting_schedule_t *
26 create_voting_schedule(const or_options_t *options, time_t now, int severity)
27 {
28  int interval, vote_delay, dist_delay;
29  time_t start;
30  time_t end;
31  networkstatus_t *consensus;
32  voting_schedule_t *new_voting_schedule;
33 
34  new_voting_schedule = tor_malloc_zero(sizeof(voting_schedule_t));
35 
36  consensus = networkstatus_get_live_consensus(now);
37 
38  if (consensus) {
39  interval = (int)( consensus->fresh_until - consensus->valid_after );
40  vote_delay = consensus->vote_seconds;
41  dist_delay = consensus->dist_seconds;
42 
43  /* Note down the consensus valid after, so that we detect outdated voting
44  * schedules in case of skewed clocks etc. */
45  new_voting_schedule->live_consensus_valid_after = consensus->valid_after;
46  } else {
47  interval = options->TestingV3AuthInitialVotingInterval;
48  vote_delay = options->TestingV3AuthInitialVoteDelay;
49  dist_delay = options->TestingV3AuthInitialDistDelay;
50  }
51 
52  tor_assert(interval > 0);
53  new_voting_schedule->interval = interval;
54 
55  if (vote_delay + dist_delay > interval/2)
56  vote_delay = dist_delay = interval / 4;
57 
58  start = new_voting_schedule->interval_starts =
61  end = voting_sched_get_start_of_interval_after(start+1, interval,
63 
64  tor_assert(end > start);
65 
66  new_voting_schedule->fetch_missing_signatures = start - (dist_delay/2);
67  new_voting_schedule->voting_ends = start - dist_delay;
68  new_voting_schedule->fetch_missing_votes =
69  start - dist_delay - (vote_delay/2);
70  new_voting_schedule->voting_starts = start - dist_delay - vote_delay;
71 
72  {
73  char tbuf[ISO_TIME_LEN+1];
74  format_iso_time(tbuf, new_voting_schedule->interval_starts);
75  tor_log(severity, LD_DIR,"Choosing expected valid-after time as %s: "
76  "consensus_set=%d, interval=%d",
77  tbuf, consensus?1:0, interval);
78  }
79 
80  return new_voting_schedule;
81 }
82 
83 #define voting_schedule_free(s) \
84  FREE_AND_NULL(voting_schedule_t, voting_schedule_free_, (s))
85 
86 /** Frees a voting_schedule_t. This should be used instead of the generic
87  * tor_free. */
88 static void
89 voting_schedule_free_(voting_schedule_t *voting_schedule_to_free)
90 {
91  if (!voting_schedule_to_free)
92  return;
93  tor_free(voting_schedule_to_free);
94 }
95 
96 voting_schedule_t voting_schedule;
97 
98 /**
99  * Return the current voting schedule, recreating it if necessary.
100  *
101  * Dirauth only.
102  **/
103 static const voting_schedule_t *
105 {
106  time_t now = approx_time();
107  bool need_to_recalculate_voting_schedule = false;
108 
109  /* This is a safe guard in order to make sure that the voting schedule
110  * static object is at least initialized. Using this function with a zeroed
111  * voting schedule can lead to bugs. */
112  if (fast_mem_is_zero((const char *) &voting_schedule,
113  sizeof(voting_schedule))) {
114  need_to_recalculate_voting_schedule = true;
115  goto done; /* no need for next check if we have to recalculate anyway */
116  }
117 
118  /* Also make sure we are not using an outdated voting schedule. If we have a
119  * newer consensus, make sure we recalculate the voting schedule. */
121  if (ns && ns->valid_after != voting_schedule.live_consensus_valid_after) {
122  log_info(LD_DIR, "Voting schedule is outdated: recalculating (%d/%d)",
123  (int) ns->valid_after,
124  (int) voting_schedule.live_consensus_valid_after);
125  need_to_recalculate_voting_schedule = true;
126  }
127 
128  done:
129  if (need_to_recalculate_voting_schedule) {
131  voting_schedule.created_on_demand = 1;
132  }
133 
134  return &voting_schedule;
135 }
136 
137 /** Return the next voting valid-after time.
138  *
139  * Dirauth only. */
140 time_t
142 {
143  return dirauth_get_voting_schedule()->interval_starts;
144 }
145 
146 /**
147  * Return our best idea of what the valid-after time for the _current_
148  * consensus, whether we have one or not.
149  *
150  * Dirauth only.
151  **/
152 time_t
154 {
155  const voting_schedule_t *sched = dirauth_get_voting_schedule();
156  time_t next_start = sched->interval_starts;
157  int interval = sched->interval;
159  return voting_sched_get_start_of_interval_after(next_start - interval - 1,
160  interval,
161  offset);
162 }
163 
164 /** Return the voting interval that we are configured to use.
165  *
166  * Dirauth only. */
167 int
169 {
171 }
172 
173 /** Set voting_schedule to hold the timing for the next vote we should be
174  * doing. All type of tor do that because HS subsystem needs the timing as
175  * well to function properly. */
176 void
178 {
179  voting_schedule_t *new_voting_schedule;
180 
181  /* get the new voting schedule */
182  new_voting_schedule = create_voting_schedule(options, now, LOG_INFO);
183  tor_assert(new_voting_schedule);
184 
185  /* Fill in the global static struct now */
186  memcpy(&voting_schedule, new_voting_schedule, sizeof(voting_schedule));
187  voting_schedule_free(new_voting_schedule);
188 }
dirauth_sched_get_cur_valid_after_time
time_t dirauth_sched_get_cur_valid_after_time(void)
Definition: voting_schedule.c:153
tor_free
#define tor_free(p)
Definition: malloc.h:52
or_options_t::TestingV3AuthVotingStartOffset
int TestingV3AuthVotingStartOffset
Definition: or_options_st.h:741
or_options_t::V3AuthVotingInterval
int V3AuthVotingInterval
Definition: or_options_st.h:709
approx_time
time_t approx_time(void)
Definition: approx_time.c:32
dirauth_sched_get_next_valid_after_time
time_t dirauth_sched_get_next_valid_after_time(void)
Definition: voting_schedule.c:141
tor_assert
#define tor_assert(expr)
Definition: util_bug.h:102
or_options_t::TestingV3AuthInitialVotingInterval
int TestingV3AuthInitialVotingInterval
Definition: or_options_st.h:729
format_iso_time
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:295
fast_mem_is_zero
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
networkstatus_get_live_consensus
networkstatus_t * networkstatus_get_live_consensus(time_t now)
Definition: networkstatus.c:1416
voting_sched_get_start_of_interval_after
time_t voting_sched_get_start_of_interval_after(time_t now, int interval, int offset)
Definition: networkstatus.c:2785
dirauth_sched_recalculate_timing
void dirauth_sched_recalculate_timing(const or_options_t *options, time_t now)
Definition: voting_schedule.c:177
networkstatus.h
Header file for networkstatus.c.
or_options_t::TestingV3AuthInitialVoteDelay
int TestingV3AuthInitialVoteDelay
Definition: or_options_st.h:733
LOG_INFO
#define LOG_INFO
Definition: log.h:45
get_options
const or_options_t * get_options(void)
Definition: config.c:932
networkstatus_t::dist_seconds
int dist_seconds
Definition: networkstatus_st.h:52
dirauth_sched_get_configured_interval
int dirauth_sched_get_configured_interval(void)
Definition: voting_schedule.c:168
voting_schedule.h
Header file for voting_schedule.c.
networkstatus_t::valid_after
time_t valid_after
Definition: networkstatus_st.h:33
or_options_t::TestingV3AuthInitialDistDelay
int TestingV3AuthInitialDistDelay
Definition: or_options_st.h:737
config.h
Header file for config.c.
tor_log
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:590
dirauth_get_voting_schedule
static const voting_schedule_t * dirauth_get_voting_schedule(void)
Definition: voting_schedule.c:104
or_options_t
Definition: or_options_st.h:64
networkstatus_t::fresh_until
time_t fresh_until
Definition: networkstatus_st.h:34
LD_DIR
#define LD_DIR
Definition: log.h:88
networkstatus_st.h
Networkstatus consensus/vote structure.
networkstatus_t
Definition: networkstatus_st.h:26
networkstatus_t::vote_seconds
int vote_seconds
Definition: networkstatus_st.h:49
or.h
Master header file for Tor-specific functionality.
voting_schedule_free_
static void voting_schedule_free_(voting_schedule_t *voting_schedule_to_free)
Definition: voting_schedule.c:89