예제 #1
0
    def __init__(self, previous=None):
        super(TriblerBandwidthTestBlock, self).__init__()
        crypto = ECCrypto()
        other = crypto.generate_key(u"curve25519").pub().key_to_bin()

        transaction = {
            'up': random.randint(201, 220),
            'down': random.randint(221, 240),
            'total_up': 0,
            'total_down': 0,
            'type': 'tribler_bandwidth'
        }

        if previous:
            self.key = previous.key
            transaction['total_up'] = previous.transaction[
                'total_up'] + transaction['up']
            transaction['total_down'] = previous.transaction[
                'total_down'] + transaction['down']
            TriblerBandwidthBlock.__init__(
                self, ('tribler_bandwidth', encode(transaction),
                       previous.public_key, previous.sequence_number + 1,
                       other, 0, previous.hash, 0, 0, 0))
        else:
            transaction['total_up'] = random.randint(241, 260)
            transaction['total_down'] = random.randint(261, 280)
            self.key = crypto.generate_key(u"curve25519")
            TriblerBandwidthBlock.__init__(
                self,
                ('tribler_bandwidth', encode(transaction),
                 self.key.pub().key_to_bin(), random.randint(50, 100), other,
                 0, sha256(str(random.randint(0, 100000))).digest(), 0, 0, 0))
        self.sign(self.key)
예제 #2
0
    def _encode_torrent(self, message):
        files = message.payload.files
        trackers = list(message.payload.trackers)
        name = message.payload.name

        # Filter out invalid trackers
        for tracker in trackers:
            if not get_uniformed_tracker_url(tracker) or len(tracker) > 200:
                trackers.remove(tracker)

        # files is a tuple of tuples (actually a list in tuple form)
        max_len = self._community.dispersy_sync_bloom_filter_bits / 8
        base_len = 20 + 8 + len(name)  # infohash, timestamp, name
        tracker_len = sum([len(tracker) for tracker in trackers])
        file_len = sum([len(f[0]) + 8 for f in files])  # file name, length
        # Check if the message fits in the bloomfilter
        if (base_len + tracker_len + file_len > max_len) and (len(trackers) > 10):
            # only use first 10 trackers, .torrents in the wild have been seen to have 1000+ trackers...
            trackers = trackers[:10]
            tracker_len = sum([len(tracker) for tracker in trackers])
        if base_len + tracker_len + file_len > max_len:
            # reduce files by the amount we are currently to big
            reduce_by = max_len / (base_len + tracker_len + file_len * 1.0)
            nr_files_to_include = int(len(files) * reduce_by)
            files = sample(files, nr_files_to_include)

        normal_msg = (pack('!20sQ', message.payload.infohash, message.payload.timestamp), message.payload.name,
                      tuple(files), tuple(trackers))

        return zlib.compress(encode(normal_msg)),
예제 #3
0
 def create_msg():
     normal_msg = pack(
         '!20sQ', message.payload.infohash,
         message.payload.timestamp), message.payload.name, tuple(
             files), tuple(trackers)
     normal_msg = encode(normal_msg)
     return zlib.compress(normal_msg)
예제 #4
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='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
예제 #5
0
    def _encode_moderation(self, message):
        dict = {"text": message.payload.text,
                "timestamp": message.payload.timestamp,
                "severity": message.payload.severity}

        dict["cause-mid"] = message.payload.cause_mid
        dict["cause-global-time"] = message.payload.cause_global_time
        return encode(dict),
예제 #6
0
 def _encode_search_request(self, message):
     packet = pack('!H',
                   message.payload.identifier), message.payload.keywords
     if message.payload.bloom_filter:
         packet = packet + (message.payload.bloom_filter.functions,
                            message.payload.bloom_filter.prefix,
                            message.payload.bloom_filter.bytes)
     packet = encode(packet)
     return packet,
예제 #7
0
    def __init__(self, previous=None):
        super(TriblerBandwidthTestBlock, self).__init__()
        crypto = ECCrypto()
        other = crypto.generate_key(u"curve25519").pub().key_to_bin()

        transaction = {'up': random.randint(201, 220), 'down': random.randint(221, 240), 'total_up': 0,
                       'total_down': 0, 'type': 'tribler_bandwidth'}

        if previous:
            self.key = previous.key
            transaction['total_up'] = previous.transaction['total_up'] + transaction['up']
            transaction['total_down'] = previous.transaction['total_down'] + transaction['down']
            TriblerBandwidthBlock.__init__(self, ('tribler_bandwidth', encode(transaction), previous.public_key,
                                                  previous.sequence_number + 1, other, 0, previous.hash, 0, 0, 0))
        else:
            transaction['total_up'] = random.randint(241, 260)
            transaction['total_down'] = random.randint(261, 280)
            self.key = crypto.generate_key(u"curve25519")
            TriblerBandwidthBlock.__init__(self, (
                'tribler_bandwidth', encode(transaction), self.key.pub().key_to_bin(), random.randint(50, 100), other,
                0, sha256(str(random.randint(0, 100000))).digest(), 0, 0, 0))
        self.sign(self.key)
