Switch v3_onions_seen_this_period to digest256map_t.

This commit is contained in:
George Kadianakis 2020-11-03 17:34:46 +02:00
parent f2da7b05b0
commit 9a98d1da30
3 changed files with 21 additions and 15 deletions

View file

@ -1828,7 +1828,7 @@ static hs_v3_stats_t *
hs_v3_stats_new(void) hs_v3_stats_new(void)
{ {
hs_v3_stats_t *new_hs_v3_stats = tor_malloc_zero(sizeof(hs_v3_stats_t)); hs_v3_stats_t *new_hs_v3_stats = tor_malloc_zero(sizeof(hs_v3_stats_t));
new_hs_v3_stats->v3_onions_seen_this_period = digestmap_new(); new_hs_v3_stats->v3_onions_seen_this_period = digest256map_new();
return new_hs_v3_stats; return new_hs_v3_stats;
} }
@ -1844,7 +1844,7 @@ hs_v3_stats_free_(hs_v3_stats_t *victim_hs_v3_stats)
return; return;
} }
digestmap_free(victim_hs_v3_stats->v3_onions_seen_this_period, NULL); digest256map_free(victim_hs_v3_stats->v3_onions_seen_this_period, NULL);
tor_free(victim_hs_v3_stats); tor_free(victim_hs_v3_stats);
} }
@ -1857,8 +1857,8 @@ rep_hist_reset_hs_v3_stats(time_t now)
hs_v3_stats = hs_v3_stats_new(); hs_v3_stats = hs_v3_stats_new();
} }
digestmap_free(hs_v3_stats->v3_onions_seen_this_period, NULL); digest256map_free(hs_v3_stats->v3_onions_seen_this_period, NULL);
hs_v3_stats->v3_onions_seen_this_period = digestmap_new(); hs_v3_stats->v3_onions_seen_this_period = digest256map_new();
hs_v3_stats->rp_v3_relay_cells_seen = 0; hs_v3_stats->rp_v3_relay_cells_seen = 0;
@ -1888,8 +1888,9 @@ rep_hist_hsdir_stored_maybe_new_v3_onion(const uint8_t *blinded_key)
return; return;
} }
bool seen_before = !!digestmap_get(hs_v3_stats->v3_onions_seen_this_period, bool seen_before =
(char*)blinded_key); !!digest256map_get(hs_v3_stats->v3_onions_seen_this_period,
blinded_key);
log_info(LD_GENERAL, "Considering v3 descriptor with %s (%sseen before)", log_info(LD_GENERAL, "Considering v3 descriptor with %s (%sseen before)",
safe_str(hex_str((char*)blinded_key, 32)), safe_str(hex_str((char*)blinded_key, 32)),
@ -1897,8 +1898,8 @@ rep_hist_hsdir_stored_maybe_new_v3_onion(const uint8_t *blinded_key)
/* Count it if we haven't seen it before. */ /* Count it if we haven't seen it before. */
if (!seen_before) { if (!seen_before) {
digestmap_set(hs_v3_stats->v3_onions_seen_this_period, digest256map_set(hs_v3_stats->v3_onions_seen_this_period,
(char*)blinded_key, (void*)(uintptr_t)1); blinded_key, (void*)(uintptr_t)1);
} }
} }
@ -1985,7 +1986,7 @@ rep_hist_format_hs_stats(time_t now, bool is_v3)
uint64_t rp_cells_seen = is_v3 ? uint64_t rp_cells_seen = is_v3 ?
hs_v3_stats->rp_v3_relay_cells_seen : hs_v2_stats->rp_v2_relay_cells_seen; hs_v3_stats->rp_v3_relay_cells_seen : hs_v2_stats->rp_v2_relay_cells_seen;
size_t onions_seen = is_v3 ? size_t onions_seen = is_v3 ?
digestmap_size(hs_v3_stats->v3_onions_seen_this_period) : digest256map_size(hs_v3_stats->v3_onions_seen_this_period) :
digestmap_size(hs_v2_stats->v2_onions_seen_this_period); digestmap_size(hs_v2_stats->v2_onions_seen_this_period);
time_t start_of_hs_stats_interval = is_v3 ? time_t start_of_hs_stats_interval = is_v3 ?
start_of_hs_v3_stats_interval : start_of_hs_v2_stats_interval; start_of_hs_v3_stats_interval : start_of_hs_v2_stats_interval;

View file

