def test_place_hold(self):
        hash1 = Sha256Hash(helpers.generate_bytearray(crypto.SHA256_HASH_LEN))
        hash2 = Sha256Hash(helpers.generate_bytearray(crypto.SHA256_HASH_LEN))

        self.sut.place_hold(hash1, self.dummy_connection)
        self.sut.place_hold(hash2, self.dummy_connection)

        self.assertEqual(2, len(self.sut._holds.contents))
        self.assertEqual(2, len(self.node.broadcast_messages))
        self.assertEqual(BlockHoldingMessage(hash1, network_num=1),
                         self.node.broadcast_messages[0][0])
        self.assertEqual(BlockHoldingMessage(hash2, network_num=1),
                         self.node.broadcast_messages[1][0])
Пример #2
0
    def place_hold(self, block_hash, connection) -> None:
        """
        Places hold on block hash and propagates message.
        :param block_hash: ObjectHash
        :param connection:
        """
        block_stats.add_block_event_by_block_hash(
            block_hash,
            BlockStatEventType.BLOCK_HOLD_REQUESTED,
            network_num=connection.network_num,
            peers=[connection],
        )

        if block_hash in self._node.blocks_seen.contents:
            return

        if block_hash not in self._holds.contents:
            self._holds.add(block_hash, BlockHold(time.time(), connection))
            conns = self._node.broadcast(
                BlockHoldingMessage(block_hash, self._node.network_num),
                broadcasting_conn=connection,
                connection_types=(ConnectionType.RELAY_BLOCK,
                                  ConnectionType.GATEWAY))
            if len(conns) > 0:
                block_stats.add_block_event_by_block_hash(
                    block_hash,
                    BlockStatEventType.BLOCK_HOLD_SENT_BY_GATEWAY_TO_PEERS,
                    network_num=self._node.network_num,
                    peers=conns,
                )
Пример #3
0
    def test_block_hold_timeout(self):
        block = btc_block()
        block_hash = block.block_hash()
        block_hold_msg_bytes = BlockHoldingMessage(block_hash, 1).rawbytes()

        helpers.receive_node_message(self.node1, self.gateway_fileno, block_hold_msg_bytes)
        helpers.receive_node_message(self.node1, self.blockchain_fileno, block.rawbytes())
        block_hold_msg_bytes_to_send = self.node1.get_bytes_to_send(self.relay_fileno)
        self.assertEqual(block_hold_msg_bytes_to_send, block_hold_msg_bytes)
        self.assertEqual(1, len(self.node1.block_processing_service._holds.contents))
        self.node1.on_bytes_sent(self.relay_fileno, len(block_hold_msg_bytes_to_send))

        time.time = MagicMock(
            return_value=time.time() + self.node1.block_processing_service._compute_hold_timeout(block))
        self.node1.alarm_queue.fire_alarms()

        # send ciphertext from node1, receipt from node2, key from node1
        relayed_block = self.node1.get_bytes_to_send(self.relay_fileno)
        self.assertIn(BroadcastMessage.MESSAGE_TYPE, relayed_block.tobytes())
        self.node1.on_bytes_sent(self.relay_fileno, len(relayed_block))

        helpers.receive_node_message(self.node2, self.relay_fileno, relayed_block)
        block_receipt = self.node2.get_bytes_to_send(self.gateway_fileno)
        self.assertIn(BlockReceivedMessage.MESSAGE_TYPE, block_receipt.tobytes())

        helpers.receive_node_message(self.node1, self.gateway_fileno, block_receipt)
        key_message = self.node1.get_bytes_to_send(self.relay_fileno)
        self.assertIn(KeyMessage.MESSAGE_TYPE, key_message.tobytes())
        self.assertNotEqual(OutputBuffer.EMPTY, bytearray(key_message.tobytes()))

        helpers.receive_node_message(self.node2, self.relay_fileno, key_message)
        bytes_to_blockchain = self.node2.get_bytes_to_send(self.blockchain_fileno)
        block_bytes = block.rawbytes().tobytes()
        self.assertEqual(block_bytes, bytes_to_blockchain[0:len(block_bytes)].tobytes())
 def test_block_hold_message(self):
     block_hash = Sha256Hash(helpers.generate_bytes(crypto.SHA256_HASH_LEN))
     network_num = 12345
     self._test_to_old_version_broadcast_message(BlockHoldingMessage(block_hash=block_hash, network_num=network_num,
                                                                     source_id=NEW_VERSION_SOURCE_ID))
     self._test_to_new_version_broadcast_message(BlockHoldingMessageV4(block_hash=block_hash,
                                                                       network_num=network_num))
Пример #5
0
    def test_msg_block_hold(self):
        self.node.block_processing_service.place_hold = MagicMock()

        block_hash = Sha256Hash(
            helpers.generate_bytearray(crypto.SHA256_HASH_LEN))
        message = BlockHoldingMessage(block_hash, network_num=123)
        self.connection.msg_block_holding(message)
        self.node.block_processing_service.place_hold.assert_called_once_with(
            block_hash, self.connection)
Пример #6
0
    def queue_block_for_processing(self, block_message, connection) -> None:
        """
        Queues up block for processing on timeout if hold message received.
        If no hold exists, compress and broadcast block immediately.
        :param block_message: block message to process
        :param connection: receiving connection (AbstractBlockchainConnection)
        """

        block_hash = block_message.block_hash()
        connection.log_info("Processing block {} from local blockchain node.",
                            block_hash)

        valid_block = self._validate_block_header_in_block_message(
            block_message)
        if not valid_block.is_valid:
            reason = valid_block.reason
            assert reason is not None
            block_stats.add_block_event_by_block_hash(
                block_hash,
                BlockStatEventType.
                BLOCK_RECEIVED_FROM_BLOCKCHAIN_NODE_FAILED_VALIDATION,
                connection.network_num,
                more_info=reason)
            return

        if block_hash in self._holds.contents:
            hold: BlockHold = self._holds.contents[block_hash]
            block_stats.add_block_event_by_block_hash(
                block_hash,
                BlockStatEventType.BLOCK_HOLD_HELD_BLOCK,
                network_num=connection.network_num,
                peers=[hold.holding_connection],
            )
            if hold.alarm is None:
                hold.alarm = self._node.alarm_queue.register_alarm(
                    self._node.opts.blockchain_block_hold_timeout_s,
                    self._holding_timeout, block_hash, hold)
                hold.block_message = block_message
                hold.connection = connection
        else:
            if self._node.opts.encrypt_blocks:
                # Broadcast holding message if gateway wants to encrypt blocks
                conns = self._node.broadcast(BlockHoldingMessage(
                    block_hash, self._node.network_num),
                                             broadcasting_conn=connection,
                                             prepend_to_queue=True,
                                             connection_types=[
                                                 ConnectionType.RELAY_BLOCK,
                                                 ConnectionType.GATEWAY
                                             ])
                if len(conns) > 0:
                    block_stats.add_block_event_by_block_hash(
                        block_hash,
                        BlockStatEventType.BLOCK_HOLD_SENT_BY_GATEWAY_TO_PEERS,
                        network_num=self._node.network_num,
                        peers=conns)
            self._process_and_broadcast_block(block_message, connection)
