def msg_compressed_block_txs(self, msg: CompressedBlockTxsMessage):
        start_time = time.time()

        self.msg_txs(msg.to_txs_message(),
                     RecoveredTxsSource.COMPRESSED_BLOCK_TXS_RECEIVED)

        block_stats.add_block_event_by_block_hash(
            msg.block_hash(),
            BlockStatEventType.ENC_BLOCK_COMPRESSED_TXS_RECEIVED_BY_GATEWAY,
            network_num=msg.network_num(),
            peers=[self],
            more_info="{}. processing time: {}".format(
                stats_format.connection(self),
                stats_format.timespan(start_time, time.time())))
Exemple #2
0
    def test_compressed_block_txs_message_to_txs_message(self):
        block_hash = Sha256Hash(helpers.generate_bytearray(32))
        txs_info = [
            TransactionInfo(Sha256Hash(helpers.generate_bytearray(32)),
                            helpers.generate_bytearray(200), 111),
            TransactionInfo(Sha256Hash(helpers.generate_bytearray(32)),
                            helpers.generate_bytearray(300), 222),
            TransactionInfo(Sha256Hash(helpers.generate_bytearray(32)),
                            helpers.generate_bytearray(400), 333)
        ]

        block_txs_message = CompressedBlockTxsMessage(self.NETWORK_NUM,
                                                      block_hash, txs_info)
        txs_message = block_txs_message.to_txs_message()

        self.assertEqual(TxsMessage.MESSAGE_TYPE, txs_message.msg_type())

        parsed_txs = txs_message.get_txs()

        self.assertEqual(len(parsed_txs), len(parsed_txs))

        for index in range(len(txs_info)):
            self.assertEqual(parsed_txs[index].short_id,
                             txs_info[index].short_id)
            self.assertEqual(parsed_txs[index].contents,
                             txs_info[index].contents)
            self.assertEqual(parsed_txs[index].hash, txs_info[index].hash)

        raw_bytes = txs_message.rawbytes()

        self.assertEqual(raw_bytes[:constants.STARTING_SEQUENCE_BYTES_LEN],
                         constants.STARTING_SEQUENCE_BYTES)
        self.assertIsNotNone(txs_message.payload())
        self.assertIsNotNone(txs_message.payload_len())
        # control flag check
        self.assertEqual(raw_bytes[-1], 1)
