def create_txs_service_msg(
        transaction_service: TransactionService,
        tx_service_snap: List[Sha256Hash],
        sync_tx_content: bool = True) -> List[TxContentShortIds]:
    task_start = time.time()
    txs_content_short_ids: List[TxContentShortIds] = []
    txs_msg_len = 0
    while tx_service_snap:
        transaction_key = transaction_service.get_transaction_key(
            tx_service_snap.pop())
        short_ids = list(
            transaction_service.get_short_ids_by_key(transaction_key))
        if sync_tx_content:
            tx_content = transaction_service.get_transaction_by_key(
                transaction_key)
        else:
            tx_content = bytearray(0)
        # TODO: evaluate short id quota type flag value
        short_id_flags = [
            transaction_service.get_short_id_transaction_type(short_id)
            for short_id in short_ids
        ]
        tx_content_short_ids: TxContentShortIds = TxContentShortIds(
            transaction_key.transaction_hash, tx_content, short_ids,
            short_id_flags)

        txs_msg_len += txs_serializer.get_serialized_tx_content_short_ids_bytes_len(
            tx_content_short_ids)

        txs_content_short_ids.append(tx_content_short_ids)
        if txs_msg_len >= constants.TXS_MSG_SIZE or time.time(
        ) - task_start > constants.TXS_SYNC_TASK_DURATION:
            break
    return txs_content_short_ids
示例#2
0
class SyncTxServiceTest(MessageFactoryTestCase):

    NETWORK_NUM = 12345

    def setUp(self) -> None:
        self.node = MockNode(helpers.get_common_opts(1234))

        self.network_num = 4
        self.transaction_service = TransactionService(self.node,
                                                      self.network_num)

    def get_message_factory(self):
        return bloxroute_message_factory

    def test_create_message_success_tx_service_sync_txs_msg(self):
        self._test_create_msg_success_tx_service_sync_with_tx_content_count(
            100)

    def test_create_message_success_tx_service_sync_txs_msg_with_exceeded_buf(
            self):
        self._test_create_msg_success_tx_service_sync_with_tx_content_count(
            1000)

    def _test_create_msg_success_tx_service_sync_with_tx_content_count(
            self, tx_content_count, sync_tx_content=True):
        short_ids = [
            list(range(1, 6)),
            list(range(11, 15)),
            list(range(53, 250)), [31],
            list(range(41, 48)), [51, 52]
        ]
        transaction_hashes = list(
            map(crypto.double_sha256, map(bytes, short_ids)))

        for i in range(len(short_ids)):
            transaction_content = bytearray(tx_content_count)
            transaction_content[:32] = transaction_hashes[i]
            transaction_key = self.transaction_service.get_transaction_key(
                transaction_hashes[i])
            self.transaction_service.set_transaction_contents_by_key(
                transaction_key, transaction_content)
            for short_id in short_ids[i]:
                self.transaction_service.assign_short_id_by_key(
                    transaction_key, short_id)

        # Six blocks received after
        for i in range(len(short_ids)):
            self.transaction_service.track_seen_short_ids(
                Sha256Hash(helpers.generate_bytearray(32)), short_ids[i])

        tx_service_snap = self.transaction_service.get_snapshot()

        txs_content_short_ids = tx_sync_service_helpers.create_txs_service_msg(
            self.transaction_service, tx_service_snap, sync_tx_content)

        if txs_content_short_ids:
            self._send_tx_msg(txs_content_short_ids, transaction_hashes)

    def _send_tx_msg(self, txs_content_short_ids, transaction_hashes):
        tx_service_sync_txs_msg: TxServiceSyncTxsMessage = \
            self.create_message_successfully(
                TxServiceSyncTxsMessage(
                    self.NETWORK_NUM, txs_content_short_ids
                ),
                TxServiceSyncTxsMessage
            )

        self.assertEqual(self.NETWORK_NUM,
                         tx_service_sync_txs_msg.network_num())
        self.assertEqual(len(txs_content_short_ids),
                         tx_service_sync_txs_msg.tx_count())
        tx_service_txs_content_short_ids = tx_service_sync_txs_msg.txs_content_short_ids(
        )
        tx_contents = [
            self.transaction_service.get_transaction(short_id).contents
            for tx_content_short_id in tx_service_txs_content_short_ids
            for short_id in tx_content_short_id.short_ids
        ]
        for tx_content_short_id in tx_service_txs_content_short_ids:
            self.assertIn(bytearray(tx_content_short_id.tx_hash),
                          transaction_hashes)
            self.assertIn(bytearray(tx_content_short_id.tx_content),
                          tx_contents)
            self.assertEqual(
                tx_content_short_id.short_ids,
                list(
                    self.transaction_service.get_short_ids_by_key(
                        self.transaction_service.get_transaction_key(
                            tx_content_short_id.tx_hash))))