<commit_before>import sys
from typing import Any, Dict, NamedTuple, Optional, Tuple

import click
from eth_utils import to_canonical_address, to_checksum_address
from web3 import Web3

from raiden.constants import Environment, RoutingMode
from raiden.exceptions import AddressWithoutCode, AddressWrongContract, ContractVersionMismatch
from raiden.network.blockchain_service import BlockChainService
from raiden.network.discovery import ContractDiscovery
from raiden.network.pathfinding import configure_pfs_or_exit
from raiden.network.proxies.secret_registry import SecretRegistry
from raiden.network.proxies.service_registry import ServiceRegistry
from raiden.network.proxies.token_network_registry import TokenNetworkRegistry
from raiden.network.proxies.user_deposit import UserDeposit
from raiden.network.throttle import TokenBucket
from raiden.network.transport import UDPTransport
from raiden.settings import DEVELOPMENT_CONTRACT_VERSION, RED_EYES_CONTRACT_VERSION
from raiden.utils.typing import Address
from raiden_contracts.constants import (
    CONTRACT_ENDPOINT_REGISTRY,
    CONTRACT_SECRET_REGISTRY,
    CONTRACT_SERVICE_REGISTRY,
    CONTRACT_TOKEN_NETWORK_REGISTRY,
    CONTRACT_USER_DEPOSIT,
    ID_TO_NETWORKNAME,
)
from raiden_contracts.contract_manager import (
    contracts_precompiled_path,
    get_contracts_deployment_info,
)

from .sync import check_discovery_registration_gas


def environment_type_to_contracts_version(environment_type: Environment) -> str:
    if environment_type == Environment.DEVELOPMENT:
        contracts_version = DEVELOPMENT_CONTRACT_VERSION
    else:
        contracts_version = RED_EYES_CONTRACT_VERSION

    return contracts_version


def setup_network_id_or_exit(
        config: Dict[str, Any],
        given_network_id: int,
        web3: Web3,
) -> Tuple[int, bool]:
    """
    Takes the given network id and checks it against the connected network

    If they don't match, exits the program with an error. If they do adds it
    to the configuration and then returns it and whether it is a known network
    """
    node_network_id = int(web3.version.network)  # pylint: disable=no-member
    known_given_network_id = given_network_id in ID_TO_NETWORKNAME
    known_node_network_id = node_network_id in ID_TO_NETWORKNAME

    if node_network_id != given_network_id:
        if known_given_network_id and known_node_network_id:
            click.secho(
                f"The chosen ethereum network '{ID_TO_NETWORKNAME[given_network_id]}' "
                f"differs from the ethereum client '{ID_TO_NETWORKNAME[node_network_id]}'. "
                "Please update your settings.",
                fg='red',
            )
        else:
            click.secho(
                f"The chosen ethereum network id '{given_network_id}' differs "
                f"from the ethereum client '{node_network_id}'. "
                "Please update your settings.",
                fg='red',
            )
        sys.exit(1)

    config['chain_id'] = given_network_id
    return given_network_id, known_node_network_id


def setup_environment(config: Dict[str, Any], environment_type: Environment) -> None:
    """Sets the config depending on the environment type"""
    # interpret the provided string argument
    if environment_type == Environment.PRODUCTION:
        # Safe configuration: restrictions for mainnet apply and matrix rooms have to be private
        config['transport']['matrix']['private_rooms'] = True

    config['environment_type'] = environment_type

    print(f'Raiden is running in {environment_type.value.lower()} mode')


def setup_contracts_or_exit(
        config: Dict[str, Any],
        network_id: int,
) -> Dict[str, Any]:
    """Sets the contract deployment data depending on the network id and environment type

    If an invalid combination of network id and environment type is provided, exits
    the program with an error
    """
    environment_type = config['environment_type']

    not_allowed = (  # for now we only disallow mainnet with test configuration
        network_id == 1 and
        environment_type == Environment.DEVELOPMENT
    )
    if not_allowed:
        click.secho(
            f'The chosen network ({ID_TO_NETWORKNAME[network_id]}) is not a testnet, '
            f'but the "development" environment was selected.\n'
            f'This is not allowed. Please start again with a safe environment setting '
            f'(--environment production).',
            fg='red',
        )
        sys.exit(1)

    contracts: Dict[str, Any] = dict()
    contracts_version = environment_type_to_contracts_version(environment_type)

    config['contracts_path'] = contracts_precompiled_path(contracts_version)

    if network_id in ID_TO_NETWORKNAME and ID_TO_NETWORKNAME[network_id] != 'smoketest':
        try:
            deployment_data = get_contracts_deployment_info(
                chain_id=network_id,
                version=contracts_version,
            )
        except ValueError:
            return contracts, False

        contracts = deployment_data['contracts']

    return contracts


def handle_contract_version_mismatch(mismatch_exception: ContractVersionMismatch) -> None:
    click.secho(
        f'{str(mismatch_exception)}. Please update your Raiden installation.',
        fg='red',
    )
    sys.exit(1)


def handle_contract_no_code(name: str, address: Address) -> None:
    hex_addr = to_checksum_address(address)
    click.secho(f'Error: Provided {name} {hex_addr} contract does not contain code', fg='red')
    sys.exit(1)


def handle_contract_wrong_address(name: str, address: Address) -> None:
    hex_addr = to_checksum_address(address)
    click.secho(
        f'Error: Provided address {hex_addr} for {name} contract'
        ' does not contain expected code.',
        fg='red',
    )
    sys.exit(1)
<commit_msg>Remove unused error return value tuple<commit_after>import sys
from typing import Any, Dict, NamedTuple, Optional, Tuple

