[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] prop250: Sort smartlist before you get most frequent SRV.



commit edea0441477cf7f1293ce848abf59b3f1033b28a
Author: George Kadianakis <desnacked@xxxxxxxxxx>
Date:   Fri May 13 13:42:33 2016 -0400

    prop250: Sort smartlist before you get most frequent SRV.
    
    Signed-off-by: David Goulet <dgoulet@xxxxxxxxxxxxxx>
---
 src/or/shared_random.c        | 10 ++++++++++
 src/test/test_shared_random.c |  6 +++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/or/shared_random.c b/src/or/shared_random.c
index d409d10..c5a62a7 100644
--- a/src/or/shared_random.c
+++ b/src/or/shared_random.c
@@ -779,6 +779,15 @@ smartlist_get_most_frequent_srv(const smartlist_t *sl, int *count_out)
   return smartlist_get_most_frequent_(sl, compare_srvs_, count_out);
 }
 
+/** Compare two SRVs. Used in smartlist sorting. */
+static int
+compare_srv_(const void **_a, const void **_b)
+{
+  const sr_srv_t *a = *_a, *b = *_b;
+  return fast_memcmp(a->value, b->value,
+                     sizeof(a->value));
+}
+
 /* Using a list of <b>votes</b>, return the SRV object from them that has
  * been voted by the majority of dirauths. If <b>current</b> is set, we look
  * for the current SRV value else the previous one. The returned pointer is
@@ -813,6 +822,7 @@ get_majority_srv_from_votes(const smartlist_t *votes, int current)
     smartlist_add(srv_list, srv_tmp);
   } SMARTLIST_FOREACH_END(v);
 
+  smartlist_sort(srv_list, compare_srv_);
   most_frequent_srv = smartlist_get_most_frequent_srv(srv_list, &count);
   if (!most_frequent_srv) {
     goto end;
diff --git a/src/test/test_shared_random.c b/src/test/test_shared_random.c
index fbb4414..5a9c123 100644
--- a/src/test/test_shared_random.c
+++ b/src/test/test_shared_random.c
@@ -844,7 +844,7 @@ test_sr_get_majority_srv_from_votes(void *arg)
   { /* Now put in 8 more votes. Let SRV_1 have majority. */
     int i;
     /* Now 7 votes believe in SRV_1 */
-    for (i = 0; i < 6; i++) {
+    for (i = 0; i < 3; i++) {
       networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
       smartlist_add(votes, vote);
     }
@@ -853,6 +853,10 @@ test_sr_get_majority_srv_from_votes(void *arg)
       networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_2);
       smartlist_add(votes, vote);
     }
+    for (i = 0; i < 3; i++) {
+      networkstatus_t *vote = get_test_vote_with_curr_srv(SRV_1);
+      smartlist_add(votes, vote);
+    }
 
     tt_int_op(smartlist_len(votes), ==, 9);
   }



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits