예제 #1
0
파일: dht_util.py 프로젝트: 5yncr/backend
    def __setitem__(self, key: Any, value: Any) -> None:
        """
        :param key: key to look up in dht
        :param value: value to put in dht under key
        """
        valuepeer = crypto_util.decode_peerlist(value)
        if valuepeer is not None:
            self.timeouts[(key, valuepeer[0])] = int(time.time())
            if key in self.data:
                peerlist = crypto_util.decode_peerlist(
                    self.data[key][1],
                )
                if peerlist is not None:
                    # remove duplicates and encode
                    new_peerlist = crypto_util.encode_peerlist(
                        list(set(peerlist + valuepeer)),
                    )
                    return super().__setitem__(key, new_peerlist)

            return super().__setitem__(
                key,
                crypto_util.encode_peerlist(valuepeer),
            )

        return super().__setitem__(key, value)
예제 #2
0
    async def request_peers(
        self,
        drop_id: bytes,
    ) -> Tuple[bool, List[Tuple[bytes, str, int]]]:
        """
        Get an entry from the dht

        :param drop_id: The drop to look up
        :return: A tuple of success and list of peers
        """
        logger.debug("requesting drop peers %s", drop_id)
        # result is bytes representation of frozen set of peers
        result = await self.node_instance.get(drop_id)
        if result is not None:
            logger.debug(
                "DHT get drop peer : %s",
                crypto_util.decode_peerlist(result),
            )
            # return list of contents of frozenset
            decoded_peerlist = crypto_util.decode_peerlist(result)
            if decoded_peerlist is None:
                return False, []
            return True, decoded_peerlist
        else:
            logger.debug("DHT failed get drop peer, drop_id: %s", str(drop_id))
            return False, []
예제 #3
0
파일: dht_util.py 프로젝트: 5yncr/backend
    def cull_peerlist(self, key: Any) -> None:
        """
        Cull peerlist at key of out of date entries
        If entry at key is not a peerlist, do nothing

        :param key: key of peerlist
        """
        current_time = int(time.time())
        item = self.data[key][1]
        itempeerlist = crypto_util.decode_peerlist(item)
        if itempeerlist is not None:
            itempeerlist = list(
                filter(
                    lambda x: (
                        ((key, x) not in self.timeouts) or
                        (
                            current_time - self.timeouts[(key, x)] <
                            TRACKER_DROP_AVAILABILITY_TTL
                        )
                    ),
                    itempeerlist,
                ),
            )
            timeout_key_list = list(
                filter(
                    lambda x: self.timeouts[x] < TRACKER_DROP_AVAILABILITY_TTL,
                    self.timeouts,
                ),
            )
            timeout_value_list = list(
                map(lambda x: self.timeouts[x], timeout_key_list),
            )
            self.timeouts = dict(zip(timeout_key_list, timeout_value_list))
예제 #4
0
def test_encode_frozenset() -> None:

    f = [
        ('12', '1234', 123),
        ('asdf', 'asdf', 123),
    ]
    epl = crypto_util.encode_peerlist(f)
    dpl = crypto_util.decode_peerlist(epl)
    assert f == dpl
예제 #5
0
def test_encode_bad_input() -> None:
    f = os.urandom(12)
    fs = crypto_util.encode_peerlist_prefix + f
    dpl = crypto_util.decode_peerlist(f)
    dpl2 = crypto_util.decode_peerlist(fs)
    assert dpl is None and dpl2 is None