Example #1
0
    def _downloadMicrodescriptors(self, initial=False):
        consensus = yield self._microconsensus_manager.getMicroconsensus()
        v2dirs = microconsensusmanager.getV2DirsFromConsensus(consensus)
        self._total_descriptors = len(consensus.routers)
        self._discardUnlistedMicrodescriptors(consensus)
        needed_digests = _getNeededDescriptorDigests(consensus,
                                                     self._microdescriptors)
        # if we already have >= 80% of descriptors, we can build circuits
        # immediately
        self._checkIfReadyToBuildCircuit()
        # only request <= REQUEST_MAX descriptors from each dircache
        blocks = [
            needed_digests[i:i + REQUEST_MAX]
            for i in xrange(0, len(needed_digests), REQUEST_MAX)
        ]

        if len(blocks) > 0:
            task_list = [
                self._downloadMicrodescriptorBlock(b, v2dirs) for b in blocks
            ]
            d = defer.gatherResults(task_list)
            d.addCallback(self._writeMicrodescriptorCacheFile)

        if initial is True:
            self._microconsensus_manager.addMicroconsensusDownloadCallback(
                self._downloadMicrodescriptors)
Example #2
0
    def test_getV2DirsFromConsensus(self):
        from stem import Flag
        mock_router_1 = mock.Mock()
        mock_router_2 = mock.Mock()
        mock_router_1.flags = [Flag.V2DIR]
        mock_router_2.flags = []
        mc = mock.Mock()
        mc.routers = {'1': mock_router_1, '2': mock_router_2}

        ret = mcm.getV2DirsFromConsensus(mc)
        self.assertEqual(ret, [mock_router_1])
Example #3
0
    def _downloadMicrodescriptors(self, initial=False):
        consensus = yield self._microconsensus_manager.getMicroconsensus()
        v2dirs = microconsensusmanager.getV2DirsFromConsensus(consensus)
        self._total_descriptors = len(consensus.routers)
        self._discardUnlistedMicrodescriptors(consensus)
        needed_digests = _getNeededDescriptorDigests(
            consensus, self._microdescriptors)
        # if we already have >= 80% of descriptors, we can build circuits
        # immediately
        self._checkIfReadyToBuildCircuit()
        # only request <= REQUEST_MAX descriptors from each dircache
        blocks = [needed_digests[i:i+REQUEST_MAX]
                  for i in xrange(0, len(needed_digests), REQUEST_MAX)]

        if len(blocks) > 0:
            task_list = [self._downloadMicrodescriptorBlock(b, v2dirs)
                         for b in blocks]
            d = defer.gatherResults(task_list)
            d.addCallback(self._writeMicrodescriptorCacheFile)

        if initial is True:
            self._microconsensus_manager.addMicroconsensusDownloadCallback(
                self._downloadMicrodescriptors)