@ -113,7 +113,7 @@ typedef struct hs_v3_stats_t {
/* The number of unique v3 onion descriptors (actually, unique v3 blind keys) /* The number of unique v3 onion descriptors (actually, unique v3 blind keys)
* we've seen during the measurement period */ * we've seen during the measurement period */
digestmap_t *v3_onions_seen_this_period; digest256map_t *v3_onions_seen_this_period;
} hs_v3_stats_t; } hs_v3_stats_t;
MOCK_DECL(STATIC bool, should_collect_v3_stats,(void)); MOCK_DECL(STATIC bool, should_collect_v3_stats,(void));

View file

@ -528,7 +528,8 @@ test_rephist_v3_onions(void *arg)
/* HS stats should be zero here */ /* HS stats should be zero here */
hs_v3_stats = rep_hist_get_hs_v3_stats(); hs_v3_stats = rep_hist_get_hs_v3_stats();
tt_int_op(digestmap_size(hs_v3_stats->v3_onions_seen_this_period), OP_EQ, 0); tt_int_op(digest256map_size(hs_v3_stats->v3_onions_seen_this_period),
OP_EQ, 0);
/* Generate a valid descriptor */ /* Generate a valid descriptor */
ret = ed25519_keypair_generate(&signing_kp1, 0); ret = ed25519_keypair_generate(&signing_kp1, 0);
@ -542,7 +543,8 @@ test_rephist_v3_onions(void *arg)
ret = hs_cache_store_as_dir(desc1_str); ret = hs_cache_store_as_dir(desc1_str);
tt_int_op(ret, OP_EQ, 0); tt_int_op(ret, OP_EQ, 0);
hs_v3_stats = rep_hist_get_hs_v3_stats(); hs_v3_stats = rep_hist_get_hs_v3_stats();
tt_int_op(digestmap_size(hs_v3_stats->v3_onions_seen_this_period), OP_EQ, 1); tt_int_op(digest256map_size(hs_v3_stats->v3_onions_seen_this_period),
OP_EQ, 1);
/* cleanup */ /* cleanup */
hs_descriptor_free(desc1); hs_descriptor_free(desc1);
@ -560,7 +562,8 @@ test_rephist_v3_onions(void *arg)
ret = hs_cache_store_as_dir(desc1_str); ret = hs_cache_store_as_dir(desc1_str);
tt_int_op(ret, OP_EQ, 0); tt_int_op(ret, OP_EQ, 0);
hs_v3_stats = rep_hist_get_hs_v3_stats(); hs_v3_stats = rep_hist_get_hs_v3_stats();
tt_int_op(digestmap_size(hs_v3_stats->v3_onions_seen_this_period), OP_EQ, 2); tt_int_op(digest256map_size(hs_v3_stats->v3_onions_seen_this_period),
OP_EQ, 2);
/* Check that storing the same descriptor twice does not work */ /* Check that storing the same descriptor twice does not work */
ret = hs_cache_store_as_dir(desc1_str); ret = hs_cache_store_as_dir(desc1_str);
@ -580,7 +583,8 @@ test_rephist_v3_onions(void *arg)
/* Store descriptor and check that stats are updated */ /* Store descriptor and check that stats are updated */
ret = hs_cache_store_as_dir(desc1_str); ret = hs_cache_store_as_dir(desc1_str);
tt_int_op(ret, OP_EQ, 0); tt_int_op(ret, OP_EQ, 0);
tt_int_op(digestmap_size(hs_v3_stats->v3_onions_seen_this_period), OP_EQ, 2); tt_int_op(digest256map_size(hs_v3_stats->v3_onions_seen_this_period),
OP_EQ, 2);
/* cleanup */ /* cleanup */
hs_descriptor_free(desc1); hs_descriptor_free(desc1);
@ -600,7 +604,8 @@ test_rephist_v3_onions(void *arg)
/* Store descriptor and check that stats are updated */ /* Store descriptor and check that stats are updated */
ret = hs_cache_store_as_dir(desc1_str); ret = hs_cache_store_as_dir(desc1_str);
tt_int_op(ret, OP_EQ, 0); tt_int_op(ret, OP_EQ, 0);
tt_int_op(digestmap_size(hs_v3_stats->v3_onions_seen_this_period), OP_EQ, 3); tt_int_op(digest256map_size(hs_v3_stats->v3_onions_seen_this_period),
OP_EQ, 3);
/* cleanup */ /* cleanup */
hs_descriptor_free(desc1); hs_descriptor_free(desc1);