dirvote: Move SR commit parsing into dirauth module

When parsing a vote in routerparse.c, only dirauth extract the commits from
the vote so move all this code into dirvote.c so we can make it specific to
the dirauth module.

If the dirauth module is disabled, the commit parsing does nothing.

Signed-off-by: David Goulet <dgoulet@torproject.org>
This commit is contained in:
David Goulet 2018-04-25 10:42:56 -04:00
parent d7e4706f22
commit 2963e65c30
3 changed files with 81 additions and 61 deletions

View File

@ -13,6 +13,7 @@
#include "dirvote_common.h"
#include "microdesc.h"
#include "networkstatus.h"
#include "parsecommon.h"
#include "policies.h"
#include "protover.h"
#include "rephist.h"
@ -3828,3 +3829,73 @@ dirvote_format_all_microdesc_vote_lines(const routerinfo_t *ri, time_t now,
return result;
}
/** Parse and extract all SR commits from <b>tokens</b> and place them in
* <b>ns</b>. */
static void
extract_shared_random_commits(networkstatus_t *ns, smartlist_t *tokens)
{
smartlist_t *chunks = NULL;
tor_assert(ns);
tor_assert(tokens);
/* Commits are only present in a vote. */
tor_assert(ns->type == NS_TYPE_VOTE);
ns->sr_info.commits = smartlist_new();
smartlist_t *commits = find_all_by_keyword(tokens, K_COMMIT);
/* It's normal that a vote might contain no commits even if it participates
* in the SR protocol. Don't treat it as an error. */
if (commits == NULL) {
goto end;
}
/* Parse the commit. We do NO validation of number of arguments or ordering
* for forward compatibility, it's the parse commit job to inform us if it's
* supported or not. */
chunks = smartlist_new();
SMARTLIST_FOREACH_BEGIN(commits, directory_token_t *, tok) {
/* Extract all arguments and put them in the chunks list. */
for (int i = 0; i < tok->n_args; i++) {
smartlist_add(chunks, tok->args[i]);
}
sr_commit_t *commit = sr_parse_commit(chunks);
smartlist_clear(chunks);
if (commit == NULL) {
/* Get voter identity so we can warn that this dirauth vote contains
* commit we can't parse. */
networkstatus_voter_info_t *voter = smartlist_get(ns->voters, 0);
tor_assert(voter);
log_warn(LD_DIR, "SR: Unable to parse commit %s from vote of voter %s.",
escaped(tok->object_body),
hex_str(voter->identity_digest,
sizeof(voter->identity_digest)));
/* Commitment couldn't be parsed. Continue onto the next commit because
* this one could be unsupported for instance. */
continue;
}
/* Add newly created commit object to the vote. */
smartlist_add(ns->sr_info.commits, commit);
} SMARTLIST_FOREACH_END(tok);
end:
smartlist_free(chunks);
smartlist_free(commits);
}
/* Using the given directory tokens in tokens, parse the shared random commits
* and put them in the given vote document ns.
*
* This also sets the SR participation flag if present in the vote. */
void
dirvote_parse_sr_commits(networkstatus_t *ns, smartlist_t *tokens)
{
/* Does this authority participates in the SR protocol? */
directory_token_t *tok = find_opt_by_keyword(tokens, K_SR_FLAG);
if (tok) {
ns->sr_info.participate = 1;
/* Get the SR commitments and reveals from the vote. */
extract_shared_random_commits(ns, tokens);
}
}

View File

@ -99,6 +99,8 @@
void dirvote_act(const or_options_t *options, time_t now);
void dirvote_free_all(void);
void dirvote_parse_sr_commits(networkstatus_t *ns, smartlist_t *tokens);
#else /* HAVE_MODULE_DIRAUTH */
static inline void
@ -113,6 +115,13 @@ dirvote_free_all(void)
{
}
static inline void
dirvote_parse_sr_commits(networkstatus_t *ns, smartlist_t *tokens)
{
(void) ns;
(void) tokens;
}
#endif /* HAVE_MODULE_DIRAUTH */
void dirvote_recalculate_timing(const or_options_t *options, time_t now);

View File

@ -3285,60 +3285,6 @@ networkstatus_verify_bw_weights(networkstatus_t *ns, int consensus_method)
return valid;
}
/** Parse and extract all SR commits from <b>tokens</b> and place them in
* <b>ns</b>. */
static void
extract_shared_random_commits(networkstatus_t *ns, smartlist_t *tokens)
{
smartlist_t *chunks = NULL;
tor_assert(ns);
tor_assert(tokens);
/* Commits are only present in a vote. */
tor_assert(ns->type == NS_TYPE_VOTE);
ns->sr_info.commits = smartlist_new();
smartlist_t *commits = find_all_by_keyword(tokens, K_COMMIT);
/* It's normal that a vote might contain no commits even if it participates
* in the SR protocol. Don't treat it as an error. */
if (commits == NULL) {
goto end;
}
/* Parse the commit. We do NO validation of number of arguments or ordering
* for forward compatibility, it's the parse commit job to inform us if it's
* supported or not. */
chunks = smartlist_new();
SMARTLIST_FOREACH_BEGIN(commits, directory_token_t *, tok) {
/* Extract all arguments and put them in the chunks list. */
for (int i = 0; i < tok->n_args; i++) {
smartlist_add(chunks, tok->args[i]);
}
sr_commit_t *commit = sr_parse_commit(chunks);
smartlist_clear(chunks);
if (commit == NULL) {
/* Get voter identity so we can warn that this dirauth vote contains
* commit we can't parse. */
networkstatus_voter_info_t *voter = smartlist_get(ns->voters, 0);
tor_assert(voter);
log_warn(LD_DIR, "SR: Unable to parse commit %s from vote of voter %s.",
escaped(tok->object_body),
hex_str(voter->identity_digest,
sizeof(voter->identity_digest)));
/* Commitment couldn't be parsed. Continue onto the next commit because
* this one could be unsupported for instance. */
continue;
}
/* Add newly created commit object to the vote. */
smartlist_add(ns->sr_info.commits, commit);
} SMARTLIST_FOREACH_END(tok);
end:
smartlist_free(chunks);
smartlist_free(commits);
}
/** Check if a shared random value of type <b>srv_type</b> is in
* <b>tokens</b>. If there is, parse it and set it to <b>srv_out</b>. Return
* -1 on failure, 0 on success. The resulting srv is allocated on the heap and
@ -3776,13 +3722,7 @@ networkstatus_parse_vote_from_string(const char *s, const char **eos_out,
/* If this is a vote document, check if information about the shared
randomness protocol is included, and extract it. */
if (ns->type == NS_TYPE_VOTE) {
/* Does this authority participates in the SR protocol? */
tok = find_opt_by_keyword(tokens, K_SR_FLAG);
if (tok) {
ns->sr_info.participate = 1;
/* Get the SR commitments and reveals from the vote. */
extract_shared_random_commits(ns, tokens);
}
dirvote_parse_sr_commits(ns, tokens);
}
/* For both a vote and consensus, extract the shared random values. */
if (ns->type == NS_TYPE_VOTE || ns->type == NS_TYPE_CONSENSUS) {