예제 #1
0
    async def test_get_statistics(self):
        """
        Testing whether the API returns the correct statistics
        """
        block = TrustChainBlock()
        block.public_key = self.session.trustchain_community.my_peer.public_key.key_to_bin()
        block.link_public_key = unhexlify(b"deadbeef")
        block.link_sequence_number = 21
        block.type = b'tribler_bandwidth'
        block.transaction = {b"up": 42, b"down": 8, b"total_up": 1024,
                             b"total_down": 2048, b"type": b"tribler_bandwidth"}
        block._transaction = encode(block.transaction)
        block.sequence_number = 3
        block.previous_hash = unhexlify(b"babecafe")
        block.signature = unhexlify(b"babebeef")
        block.hash = block.calculate_hash()
        self.session.trustchain_community.persistence.add_block(block)

        response_dict = await self.do_request('trustchain/statistics', expected_code=200)
        self.assertTrue("statistics" in response_dict)
        stats = response_dict["statistics"]
        self.assertEqual(stats["id"], hexlify(self.session.trustchain_community.
                                              my_peer.public_key.key_to_bin()))
        self.assertEqual(stats["total_blocks"], 3)
        self.assertEqual(stats["total_up"], 1024)
        self.assertEqual(stats["total_down"], 2048)
        self.assertEqual(stats["peers_that_pk_helped"], 1)
        self.assertEqual(stats["peers_that_helped_pk"], 1)
예제 #2
0
    def create(cls,
               block_type,
               transaction,
               database,
               public_key,
               link=None,
               link_pk=None,
               additional_info=None):
        """
        Create an empty next block.
        :param block_type: the type of the block to be created
        :param transaction: the transaction to use in this block
        :param database: the database to use as information source
        :param public_key: the public key to use for this block
        :param link: optionally create the block as a linked block to this block
        :param link_pk: the public key of the counterparty in this transaction
        :param additional_info: additional information, which has a higher priority than the
               transaction when link exists
        :return: A newly created block
        """
        latest_bw_block = database.get_latest(public_key,
                                              block_type=b'tribler_bandwidth')
        latest_block = database.get_latest(public_key)
        ret = cls()
        if link:
            ret.type = link.type
            ret.transaction["up"] = link.transaction[
                "down"] if "down" in link.transaction else 0
            ret.transaction["down"] = link.transaction[
                "up"] if "up" in link.transaction else 0
            ret.link_public_key = link.public_key
            ret.link_sequence_number = link.sequence_number
        else:
            ret.type = block_type
            ret.transaction[
                "up"] = transaction["up"] if "up" in transaction else 0
            ret.transaction[
                "down"] = transaction["down"] if "down" in transaction else 0
            ret.link_public_key = link_pk

        if latest_bw_block:
            ret.transaction["total_up"] = latest_bw_block.transaction[
                "total_up"] + ret.transaction["up"]
            ret.transaction["total_down"] = latest_bw_block.transaction[
                "total_down"] + ret.transaction["down"]
        else:
            ret.transaction["total_up"] = ret.transaction["up"]
            ret.transaction["total_down"] = ret.transaction["down"]

        if latest_block:
            ret.sequence_number = latest_block.sequence_number + 1
            ret.previous_hash = latest_block.hash

        ret._transaction = encode(ret.transaction)
        ret.public_key = public_key
        ret.signature = EMPTY_SIG
        ret.hash = ret.calculate_hash()

        return ret
예제 #3
0
    def test_encode_tuple(self):
        """
        Check if a tuple can be encoded and decoded.
        """
        value = (42, 42L, 42.0, u"42", '\x42')

        encoded = encode(value)
        _, decoded = decode(encoded)

        self.assertEqual(value, decoded)
예제 #4
0
    def test_encode_dict(self):
        """
        Check if a dictionary can be encoded and decoded.
        """
        value = {42: None, 42L: 42.0, u"42": '\x42'}

        encoded = encode(value)
        _, decoded = decode(encoded)

        self.assertEqual(value, decoded)
예제 #5
0
    def test_encode_int(self):
        """
        Check if an int can be encoded and decoded.
        """
        value = 42

        encoded = encode(value)
        _, decoded = decode(encoded)

        self.assertEqual(value, decoded)
예제 #6
0
    def test_encode_set(self):
        """
        Check if a set can be encoded and decoded.
        """
        value = {42, 42L, 42.0, u"42", '\x42'}

        encoded = encode(value)
        _, decoded = decode(encoded)

        self.assertEqual(value, decoded)
예제 #7
0
    def test_encode_unicode(self):
        """
        Check if a unicode value can be encoded and decoded.
        """
        value = u"42"

        encoded = encode(value)
        _, decoded = decode(encoded)

        self.assertEqual(value, decoded)
예제 #8
0
    def test_encode_long(self):
        """
        Check if an long can be encoded and decoded.
        """
        value = 42L

        encoded = encode(value)
        _, decoded = decode(encoded)

        self.assertEqual(value, decoded)
예제 #9
0
    def test_encode_bool_false(self):
        """
        Check if a (false) boolean can be encoded and decoded.
        """
        value = False

        encoded = encode(value)
        _, decoded = decode(encoded)

        self.assertEqual(value, decoded)
예제 #10
0
    def test_encode_bool(self):
        """
        Check if a (true) boolean can be encoded and decoded.
        """
        value = True

        encoded = encode(value)
        _, decoded = decode(encoded)

        self.assertEqual(value, decoded)
예제 #11
0
    def test_encode_none(self):
        """
        Check if a None can be encoded and decoded.
        """
        value = None

        encoded = encode(value)
        _, decoded = decode(encoded)

        self.assertEqual(value, decoded)
예제 #12
0
    async def test_get_bootstrap_identity_not_enough_tokens_2(self):
        """
        Testing whether the API returns error 400 if bandwidth is to low when bootstrapping a new identity
        """
        transaction = {b'up': 0, b'down': 100, b'total_up': 0, b'total_down': 100}
        test_block = TrustChainBlock()
        test_block.type = b'tribler_bandwidth'
        test_block.transaction = transaction
        test_block._transaction = encode(transaction)
        test_block.public_key = self.session.trustchain_community.my_peer.public_key.key_to_bin()
        test_block.hash = test_block.calculate_hash()
        self.session.trustchain_community.persistence.add_block(test_block)

        await self.do_request('trustchain/bootstrap?amount=10', expected_code=400)
예제 #13
0
    async def test_get_bootstrap_identity_all_tokens(self):
        """
        Testing whether the API return all available tokens when no argument is supplied
        """
        transaction = {b'up': 100, b'down': 0, b'total_up': 100, b'total_down': 0}
        transaction2 = {'up': 100, 'down': 0}

        test_block = TrustChainBlock()
        test_block.type = b'tribler_bandwidth'
        test_block.transaction = transaction
        test_block._transaction = encode(transaction)
        test_block.public_key = self.session.trustchain_community.my_peer.public_key.key_to_bin()
        test_block.hash = test_block.calculate_hash()
        self.session.trustchain_community.persistence.add_block(test_block)

        response_dict = await self.do_request('trustchain/bootstrap', expected_code=200)
        self.assertEqual(response_dict['transaction'], transaction2)
예제 #14
0
    async def test_trustview_max_response(self):
        """
        Test whether the trust graph response is limited.
        Here we redefine the max peers and max transactions limit for trust graph and add more peers and transactions,
        then test if the endpoint response is limited.
        """
        max_peers = 10
        max_tx = 10
        self.endpoint.initialize_graph()
        self.endpoint.trust_graph.set_limits(max_peers, max_tx)

        def get_dummy_tx():
            return {
                b'up': random.randint(1, 101),
                b'down': random.randint(1, 101),
                b'total_up': random.randint(1, 101),
                b'total_down': random.randint(1, 101),
            }

        test_block = TestBlock(
            key=self.session.trustchain_community.my_peer.key)
        test_block.sequence_number = 0
        test_block.type = b'tribler_bandwidth'
        for _ in range(max_peers * 2):
            test_block.transaction = get_dummy_tx()
            test_block._transaction = encode(test_block.transaction)
            test_block.link_public_key = default_eccrypto.generate_key(
                u"very-low").pub().key_to_bin()
            test_block.hash = test_block.calculate_hash()
            self.session.trustchain_community.persistence.add_block(test_block)
            test_block.sequence_number = test_block.sequence_number + 1

        response_json = await self.do_request('trustview?depth=0',
                                              expected_code=200)
        self.assertIsNotNone(response_json['graph'])
        self.assertLessEqual(response_json['num_tx'], max_tx)
        self.assertLessEqual(len(response_json['graph']['node']), max_peers)
