Browse Source

Make the weighted relay picking algorithm more similar to the one we'll use for WO

Pick a number between 0 and totbw-1 inclusive, not between 1 and totbw.
Then search for the rightmost entry in the bw cdf array less than or
equal to that value, rather than strictly less than.
Ian Goldberg 4 years ago
parent
commit
df118a3820
2 changed files with 12 additions and 2 deletions
  1. 6 0
      client.py
  2. 6 2
      dirauth.py

+ 6 - 0
client.py

@@ -175,6 +175,12 @@ if __name__ == '__main__':
             if ch.peer.cellhandler.channels[caddr].peer is not ch:
                 print('asymmetry:', caddr, ad, ch, ch.peer.cellhandler.channels[caddr].peer)
 
+    # Pick a bunch of bw-weighted random relays and look at the
+    # distribution
+    for i in range(100):
+        r = clients[0].cellhandler.consensus.select_weighted_relay(clients[0].cellhandler.consensus_cdf)
+        print("relay",r.descdict["addr"])
+
     relays[3].terminate()
     del relays[3]
 

+ 6 - 2
dirauth.py

@@ -100,8 +100,12 @@ class Consensus:
     def select_weighted_relay(self, cdf):
         """Use the cdf generated by bw_cdf to select a relay with
         probability proportional to its bw weight."""
-        val = random.randint(1, self.consdict['totbw'])
-        idx = bisect.bisect_left(cdf, val)
+        totbw = self.consdict['totbw']
+        if totbw < 1:
+            raise ValueError("No relays to choose from")
+        val = random.randint(0, totbw-1)
+        # Find the rightmost entry less than or equal to val
+        idx = bisect.bisect_right(cdf, val)
         return self.consdict['relays'][idx-1]