예제 #8
0
    def test_get_statistics(self):
        """
        Testing whether the API returns the correct statistics
        """
        block = TrustChainBlock()
        block.public_key = self.session.lm.trustchain_community.my_peer.public_key.key_to_bin(
        )
        block.link_public_key = "deadbeef".decode("HEX")
        block.link_sequence_number = 21
        block.type = 'tribler_bandwidth'
        block.transaction = {
            "up": 42,
            "down": 8,
            "total_up": 1024,
            "total_down": 2048,
            "type": "tribler_bandwidth"
        }
        block._transaction = encode(block.transaction)
        block.sequence_number = 3
        block.previous_hash = "babecafe".decode("HEX")
        block.signature = "babebeef".decode("HEX")
        block.hash = block.calculate_hash()
        self.session.lm.trustchain_community.persistence.add_block(block)

        def verify_response(response):
            response_json = json.loads(response)
            self.assertTrue("statistics" in response_json)
            stats = response_json["statistics"]
            self.assertEqual(
                stats["id"],
                self.session.lm.trustchain_community.my_peer.public_key.
                key_to_bin().encode("HEX"))
            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)
            self.assertIn("latest_block", stats)
            self.assertNotEqual(stats["latest_block"]["insert_time"], "")
            self.assertEqual(stats["latest_block"]["hash"],
                             block.hash.encode("HEX"))
            self.assertEqual(stats["latest_block"]["link_public_key"],
                             "deadbeef")
            self.assertEqual(stats["latest_block"]["link_sequence_number"], 21)
            self.assertEqual(stats["latest_block"]["up"], 42)
            self.assertEqual(stats["latest_block"]["down"], 8)

        self.should_check_equality = False
        return self.do_request('trustchain/statistics',
                               expected_code=200).addCallback(verify_response)
예제 #9
0
    def _encode_modification(self, message):
        modification_on = message.payload.modification_on.load_message()
        dict = {"modification-type": message.payload.modification_type,
                "modification-value": message.payload.modification_value,
                "timestamp": message.payload.timestamp,
                "modification-on-mid": modification_on.authentication.member.mid,
                "modification-on-global-time": modification_on.distribution.global_time}

        prev_modification = message.payload.prev_modification_packet
        if prev_modification:
            message = prev_modification.load_message()
            dict["prev-modification-mid"] = message.authentication.member.mid
            dict["prev-modification-global-time"] = message.distribution.global_time

        return encode(dict),
예제 #10
0
    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 = {'up': 0, 'down': 100, 'total_up': 0, 'total_down': 100}
        test_block = TrustChainBlock()
        test_block.type = 'tribler_bandwidth'
        test_block.transaction = transaction
        test_block._transaction = encode(transaction)
        test_block.public_key = self.session.lm.trustchain_community.my_peer.public_key.key_to_bin()
        test_block.hash = test_block.calculate_hash()
        self.session.lm.trustchain_community.persistence.add_block(test_block)

        self.should_check_equality = False
        return self.do_request('trustchain/bootstrap?amount=10', expected_code=400)
예제 #11
0
    def test_decode_torrent(self):
        """
        Test the decoding of a torrent message
        """
        self.assertRaises(DropPacket, self.conversion._decode_torrent, None, 0, "abcd")
        self.assertRaises(DropPacket, self.conversion._decode_torrent, None, 0, zlib.compress("abcd"))

        # Test a successful decoding
        meta = self.channel_community.get_meta_message(u"torrent")
        msg = MockObject()
        msg.meta = meta

        torrent_msg = encode((pack('!20sQ', 'a' * 20, 12345), u'torrent', ((u'a', 1234),), ('http://track.er',)))
        _, msg = self.conversion._decode_torrent(msg, 0, zlib.compress(torrent_msg))

        self.assertEqual(msg.infohash, 'a' * 20)
        self.assertEqual(msg.name, u'torrent')
예제 #12
0
    def test_decode_torrent(self):
        """
        Test the decoding of a torrent message
        """
        self.assertRaises(DropPacket, self.conversion._decode_torrent, None, 0, "abcd")
        self.assertRaises(DropPacket, self.conversion._decode_torrent, None, 0, zlib.compress("abcd"))

        # Test a successful decoding
        meta = self.channel_community.get_meta_message(u"torrent")
        msg = MockObject()
        msg.meta = meta

        torrent_msg = encode((pack('!20sQ', 'a' * 20, 12345), u'torrent', ((u'a', 1234),), ('http://track.er',)))
        _, msg = self.conversion._decode_torrent(msg, 0, zlib.compress(torrent_msg))

        self.assertEqual(msg.infohash, 'a' * 20)
        self.assertEqual(msg.name, u'torrent')
