]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
SR: Calculate current SRV phase/run duration.
authorGeorge Kadianakis <desnacked@riseup.net>
Mon, 24 Jul 2017 10:30:04 +0000 (13:30 +0300)
committerNick Mathewson <nickm@torproject.org>
Wed, 9 Aug 2017 00:29:34 +0000 (20:29 -0400)
This is also needed to make the HS desc overlap mode function
independent of absolute hours.

src/or/shared_random_state.c
src/or/shared_random_state.h
src/test/test_shared_random.c

index 1fc1440d0dcbaedc1e0cdcdd341fcac12f4f62bb..7f8094dafd26ec38ac0879d183aeb166d0b0fc3b 100644 (file)
@@ -176,6 +176,22 @@ sr_state_get_start_time_of_current_protocol_run(time_t now)
   return beginning_of_current_round - time_elapsed_since_start_of_run;
 }
 
+/** Return the time (in seconds) it takes to complete a full SR protocol phase
+ *  (e.g. the commit phase). */
+unsigned int
+sr_state_get_phase_duration(void)
+{
+  return SHARED_RANDOM_N_ROUNDS * get_voting_interval();
+}
+
+/** Return the time (in seconds) it takes to complete a full SR protocol run */
+unsigned int
+sr_state_get_protocol_run_duration(void)
+{
+  int total_protocol_rounds = SHARED_RANDOM_N_ROUNDS * SHARED_RANDOM_N_PHASES;
+  return total_protocol_rounds * get_voting_interval();
+}
+
 /* 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
index ae1c5ac219172cfb6e1dfbfba27deff81383840a..03dd5eb37e85905b5bb71605ef20ed964ce737c6 100644 (file)
@@ -122,6 +122,8 @@ void sr_state_save(void);
 void sr_state_free(void);
 
 time_t sr_state_get_start_time_of_current_protocol_run(time_t now);
+unsigned int sr_state_get_phase_duration(void);
+unsigned int sr_state_get_protocol_run_duration(void);
 
 #ifdef SHARED_RANDOM_STATE_PRIVATE
 
index 3eb47dfbc355be0e6b27009d24398b33e64239c9..ea037d417b5adb88a0a22b39fa5ba74131ba6a1a 100644 (file)
@@ -260,6 +260,25 @@ test_get_start_time_of_current_run(void *arg)
   ;
 }
 
+static void
+test_get_sr_protocol_duration(void *arg)
+{
+  (void) arg;
+
+  /* Check that by default an SR phase is 12 hours */
+  tt_int_op(sr_state_get_phase_duration(), ==, 12*60*60);
+  tt_int_op(sr_state_get_protocol_run_duration(), ==, 24*60*60);
+
+  /* Now alter the voting interval and check that the SR phase is 2 mins long
+   * if voting happens every 10 seconds (10*12 seconds = 2 mins) */
+  or_options_t *options = get_options_mutable();
+  options->V3AuthVotingInterval = 10;
+  tt_int_op(sr_state_get_phase_duration(), ==, 2*60);
+  tt_int_op(sr_state_get_protocol_run_duration(), ==, 4*60);
+
+ done: ;
+}
+
 /* Mock function to immediately return our local 'mock_consensus'. */
 static networkstatus_t *
 mock_networkstatus_get_live_consensus(time_t now)
@@ -1345,6 +1364,8 @@ struct testcase_t sr_tests[] = {
     NULL, NULL },
   { "get_start_time_of_current_run", test_get_start_time_of_current_run,
     TT_FORK, NULL, NULL },
+  { "get_sr_protocol_duration", test_get_sr_protocol_duration, TT_FORK,
+    NULL, NULL },
   { "get_state_valid_until_time", test_get_state_valid_until_time, TT_FORK,
     NULL, NULL },
   { "vote", test_vote, TT_FORK,