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