Пример #7
0
    def test_message_preview_success_all_gateway_types(self):
        self.get_message_preview_successfully(
            GatewayHelloMessage(123, 1, "127.0.0.1", 40000, 1),
            GatewayMessageType.HELLO,
            GatewayHelloMessage.PAYLOAD_LENGTH,
        )
        self.get_message_preview_successfully(
            BlockReceivedMessage(self.HASH),
            GatewayMessageType.BLOCK_RECEIVED,
            BlockReceivedMessage.PAYLOAD_LENGTH,
        )
        self.get_message_preview_successfully(
            BlockPropagationRequestMessage(self.BLOCK),
            GatewayMessageType.BLOCK_PROPAGATION_REQUEST,
            len(self.BLOCK) + constants.CONTROL_FLAGS_LEN,
        )
        self.get_message_preview_successfully(
            BlockHoldingMessage(self.HASH, network_num=123),
            BloxrouteMessageType.BLOCK_HOLDING,
            BlockHoldingMessage.PAYLOAD_LENGTH,
        )
        self.get_message_preview_successfully(
            ConfirmedTxMessage(self.HASH, self.TX_VAL),
            GatewayMessageType.CONFIRMED_TX,
            ConfirmedTxMessage.PAYLOAD_LENGTH + len(self.TX_VAL),
        )
        self.get_message_preview_successfully(
            RequestTxStreamMessage(),
            GatewayMessageType.REQUEST_TX_STREAM,
            constants.CONTROL_FLAGS_LEN,
        )

        hash_val = BtcObjectHash(buf=crypto.double_sha256(b"123"),
                                 length=crypto.SHA256_HASH_LEN)
        blockchain_message = GetBlocksBtcMessage(12345, 23456, [hash_val],
                                                 hash_val).rawbytes()
        self.get_message_preview_successfully(
            BlockchainSyncRequestMessage(GetBlocksBtcMessage.MESSAGE_TYPE,
                                         blockchain_message),
            BlockchainSyncRequestMessage.MESSAGE_TYPE,
            MSG_TYPE_LEN + len(blockchain_message),
        )
        self.get_message_preview_successfully(
            BlockchainSyncResponseMessage(GetBlocksBtcMessage.MESSAGE_TYPE,
                                          blockchain_message),
            BlockchainSyncResponseMessage.MESSAGE_TYPE,
            MSG_TYPE_LEN + len(blockchain_message),
        )
Пример #8
0
    def test_send_receive_block_and_key_block_hold_removed(self):
        block = btc_block()
        block_hash = block.block_hash()

        block_hold_msg_bytes = BlockHoldingMessage(block_hash, 1).rawbytes()
        helpers.receive_node_message(self.node1, self.gateway_fileno,
                                     block_hold_msg_bytes)
        helpers.receive_node_message(self.node1, self.blockchain_fileno,
                                     block.rawbytes())
        bytes_to_send = self.node1.get_bytes_to_send(self.relay_fileno)
        self.assertEqual(bytes_to_send, block_hold_msg_bytes)
        self.assertEqual(
            1, len(self.node1.block_processing_service._holds.contents))

        # node 2 encrypts and sends over block
        helpers.receive_node_message(self.node2, self.blockchain_fileno,
                                     block.rawbytes())
        block_hold_request_relay = self.node2.get_bytes_to_send(
            self.relay_fileno)
        self.assertIn(BlockHoldingMessage.MESSAGE_TYPE,
                      block_hold_request_relay.tobytes())
        self.node2.on_bytes_sent(self.relay_fileno,
                                 len(block_hold_request_relay))

        relayed_block = self.node2.get_bytes_to_send(self.relay_fileno)
        self.assertIn(BroadcastMessage.MESSAGE_TYPE, relayed_block.tobytes())
        self.node2.on_bytes_sent(self.relay_fileno, len(relayed_block))

        # send receipt
        helpers.receive_node_message(self.node1, self.relay_fileno,
                                     relayed_block)
        block_receipt = self.node1.get_bytes_to_send(self.gateway_fileno)
        self.assertIn(BlockReceivedMessage.MESSAGE_TYPE,
                      block_receipt.tobytes())

        # send key
        helpers.receive_node_message(self.node2, self.gateway_fileno,
                                     block_receipt)
        key_message = self.node2.get_bytes_to_send(self.relay_fileno)
        self.assertIn(KeyMessage.MESSAGE_TYPE, key_message.tobytes())

        # receive key and lift hold
        helpers.receive_node_message(self.node1, self.relay_fileno,
                                     key_message)
        inv_message = self.node1.get_bytes_to_send(self.blockchain_fileno)
        self.assertIn(InvBtcMessage.MESSAGE_TYPE, inv_message.tobytes())
        self.assertEqual(
            0, len(self.node1.block_processing_service._holds.contents))
    def test_queue_block_no_hold(self):
        block_hash = Sha256Hash(
            helpers.generate_bytearray(crypto.SHA256_HASH_LEN))
        block_message = MockBlockMessage(block_hash)
        connection = MockBlockchainConnection(
            MockSocketConnection(node=self.node,
                                 ip_address=LOCALHOST,
                                 port=8000), self.node)

        self.sut.queue_block_for_processing(block_message, connection)
        self._assert_block_propagated(block_hash)

        broadcasted_messages = self.node.broadcast_messages
        self.assertEqual(1, len(broadcasted_messages))
        self.assertEqual(BlockHoldingMessage(block_hash, network_num=1),
                         broadcasted_messages[0][0])
