shared_random_state.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (c) 2016-2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #ifndef TOR_SHARED_RANDOM_STATE_H
  4. #define TOR_SHARED_RANDOM_STATE_H
  5. #include "shared_random.h"
  6. /* Action that can be performed on the state for any objects. */
  7. typedef enum {
  8. SR_STATE_ACTION_GET = 1,
  9. SR_STATE_ACTION_PUT = 2,
  10. SR_STATE_ACTION_DEL = 3,
  11. SR_STATE_ACTION_DEL_ALL = 4,
  12. SR_STATE_ACTION_SAVE = 5,
  13. } sr_state_action_t;
  14. /* Object in the state that can be queried through the state API. */
  15. typedef enum {
  16. /* Will return a single commit using an authority identity key. */
  17. SR_STATE_OBJ_COMMIT,
  18. /* Returns the entire list of commits from the state. */
  19. SR_STATE_OBJ_COMMITS,
  20. /* Return the current SRV object pointer. */
  21. SR_STATE_OBJ_CURSRV,
  22. /* Return the previous SRV object pointer. */
  23. SR_STATE_OBJ_PREVSRV,
  24. /* Return the phase. */
  25. SR_STATE_OBJ_PHASE,
  26. /* Get or Put the valid after time. */
  27. SR_STATE_OBJ_VALID_AFTER,
  28. } sr_state_object_t;
  29. /* State of the protocol. It's also saved on disk in fname. This data
  30. * structure MUST be synchronized at all time with the one on disk. */
  31. typedef struct sr_state_t {
  32. /* Filename of the state file on disk. */
  33. char *fname;
  34. /* Version of the protocol. */
  35. uint32_t version;
  36. /* The valid-after of the voting period we have prepared the state for. */
  37. time_t valid_after;
  38. /* Until when is this state valid? */
  39. time_t valid_until;
  40. /* Protocol phase. */
  41. sr_phase_t phase;
  42. /* Number of runs completed. */
  43. uint64_t n_protocol_runs;
  44. /* The number of commitment rounds we've performed in this protocol run. */
  45. unsigned int n_commit_rounds;
  46. /* The number of reveal rounds we've performed in this protocol run. */
  47. unsigned int n_reveal_rounds;
  48. /* A map of all the received commitments for this protocol run. This is
  49. * indexed by authority RSA identity digest. */
  50. digestmap_t *commits;
  51. /* Current and previous shared random value. */
  52. sr_srv_t *previous_srv;
  53. sr_srv_t *current_srv;
  54. /* Indicate if the state contains an SRV that was _just_ generated. This is
  55. * used during voting so that we know whether to use the super majority rule
  56. * or not when deciding on keeping it for the consensus. It is _always_ set
  57. * to 0 post consensus.
  58. *
  59. * EDGE CASE: if an authority computes a new SRV then immediately reboots
  60. * and, once back up, votes for the current round, it won't know if the
  61. * SRV is fresh or not ultimately making it _NOT_ use the super majority
  62. * when deciding to put or not the SRV in the consensus. This is for now
  63. * an acceptable very rare edge case. */
  64. unsigned int is_srv_fresh:1;
  65. } sr_state_t;
  66. /* Persistent state of the protocol, as saved to disk. */
  67. typedef struct sr_disk_state_t {
  68. uint32_t magic_;
  69. /* Version of the protocol. */
  70. int Version;
  71. /* Version of our running tor. */
  72. char *TorVersion;
  73. /* Creation time of this state */
  74. time_t ValidAfter;
  75. /* State valid until? */
  76. time_t ValidUntil;
  77. /* All commits seen that are valid. */
  78. config_line_t *Commit;
  79. /* Previous and current shared random value. */
  80. config_line_t *SharedRandValues;
  81. /* Extra Lines for configuration we might not know. */
  82. config_line_t *ExtraLines;
  83. } sr_disk_state_t;
  84. /* API */
  85. /* Public methods: */
  86. void sr_state_update(time_t valid_after);
  87. /* Private methods (only used by shared-random.c): */
  88. void sr_state_set_valid_after(time_t valid_after);
  89. sr_phase_t sr_state_get_phase(void);
  90. const sr_srv_t *sr_state_get_previous_srv(void);
  91. const sr_srv_t *sr_state_get_current_srv(void);
  92. void sr_state_set_previous_srv(const sr_srv_t *srv);
  93. void sr_state_set_current_srv(const sr_srv_t *srv);
  94. void sr_state_clean_srvs(void);
  95. digestmap_t *sr_state_get_commits(void);
  96. sr_commit_t *sr_state_get_commit(const char *rsa_fpr);
  97. void sr_state_add_commit(sr_commit_t *commit);
  98. void sr_state_delete_commits(void);
  99. void sr_state_copy_reveal_info(sr_commit_t *saved_commit,
  100. const sr_commit_t *commit);
  101. unsigned int sr_state_srv_is_fresh(void);
  102. void sr_state_set_fresh_srv(void);
  103. void sr_state_unset_fresh_srv(void);
  104. int sr_state_init(int save_to_disk, int read_from_disk);
  105. int sr_state_is_initialized(void);
  106. void sr_state_save(void);
  107. void sr_state_free_all(void);
  108. time_t sr_state_get_start_time_of_current_protocol_run(time_t now);
  109. unsigned int sr_state_get_phase_duration(void);
  110. unsigned int sr_state_get_protocol_run_duration(void);
  111. #ifdef SHARED_RANDOM_STATE_PRIVATE
  112. STATIC int disk_state_load_from_disk_impl(const char *fname);
  113. STATIC sr_phase_t get_sr_protocol_phase(time_t valid_after);
  114. STATIC time_t get_start_time_of_current_round(void);
  115. STATIC time_t get_state_valid_until_time(time_t now);
  116. STATIC const char *get_phase_str(sr_phase_t phase);
  117. STATIC void reset_state_for_new_protocol_run(time_t valid_after);
  118. STATIC void new_protocol_run(time_t valid_after);
  119. STATIC void state_rotate_srv(void);
  120. STATIC int is_phase_transition(sr_phase_t next_phase);
  121. #endif /* defined(SHARED_RANDOM_STATE_PRIVATE) */
  122. #ifdef TOR_UNIT_TESTS
  123. STATIC void set_sr_phase(sr_phase_t phase);
  124. STATIC sr_state_t *get_sr_state(void);
  125. #endif /* defined(TOR_UNIT_TESTS) */
  126. #endif /* !defined(TOR_SHARED_RANDOM_STATE_H) */