def test_action_monitoring_triggered_event_handler_with_insufficient_reward_amount_does_not_trigger_monitor_call( # noqa context: Context, ): """ Tests that `monitor` is not called when the ActionMonitoringTriggeredEvent is triggered but the monitor request shows an insufficient reward amount """ context = setup_state_with_closed_channel(context) context.db.upsert_monitor_request( create_signed_monitor_request(nonce=Nonce(6), reward_amount=TokenAmount(0))) trigger_event = ActionMonitoringTriggeredEvent( token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS, channel_identifier=DEFAULT_CHANNEL_IDENTIFIER, non_closing_participant=DEFAULT_PARTICIPANT2, ) channel = context.db.get_channel(trigger_event.token_network_address, trigger_event.channel_identifier) assert channel assert channel.closing_tx_hash is None context.user_deposit_contract.functions.effectiveBalance( DEFAULT_PARTICIPANT2).call.return_value = 21 action_monitoring_triggered_event_handler(trigger_event, context) # check that the monitor call has been done assert context.monitoring_service_contract.functions.monitor.called is False
def test_action_claim_reward_triggered_event_handler_without_update_state_doesnt_trigger_claim_call( # noqa context: Context, ): """ Tests that `claimReward` is called when the ActionMonitoringTriggeredEvent is triggered and user has sufficient balance in user deposit contract """ context = setup_state_with_closed_channel(context) context.db.upsert_monitor_request( create_signed_monitor_request(nonce=Nonce(6), reward_amount=TokenAmount(0))) trigger_event = ActionClaimRewardTriggeredEvent( token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS, channel_identifier=DEFAULT_CHANNEL_IDENTIFIER, non_closing_participant=DEFAULT_PARTICIPANT2, ) channel = context.db.get_channel(trigger_event.token_network_address, trigger_event.channel_identifier) assert channel assert channel.claim_tx_hash is None # Set update state channel.update_status = OnChainUpdateStatus(update_sender_address=Address( bytes([1] * 20)), nonce=Nonce(6)) context.db.upsert_channel(channel) action_claim_reward_triggered_event_handler(trigger_event, context) # check that the monitor call has been done assert context.monitoring_service_contract.functions.claimReward.called is False
def test_action_monitoring_triggered_event_handler_without_sufficient_balance_doesnt_trigger_monitor_call( # noqa context: Context, ): """ Tests that `monitor` is not called when user has insufficient balance in user deposit contract Also a test for https://github.com/raiden-network/raiden-services/issues/29 , as the MR is sent after the channel has been closed. """ context = setup_state_with_closed_channel(context) context.db.upsert_monitor_request( create_signed_monitor_request(nonce=Nonce(6), reward_amount=TokenAmount(10))) trigger_event = ActionMonitoringTriggeredEvent( token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS, channel_identifier=DEFAULT_CHANNEL_IDENTIFIER, non_closing_participant=DEFAULT_PARTICIPANT2, ) channel = context.db.get_channel(trigger_event.token_network_address, trigger_event.channel_identifier) assert channel assert channel.closing_tx_hash is None context.user_deposit_contract.functions.effectiveBalance( DEFAULT_PARTICIPANT2).call.return_value = 0 action_monitoring_triggered_event_handler(trigger_event, context) # check that the monitor call has been done assert context.monitoring_service_contract.functions.monitor.called is False
def test_action_monitoring_rescheduling_when_user_lacks_funds(context: Context): reward_amount = TokenAmount(10) context = setup_state_with_closed_channel(context) context.db.upsert_monitor_request( create_signed_monitor_request(nonce=Nonce(6), reward_amount=reward_amount) ) event = ActionMonitoringTriggeredEvent( token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS, channel_identifier=DEFAULT_CHANNEL_IDENTIFIER, non_closing_participant=DEFAULT_PARTICIPANT2, ) scheduled_events_before = context.db.get_scheduled_events(max_trigger_block=BlockNumber(10000)) # Try to call monitor when the user has insufficient funds with patch("monitoring_service.handlers.get_pessimistic_udc_balance", Mock(return_value=0)): action_monitoring_triggered_event_handler(event, context) assert not context.monitoring_service_contract.functions.monitor.called # Now the event must have been rescheduled # TODO: check that the event is rescheduled to trigger at the right block scheduled_events_after = context.db.get_scheduled_events(max_trigger_block=BlockNumber(10000)) new_events = set(scheduled_events_after) - set(scheduled_events_before) assert len(new_events) == 1 assert new_events.pop().event == event # With sufficient funds it must succeed with patch( "monitoring_service.handlers.get_pessimistic_udc_balance", Mock(return_value=reward_amount * UDC_SECURITY_MARGIN_FACTOR_MS), ): action_monitoring_triggered_event_handler(event, context) assert context.monitoring_service_contract.functions.monitor.called
def test_mr_with_unknown_signatures(context: Context): """ The signatures are valid but don't belong to the participants. """ context = setup_state_with_closed_channel(context) def assert_mr_is_ignored(mr): context.db.upsert_monitor_request(mr) event = ActionMonitoringTriggeredEvent( token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS, channel_identifier=DEFAULT_CHANNEL_IDENTIFIER, non_closing_participant=DEFAULT_PARTICIPANT2, ) action_monitoring_triggered_event_handler(event, context) assert not context.monitoring_service_contract.functions.monitor.called assert_mr_is_ignored(create_signed_monitor_request(closing_privkey=get_random_privkey())) assert_mr_is_ignored(create_signed_monitor_request(nonclosing_privkey=get_random_privkey()))
def test_save_and_load_monitor_request(ms_database: Database): request = create_signed_monitor_request() ms_database.upsert_monitor_request(request) restored = ms_database.get_monitor_request( token_network_address=request.token_network_address, channel_id=request.channel_identifier, non_closing_signer=request.non_closing_signer, ) assert request == restored
def test_channel_closed_event_handler_trigger_action_monitor_event_with_monitor_request( context: Context, ): context = setup_state_with_open_channel(context) # add MR to DB context.db.upsert_monitor_request(create_signed_monitor_request()) event = ReceiveChannelClosedEvent( token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS, channel_identifier=DEFAULT_CHANNEL_IDENTIFIER, closing_participant=DEFAULT_PARTICIPANT2, block_number=BlockNumber(52), ) channel_closed_event_handler(event, context) assert context.db.scheduled_event_count() == 1
def test_action_monitoring_triggered_event_handler_does_not_trigger_monitor_call_when_nonce_to_small( # noqa context: Context, ): context = setup_state_with_closed_channel(context) event3 = ReceiveMonitoringNewBalanceProofEvent( token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS, channel_identifier=DEFAULT_CHANNEL_IDENTIFIER, reward_amount=TokenAmount(1), nonce=Nonce(5), ms_address=Address(bytes([3] * 20)), raiden_node_address=DEFAULT_PARTICIPANT2, block_number=BlockNumber(23), ) channel = context.db.get_channel(event3.token_network_address, event3.channel_identifier) assert channel assert channel.update_status is None monitor_new_balance_proof_event_handler(event3, context) # add MR to DB, with nonce being smaller than in event3 context.db.upsert_monitor_request( create_signed_monitor_request(nonce=Nonce(4))) event4 = ActionMonitoringTriggeredEvent( token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS, channel_identifier=DEFAULT_CHANNEL_IDENTIFIER, non_closing_participant=DEFAULT_PARTICIPANT2, ) channel = context.db.get_channel(event4.token_network_address, event4.channel_identifier) assert channel assert channel.update_status is not None assert channel.closing_tx_hash is None action_monitoring_triggered_event_handler(event4, context) assert context.db.channel_count() == 1 assert channel assert channel.closing_tx_hash is None
def test_mr_available_before_channel_triggers_monitor_call(context: Context): """ Tests that the MR is read from the DB, even if it is supplied before the channel was opened. See https://github.com/raiden-network/raiden-services/issues/26 """ # add MR to DB context.db.upsert_monitor_request(create_signed_monitor_request()) context = setup_state_with_closed_channel(context) event = ActionMonitoringTriggeredEvent( token_network_address=DEFAULT_TOKEN_NETWORK_ADDRESS, channel_identifier=DEFAULT_CHANNEL_IDENTIFIER, non_closing_participant=DEFAULT_PARTICIPANT2, ) context.user_deposit_contract.functions.effectiveBalance( DEFAULT_PARTICIPANT2).call.return_value = 100 action_monitoring_triggered_event_handler(event, context) # check that the monitor call has been done assert context.monitoring_service_contract.functions.monitor.called is True