summaryrefslogtreecommitdiff
path: root/src/or/shared_random_state.c
diff options
context:
space:
mode:
authorGeorge Kadianakis <desnacked@riseup.net>2017-07-17 14:45:14 +0300
committerNick Mathewson <nickm@torproject.org>2017-08-08 20:29:34 -0400
commit2af254096f68f0cefadb5cb06b010d19edd2e6e1 (patch)
tree03a839b50a812bd17edf04f42f2ff8cc6ae6b450 /src/or/shared_random_state.c
parent85c80adf4a75e1f44250379a4806796a26e861e3 (diff)
downloadtor-2af254096f68f0cefadb5cb06b010d19edd2e6e1.tar.gz
tor-2af254096f68f0cefadb5cb06b010d19edd2e6e1.zip
SR: Compute the start time of the current protocol run.
This function will be used to make the HS desc overlap function be independent of absolute times.
Diffstat (limited to 'src/or/shared_random_state.c')
-rw-r--r--src/or/shared_random_state.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/or/shared_random_state.c b/src/or/shared_random_state.c
index 89d2e8d7f6..1fc1440d0d 100644
--- a/src/or/shared_random_state.c
+++ b/src/or/shared_random_state.c
@@ -156,6 +156,26 @@ get_start_time_of_current_round(time_t now)
return curr_start;
}
+/** Return the start time of the current SR protocol run. For example, if the
+ * time is 23/06/2017 23:47:08 and a full SR protocol run is 24 hours, this
+ * function should return 23/06/2017 00:00:00. */
+time_t
+sr_state_get_start_time_of_current_protocol_run(time_t now)
+{
+ int total_rounds = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
+ int voting_interval = get_voting_interval();
+ /* Find the time the current round started. */
+ time_t beginning_of_current_round = get_start_time_of_current_round(now);
+
+ /* Get current SR protocol round */
+ int current_round = (now / voting_interval) % total_rounds;
+
+ /* Get start time by subtracting the time elapsed from the beginning of the
+ protocol run */
+ time_t time_elapsed_since_start_of_run = current_round * voting_interval;
+ return beginning_of_current_round - time_elapsed_since_start_of_run;
+}
+
/* Return the time we should expire the state file created at <b>now</b>.
* We expire the state file in the beginning of the next protocol run. */
STATIC time_t