예제 #13
0
    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 = {'up': 0, 'down': 100, 'total_up': 0, 'total_down': 100}
        test_block = TrustChainBlock()
        test_block.type = 'tribler_bandwidth'
        test_block.transaction = transaction
        test_block._transaction = encode(transaction)
        test_block.public_key = self.session.lm.trustchain_community.my_peer.public_key.key_to_bin(
        )
        test_block.hash = test_block.calculate_hash()
        self.session.lm.trustchain_community.persistence.add_block(test_block)

        self.should_check_equality = False
        return self.do_request('trustchain/bootstrap?amount=10',
                               expected_code=400)
예제 #14
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
예제 #15
0
    def test_get_bootstrap_identity_partial_tokens(self):
        """
        Testing whether the API return partial available credit when argument is supplied
        """
        transaction = {'up': 100, 'down': 0, 'total_up': 100, 'total_down': 0}
        transaction2 = {'up': 50, 'down': 0}

        def verify_response(response):
            response_json = json.loads(response)
            self.assertEqual(response_json['transaction'], transaction2)

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

        self.should_check_equality = False
        return self.do_request('trustchain/bootstrap?amount=50', expected_code=200).addCallback(verify_response)
예제 #16
0
    def test_get_statistics(self):
        """
        Testing whether the API returns the correct statistics
        """
        block = TrustChainBlock()
        block.public_key = self.session.lm.trustchain_community.my_peer.public_key.key_to_bin()
        block.link_public_key = "deadbeef".decode("HEX")
        block.link_sequence_number = 21
        block.type = 'tribler_bandwidth'
        block.transaction = {"up": 42, "down": 8, "total_up": 1024, "total_down": 2048, "type": "tribler_bandwidth"}
        block._transaction = encode(block.transaction)
        block.sequence_number = 3
        block.previous_hash = "babecafe".decode("HEX")
        block.signature = "babebeef".decode("HEX")
        block.hash = block.calculate_hash()
        self.session.lm.trustchain_community.persistence.add_block(block)

        def verify_response(response):
            response_json = json.loads(response)
            self.assertTrue("statistics" in response_json)
            stats = response_json["statistics"]
            self.assertEqual(stats["id"], self.session.lm.trustchain_community.my_peer.
                             public_key.key_to_bin().encode("HEX"))
            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)
            self.assertIn("latest_block", stats)
            self.assertNotEqual(stats["latest_block"]["insert_time"], "")
            self.assertEqual(stats["latest_block"]["hash"], block.hash.encode("HEX"))
            self.assertEqual(stats["latest_block"]["link_public_key"], "deadbeef")
            self.assertEqual(stats["latest_block"]["link_sequence_number"], 21)
            self.assertEqual(stats["latest_block"]["up"], 42)
            self.assertEqual(stats["latest_block"]["down"], 8)

        self.should_check_equality = False
        return self.do_request('trustchain/statistics', expected_code=200).addCallback(verify_response)
예제 #17
0
    def test_get_bootstrap_identity_partial_tokens(self):
        """
        Testing whether the API return partial available credit when argument is supplied
        """
        transaction = {'up': 100, 'down': 0, 'total_up': 100, 'total_down': 0}
        transaction2 = {'up': 50, 'down': 0}

        def verify_response(response):
            response_json = json.loads(response)
            self.assertEqual(response_json['transaction'], transaction2)

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

        self.should_check_equality = False
        return self.do_request('trustchain/bootstrap?amount=50',
                               expected_code=200).addCallback(verify_response)
예제 #18
0
    def _encode_comment(self, message):
        dict = {"text": message.payload.text,
                "timestamp": message.payload.timestamp}

        playlist_packet = message.payload.playlist_packet
        infohash = message.payload.infohash

        if message.payload.reply_to_mid:
            dict["reply-to-mid"] = message.payload.reply_to_mid
            dict["reply-to-global-time"] = message.payload.reply_to_global_time

        if message.payload.reply_after_mid:
            dict["reply-after-mid"] = message.payload.reply_after_mid
            dict["reply-after-global-time"] = message.payload.reply_after_global_time

        if playlist_packet:
            message = playlist_packet.load_message()
            dict["playlist-mid"] = message.authentication.member.mid
            dict["playlist-global-time"] = message.distribution.global_time

        if infohash:
            dict['infohash'] = infohash
        return encode(dict),
예제 #19
0
 def _encode_channelsearch_response(self, message):
     packet = encode((message.payload.keywords, message.payload.torrents))
     return packet,
예제 #20
0
 def _encode_channelsearch(self, message):
     packet = encode(message.payload.keywords)
     return packet,
예제 #21
0
 def create_msg():
     return encode(message.payload.torrents)
예제 #22
0
    def _encode_mark_torrent(self, message):
        dict = {"infohash": message.payload.infohash,
                "timestamp": message.payload.timestamp,
                "type": message.payload.type}

        return encode(dict),
예제 #23
0
 def _encode_channel(self, message):
     return encode((message.payload.name, message.payload.description)),
예제 #24
0
 def _encode_search_response(self, message):
     packet = pack('!H',
                   message.payload.identifier), message.payload.results
     return encode(packet),