Beispiel #1
0
    def test_broadcast_to_connection_type(self):
        relay_all_conn = self._add_connection(0, 9000, ALL_NETWORK_NUM, ConnectionType.RELAY_ALL)
        relay_block_conn = self._add_connection(1, 9001, ALL_NETWORK_NUM, ConnectionType.RELAY_BLOCK)
        relay_transaction_conn = self._add_connection(2, 9002, ALL_NETWORK_NUM, ConnectionType.RELAY_TRANSACTION)
        gateway_conn = self._add_connection(3, 9003, ALL_NETWORK_NUM, ConnectionType.EXTERNAL_GATEWAY)

        block_message = BroadcastMessage(Sha256Hash(helpers.generate_hash()), ALL_NETWORK_NUM, "",
                                         BroadcastMessageType.BLOCK, False, helpers.generate_bytearray(250))
        self.sut.broadcast(block_message, BroadcastOptions(connection_types=[ConnectionType.RELAY_BLOCK]))

        tx_message = BroadcastMessage(Sha256Hash(helpers.generate_hash()), ALL_NETWORK_NUM, "",
                                      BroadcastMessageType.BLOCK, False, helpers.generate_bytearray(250))
        self.sut.broadcast(tx_message, BroadcastOptions(connection_types=[ConnectionType.RELAY_TRANSACTION]))

        gateway_message = BroadcastMessage(Sha256Hash(helpers.generate_hash()), ALL_NETWORK_NUM, "",
                                           BroadcastMessageType.BLOCK, False, helpers.generate_bytearray(250))
        self.sut.broadcast(gateway_message, BroadcastOptions(connection_types=[ConnectionType.GATEWAY]))

        self.assertIn(block_message, relay_all_conn.enqueued_messages)
        self.assertIn(block_message, relay_block_conn.enqueued_messages)
        self.assertNotIn(block_message, relay_transaction_conn.enqueued_messages)
        self.assertNotIn(block_message, gateway_conn.enqueued_messages)

        self.assertIn(tx_message, relay_all_conn.enqueued_messages)
        self.assertNotIn(tx_message, relay_block_conn.enqueued_messages)
        self.assertIn(tx_message, relay_transaction_conn.enqueued_messages)
        self.assertNotIn(tx_message, gateway_conn.enqueued_messages)

        self.assertNotIn(gateway_message, relay_all_conn.enqueued_messages)
        self.assertNotIn(gateway_message, relay_block_conn.enqueued_messages)
        self.assertNotIn(gateway_message, relay_transaction_conn.enqueued_messages)
        self.assertIn(gateway_message, gateway_conn.enqueued_messages)
Beispiel #2
0
 def broadcast(self, msg: AbstractMessage, broadcasting_conn: Optional[AbstractConnection] = None,
               prepend_to_queue: bool = False, connection_types: Optional[List[ConnectionType]] = None) \
         -> List[AbstractConnection]:
     """
     Broadcasts message msg to connections of the specified type except requester.
     """
     if connection_types is None:
         connection_types = [ConnectionType.RELAY_ALL]
     options = BroadcastOptions(broadcasting_conn, prepend_to_queue,
                                connection_types)
     return self.broadcast_service.broadcast(msg, options)
Beispiel #3
0
    def test_broadcast_to_network_numbers(self):
        all_matching_network_num = self._add_connection(0, 9000, ALL_NETWORK_NUM)
        matching_network_num = self._add_connection(1, 9001, 1)
        not_matching_network_num = self._add_connection(2, 9002, 2)

        message = BroadcastMessage(Sha256Hash(helpers.generate_hash()), 1, "", BroadcastMessageType.BLOCK, False,
                                   helpers.generate_bytearray(250))
        self.sut.broadcast(message, BroadcastOptions(connection_types=[MockConnection.CONNECTION_TYPE]))

        self.assertIn(message, all_matching_network_num.enqueued_messages)
        self.assertIn(message, matching_network_num.enqueued_messages)
        self.assertNotIn(message, not_matching_network_num.enqueued_messages)