Exemple #3
0
    def test_create_message_success_all_types(self):
        test_network_num = 10
        test_protocol_version = bloxroute_version_manager.CURRENT_PROTOCOL_VERSION

        hello_message = self.create_message_successfully(
            HelloMessage(protocol_version=test_protocol_version,
                         network_num=test_network_num,
                         node_id=self.NODE_ID), HelloMessage)
        self.assertEqual(test_protocol_version,
                         hello_message.protocol_version())
        self.assertEqual(test_network_num, hello_message.network_num())
        self.assertEqual(self.NODE_ID, hello_message.node_id())
        self.create_message_successfully(AckMessage(), AckMessage)
        self.create_message_successfully(PingMessage(), PingMessage)
        self.create_message_successfully(PongMessage(), PongMessage)

        blob = bytearray(4)
        broadcast_message = self.create_message_successfully(
            BroadcastMessage(self.HASH,
                             network_num=test_network_num,
                             is_encrypted=True,
                             source_id=self.NODE_ID,
                             blob=blob), BroadcastMessage)
        self.assertEqual(self.HASH, broadcast_message.block_hash())
        self.assertEqual(test_network_num, broadcast_message.network_num())
        self.assertEqual(self.NODE_ID, broadcast_message.source_id())
        self.assertTrue(broadcast_message.is_encrypted())
        self.assertEqual(blob, broadcast_message.blob().tobytes())

        sid = 12
        tx_val = bytes(1 for _ in range(5))
        tx_message = self.create_message_successfully(
            TxMessage(self.HASH,
                      network_num=test_network_num,
                      source_id=self.NODE_ID,
                      short_id=sid,
                      tx_val=tx_val), TxMessage)
        self.assertEqual(self.HASH, tx_message.tx_hash())
        self.assertEqual(self.NODE_ID, tx_message.source_id())
        self.assertEqual(sid, tx_message.short_id())
        self.assertEqual(test_network_num, tx_message.network_num())
        self.assertEqual(tx_val, tx_message.tx_val())

        key = bytearray(1 for _ in range(KEY_SIZE))
        key_message = self.create_message_successfully(
            KeyMessage(self.HASH, test_network_num, self.NODE_ID,
                       bytearray(1 for _ in range(KEY_SIZE))), KeyMessage)
        self.assertEqual(key, key_message.key())
        self.assertEqual(self.NODE_ID, key_message.source_id())
        self.assertEqual(test_network_num, key_message.network_num())
        self.assertEqual(self.HASH, key_message.block_hash())

        block_holding_message = self.create_message_successfully(
            BlockHoldingMessage(self.HASH, test_network_num, self.NODE_ID),
            BlockHoldingMessage)
        self.assertEqual(self.NODE_ID, block_holding_message.source_id())
        self.assertEqual(test_network_num, block_holding_message.network_num())
        self.assertEqual(self.HASH, block_holding_message.block_hash())

        get_txs = [1, 2, 3]
        get_txs_message = self.create_message_successfully(
            GetTxsMessage(get_txs), GetTxsMessage)
        self.assertEqual(get_txs, get_txs_message.get_short_ids())

        txs = [
            TransactionInfo(Sha256Hash(crypto.double_sha256(b"123")),
                            bytearray(4), 1),
            TransactionInfo(Sha256Hash(crypto.double_sha256(b"234")),
                            bytearray(8), 2)
        ]
        txs_message = self.create_message_successfully(TxsMessage(txs),
                                                       TxsMessage)
        result_txs = txs_message.get_txs()
        for i, result_tx in enumerate(result_txs):
            self.assertEqual(txs[i].hash, result_tx.hash)
            self.assertEqual(txs[i].contents, result_tx.contents)
            self.assertEqual(txs[i].short_id, result_tx.short_id)

        get_tx_contents_message = self.create_message_successfully(
            GetTxContentsMessage(test_network_num, sid), GetTxContentsMessage)
        self.assertEqual(sid, get_tx_contents_message.get_short_id())
        self.assertEqual(test_network_num,
                         get_tx_contents_message.network_num())

        tx_info = TransactionInfo(Sha256Hash(crypto.double_sha256(b"123")),
                                  bytearray(4), 1)
        tx_contents_message = self.create_message_successfully(
            TxContentsMessage(test_network_num, tx_info), TxContentsMessage)
        self.assertEqual(test_network_num, tx_contents_message.network_num())
        result_tx_info = tx_contents_message.get_tx_info()
        self.assertEqual(tx_info.hash, result_tx_info.hash)
        self.assertEqual(tx_info.contents, result_tx_info.contents)
        self.assertEqual(tx_info.short_id, result_tx_info.short_id)

        short_ids = [1, 2, 33, 4444, 1234]
        block_hash = Sha256Hash(helpers.generate_bytearray(32))

        get_block_txs_message: GetCompressedBlockTxsMessage = self.create_message_successfully(
            GetCompressedBlockTxsMessage(self.NETWORK_NUM, block_hash,
                                         short_ids),
            GetCompressedBlockTxsMessage)
        self.assertEqual(self.NETWORK_NUM, get_block_txs_message.network_num())
        self.assertEqual(block_hash, get_block_txs_message.block_hash())
        self.assertEqual(len(short_ids), len(get_block_txs_message))
        self.assertEqual(short_ids, get_block_txs_message.get_short_ids())

        txs_info = [
            TransactionInfo(Sha256Hash(helpers.generate_bytearray(32)),
                            helpers.generate_bytearray(200), 111),
            TransactionInfo(Sha256Hash(helpers.generate_bytearray(32)),
                            helpers.generate_bytearray(300), 222),
            TransactionInfo(Sha256Hash(helpers.generate_bytearray(32)),
                            helpers.generate_bytearray(400), 333)
        ]

        block_txs_message: CompressedBlockTxsMessage = self.create_message_successfully(
            CompressedBlockTxsMessage(self.NETWORK_NUM, block_hash, txs_info),
            CompressedBlockTxsMessage)
        self.assertEqual(self.NETWORK_NUM, block_txs_message.network_num())
        self.assertEqual(block_hash, block_txs_message.block_hash())
        self.assertEqual(len(txs_info), len(block_txs_message))

        parsed_txs = block_txs_message.get_txs()

        for index in range(len(txs_info)):
            self.assertEqual(parsed_txs[index].short_id,
                             txs_info[index].short_id)
            self.assertEqual(parsed_txs[index].contents,
                             txs_info[index].contents)
            self.assertEqual(parsed_txs[index].hash, txs_info[index].hash)