def test_token_channel_closed(pathfinding_service_mock, token_network_model): setup_channel(pathfinding_service_mock, token_network_model) token_network_address = make_token_network_address() # Test invalid token network address close_event = ReceiveChannelClosedEvent( token_network_address=token_network_address, channel_identifier=ChannelID(1), closing_participant=PARTICIPANT1, block_number=BlockNumber(2), ) pathfinding_service_mock.handle_event(close_event) assert len(pathfinding_service_mock.token_networks) == 1 assert len(token_network_model.channel_id_to_addresses) == 1 # Test proper token network address close_event = dataclasses.replace( close_event, token_network_address=token_network_model.address) pathfinding_service_mock.handle_event(close_event) assert len(pathfinding_service_mock.token_networks) == 1 assert len(token_network_model.channel_id_to_addresses) == 0 # Test non-existent channel close_event = dataclasses.replace(close_event, channel_identifier=ChannelID(123)) pathfinding_service_mock.handle_event(close_event) assert len(pathfinding_service_mock.token_networks) == 1 assert len(token_network_model.channel_id_to_addresses) == 0
def test_saveing_multiple_channel(ms_database: Database): ms_database.conn.execute( "INSERT INTO token_network (address) VALUES (?)", [to_checksum_address(DEFAULT_TOKEN_NETWORK_ADDRESS)], ) tn_address2 = make_token_network_address() ms_database.conn.execute("INSERT INTO token_network (address) VALUES (?)", [to_checksum_address(tn_address2)]) channel1 = create_channel() channel2 = create_channel() channel2.token_network_address = tn_address2 ms_database.upsert_channel(channel1) loaded_channel1 = ms_database.get_channel( token_network_address=channel1.token_network_address, channel_id=channel1.identifier) assert loaded_channel1 == channel1 assert ms_database.channel_count() == 1 ms_database.upsert_channel(channel2) loaded_channel2 = ms_database.get_channel( token_network_address=channel2.token_network_address, channel_id=channel2.identifier) assert loaded_channel2 == channel2 assert ms_database.channel_count() == 2
def test_post_pfs_feedback(query_paths_args): """ Test POST feedback to PFS """ feedback_token = uuid4() token_network_address = factories.make_token_network_address() route = [factories.make_address(), factories.make_address()] with patch.object(session, "post", return_value=mocked_json_response()) as feedback: post_pfs_feedback( routing_mode=RoutingMode.PFS, pfs_config=query_paths_args["pfs_config"], token_network_address=token_network_address, route=route, token=feedback_token, successful=True, ) assert feedback.called assert feedback.call_args[0][0].find( to_checksum_address(token_network_address)) > 0 payload = feedback.call_args[1]["json"] assert payload["token"] == feedback_token.hex assert payload["success"] is True assert payload["path"] == [to_checksum_address(addr) for addr in route] with patch.object(session, "post", return_value=mocked_json_response()) as feedback: post_pfs_feedback( routing_mode=RoutingMode.PFS, pfs_config=query_paths_args["pfs_config"], token_network_address=token_network_address, route=route, token=feedback_token, successful=False, ) assert feedback.called assert feedback.call_args[0][0].find( to_checksum_address(token_network_address)) > 0 payload = feedback.call_args[1]["json"] assert payload["token"] == feedback_token.hex assert payload["success"] is False assert payload["path"] == [to_checksum_address(addr) for addr in route] with patch.object(session, "post", return_value=mocked_json_response()) as feedback: post_pfs_feedback( routing_mode=RoutingMode.PRIVATE, pfs_config=query_paths_args["pfs_config"], token_network_address=token_network_address, route=route, token=feedback_token, successful=False, ) assert not feedback.called
def test_channel_constraints(pathfinding_service_mock): """Regression test for https://github.com/raiden-network/raiden-services/issues/693""" # Participants need to be ordered parts = sorted([make_address(), make_address()]) token_network_address1 = make_token_network_address() token_network_address2 = make_token_network_address() # register token network internally database = pathfinding_service_mock.database database.upsert_token_network(token_network_address1) database.upsert_token_network(token_network_address2) channel1 = Channel( token_network_address=token_network_address1, channel_id=ChannelID(1), participant1=parts[0], participant2=parts[1], settle_timeout=BlockTimeout(100), ) channel2 = Channel( token_network_address=token_network_address2, channel_id=ChannelID(1), participant1=parts[0], participant2=parts[1], settle_timeout=BlockTimeout(100), ) # Test `upsert_channel` and `get_channels` database.upsert_channel(channel1) assert [chan.channel_id for chan in database.get_channels()] == [channel1.channel_id] database.upsert_channel(channel2) assert [chan.channel_id for chan in database.get_channels()] == [ channel1.channel_id, channel2.channel_id, ]
def setup_pfs_handler_test( set_feedback_token: bool, ) -> Tuple[RaidenService, PFSFeedbackEventHandler, TokenNetworkRegistryAddress, TokenNetworkAddress, List[Address], Optional[UUID], ]: channel_identifier = make_channel_identifier() token_network_registry_address = make_token_network_registry_address() token_network_address = make_token_network_address() participant = make_address() raiden = make_raiden_service_mock( token_network_registry_address=token_network_registry_address, token_network_address=token_network_address, channel_identifier=channel_identifier, partner=participant, ) default_handler = RaidenEventHandler() pfs_handler = PFSFeedbackEventHandler(default_handler) route = [make_address(), make_address(), make_address()] # Set PFS config and feedback token pfs_config = True # just a truthy value raiden.config.pfs_config = pfs_config feedback_uuid = None if set_feedback_token: feedback_uuid = uuid4() raiden.route_to_feedback_token[tuple(route)] = feedback_uuid return ( raiden, pfs_handler, token_network_registry_address, token_network_address, route, feedback_uuid, )
def test_handle_contract_send_channelunlock_already_unlocked(): """This is a test for the scenario where the onchain unlock has already happened when we get to handle our own send unlock transaction. Regression test for https://github.com/raiden-network/raiden/issues/3152 """ channel_identifier = ChannelID(1) token_network_registry_address = make_token_network_registry_address() token_network_address = make_token_network_address() participant = make_address() raiden = make_raiden_service_mock( token_network_registry_address=token_network_registry_address, token_network_address=token_network_address, channel_identifier=channel_identifier, partner=participant, ) channel_state = get_channelstate_by_token_network_and_partner( chain_state=state_from_raiden(raiden), token_network_address=token_network_address, partner_address=participant, ) assert channel_state channel_state.our_state.onchain_locksroot = LOCKSROOT_OF_NO_LOCKS channel_state.partner_state.onchain_locksroot = LOCKSROOT_OF_NO_LOCKS def detail_participants(_participant1, _participant2, _block_identifier, _channel_identifier): transferred_amount = TokenAmount(1) locked_amount = LockedAmount(1) locksroot = make_locksroot() balance_hash = hash_balance_data(transferred_amount, locked_amount, locksroot) our_details = ParticipantDetails( address=raiden.address, deposit=TokenAmount(5), withdrawn=WithdrawAmount(0), is_closer=False, balance_hash=balance_hash, nonce=Nonce(1), locksroot=locksroot, locked_amount=locked_amount, ) transferred_amount = TokenAmount(1) locked_amount = LockedAmount(1) # Let's mock here that partner locksroot is 0x0 balance_hash = hash_balance_data(transferred_amount, locked_amount, locksroot) partner_details = ParticipantDetails( address=participant, deposit=TokenAmount(5), withdrawn=WithdrawAmount(0), is_closer=True, balance_hash=balance_hash, nonce=Nonce(1), locksroot=LOCKSROOT_OF_NO_LOCKS, locked_amount=locked_amount, ) return ParticipantsDetails(our_details, partner_details) # make sure detail_participants returns partner data with a locksroot of 0x0 raiden.proxy_manager.token_network.detail_participants = detail_participants event = ContractSendChannelBatchUnlock( canonical_identifier=make_canonical_identifier( token_network_address=token_network_address, channel_identifier=channel_identifier), sender=participant, triggered_by_block_hash=make_block_hash(), ) # This should not throw an unrecoverable error RaidenEventHandler().on_raiden_events( raiden=raiden, chain_state=raiden.wal.state_manager.current_state, events=[event])
def test_encoding_and_decoding(): message_factories = ( factories.LockedTransferProperties(), factories.RefundTransferProperties(), factories.LockExpiredProperties(), factories.UnlockProperties(), ) messages = [factories.create(factory) for factory in message_factories] # TODO Handle these with factories once #5091 is implemented messages.append( Delivered( delivered_message_identifier=factories.make_message_identifier(), signature=factories.make_signature(), )) messages.append( Processed( message_identifier=factories.make_message_identifier(), signature=factories.make_signature(), )) messages.append( RevealSecret( message_identifier=factories.make_message_identifier(), secret=factories.make_secret(), signature=factories.make_signature(), )) messages.append( SecretRequest( message_identifier=factories.make_message_identifier(), payment_identifier=factories.make_payment_id(), secrethash=factories.make_secret_hash(), amount=factories.make_token_amount(), expiration=factories.make_block_number(), signature=factories.make_signature(), )) messages.append( WithdrawRequest( message_identifier=factories.make_message_identifier(), chain_id=factories.make_chain_id(), token_network_address=factories.make_token_network_address(), channel_identifier=factories.make_channel_identifier(), participant=factories.make_address(), total_withdraw=factories.make_token_amount(), nonce=factories.make_nonce(), expiration=factories.make_block_number(), signature=factories.make_signature(), )) messages.append( WithdrawConfirmation( message_identifier=factories.make_message_identifier(), chain_id=factories.make_chain_id(), token_network_address=factories.make_token_network_address(), channel_identifier=factories.make_channel_identifier(), participant=factories.make_address(), total_withdraw=factories.make_token_amount(), nonce=factories.make_nonce(), expiration=factories.make_block_number(), signature=factories.make_signature(), )) messages.append( WithdrawExpired( message_identifier=factories.make_message_identifier(), chain_id=factories.make_chain_id(), token_network_address=factories.make_token_network_address(), channel_identifier=factories.make_channel_identifier(), participant=factories.make_address(), total_withdraw=factories.make_token_amount(), nonce=factories.make_nonce(), expiration=factories.make_block_number(), signature=factories.make_signature(), )) messages.append( PFSCapacityUpdate( canonical_identifier=factories.make_canonical_identifier(), updating_participant=factories.make_address(), other_participant=factories.make_address(), updating_nonce=factories.make_nonce(), other_nonce=factories.make_nonce(), updating_capacity=factories.make_token_amount(), other_capacity=factories.make_token_amount(), reveal_timeout=factories.make_uint64(), signature=factories.make_signature(), )) messages.append( PFSFeeUpdate( canonical_identifier=factories.make_canonical_identifier(), updating_participant=factories.make_address(), fee_schedule=factories.create( factories.FeeScheduleStateProperties()), timestamp=datetime.now(), signature=factories.make_signature(), )) messages.append( RequestMonitoring( reward_amount=factories.make_token_amount(), balance_proof=SignedBlindedBalanceProof. from_balance_proof_signed_state( factories.create( factories.BalanceProofSignedStateProperties())), monitoring_service_contract_address=factories.make_address(), non_closing_participant=factories.make_address(), non_closing_signature=factories.make_signature(), signature=factories.make_signature(), )) for message in messages: serialized = MessageSerializer.serialize(message) deserialized = MessageSerializer.deserialize(serialized) assert deserialized == message
) messages.append( SecretRequest( message_identifier=factories.make_message_identifier(), payment_identifier=factories.make_payment_id(), secrethash=factories.make_secret_hash(), amount=factories.make_payment_amount(), expiration=factories.make_block_expiration_number(), signature=factories.make_signature(), ) ) messages.append( WithdrawRequest( message_identifier=factories.make_message_identifier(), chain_id=factories.make_chain_id(), token_network_address=factories.make_token_network_address(), channel_identifier=factories.make_channel_identifier(), participant=factories.make_address(), total_withdraw=factories.make_withdraw_amount(), nonce=factories.make_nonce(), expiration=factories.make_block_expiration_number(), signature=factories.make_signature(), ) ) messages.append( WithdrawConfirmation( message_identifier=factories.make_message_identifier(), chain_id=factories.make_chain_id(), token_network_address=factories.make_token_network_address(), channel_identifier=factories.make_channel_identifier(), participant=factories.make_address(),