Пример #10
0
    def queue_block_for_processing(self, block_message, connection):
        """
        Queues up block for processing on timeout if hold message received.
        If no hold exists, compress and broadcast block immediately.
        :param block_message: block message to process
        :param connection: receiving connection (AbstractBlockchainConnection)
        """

        block_hash = block_message.block_hash()
        connection.log_info("Processing block {} from local blockchain node.",
                            block_hash)

        if block_hash in self._holds.contents:
            hold: BlockHold = self._holds.contents[block_hash]
            block_stats.add_block_event_by_block_hash(
                block_hash,
                BlockStatEventType.BLOCK_HOLD_HELD_BLOCK,
                network_num=connection.network_num,
                more_info=stats_format.connection(hold.holding_connection))
            if hold.alarm is None:
                hold.alarm = self._node.alarm_queue.register_alarm(
                    self._node.opts.blockchain_block_hold_timeout_s,
                    self._holding_timeout, block_hash, hold)
                hold.block_message = block_message
                hold.connection = connection
        else:
            # Broadcast BlockHoldingMessage through relays and gateways
            conns = self._node.broadcast(BlockHoldingMessage(
                block_hash, self._node.network_num),
                                         broadcasting_conn=connection,
                                         prepend_to_queue=True,
                                         connection_types=[
                                             ConnectionType.RELAY_BLOCK,
                                             ConnectionType.GATEWAY
                                         ])
            if len(conns) > 0:
                block_stats.add_block_event_by_block_hash(
                    block_hash,
                    BlockStatEventType.BLOCK_HOLD_SENT_BY_GATEWAY_TO_PEERS,
                    network_num=self._node.network_num,
                    more_info=stats_format.connections(conns))
            self._process_and_broadcast_block(block_message, connection)
    def test_message_preview_success_all_types(self):
        self.get_message_preview_successfully(HelloMessage(protocol_version=1, network_num=2),
                                              HelloMessage.MESSAGE_TYPE,
                                              VersionMessage.VERSION_MESSAGE_LENGTH + UL_INT_SIZE_IN_BYTES +
                                              NODE_ID_SIZE_IN_BYTES - UL_INT_SIZE_IN_BYTES)
        self.get_message_preview_successfully(AckMessage(), AckMessage.MESSAGE_TYPE, constants.CONTROL_FLAGS_LEN)
        self.get_message_preview_successfully(PingMessage(), PingMessage.MESSAGE_TYPE, 9)
        self.get_message_preview_successfully(PongMessage(), PongMessage.MESSAGE_TYPE, 9)

        blob = bytearray(1 for _ in range(4))
        self.get_message_preview_successfully(BroadcastMessage(self.HASH, 1, self.NODE_ID, True, blob),
                                              BroadcastMessage.MESSAGE_TYPE,
                                              SHA256_HASH_LEN + NETWORK_NUM_LEN + BLOCK_ENCRYPTED_FLAG_LEN +
                                              constants.NODE_ID_SIZE_IN_BYTES + len(blob) + constants.CONTROL_FLAGS_LEN)
        self.get_message_preview_successfully(TxMessage(self.HASH, 1, self.NODE_ID, 12, blob),
                                              TxMessage.MESSAGE_TYPE,
                                              SHA256_HASH_LEN + NETWORK_NUM_LEN + UL_INT_SIZE_IN_BYTES +
                                              constants.NODE_ID_SIZE_IN_BYTES + len(blob) + constants.CONTROL_FLAGS_LEN)
        self.get_message_preview_successfully(KeyMessage(self.HASH, 1, self.NODE_ID,
                                                         bytearray(1 for _ in range(KEY_SIZE))),
                                              KeyMessage.MESSAGE_TYPE,
                                              SHA256_HASH_LEN + KEY_SIZE + NETWORK_NUM_LEN +
                                              constants.NODE_ID_SIZE_IN_BYTES + constants.CONTROL_FLAGS_LEN)
        self.get_message_preview_successfully(BlockHoldingMessage(self.HASH, 1, self.NODE_ID),
                                              BlockHoldingMessage.MESSAGE_TYPE,
                                              SHA256_HASH_LEN + NETWORK_NUM_LEN + constants.NODE_ID_SIZE_IN_BYTES +
                                              constants.CONTROL_FLAGS_LEN)

        get_txs = [1, 2, 3]
        self.get_message_preview_successfully(GetTxsMessage(get_txs), GetTxsMessage.MESSAGE_TYPE,
                                              UL_INT_SIZE_IN_BYTES + UL_INT_SIZE_IN_BYTES * len(
                                                  get_txs) + constants.CONTROL_FLAGS_LEN)

        txs = [TransactionInfo(crypto.double_sha256(b"123"), bytearray(4), 1),
               TransactionInfo(crypto.double_sha256(b"234"), bytearray(8), 2)]
        expected_length = (UL_INT_SIZE_IN_BYTES +
                           sum(UL_INT_SIZE_IN_BYTES + SHA256_HASH_LEN + UL_INT_SIZE_IN_BYTES +
                               len(tx.contents) for tx in txs) + constants.CONTROL_FLAGS_LEN)
        self.get_message_preview_successfully(TxsMessage(txs), TxsMessage.MESSAGE_TYPE, expected_length)
    def msg_consensus(self, msg: OntConsensusMessage):
        if not self.node.opts.is_consensus:
            return
        if msg.consensus_data_type() != ont_constants.BLOCK_PROPOSAL_CONSENSUS_MESSAGE_TYPE:
            return

        block_hash = msg.block_hash()
        node = self.connection.node
        if not node.should_process_block_hash(block_hash):
            return

        node.block_cleanup_service.on_new_block_received(block_hash, msg.prev_block_hash())
        block_stats.add_block_event_by_block_hash(block_hash,
                                                  BlockStatEventType.BLOCK_RECEIVED_FROM_BLOCKCHAIN_NODE,
                                                  network_num=self.connection.network_num,
                                                  broadcast_type=BroadcastMessageType.CONSENSUS,
                                                  more_info="Protocol: {}, Network: {}".format(
                                                      node.opts.blockchain_protocol,
                                                      node.opts.blockchain_network
                                                  ),
                                                  msg_size=len(msg.rawbytes())
                                                  )

        if block_hash in self.connection.node.blocks_seen.contents:
            self.node.on_block_seen_by_blockchain_node(block_hash, self.connection)
            block_stats.add_block_event_by_block_hash(block_hash,
                                                      BlockStatEventType.BLOCK_RECEIVED_FROM_BLOCKCHAIN_NODE_IGNORE_SEEN,
                                                      network_num=self.connection.network_num,
                                                      broadcast_type=BroadcastMessageType.CONSENSUS)
            self.connection.log_info(
                "Discarding duplicate consensus block {} from local blockchain node.",
                block_hash
            )
            return

        node.track_block_from_node_handling_started(block_hash)

        self.connection.log_info(
            "Processing consensus block {} from local blockchain node.",
            block_hash
        )

        # Broadcast BlockHoldingMessage through relays and gateways
        conns = self.node.broadcast(BlockHoldingMessage(block_hash, self.node.network_num),
                                    broadcasting_conn=self.connection, prepend_to_queue=True,
                                    connection_types=[ConnectionType.RELAY_BLOCK, ConnectionType.GATEWAY])
        if len(conns) > 0:
            block_stats.add_block_event_by_block_hash(block_hash,
                                                      BlockStatEventType.BLOCK_HOLD_SENT_BY_GATEWAY_TO_PEERS,
                                                      network_num=self.node.network_num,
                                                      broadcast_type=BroadcastMessageType.CONSENSUS,
                                                      peers=conns
                                                      )

        try:
            bx_block, block_info = self.node.consensus_message_converter.block_to_bx_block(
                msg,
                self.node.get_tx_service(),
                self.node.opts.enable_block_compression,
                self.node.network.min_tx_age_seconds
            )
        except MessageConversionError as e:
            block_stats.add_block_event_by_block_hash(
                e.msg_hash,
                BlockStatEventType.BLOCK_CONVERSION_FAILED,
                network_num=self.connection.network_num,
                broadcast_type=BroadcastMessageType.CONSENSUS,
                conversion_type=e.conversion_type.value
            )
            self.connection.log_error(log_messages.BLOCK_COMPRESSION_FAIL_ONT_CONSENSUS, e.msg_hash, e)
            return

        if block_info.ignored_short_ids:
            self.connection.log_debug(
                "Ignoring {} new SIDs for {}: {}",
                len(block_info.ignored_short_ids), block_info.block_hash, block_info.ignored_short_ids
            )

        block_stats.add_block_event_by_block_hash(block_hash,
                                                  BlockStatEventType.BLOCK_COMPRESSED,
                                                  start_date_time=block_info.start_datetime,
                                                  end_date_time=block_info.end_datetime,
                                                  network_num=self.connection.network_num,
                                                  broadcast_type=BroadcastMessageType.CONSENSUS,
                                                  prev_block_hash=block_info.prev_block_hash,
                                                  original_size=block_info.original_size,
                                                  txs_count=block_info.txn_count,
                                                  blockchain_network=self.node.opts.blockchain_network,
                                                  blockchain_protocol=self.node.opts.blockchain_protocol,
                                                  matching_block_hash=block_info.compressed_block_hash,
                                                  matching_block_type=StatBlockType.COMPRESSED.value,
                                                  more_info="Consensus compression: {}->{} bytes, {}, {}; "
                                                            "Tx count: {}".format(
                                                      block_info.original_size,
                                                      block_info.compressed_size,
                                                      stats_format.percentage(block_info.compression_rate),
                                                      stats_format.duration(block_info.duration_ms),
                                                      block_info.txn_count
                                                  )
                                                  )

        self.node.block_processing_service._process_and_broadcast_compressed_block(
            bx_block, self.connection, block_info, block_hash
        )
