Exemple #1
0
 def bx_block_to_block(
         self, bx_block_msg: memoryview,
         tx_service: ExtensionTransactionService
 ) -> BlockDecompressionResult:
     decompress_start_datetime = datetime.utcnow()
     decompress_start_timestamp = time.time()
     tsk = self.decompression_tasks.borrow_task()
     tsk.init(tpe.InputBytes(bx_block_msg), tx_service.proxy)
     try:
         task_pool_proxy.run_task(tsk)
     except tpe.AggregatedException as e:
         self.decompression_tasks.return_task(tsk)
         header_info = ont_normal_message_converter.parse_bx_block_header(
             bx_block_msg, deque())
         raise message_conversion_error.btc_block_decompression_error(
             header_info.block_hash, e)
     total_tx_count = tsk.tx_count()
     unknown_tx_hashes = [
         Sha256Hash(bytearray(unknown_tx_hash.binary()))
         for unknown_tx_hash in tsk.unknown_tx_hashes()
     ]
     unknown_tx_sids = tsk.unknown_tx_sids()
     block_hash = OntObjectHash(
         binary=convert.hex_to_bytes(tsk.block_hash().hex_string()))
     if tsk.success():
         ont_block_msg = BlockOntMessage(
             buf=memoryview(tsk.block_message()))
         logger.debug(
             "Successfully parsed block broadcast message. {} transactions "
             "in block {}", total_tx_count, block_hash)
     else:
         ont_block_msg = None
         logger.debug(
             "Block recovery needed for {}. Missing {} sids, {} tx hashes. "
             "Total txs in block: {}", block_hash, len(unknown_tx_sids),
             len(unknown_tx_hashes), total_tx_count)
     block_info = get_block_info(bx_block_msg, block_hash, tsk.short_ids(),
                                 decompress_start_datetime,
                                 decompress_start_timestamp, total_tx_count,
                                 ont_block_msg)
     self.decompression_tasks.return_task(tsk)
     return BlockDecompressionResult(ont_block_msg, block_info,
                                     unknown_tx_sids, unknown_tx_hashes)
    def bx_block_to_block(
            self, bx_block_msg: memoryview,
            tx_service: TransactionService) -> BlockDecompressionResult:
        """
        Uncompresses a bx_block from a broadcast bx_block message and converts to a raw Ontology bx_block.

        bx_block must be a memoryview, since memoryview[offset] returns a bytearray, while bytearray[offset] returns
        a byte.
        """
        # pyre-fixme[25]: Assertion will always fail.
        if not isinstance(bx_block_msg, memoryview):
            bx_block_msg = memoryview(bx_block_msg)

        decompress_start_datetime = datetime.utcnow()
        decompress_start_timestamp = time.time()

        # Initialize tracking of transaction and SID mapping
        block_pieces = deque()
        header_info = parse_bx_block_header(bx_block_msg, block_pieces)
        unknown_tx_sids, unknown_tx_hashes, offset = parse_bx_block_transactions(
            header_info.block_hash, bx_block_msg, header_info.offset,
            header_info.short_ids, header_info.block_offsets, tx_service,
            block_pieces)
        total_tx_count = header_info.txn_count

        if not unknown_tx_sids and not unknown_tx_hashes:
            ont_block_msg, offset = build_ont_block(block_pieces, offset)
            logger.debug(
                "Successfully parsed bx_block broadcast message. {} transactions in bx_block",
                total_tx_count)
        else:
            ont_block_msg = None
            logger.warning(log_messages.BLOCK_RECOVERY_NEEDED,
                           len(unknown_tx_sids), len(unknown_tx_hashes),
                           total_tx_count)
        block_info = get_block_info(bx_block_msg, header_info.block_hash,
                                    header_info.short_ids,
                                    decompress_start_datetime,
                                    decompress_start_timestamp, total_tx_count,
                                    ont_block_msg)
        return BlockDecompressionResult(ont_block_msg, block_info,
                                        unknown_tx_sids, unknown_tx_hashes)