dirauth: Refactor dirvote_add_vote()

Refactor dirvote_add_vote() by splitting some code out into static
functions.

Cleanup after 4631.
This commit is contained in:
teor 2020-02-18 12:22:13 +10:00
parent 56f715d300
commit 8b5b574a81
No known key found for this signature in database
GPG Key ID: 10FEAA0E7075672A

View File

@ -3119,6 +3119,37 @@ list_v3_auth_ids(void)
return keys;
}
/* Check the voter information <b>vi</b>, and assert that at least one
* signature is good. Asserts on failure. */
static void
assert_any_sig_good(const networkstatus_voter_info_t *vi)
{
int any_sig_good = 0;
SMARTLIST_FOREACH(vi->sigs, document_signature_t *, sig,
if (sig->good_signature)
any_sig_good = 1);
tor_assert(any_sig_good);
}
/* Add <b>cert</b> to our list of known authority certificates. */
static void
add_new_cert_if_needed(const struct authority_cert_t *cert)
{
tor_assert(cert);
if (!authority_cert_get_by_digests(cert->cache_info.identity_digest,
cert->signing_key_digest)) {
/* Hey, it's a new cert! */
trusted_dirs_load_certs_from_string(
cert->cache_info.signed_descriptor_body,
TRUSTED_DIRS_CERTS_SRC_FROM_VOTE, 1 /*flush*/,
NULL);
if (!authority_cert_get_by_digests(cert->cache_info.identity_digest,
cert->signing_key_digest)) {
log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
}
}
}
/** Called when we have received a networkstatus vote in <b>vote_body</b>.
* Parse and validate it, and on success store it as a pending vote (which we
* then return). Return NULL on failure. Sets *<b>msg_out</b> and
@ -3157,13 +3188,7 @@ dirvote_add_vote(const char *vote_body, time_t time_posted,
}
tor_assert(smartlist_len(vote->voters) == 1);
vi = get_voter(vote);
{
int any_sig_good = 0;
SMARTLIST_FOREACH(vi->sigs, document_signature_t *, sig,
if (sig->good_signature)
any_sig_good = 1);
tor_assert(any_sig_good);
}
assert_any_sig_good(vi);
ds = trusteddirserver_get_by_v3_auth_digest(vi->identity_digest);
if (!ds) {
char *keys = list_v3_auth_ids();
@ -3176,19 +3201,7 @@ dirvote_add_vote(const char *vote_body, time_t time_posted,
*msg_out = "Vote not from a recognized v3 authority";
goto err;
}
tor_assert(vote->cert);
if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
vote->cert->signing_key_digest)) {
/* Hey, it's a new cert! */
trusted_dirs_load_certs_from_string(
vote->cert->cache_info.signed_descriptor_body,
TRUSTED_DIRS_CERTS_SRC_FROM_VOTE, 1 /*flush*/,
NULL);
if (!authority_cert_get_by_digests(vote->cert->cache_info.identity_digest,
vote->cert->signing_key_digest)) {
log_warn(LD_BUG, "We added a cert, but still couldn't find it.");
}
}
add_new_cert_if_needed(vote->cert);
/* Is it for the right period? */
if (vote->valid_after != voting_schedule.interval_starts) {