[tor-commits] [bridgedb/main] Delete unused code
meskio at torproject.org
meskio at torproject.org
Tue Nov 23 16:01:14 UTC 2021
commit 11d6545a10df2fa0151dde36b63eff26b4a8f43c
Author: meskio <meskio at torproject.org>
Date: Fri Nov 5 15:33:33 2021 +0100
Delete unused code
Just removing the obvious easy to remove.
---
bridgedb/bridgerings.py | 117 --------------------------------
bridgedb/metrics.py | 21 ------
bridgedb/test/test_bridgerings.py | 137 --------------------------------------
bridgedb/test/test_metrics.py | 13 ----
4 files changed, 288 deletions(-)
diff --git a/bridgedb/bridgerings.py b/bridgedb/bridgerings.py
index 5b55610..ebbaa9b 100644
--- a/bridgedb/bridgerings.py
+++ b/bridgedb/bridgerings.py
@@ -412,123 +412,6 @@ class UnallocatedHolder(object):
f.write("%s %s\n" % (bridge.hex_key, " ".join(desc).strip()))
-class BridgeSplitter(object):
- """Splits incoming bridges up based on an HMAC, and assigns them to
- sub-bridgeholders with different probabilities. Bridge ââ BridgeSplitter
- associations are recorded in a store.
- """
- def __init__(self, key):
- self.hmac = getHMACFunc(key, hex=True)
- self.ringsByName = {}
- self.totalP = 0
- self.pValues = []
- self.rings = []
- self.statsHolders = []
-
- def __len__(self):
- n = 0
- for r in self.ringsByName.values():
- n += len(r)
- return n
-
- def addRing(self, ring, ringname, p=1):
- """Add a new subring.
-
- :param ring: The subring to add.
- :param str ringname: This is used to record which bridges have been
- assigned where in the store.
- :param int p: The relative proportion of bridges to assign to this
- bridgeholder.
- """
- self.ringsByName[ringname] = ring
- self.pValues.append(self.totalP)
- self.rings.append(ringname)
- self.totalP += p
-
- def addTracker(self, t):
- """Adds a statistics tracker that gets told about every bridge we see.
- """
- self.statsHolders.append(t)
-
- def clear(self):
- for r in self.ringsByName.values():
- r.clear()
-
- def insert(self, bridge):
- assert self.rings
-
- for s in self.statsHolders:
- s.insert(bridge)
-
- # The bridge must be running to insert it:
- if not bridge.flags.running:
- return
-
- validRings = self.rings
- distribution_method = orig_method = None
-
- with bridgedb.Storage.getDB() as db:
- orig_method = db.getBridgeDistributor(bridge, validRings)
- if orig_method is not None:
- distribution_method = orig_method
- logging.info("So far, bridge %s was in hashring %s" %
- (bridge, orig_method))
-
- # Check if the bridge requested a distribution method and if so, try to
- # use it.
- if bridge.distribution_request:
- distribution_method = bridge.distribution_request
- logging.info("Bridge %s requested placement in hashring %s" %
- (bridge, distribution_method))
-
- # Is the bridge requesting a distribution method that's different
- # from the one we have on record? If so, we have to delete it from
- # its previous ring.
- if orig_method is not None and \
- orig_method != distribution_method and \
- distribution_method in (validRings + ["none"]):
- logging.info("Bridge %s is in %s but wants to be in %s." %
- (bridge, orig_method, distribution_method))
- prevRing = self.ringsByName.get(orig_method)
- prevRing.remove(bridge)
-
- # If they requested not to be distributed, honor the request:
- if distribution_method == "none":
- logging.info("Bridge %s requested to not be distributed." % bridge)
- return
-
- # If we didn't know what they are talking about, or they requested
- # "any" distribution method, and we've never seen this bridge
- # before, then deterministically determine where to place it.
- if ((distribution_method not in validRings) or
- (distribution_method == "any")):
-
- pos = self.hmac(bridge.identity)
- n = int(pos[:8], 16) % self.totalP
- pos = bisect.bisect_right(self.pValues, n) - 1
- assert 0 <= pos < len(self.rings)
- distribution_method = self.rings[pos]
- logging.info(("%s placing bridge %s into hashring %s (via n=%s,"
- " pos=%s).") % (self.__class__.__name__, bridge,
- distribution_method, n, pos))
-
- with bridgedb.Storage.getDB() as db:
- ringname = db.insertBridgeAndGetRing(bridge, distribution_method,
- time.time(), validRings)
- db.commit()
-
- ring = self.ringsByName.get(ringname)
- ring.insert(bridge)
-
- if ring is None:
- logging.warn("Couldn't recognise ring named: '%s'" % ringname)
- logging.info("Current rings: %s" % " ".join(self.ringsByName))
-
- def dumpAssignments(self, f, description=""):
- for name,ring in self.ringsByName.items():
- ring.dumpAssignments(f, "%s %s" % (description, name))
-
-
class FilteredBridgeSplitter(object):
"""Places bridges into subrings based upon sets of filters.
diff --git a/bridgedb/metrics.py b/bridgedb/metrics.py
index 7751e94..c56b945 100644
--- a/bridgedb/metrics.py
+++ b/bridgedb/metrics.py
@@ -422,27 +422,6 @@ class InternalMetrics(Metrics):
self.set("{}.lower-whisker".format(handoutsPrefix), lowerWhisker)
self.set("{}.upper-whisker".format(handoutsPrefix), upperWhisker)
- def recordBridgesInHashring(self, ringName, subRingName, numBridges):
- """
- Record the number of bridges per hashring.
-
- :param str ringName: The name of the ring, e.g., "https".
- :param str subRingName: The name of the subring, e.g.,
- "byIPv6-bySubring1of4".
- :param int numBridges: The number of bridges in the given subring.
- """
-
- if not ringName or not subRingName:
- logging.warning("Ring name ({}) and subring name ({}) cannot be "
- "empty.".format(ringName, subRingName))
- return
-
- logging.info("Recording metrics for bridge (sub)rings: %s/%s/%d." %
- (ringName, subRingName, numBridges))
- # E.g, concatenate "https" with "byipv6-bysubring1of4".
- key = "{}.{}.{}".format(self.keyPrefix, ringName, subRingName.lower())
- self.set(key, numBridges)
-
class HTTPSMetrics(Metrics):
diff --git a/bridgedb/test/test_bridgerings.py b/bridgedb/test/test_bridgerings.py
index cca27d5..7c899ab 100644
--- a/bridgedb/test/test_bridgerings.py
+++ b/bridgedb/test/test_bridgerings.py
@@ -118,140 +118,3 @@ class BridgeRingTests(unittest.TestCase):
# The first bridge's fingerprint should be within the data somewhere
self.assertIn(first, data)
-
-
-class BridgeSplitterTests(unittest.TestCase):
- """Unittests for :class:`bridgedb.bridgerings.BridgeSplitter`."""
-
- def setUp(self):
-
- self.bridges = copy.deepcopy(util.generateFakeBridges())
-
- self.fd, self.fname = tempfile.mkstemp(suffix=".sqlite", dir=os.getcwd())
- bridgedb.Storage.initializeDBLock()
- self.db = bridgedb.Storage.openDatabase(self.fname)
- bridgedb.Storage.setDBFilename(self.fname)
-
- key = 'fake-hmac-key'
- self.splitter = bridgerings.BridgeSplitter(key)
- ringParams = bridgerings.BridgeRingParameters(needPorts=[(443, 1)],
- needFlags=[("Stable", 1)])
- self.https_distributor = HTTPSDistributor(
- 4,
- crypto.getHMAC(key, "HTTPS-IP-Dist-Key"),
- None,
- answerParameters=ringParams)
- self.moat_distributor = MoatDistributor(
- 4,
- crypto.getHMAC(key, "Moat-Dist-Key"),
- None,
- answerParameters=ringParams)
- self.unallocated_distributor = bridgerings.UnallocatedHolder()
-
- self.splitter.addRing(self.https_distributor.hashring, "https", p=10)
- self.splitter.addRing(self.moat_distributor.hashring, "moat", p=10)
- self.splitter.addRing(self.unallocated_distributor, "unallocated", p=10)
- self.https_ring = self.splitter.ringsByName.get("https")
- self.moat_ring = self.splitter.ringsByName.get("moat")
- self.unallocated_ring = self.splitter.ringsByName.get("unallocated")
-
- def tearDown(self):
- self.db.close()
- os.close(self.fd)
- os.unlink(self.fname)
-
- def _len_all_subrings(self, ring):
- """Return the sum of the length of all subrings."""
- all_subrings = [subring for _, subring in ring.filterRings.values()]
- return sum([len(subring) for subring in all_subrings])
-
- def test_no_distribution(self):
- """Make sure that bridges can un-distribute themselves."""
- bridge = self.bridges[0]
- bridge.distribution_request = "https"
-
- # Assume a bridge wants to be distributed over HTTPS.
- self.splitter.insert(bridge)
- self.assertEqual(len(self.https_ring), 1)
-
- # ...and now the bridge no longer wants to be distributed.
- bridge.distribution_request = "none"
- self.splitter.insert(bridge)
- self.assertEqual(len(self.https_ring), 0)
- self.assertEqual(len(self.moat_ring), 0)
- self.assertEqual(len(self.unallocated_ring), 0)
-
- def test_change_distribution(self):
- """Make sure that bridges can change their distribution mechanism."""
- bridge = self.bridges[0]
- # We hard-code our identity to make this test deterministic.
- bridge.identity = b"\xfd{\xe7\x90a'\n\xf483 at H\xd6-\x9c\xf3\x8f\x12~$"
- bridge.distribution_request = "https"
-
- # Assume a bridge wants to be distributed over HTTPS.
- self.splitter.insert(bridge)
- self.assertEqual(len(self.https_ring), 1)
- self.assertEqual(len(self.moat_ring), 0)
- self.assertEqual(self._len_all_subrings(self.moat_ring), 0)
-
- # ...and now the bridge changes its mind and wants Moat.
- bridge.distribution_request = "moat"
- self.splitter.insert(bridge)
- self.assertEqual(len(self.https_ring), 0)
- self.assertEqual(self._len_all_subrings(self.https_ring), 0)
- self.assertEqual(len(self.moat_ring), 1)
-
- # ...if the bridge uses "any", it should stay where it is.
- bridge.distribution_request = "any"
- self.splitter.insert(bridge)
- self.assertEqual(len(self.https_ring), 0)
- self.assertEqual(self._len_all_subrings(self.https_ring), 0)
- self.assertEqual(len(self.moat_ring), 1)
-
- # ...if it uses "none", it shouldn't be distributed at all.
- bridge.distribution_request = "none"
- self.splitter.insert(bridge)
- self.assertEqual(len(self.https_ring), 0)
- self.assertEqual(self._len_all_subrings(self.https_ring), 0)
- self.assertEqual(len(self.moat_ring), 0)
- self.assertEqual(self._len_all_subrings(self.moat_ring), 0)
-
- # ...if the distribution method is unrecognised, it should be treated
- # as "any".
- bridge.distribution_request = "foobar"
- self.splitter.insert(bridge)
- self.assertEqual(len(self.https_ring), 0)
- self.assertEqual(self._len_all_subrings(self.https_ring), 0)
- self.assertEqual(len(self.moat_ring), 1)
-
- # ...and finally, it wants HTTPS again.
- bridge.distribution_request = "https"
- self.splitter.insert(bridge)
- self.assertEqual(len(self.https_ring), 1)
- self.assertEqual(len(self.moat_ring), 0)
- self.assertEqual(self._len_all_subrings(self.moat_ring), 0)
-
- def test_https_remove(self):
- """Make sure that we can remove bridges from our BridgeRing."""
- bridge = self.bridges[0]
-
- self.assertEqual(len(self.https_ring), 0)
- self.https_ring.insert(bridge)
- self.https_distributor.prepopulateRings()
- self.assertEqual(len(self.https_ring), 1)
-
- self.https_ring.remove(bridge)
- self.assertEqual(len(self.https_ring), 0)
- self.assertEqual(self._len_all_subrings(self.https_ring), 0)
-
- def test_unallocated_remove(self):
- """Make sure that we can remove bridges from our UnallocatedHolder."""
- bridge = self.bridges[0]
- bridge.distribution_request = "unallocated"
-
- self.assertEqual(len(self.unallocated_distributor), 0)
- self.splitter.insert(bridge)
- self.assertEqual(len(self.unallocated_distributor), 1)
-
- self.unallocated_distributor.remove(bridge)
- self.assertEqual(len(self.unallocated_distributor), 0)
diff --git a/bridgedb/test/test_metrics.py b/bridgedb/test/test_metrics.py
index 12b3ee1..60b7617 100644
--- a/bridgedb/test/test_metrics.py
+++ b/bridgedb/test/test_metrics.py
@@ -269,19 +269,6 @@ class StateTest(unittest.TestCase):
self.assertIn("internal.moat.empty-response 20", lines)
self.assertIn("internal.https.empty-response 10", lines)
- def test_rings(self):
-
- metrix = metrics.InternalMetrics()
-
- # Empty parameters must not be recorded.
- metrix.recordBridgesInHashring("", "", 20)
- self.assertEqual(len(metrix.hotMetrics), 0)
-
- metrix.recordBridgesInHashring("https", "byIPv6-bySubring1of4", 20)
- self.assertEqual(len(metrix.hotMetrics), 1)
- self.assertEqual(list(metrix.hotMetrics.keys()),
- ["internal.https.byipv6-bysubring1of4"])
-
def test_ipv4_ipv6_requests(self):
metrix = metrics.InternalMetrics()
More information about the tor-commits
mailing list