Пример #13
0
    def test_create_message_success_all_gateway_types(self):
        hello_message = self.create_message_successfully(
            GatewayHelloMessage(123, 1, "127.0.0.1", 40001, 1),
            GatewayHelloMessage)
        self.assertEqual(123, hello_message.protocol_version())
        self.assertEqual(1, hello_message.network_num())
        self.assertEqual("127.0.0.1", hello_message.ip())
        self.assertEqual(40001, hello_message.port())
        self.assertEqual(1, hello_message.ordering())

        block_recv_message = self.create_message_successfully(
            BlockReceivedMessage(self.HASH), BlockReceivedMessage)
        self.assertEqual(self.HASH, block_recv_message.block_hash())

        block_holding_message = self.create_message_successfully(
            BlockHoldingMessage(self.HASH, network_num=123),
            BlockHoldingMessage)
        self.assertEqual(self.HASH, block_holding_message.block_hash())

        block_propagation_request_message = self.create_message_successfully(
            BlockPropagationRequestMessage(self.BLOCK),
            BlockPropagationRequestMessage)
        self.assertEqual(self.BLOCK, block_propagation_request_message.blob())

        hash_val = BtcObjectHash(buf=crypto.double_sha256(b"123"),
                                 length=crypto.SHA256_HASH_LEN)
        blockchain_message_in = GetBlocksBtcMessage(12345, 23456, [hash_val],
                                                    hash_val).rawbytes()
        sync_request_message = self.create_message_successfully(
            BlockchainSyncRequestMessage(GetBlocksBtcMessage.MESSAGE_TYPE,
                                         blockchain_message_in),
            BlockchainSyncRequestMessage,
        )
        blockchain_message_out = GetBlocksBtcMessage(
            buf=sync_request_message.payload())
        self.assertEqual(12345, blockchain_message_out.magic())
        self.assertEqual(23456, blockchain_message_out.version())
        self.assertEqual(1, blockchain_message_out.hash_count())
        self.assertEqual(hash_val, blockchain_message_out.hash_stop())

        self.create_message_successfully(
            BlockchainSyncResponseMessage(GetBlocksBtcMessage.MESSAGE_TYPE,
                                          blockchain_message_in),
            BlockchainSyncResponseMessage,
        )

        blockchain_message_out = GetBlocksBtcMessage(
            buf=sync_request_message.payload())
        self.assertEqual(12345, blockchain_message_out.magic())
        self.assertEqual(23456, blockchain_message_out.version())
        self.assertEqual(1, blockchain_message_out.hash_count())
        self.assertEqual(hash_val, blockchain_message_out.hash_stop())

        confirmed_tx = self.create_message_successfully(
            ConfirmedTxMessage(self.HASH, self.TX_VAL), ConfirmedTxMessage)
        self.assertEqual(self.HASH, confirmed_tx.tx_hash())
        self.assertEqual(self.TX_VAL, confirmed_tx.tx_val())

        confirmed_tx_no_content = self.create_message_successfully(
            ConfirmedTxMessage(self.HASH), ConfirmedTxMessage)
        self.assertEqual(self.HASH, confirmed_tx_no_content.tx_hash())
        self.assertEqual(TxMessage.EMPTY_TX_VAL,
                         confirmed_tx_no_content.tx_val())

        self.create_message_successfully(RequestTxStreamMessage(),
                                         RequestTxStreamMessage)
