[tor-commits] [tor/master] SR: Compute the start time of the current protocol run.
nickm at torproject.org
nickm at torproject.org
Wed Aug 9 00:36:38 UTC 2017
commit 2af254096f68f0cefadb5cb06b010d19edd2e6e1
Author: George Kadianakis <desnacked at riseup.net>
Date: Mon Jul 17 14:45:14 2017 +0300
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.
---
src/or/shared_random_state.c | 20 ++++++++++++
src/or/shared_random_state.h | 2 ++
src/test/test_shared_random.c | 73 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 95 insertions(+)
diff --git a/src/or/shared_random_state.c b/src/or/shared_random_state.c
index 89d2e8d7f..1fc1440d0 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
diff --git a/src/or/shared_random_state.h b/src/or/shared_random_state.h
index 3526ad47d..ae1c5ac21 100644
--- a/src/or/shared_random_state.h
+++ b/src/or/shared_random_state.h
@@ -121,6 +121,8 @@ int sr_state_is_initialized(void);
void sr_state_save(void);
void sr_state_free(void);
+time_t sr_state_get_start_time_of_current_protocol_run(time_t now);
+
#ifdef SHARED_RANDOM_STATE_PRIVATE
STATIC int disk_state_load_from_disk_impl(const char *fname);
diff --git a/src/test/test_shared_random.c b/src/test/test_shared_random.c
index 026a0f382..3eb47dfbc 100644
--- a/src/test/test_shared_random.c
+++ b/src/test/test_shared_random.c
@@ -189,6 +189,77 @@ test_get_state_valid_until_time(void *arg)
;
}
+/** Test the function that calculates the start time of the current SRV
+ * protocol run. */
+static void
+test_get_start_time_of_current_run(void *arg)
+{
+ int retval;
+ char tbuf[ISO_TIME_LEN + 1];
+ time_t current_time, run_start_time;
+
+ (void) arg;
+
+ {
+ /* Get start time if called at 00:00:01 */
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:01 UTC",
+ ¤t_time);
+ tt_int_op(retval, ==, 0);
+ run_start_time =
+ sr_state_get_start_time_of_current_protocol_run(current_time);
+
+ /* Compare it with the correct result */
+ format_iso_time(tbuf, run_start_time);
+ tt_str_op("2015-04-20 00:00:00", OP_EQ, tbuf);
+ }
+
+ {
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 23:59:59 UTC",
+ ¤t_time);
+ tt_int_op(retval, ==, 0);
+ run_start_time =
+ sr_state_get_start_time_of_current_protocol_run(current_time);
+
+ /* Compare it with the correct result */
+ format_iso_time(tbuf, run_start_time);
+ tt_str_op("2015-04-20 00:00:00", OP_EQ, tbuf);
+ }
+
+ {
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:00:00 UTC",
+ ¤t_time);
+ tt_int_op(retval, ==, 0);
+ run_start_time =
+ sr_state_get_start_time_of_current_protocol_run(current_time);
+
+ /* Compare it with the correct result */
+ format_iso_time(tbuf, run_start_time);
+ tt_str_op("2015-04-20 00:00:00", OP_EQ, tbuf);
+ }
+
+ /* Now let's alter the voting schedule and check the correctness of the
+ * function. Voting interval of 10 seconds, means that an SRV protocol run
+ * takes 10 seconds * 24 rounds = 4 mins */
+ {
+ or_options_t *options = get_options_mutable();
+ options->V3AuthVotingInterval = 10;
+ options->TestingV3AuthInitialVotingInterval = 10;
+ retval = parse_rfc1123_time("Mon, 20 Apr 2015 00:15:32 UTC",
+ ¤t_time);
+
+ tt_int_op(retval, ==, 0);
+ run_start_time =
+ sr_state_get_start_time_of_current_protocol_run(current_time);
+
+ /* Compare it with the correct result */
+ format_iso_time(tbuf, run_start_time);
+ tt_str_op("2015-04-20 00:12:00", OP_EQ, tbuf);
+ }
+
+ done:
+ ;
+}
+
/* Mock function to immediately return our local 'mock_consensus'. */
static networkstatus_t *
mock_networkstatus_get_live_consensus(time_t now)
@@ -1272,6 +1343,8 @@ struct testcase_t sr_tests[] = {
NULL, NULL },
{ "get_next_valid_after_time", test_get_next_valid_after_time, TT_FORK,
NULL, NULL },
+ { "get_start_time_of_current_run", test_get_start_time_of_current_run,
+ TT_FORK, NULL, NULL },
{ "get_state_valid_until_time", test_get_state_valid_until_time, TT_FORK,
NULL, NULL },
{ "vote", test_vote, TT_FORK,
More information about the tor-commits
mailing list