예제 #15
0
    async def test_trustview_response(self):
        """
        Test whether the trust graph response is correctly returned.
        """
        root_key = self.session.trustchain_community.my_peer.public_key.key_to_bin(
        )
        friends = [
            "4c69624e61434c504b3a2ee28ce24a2259b4e585b81106cdff4359fcf48e93336c11d133b01613f30b03b4db06df27"
            "80daac2cdf2ee60be611bf7367a9c1071ac50d65ca5858a50e9578",
            "4c69624e61434c504b3a5368c7b39a82063e29576df6d74fba3e0dba3af8e7a304b553b71f08ea6a0730e8cef767a4"
            "85dc6f390b6da5631f772941ea69ce2c098d802b7a28b500edf2b3",
            "4c69624e61434c504b3a0f3f6318e49ffeb0a160e7fcac5c1d3337ba409b45e1371ddca5e3b364ebdd1b73c775318a"
            "533a25335a5c36ae3695f1c3036b651893659fbf2e1f2bce66cf65",
        ]

        fofs = [
            "4c69624e61434c504b3a2ee28ce24a2259b4e585b81106cdff4359fcf48e93336c11d133b01613f30b03b4db06df27"
            "80daac2cdf2ee60be611bf7367a9c1071ac50d65ca5858a50e9579",
            "4c69624e61434c504b3a5368c7b39a82063e29576df6d74fba3e0dba3af8e7a304b553b71f08ea6a0730e8cef767a4"
            "85dc6f390b6da5631f772941ea69ce2c098d802b7a28b500edf2b4",
            "4c69624e61434c504b3a0f3f6318e49ffeb0a160e7fcac5c1d3337ba409b45e1371ddca5e3b364ebdd1b73c775318a"
            "533a25335a5c36ae3695f1c3036b651893659fbf2e1f2bce66cf66",
        ]

        fofofs = [
            "4c69624e61434c504b3a2ee28ce24a2259b4e585b81106cdff4359fcf48e93336c11d133b01613f30b03b4db06df27"
            "80daac2cdf2ee60be611bf7367a9c1071ac50d65ca5858a50e9580",
            "4c69624e61434c504b3a5368c7b39a82063e29576df6d74fba3e0dba3af8e7a304b553b71f08ea6a0730e8cef767a4"
            "85dc6f390b6da5631f772941ea69ce2c098d802b7a28b500edf2b5",
            "4c69624e61434c504b3a0f3f6318e49ffeb0a160e7fcac5c1d3337ba409b45e1371ddca5e3b364ebdd1b73c775318a"
            "533a25335a5c36ae3695f1c3036b651893659fbf2e1f2bce66cf67",
        ]

        def get_dummy_tx():
            return {
                b'up': random.randint(1, 101),
                b'down': random.randint(1, 101),
                b'total_up': random.randint(1, 101),
                b'total_down': random.randint(1, 101),
            }

        def verify_response(response_json, nodes, tx):
            self.assertIsNotNone(response_json['graph'])
            self.assertEqual(response_json['num_tx'], tx)
            self.assertEqual(len(response_json['graph']['node']), nodes)

        test_block = TrustChainBlock()
        test_block.type = b'tribler_bandwidth'

        for seq, pub_key in enumerate(friends):
            test_block.transaction = get_dummy_tx()
            test_block._transaction = encode(test_block.transaction)

            test_block.sequence_number = seq
            test_block.public_key = root_key
            test_block.link_public_key = unhexlify(pub_key)

            test_block.hash = test_block.calculate_hash()
            self.session.trustchain_community.persistence.add_block(test_block)

        for ind, friend in enumerate(friends):
            for ind2, fof in enumerate(fofs):
                test_block.transaction = get_dummy_tx()
                test_block._transaction = encode(test_block.transaction)

                test_block.sequence_number = ind + ind2
                test_block.public_key = unhexlify(friend)
                test_block.link_public_key = unhexlify(fof)

                test_block.hash = test_block.calculate_hash()
                self.session.trustchain_community.persistence.add_block(
                    test_block)

        for ind3, fof in enumerate(fofs):
            for ind4, fofof in enumerate(fofofs):
                test_block.transaction = get_dummy_tx()
                test_block._transaction = encode(test_block.transaction)

                test_block.sequence_number = ind3 + ind4
                test_block.public_key = unhexlify(fof)
                test_block.link_public_key = unhexlify(fofof)
                test_block.hash = test_block.calculate_hash()
                self.session.trustchain_community.persistence.add_block(
                    test_block)

        res = await self.do_request('trustview?depth=1', expected_code=200)
        verify_response(res, 4, 3)
        res = await self.do_request('trustview?depth=2', expected_code=200)
        verify_response(res, 7, 12)
        res = await self.do_request('trustview?depth=3', expected_code=200)
        verify_response(res, 10, 21)
        res = await self.do_request('trustview?depth=4', expected_code=200)
        verify_response(res, 10, 21)