Пример #14
0
    def test_message_preview_success_all_types(self):
        self.get_message_preview_successfully(
            HelloMessage(protocol_version=1,
                         network_num=2), HelloMessage.MESSAGE_TYPE,
            VersionMessage.VERSION_MESSAGE_LENGTH + UL_INT_SIZE_IN_BYTES +
            NODE_ID_SIZE_IN_BYTES - UL_INT_SIZE_IN_BYTES)
        self.get_message_preview_successfully(AckMessage(),
                                              AckMessage.MESSAGE_TYPE,
                                              constants.CONTROL_FLAGS_LEN)
        self.get_message_preview_successfully(PingMessage(),
                                              PingMessage.MESSAGE_TYPE, 9)
        self.get_message_preview_successfully(PongMessage(),
                                              PongMessage.MESSAGE_TYPE, 17)

        blob = bytearray(1 for _ in range(4))
        self.get_message_preview_successfully(
            BroadcastMessage(self.HASH, 1, self.NODE_ID, self.BROADCAST_TYPE,
                             True, blob), BroadcastMessage.MESSAGE_TYPE,
            SHA256_HASH_LEN + NETWORK_NUM_LEN + constants.BROADCAST_TYPE_LEN +
            BLOCK_ENCRYPTED_FLAG_LEN + constants.NODE_ID_SIZE_IN_BYTES +
            len(blob) + constants.CONTROL_FLAGS_LEN)
        self.get_message_preview_successfully(
            TxMessage(self.HASH, 1, self.NODE_ID, 12,
                      blob), TxMessage.MESSAGE_TYPE, SHA256_HASH_LEN +
            NETWORK_NUM_LEN + UL_INT_SIZE_IN_BYTES + TRANSACTION_FLAG_LEN +
            constants.NODE_ID_SIZE_IN_BYTES + len(blob) +
            constants.DOUBLE_SIZE_IN_BYTES + constants.CONTROL_FLAGS_LEN)
        self.get_message_preview_successfully(
            KeyMessage(self.HASH, 1, self.NODE_ID,
                       bytearray(1 for _ in range(KEY_SIZE))),
            KeyMessage.MESSAGE_TYPE,
            SHA256_HASH_LEN + KEY_SIZE + NETWORK_NUM_LEN +
            constants.NODE_ID_SIZE_IN_BYTES + constants.CONTROL_FLAGS_LEN)
        self.get_message_preview_successfully(
            BlockHoldingMessage(self.HASH, 1, self.NODE_ID),
            BlockHoldingMessage.MESSAGE_TYPE,
            SHA256_HASH_LEN + NETWORK_NUM_LEN +
            constants.NODE_ID_SIZE_IN_BYTES + constants.CONTROL_FLAGS_LEN)

        get_txs = [1, 2, 3]
        self.get_message_preview_successfully(
            GetTxsMessage(get_txs), GetTxsMessage.MESSAGE_TYPE,
            UL_INT_SIZE_IN_BYTES + UL_INT_SIZE_IN_BYTES * len(get_txs) +
            constants.CONTROL_FLAGS_LEN)

        txs = [
            TransactionInfo(Sha256Hash(crypto.double_sha256(b"123")),
                            bytearray(4), 1),
            TransactionInfo(Sha256Hash(crypto.double_sha256(b"234")),
                            bytearray(8), 2)
        ]
        expected_length = (UL_INT_SIZE_IN_BYTES +
                           sum(UL_INT_SIZE_IN_BYTES + SHA256_HASH_LEN +
                               UL_INT_SIZE_IN_BYTES + len(tx.contents)
                               for tx in txs) + constants.CONTROL_FLAGS_LEN)
        self.get_message_preview_successfully(TxsMessage(txs),
                                              TxsMessage.MESSAGE_TYPE,
                                              expected_length)

        expected_length = (2 * constants.DOUBLE_SIZE_IN_BYTES) + (5 * constants.UL_SHORT_SIZE_IN_BYTES) + \
                          (7 * constants.UL_INT_SIZE_IN_BYTES) + constants.IP_ADDR_SIZE_IN_BYTES + \
                          constants.CONTROL_FLAGS_LEN
        node_stats = {}
        helpers.add_stats_to_node_stats(node_stats, "127.0.0.1", 8001, 200,
                                        300, 400, 500, 600, 700, 800, 100, 50)
        self.get_message_preview_successfully(
            BdnPerformanceStatsMessage(datetime.utcnow(), datetime.utcnow(),
                                       100, node_stats),
            BdnPerformanceStatsMessage.MESSAGE_TYPE, expected_length)

        # multi node bdn stats message
        expected_length = (
            constants.CONTROL_FLAGS_LEN +
            (2 * constants.DOUBLE_SIZE_IN_BYTES) +  # start/end time
            constants.UL_SHORT_SIZE_IN_BYTES +  # memory
            constants.UL_SHORT_SIZE_IN_BYTES +  # num blockchain peers
            (
                3 *  # num blockchain peers
                (
                    constants.IP_ADDR_SIZE_IN_BYTES +  # ip
                    constants.UL_SHORT_SIZE_IN_BYTES +  # port
                    (2 * constants.UL_SHORT_SIZE_IN_BYTES)
                    +  # original block stats
                    (7 * constants.UL_INT_SIZE_IN_BYTES))))  # rest of stats

        node_stats = {}
        helpers.add_stats_to_node_stats(node_stats, "127.0.0.1", 8001, 200,
                                        300, 400, 500, 600, 700, 800, 100, 50)
        helpers.add_stats_to_node_stats(node_stats, "127.0.0.2", 8002, 200,
                                        300, 400, 500, 600, 700, 800, 100, 50)
        helpers.add_stats_to_node_stats(node_stats, "127.0.0.3", 8003, 200,
                                        300, 400, 500, 600, 700, 800, 100, 50)
        self.get_message_preview_successfully(
            BdnPerformanceStatsMessage(datetime.utcnow(), datetime.utcnow(),
                                       100, node_stats),
            BdnPerformanceStatsMessage.MESSAGE_TYPE, expected_length)

        tx_info = TransactionInfo(crypto.double_sha256(b"123"), bytearray(4),
                                  1)
        expected_length = constants.NETWORK_NUM_LEN + constants.SID_LEN + SHA256_HASH_LEN + \
                          constants.UL_INT_SIZE_IN_BYTES + constants.CONTROL_FLAGS_LEN + len(tx_info.contents)
        self.get_message_preview_successfully(TxContentsMessage(5, tx_info),
                                              TxContentsMessage.MESSAGE_TYPE,
                                              expected_length)

        expected_length = constants.NETWORK_NUM_LEN + constants.SID_LEN + constants.CONTROL_FLAGS_LEN
        self.get_message_preview_successfully(
            GetTxContentsMessage(1, 2), GetTxContentsMessage.MESSAGE_TYPE,
            expected_length)
Пример #15
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)
    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)