def wait_for_room_with_address(transport: MatrixTransport, address: Address, timeout: int = 10): with Timeout(timeout): room = transport._get_room_for_address(address) while room is None: transport._client.processed.wait() room = transport._get_room_for_address(address)
def test_join_invalid_discovery( local_matrix_servers, retry_interval_initial, retry_interval_max, retries_before_backoff, broadcast_rooms, ): """join_broadcast_room tries to join on all servers on available_servers config If any of the servers isn't reachable by synapse, it'll return a 500 response, which needs to be handled, and if no discovery room is found on any of the available_servers, one in our current server should be created """ transport = MatrixTransport( config=MatrixTransportConfig( broadcast_rooms=broadcast_rooms, retries_before_backoff=retries_before_backoff, retry_interval_initial=retry_interval_initial, retry_interval_max=retry_interval_max, server=local_matrix_servers[0], available_servers=["http://invalid.server"], ), environment=Environment.DEVELOPMENT, ) transport._client.api.retry_timeout = 0 transport._send_raw = MagicMock() raiden_service = MockRaidenService(None) transport.start(raiden_service, [], None) transport.log = MagicMock() discovery_room_name = make_room_alias(transport.chain_id, "discovery") assert isinstance(transport._broadcast_rooms.get(discovery_room_name), Room) transport.stop() transport.greenlet.get()
def test_matrix_broadcast( local_matrix_servers, retries_before_backoff, retry_interval_initial, retry_interval_max, broadcast_rooms, ): transport = MatrixTransport( config=MatrixTransportConfig( broadcast_rooms=broadcast_rooms, retries_before_backoff=retries_before_backoff, retry_interval_initial=retry_interval_initial, retry_interval_max=retry_interval_max, server=local_matrix_servers[0], available_servers=[local_matrix_servers[0]], ), environment=Environment.DEVELOPMENT, ) transport.start(MockRaidenService(None), [], "") gevent.idle() ms_room_name = make_room_alias(transport.chain_id, MONITORING_BROADCASTING_ROOM) ms_room = transport._broadcast_rooms.get(ms_room_name) assert isinstance(ms_room, Room) ms_room.send_text = MagicMock(spec=ms_room.send_text) for i in range(5): message = Processed(message_identifier=i, signature=EMPTY_SIGNATURE) transport._raiden_service.sign(message) transport.broadcast(MONITORING_BROADCASTING_ROOM, message) transport._schedule_new_greenlet(transport._broadcast_worker) gevent.idle() assert ms_room.send_text.call_count >= 1 # messages could have been bundled call_args_str = " ".join( str(arg) for arg in ms_room.send_text.call_args_list) for i in range(5): assert f'"message_identifier": "{i}"' in call_args_str transport.stop() transport.greenlet.get()
def test_matrix_discovery_room_offline_server( local_matrix_servers, retries_before_backoff, retry_interval_initial, retry_interval_max, broadcast_rooms, ): transport = MatrixTransport( config=MatrixTransportConfig( broadcast_rooms=broadcast_rooms, retries_before_backoff=retries_before_backoff, retry_interval_initial=retry_interval_initial, retry_interval_max=retry_interval_max, server=local_matrix_servers[0], available_servers=[local_matrix_servers[0], "https://localhost:1"], ), environment=Environment.DEVELOPMENT, ) transport.start(MockRaidenService(None), [], "") discovery_room_name = make_room_alias(transport.chain_id, "discovery") with gevent.Timeout(1): while not isinstance(transport._broadcast_rooms.get(discovery_room_name), Room): gevent.sleep(0.1) transport.stop() transport.greenlet.get()
def test_pfs_broadcast_messages( local_matrix_servers, retry_interval_initial, retry_interval_max, retries_before_backoff, monkeypatch, broadcast_rooms, route_mode, ): """ Test that RaidenService broadcasts PFSCapacityUpdate messages to PATH_FINDING_BROADCASTING_ROOM room on newly received balance proofs. """ transport = MatrixTransport( config=MatrixTransportConfig( broadcast_rooms=broadcast_rooms, retries_before_backoff=retries_before_backoff, retry_interval_initial=retry_interval_initial, retry_interval_max=retry_interval_max, server=local_matrix_servers[0], available_servers=[local_matrix_servers[0]], ), environment=Environment.DEVELOPMENT, ) transport._client.api.retry_timeout = 0 transport._send_raw = MagicMock() raiden_service = MockRaidenService(None) raiden_service.config.services.monitoring_enabled = True raiden_service.routing_mode = route_mode transport.start(raiden_service, [], None) pfs_room_name = make_room_alias(transport.chain_id, PATH_FINDING_BROADCASTING_ROOM) pfs_room = transport._broadcast_rooms.get(pfs_room_name) assert isinstance(pfs_room, Room) pfs_room.send_text = MagicMock(spec=pfs_room.send_text) raiden_service.transport = transport transport.log = MagicMock() # send PFSCapacityUpdate balance_proof = factories.create(HOP1_BALANCE_PROOF) channel_state = factories.create(factories.NettingChannelStateProperties()) channel_state.our_state.balance_proof = balance_proof channel_state.partner_state.balance_proof = balance_proof monkeypatch.setattr( raiden.transfer.views, "get_channelstate_by_canonical_identifier", lambda *a, **kw: channel_state, ) send_pfs_update(raiden=raiden_service, canonical_identifier=balance_proof.canonical_identifier) gevent.idle() with gevent.Timeout(2): while pfs_room.send_text.call_count < 1: gevent.idle() assert pfs_room.send_text.call_count == 1 # send PFSFeeUpdate channel_state = factories.create(factories.NettingChannelStateProperties()) fee_update = PFSFeeUpdate.from_channel_state(channel_state) fee_update.sign(raiden_service.signer) raiden_service.transport.broadcast(PATH_FINDING_BROADCASTING_ROOM, fee_update) with gevent.Timeout(2): while pfs_room.send_text.call_count < 2: gevent.idle() assert pfs_room.send_text.call_count == 2 msg_data = json.loads(pfs_room.send_text.call_args[0][0]) assert msg_data["type"] == "PFSFeeUpdate" transport.stop() transport.greenlet.get()
def test_monitoring_broadcast_messages( local_matrix_servers, retry_interval_initial, retry_interval_max, retries_before_backoff, monkeypatch, broadcast_rooms, ): """ Test that RaidenService broadcast RequestMonitoring messages to MONITORING_BROADCASTING_ROOM room on newly received balance proofs. """ transport = MatrixTransport( config=MatrixTransportConfig( broadcast_rooms=broadcast_rooms + [MONITORING_BROADCASTING_ROOM], retries_before_backoff=retries_before_backoff, retry_interval_initial=retry_interval_initial, retry_interval_max=retry_interval_max, server=local_matrix_servers[0], available_servers=[local_matrix_servers[0]], ), environment=Environment.DEVELOPMENT, ) transport._client.api.retry_timeout = 0 transport._send_raw = MagicMock() raiden_service = MockRaidenService(None) raiden_service.config = RaidenConfig( chain_id=1234, environment_type=Environment.DEVELOPMENT, services=ServiceConfig(monitoring_enabled=True), ) transport.start(raiden_service, [], None) ms_room_name = make_room_alias(transport.chain_id, MONITORING_BROADCASTING_ROOM) ms_room = transport._broadcast_rooms.get(ms_room_name) assert isinstance(ms_room, Room) ms_room.send_text = MagicMock(spec=ms_room.send_text) raiden_service.transport = transport transport.log = MagicMock() balance_proof = factories.create(HOP1_BALANCE_PROOF) channel_state = factories.create(factories.NettingChannelStateProperties()) channel_state.our_state.balance_proof = balance_proof channel_state.partner_state.balance_proof = balance_proof monkeypatch.setattr( raiden.transfer.views, "get_channelstate_by_canonical_identifier", lambda *a, **kw: channel_state, ) monkeypatch.setattr(raiden.transfer.channel, "get_balance", lambda *a, **kw: 123) raiden_service.user_deposit.effective_balance.return_value = MONITORING_REWARD update_monitoring_service_from_balance_proof( raiden=raiden_service, chain_state=None, new_balance_proof=balance_proof, non_closing_participant=HOP1, ) gevent.idle() with gevent.Timeout(2): while ms_room.send_text.call_count < 1: gevent.idle() assert ms_room.send_text.call_count == 1 transport.stop() transport.greenlet.get()
def test_matrix_message_retry( local_matrix_servers, retry_interval_initial, retry_interval_max, retries_before_backoff, broadcast_rooms, ): """ Test the retry mechanism implemented into the matrix client. The test creates a transport and sends a message. Given that the receiver was online, the initial message is sent but the receiver doesn't respond in time and goes offline. The retrier should then wait for the `retry_interval` duration to pass and send the message again but this won't work because the receiver is offline. Once the receiver comes back again, the message should be sent again. """ partner_address = factories.make_address() transport = MatrixTransport( config=MatrixTransportConfig( broadcast_rooms=broadcast_rooms, retries_before_backoff=retries_before_backoff, retry_interval_initial=retry_interval_initial, retry_interval_max=retry_interval_max, server=local_matrix_servers[0], available_servers=[local_matrix_servers[0]], ), environment=Environment.DEVELOPMENT, ) transport._send_raw = MagicMock() raiden_service = MockRaidenService(None) transport.start(raiden_service, [], None) transport.log = MagicMock() # Receiver is online transport._address_mgr._address_to_reachabilitystate[partner_address] = ReachabilityState( AddressReachability.REACHABLE, datetime.now() ) queueid = QueueIdentifier( recipient=partner_address, canonical_identifier=CANONICAL_IDENTIFIER_UNORDERED_QUEUE ) chain_state = raiden_service.wal.state_manager.current_state retry_queue: _RetryQueue = transport._get_retrier(partner_address) assert bool(retry_queue), "retry_queue not running" # Send the initial message message = Processed(message_identifier=0, signature=EMPTY_SIGNATURE) transport._raiden_service.sign(message) chain_state.queueids_to_queues[queueid] = [message] retry_queue.enqueue_unordered(message) gevent.idle() assert transport._send_raw.call_count == 1 # Receiver goes offline transport._address_mgr._address_to_reachabilitystate[partner_address] = ReachabilityState( AddressReachability.UNREACHABLE, datetime.now() ) with gevent.Timeout(retry_interval_initial + 2): wait_assert( transport.log.debug.assert_called_with, "Partner not reachable. Skipping.", partner=to_checksum_address(partner_address), status=AddressReachability.UNREACHABLE, ) # Retrier did not call send_raw given that the receiver is still offline assert transport._send_raw.call_count == 1 # Receiver comes back online transport._address_mgr._address_to_reachabilitystate[partner_address] = ReachabilityState( AddressReachability.REACHABLE, datetime.now() ) # Retrier should send the message again with gevent.Timeout(retry_interval_initial + 2): while transport._send_raw.call_count != 2: gevent.sleep(0.1) transport.stop() transport.greenlet.get()