import click
from eth_utils import to_canonical_address, to_checksum_address
from web3 import Web3

from raiden.constants import Environment, RoutingMode
from raiden.exceptions import AddressWithoutCode, AddressWrongContract, ContractVersionMismatch
from raiden.network.blockchain_service import BlockChainService
from raiden.network.discovery import ContractDiscovery
from raiden.network.pathfinding import configure_pfs_or_exit
from raiden.network.proxies.secret_registry import SecretRegistry
from raiden.network.proxies.service_registry import ServiceRegistry
from raiden.network.proxies.token_network_registry import TokenNetworkRegistry
from raiden.network.proxies.user_deposit import UserDeposit
from raiden.network.throttle import TokenBucket
from raiden.network.transport import UDPTransport
from raiden.settings import DEVELOPMENT_CONTRACT_VERSION, RED_EYES_CONTRACT_VERSION
from raiden.utils.typing import Address
from raiden_contracts.constants import (
    CONTRACT_ENDPOINT_REGISTRY,
    CONTRACT_SECRET_REGISTRY,
    CONTRACT_SERVICE_REGISTRY,
    CONTRACT_TOKEN_NETWORK_REGISTRY,
    CONTRACT_USER_DEPOSIT,
    ID_TO_NETWORKNAME,
)
from raiden_contracts.contract_manager import (
    contracts_precompiled_path,
    get_contracts_deployment_info,
)

from .sync import check_discovery_registration_gas


def environment_type_to_contracts_version(environment_type: Environment) -> str:
    if environment_type == Environment.DEVELOPMENT:
        contracts_version = DEVELOPMENT_CONTRACT_VERSION
    else:
        contracts_version = RED_EYES_CONTRACT_VERSION

    return contracts_version


def setup_network_id_or_exit(
        config: Dict[str, Any],
        given_network_id: int,
        web3: Web3,
) -> Tuple[int, bool]:
    """
    Takes the given network id and checks it against the connected network

    If they don't match, exits the program with an error. If they do adds it
    to the configuration and then returns it and whether it is a known network
    """
    node_network_id = int(web3.version.network)  # pylint: disable=no-member
    known_given_network_id = given_network_id in ID_TO_NETWORKNAME
    known_node_network_id = node_network_id in ID_TO_NETWORKNAME

    if node_network_id != given_network_id:
        if known_given_network_id and known_node_network_id:
            click.secho(
                f"The chosen ethereum network '{ID_TO_NETWORKNAME[given_network_id]}' "
                f"differs from the ethereum client '{ID_TO_NETWORKNAME[node_network_id]}'. "
                "Please update your settings.",
                fg='red',
            )
        else:
            click.secho(
                f"The chosen ethereum network id '{given_network_id}' differs "
                f"from the ethereum client '{node_network_id}'. "
                "Please update your settings.",
                fg='red',
            )
        sys.exit(1)

    config['chain_id'] = given_network_id
    return given_network_id, known_node_network_id


def setup_environment(config: Dict[str, Any], environment_type: Environment) -> None:
    """Sets the config depending on the environment type"""
    # interpret the provided string argument
    if environment_type == Environment.PRODUCTION:
        # Safe configuration: restrictions for mainnet apply and matrix rooms have to be private
        config['transport']['matrix']['private_rooms'] = True

    config['environment_type'] = environment_type

    print(f'Raiden is running in {environment_type.value.lower()} mode')


def setup_contracts_or_exit(
        config: Dict[str, Any],
        network_id: int,
) -> Dict[str, Any]:
    """Sets the contract deployment data depending on the network id and environment type

    If an invalid combination of network id and environment type is provided, exits
    the program with an error
    """
    environment_type = config['environment_type']

    not_allowed = (  # for now we only disallow mainnet with test configuration
        network_id == 1 and
        environment_type == Environment.DEVELOPMENT
    )
    if not_allowed:
        click.secho(
            f'The chosen network ({ID_TO_NETWORKNAME[network_id]}) is not a testnet, '
            f'but the "development" environment was selected.\n'
            f'This is not allowed. Please start again with a safe environment setting '
            f'(--environment production).',
            fg='red',
        )
        sys.exit(1)

    contracts: Dict[str, Any] = dict()
    contracts_version = environment_type_to_contracts_version(environment_type)

    config['contracts_path'] = contracts_precompiled_path(contracts_version)

    if network_id in ID_TO_NETWORKNAME and ID_TO_NETWORKNAME[network_id] != 'smoketest':
        try:
            deployment_data = get_contracts_deployment_info(
                chain_id=network_id,
                version=contracts_version,
            )
        except ValueError:
            return contracts

        contracts = deployment_data['contracts']

    return contracts


def handle_contract_version_mismatch(mismatch_exception: ContractVersionMismatch) -> None:
    click.secho(
        f'{str(mismatch_exception)}. Please update your Raiden installation.',
        fg='red',
    )
    sys.exit(1)


def handle_contract_no_code(name: str, address: Address) -> None:
    hex_addr = to_checksum_address(address)
    click.secho(f'Error: Provided {name} {hex_addr} contract does not contain code', fg='red')
    sys.exit(1)


def handle_contract_wrong_address(name: str, address: Address) -> None:
    hex_addr = to_checksum_address(address)
    click.secho(
        f'Error: Provided address {hex_addr} for {name} contract'
        ' does not contain expected code.',
        fg='red',
    )
    sys.exit(1)

{'fraction_non_alphanumeric': 0.0713798396725226, 'fraction_numerical': 0.001876172607879925, 'mean_word_length': 3.4810852120748947, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2184477', 'n_tokens_mistral': 3428, 'n_tokens_neox': 3315, 'n_words': 888}