shared_random_state.h 4.8 KB

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