Exemplo n.º 1
0
def test_configure_pfs(service_registry_address, private_keys, web3,
                       contract_manager):
    chain_id = ChainID(int(web3.net.version))
    service_registry, urls = deploy_service_registry_and_set_urls(
        private_keys=private_keys,
        web3=web3,
        contract_manager=contract_manager,
        service_registry_address=service_registry_address,
    )
    json_data = {
        "price_info":
        0,
        "network_info": {
            "chain_id":
            chain_id,
            "token_network_registry_address":
            to_checksum_address(token_network_registry_address_test_default),
            "user_deposit_address":
            to_checksum_address(privatekey_to_address(private_keys[1])),
            "confirmed_block": {
                "number": 10
            },
        },
        "message":
        "This is your favorite pathfinding service",
        "operator":
        "John Doe",
        "version":
        "0.0.1",
        "payment_address":
        to_checksum_address(privatekey_to_address(private_keys[0])),
    }

    response = mocked_json_response(response_data=json_data)

    # With local routing configure_pfs should raise assertion
    with pytest.raises(AssertionError):
        _ = configure_pfs_or_exit(
            pfs_url="",
            routing_mode=RoutingMode.LOCAL,
            service_registry=service_registry,
            node_network_id=chain_id,
            token_network_registry_address=
            token_network_registry_address_test_default,
            pathfinding_max_fee=DEFAULT_PATHFINDING_MAX_FEE,
        )

    # With private routing configure_pfs should raise assertion
    with pytest.raises(AssertionError):
        _ = configure_pfs_or_exit(
            pfs_url="",
            routing_mode=RoutingMode.PRIVATE,
            service_registry=service_registry,
            node_network_id=chain_id,
            token_network_registry_address=
            token_network_registry_address_test_default,
            pathfinding_max_fee=DEFAULT_PATHFINDING_MAX_FEE,
        )

    # Asking for auto address
    # To make this deterministic we need to patch the random selection function
    patch_random = patch("raiden.network.pathfinding.get_random_pfs",
                         return_value="http://foo")
    with patch.object(requests, "get", return_value=response), patch_random:
        config = configure_pfs_or_exit(
            pfs_url=MATRIX_AUTO_SELECT_SERVER,
            routing_mode=RoutingMode.PFS,
            service_registry=service_registry,
            node_network_id=chain_id,
            token_network_registry_address=
            token_network_registry_address_test_default,
            pathfinding_max_fee=DEFAULT_PATHFINDING_MAX_FEE,
        )
    assert config.url in urls
    assert is_canonical_address(config.payment_address)

    # Configuring a valid given address
    given_address = "http://foo"
    with patch.object(requests, "get", return_value=response):
        config = configure_pfs_or_exit(
            pfs_url=given_address,
            routing_mode=RoutingMode.PFS,
            service_registry=service_registry,
            node_network_id=chain_id,
            token_network_registry_address=
            token_network_registry_address_test_default,
            pathfinding_max_fee=DEFAULT_PATHFINDING_MAX_FEE,
        )
    assert config.url == given_address
    assert is_same_address(config.payment_address,
                           json_data["payment_address"])
    assert config.price == json_data["price_info"]

    # Bad address, should exit the program
    bad_address = "http://badaddress"
    with pytest.raises(RaidenError):
        with patch.object(requests,
                          "get",
                          side_effect=requests.RequestException()):
            # Configuring a given address
            _ = configure_pfs_or_exit(
                pfs_url=bad_address,
                routing_mode=RoutingMode.PFS,
                service_registry=service_registry,
                node_network_id=chain_id,
                token_network_registry_address=
                token_network_registry_address_test_default,
                pathfinding_max_fee=DEFAULT_PATHFINDING_MAX_FEE,
            )

    # Addresses of token network registries of pfs and client conflict, should exit the client
    response = mocked_json_response(response_data=json_data)
    with pytest.raises(RaidenError):
        with patch.object(requests, "get", return_value=response):
            _ = configure_pfs_or_exit(
                pfs_url="http://foo",
                routing_mode=RoutingMode.PFS,
                service_registry=service_registry,
                node_network_id=chain_id,
                token_network_registry_address=TokenNetworkRegistryAddress(
                    to_canonical_address(
                        "0x2222222222222222222222222222222222222221")),
                pathfinding_max_fee=DEFAULT_PATHFINDING_MAX_FEE,
            )

    # ChainIDs of pfs and client conflict, should exit the client
    response = mocked_json_response(response_data=json_data)
    with pytest.raises(RaidenError):
        with patch.object(requests, "get", return_value=response):
            configure_pfs_or_exit(
                pfs_url="http://foo",
                routing_mode=RoutingMode.PFS,
                service_registry=service_registry,
                node_network_id=ChainID(chain_id + 1),
                token_network_registry_address=
                token_network_registry_address_test_default,
                pathfinding_max_fee=DEFAULT_PATHFINDING_MAX_FEE,
            )
Exemplo n.º 2
0
def services_bundle_from_contracts_deployment(
    config: RaidenConfig,
    proxy_manager: ProxyManager,
    routing_mode: RoutingMode,
    deployed_addresses: DeploymentAddresses,
    pathfinding_service_address: str,
    enable_monitoring: bool,
) -> ServicesBundle:
    """
    Initialize and setup the contract proxies.

    Depending on the provided contract addresses via the CLI, the routing mode,
    the environment type and the network id try to initialize the proxies.
    Returns the initialized proxies or exits the application with an error if
    there is a problem.

    Also depending on the given arguments populate config with PFS related settings
    """
    node_network_id = config.chain_id
    environment_type = config.environment_type

    user_deposit_address = deployed_addresses.user_deposit_address
    service_registry_address = deployed_addresses.service_registry_address
    token_network_registry_address = deployed_addresses.token_network_registry_address

    contractname_address: List[Tuple[str, Address, Callable]] = [
        ("user_deposit", Address(user_deposit_address), proxy_manager.user_deposit)
    ]
    if routing_mode == RoutingMode.PFS:
        contractname_address.append(
            ("service_registry", Address(service_registry_address), proxy_manager.service_registry)
        )
    if enable_monitoring or routing_mode == RoutingMode.PFS:
        contractname_address.append(
            (
                "monitoring_service",
                Address(deployed_addresses.monitoring_service_address),
                proxy_manager.monitoring_service,
            )
        )
        contractname_address.append(
            ("one_to_n", Address(deployed_addresses.one_to_n_address), proxy_manager.one_to_n)
        )

    proxies = dict()

    for contractname, address, constructor in contractname_address:
        try:
            proxy = constructor(address)
        except ContractCodeMismatch as e:
            handle_contract_code_mismatch(e)
        except AddressWithoutCode:
            handle_contract_no_code(contractname, address)
        except AddressWrongContract:
            handle_contract_wrong_address(contractname, address)

        proxies[contractname] = proxy

    if routing_mode == RoutingMode.PFS:
        check_pfs_configuration(pathfinding_service_address=pathfinding_service_address)

        pfs_info = configure_pfs_or_exit(
            pfs_url=pathfinding_service_address,
            routing_mode=routing_mode,
            service_registry=proxies["service_registry"],
            node_network_id=node_network_id,
            token_network_registry_address=TokenNetworkRegistryAddress(
                token_network_registry_address
            ),
            pathfinding_max_fee=config.services.pathfinding_max_fee,
        )
        msg = "Eth address of selected pathfinding service is unknown."
        assert pfs_info.payment_address is not None, msg

        # Only check that PFS is registered in production mode
        if environment_type == Environment.PRODUCTION:
            check_pfs_for_production(
                service_registry=proxies["service_registry"], pfs_info=pfs_info
            )

        config.pfs_config = PFSConfig(
            info=pfs_info,
            maximum_fee=config.services.pathfinding_max_fee,
            iou_timeout=config.services.pathfinding_iou_timeout,
            max_paths=config.services.pathfinding_max_paths,
        )
    else:
        config.pfs_config = None

    return ServicesBundle(
        user_deposit=cast(UserDeposit, proxies.get("user_deposit")),
        service_registry=cast(ServiceRegistry, proxies.get("service_registry")),
        monitoring_service=cast(MonitoringService, proxies.get("monitoring_service")),
        one_to_n=cast(OneToN, proxies.get("one_to_n")),
    )
Exemplo n.º 3
0
def test_configure_pfs(service_registry_address, private_keys, web3,
                       contract_manager):
    service_proxy, urls = deploy_service_registry_and_set_urls(
        private_keys=private_keys,
        web3=web3,
        contract_manager=contract_manager,
        service_registry_address=service_registry_address,
    )
    json_data = {
        "price_info": 0,
        "network_info": {
            "chain_id": 1,
            "registry_address": "0xB9633dd9a9a71F22C933bF121d7a22008f66B908",
        },
        "message": "This is your favorite pathfinding service",
        "operator": "John Doe",
        "version": "0.0.1",
    }

    response = Mock()
    response.configure_mock(status_code=200)
    response.json = Mock(return_value=json_data)

    # With basic routing configure pfs should raise assertion
    with pytest.raises(AssertionError):
        config = configure_pfs_or_exit(
            pfs_address=None,
            pfs_eth_address=None,
            routing_mode=RoutingMode.BASIC,
            service_registry=service_proxy,
        )

    # Asking for auto address
    with patch.object(requests, "get", return_value=response):
        config = configure_pfs_or_exit(
            pfs_address="auto",
            pfs_eth_address=None,
            routing_mode=RoutingMode.PFS,
            service_registry=service_proxy,
        )
    assert config.url in urls
    assert is_checksum_address(config.eth_address)

    # Configuring a given address
    given_address = "http://ourgivenaddress"
    given_eth_address = "0x22222222222222222222"
    with patch.object(requests, "get", return_value=response):
        config = configure_pfs_or_exit(
            pfs_address=given_address,
            pfs_eth_address=given_eth_address,
            routing_mode=RoutingMode.PFS,
            service_registry=service_proxy,
        )
    assert config.url == given_address
    assert config.eth_address == given_eth_address
    assert config.fee == json_data["price_info"]

    # Bad address, should exit the program
    response = Mock()
    response.configure_mock(status_code=400)
    bad_address = "http://badaddress"
    pfs_eth_address = "0x22222222222222222222"
    with pytest.raises(SystemExit):
        with patch.object(requests,
                          "get",
                          side_effect=requests.RequestException()):
            # Configuring a given address
            config = configure_pfs_or_exit(
                pfs_address=bad_address,
                pfs_eth_address=pfs_eth_address,
                routing_mode=RoutingMode.PFS,
                service_registry=service_proxy,
            )
Exemplo n.º 4
0
def setup_proxies_or_exit(
    config: Dict[str, Any],
    tokennetwork_registry_contract_address: Address,
    secret_registry_contract_address: Address,
    endpoint_registry_contract_address: Address,
    user_deposit_contract_address: Address,
    service_registry_contract_address: Address,
    blockchain_service: BlockChainService,
    contracts: Dict[str, Any],
    routing_mode: RoutingMode,
    pathfinding_service_address: str,
) -> Proxies:
    """
    Initialize and setup the contract proxies.

    Depending on the provided contract addresses via the CLI, the routing mode,
    the environment type and the network id try to initialize the proxies.
    Returns the initialized proxies or exits the application with an error if
    there is a problem.

    Also depending on the given arguments populate config with PFS related settings
    """
    node_network_id = config["chain_id"]
    environment_type = config["environment_type"]

    check_smart_contract_addresses(
        environment_type,
        node_network_id,
        tokennetwork_registry_contract_address,
        secret_registry_contract_address,
        endpoint_registry_contract_address,
        contracts,
    )
    try:
        registered_address: Address
        if tokennetwork_registry_contract_address is not None:
            registered_address = Address(
                tokennetwork_registry_contract_address)
        else:
            registered_address = to_canonical_address(
                contracts[CONTRACT_TOKEN_NETWORK_REGISTRY]["address"])
        token_network_registry = blockchain_service.token_network_registry(
            registered_address)
    except ContractVersionMismatch as e:
        handle_contract_version_mismatch(e)
    except AddressWithoutCode:
        handle_contract_no_code("token network registry",
                                tokennetwork_registry_contract_address)
    except AddressWrongContract:
        handle_contract_wrong_address("token network registry",
                                      tokennetwork_registry_contract_address)

    try:
        secret_registry = blockchain_service.secret_registry(
            secret_registry_contract_address or to_canonical_address(
                contracts[CONTRACT_SECRET_REGISTRY]["address"]))
    except ContractVersionMismatch as e:
        handle_contract_version_mismatch(e)
    except AddressWithoutCode:
        handle_contract_no_code("secret registry",
                                secret_registry_contract_address)
    except AddressWrongContract:
        handle_contract_wrong_address("secret registry",
                                      secret_registry_contract_address)

    # If services contracts are provided via the CLI use them instead
    if user_deposit_contract_address is not None:
        contracts[CONTRACT_USER_DEPOSIT] = user_deposit_contract_address
    if service_registry_contract_address is not None:
        contracts[
            CONTRACT_SERVICE_REGISTRY] = service_registry_contract_address

    user_deposit = None
    should_use_user_deposit = (
        environment_type == Environment.DEVELOPMENT
        and ID_TO_NETWORKNAME.get(node_network_id) != "smoketest"
        and CONTRACT_USER_DEPOSIT in contracts)
    if should_use_user_deposit:
        try:
            user_deposit = blockchain_service.user_deposit(
                user_deposit_contract_address or to_canonical_address(
                    contracts[CONTRACT_USER_DEPOSIT]["address"]))
        except ContractVersionMismatch as e:
            handle_contract_version_mismatch(e)
        except AddressWithoutCode:
            handle_contract_no_code("user deposit",
                                    user_deposit_contract_address)
        except AddressWrongContract:
            handle_contract_wrong_address("user_deposit",
                                          user_deposit_contract_address)

    service_registry = None
    if CONTRACT_SERVICE_REGISTRY in contracts or service_registry_contract_address:
        try:
            service_registry = blockchain_service.service_registry(
                service_registry_contract_address or to_canonical_address(
                    contracts[CONTRACT_SERVICE_REGISTRY]["address"]))
        except ContractVersionMismatch as e:
            handle_contract_version_mismatch(e)
        except AddressWithoutCode:
            handle_contract_no_code("service registry",
                                    service_registry_contract_address)
        except AddressWrongContract:
            handle_contract_wrong_address("secret registry",
                                          service_registry_contract_address)

    if routing_mode == RoutingMode.PFS:
        check_pfs_configuration(routing_mode, environment_type,
                                service_registry, pathfinding_service_address)

        pfs_config = configure_pfs_or_exit(
            pfs_address=pathfinding_service_address,
            routing_mode=routing_mode,
            service_registry=service_registry,
        )
        msg = "Eth address of selected pathfinding service is unknown."
        assert pfs_config.eth_address is not None, msg
        config["services"]["pathfinding_service_address"] = pfs_config.url
        config["services"]["pathfinding_eth_address"] = pfs_config.eth_address
        config["services"]["pathfinding_fee"] = pfs_config.fee
    else:
        config["services"]["pathfinding_service_address"] = None
        config["services"]["pathfinding_eth_address"] = None

    proxies = Proxies(
        token_network_registry=token_network_registry,
        secret_registry=secret_registry,
        user_deposit=user_deposit,
        service_registry=service_registry,
    )
    return proxies
Exemplo n.º 5
0
def setup_proxies_or_exit(
    config: Dict[str, Any],
    tokennetwork_registry_contract_address: TokenNetworkRegistryAddress,
    secret_registry_contract_address: Address,
    user_deposit_contract_address: Address,
    service_registry_contract_address: Address,
    proxy_manager: ProxyManager,
    contracts: Dict[str, Any],
    routing_mode: RoutingMode,
    pathfinding_service_address: str,
) -> Proxies:
    """
    Initialize and setup the contract proxies.

    Depending on the provided contract addresses via the CLI, the routing mode,
    the environment type and the network id try to initialize the proxies.
    Returns the initialized proxies or exits the application with an error if
    there is a problem.

    Also depending on the given arguments populate config with PFS related settings
    """
    node_network_id = config["chain_id"]
    environment_type = config["environment_type"]

    check_smart_contract_addresses(
        environment_type=environment_type,
        node_network_id=node_network_id,
        tokennetwork_registry_contract_address=
        tokennetwork_registry_contract_address,
        secret_registry_contract_address=secret_registry_contract_address,
        contracts=contracts,
    )

    token_network_registry = None
    try:
        if tokennetwork_registry_contract_address is not None:
            registered_address = tokennetwork_registry_contract_address
        else:
            registered_address = to_canonical_address(
                contracts[CONTRACT_TOKEN_NETWORK_REGISTRY]["address"])
        token_network_registry = proxy_manager.token_network_registry(
            registered_address)
    except ContractCodeMismatch as e:
        handle_contract_code_mismatch(e)
    except AddressWithoutCode:
        handle_contract_no_code(
            "token network registry",
            Address(tokennetwork_registry_contract_address))
    except AddressWrongContract:
        handle_contract_wrong_address(
            "token network registry",
            Address(tokennetwork_registry_contract_address))

    secret_registry = None
    try:
        secret_registry = proxy_manager.secret_registry(
            secret_registry_contract_address or to_canonical_address(
                contracts[CONTRACT_SECRET_REGISTRY]["address"]))
    except ContractCodeMismatch as e:
        handle_contract_code_mismatch(e)
    except AddressWithoutCode:
        handle_contract_no_code("secret registry",
                                secret_registry_contract_address)
    except AddressWrongContract:
        handle_contract_wrong_address("secret registry",
                                      secret_registry_contract_address)

    # If services contracts are provided via the CLI use them instead
    if user_deposit_contract_address is not None:
        contracts[CONTRACT_USER_DEPOSIT] = user_deposit_contract_address
    if service_registry_contract_address is not None:
        contracts[
            CONTRACT_SERVICE_REGISTRY] = service_registry_contract_address

    user_deposit = None
    should_use_user_deposit = (
        environment_type == Environment.DEVELOPMENT
        and ID_TO_NETWORKNAME.get(node_network_id) != "smoketest"
        and CONTRACT_USER_DEPOSIT in contracts)
    if should_use_user_deposit:
        try:
            user_deposit = proxy_manager.user_deposit(
                user_deposit_contract_address or to_canonical_address(
                    contracts[CONTRACT_USER_DEPOSIT]["address"]))
        except ContractCodeMismatch as e:
            handle_contract_code_mismatch(e)
        except AddressWithoutCode:
            handle_contract_no_code("user deposit",
                                    user_deposit_contract_address)
        except AddressWrongContract:
            handle_contract_wrong_address("user_deposit",
                                          user_deposit_contract_address)

    service_registry = None
    if CONTRACT_SERVICE_REGISTRY in contracts or service_registry_contract_address:
        try:
            service_registry = proxy_manager.service_registry(
                service_registry_contract_address or to_canonical_address(
                    contracts[CONTRACT_SERVICE_REGISTRY]["address"]))
        except ContractCodeMismatch as e:
            handle_contract_code_mismatch(e)
        except AddressWithoutCode:
            handle_contract_no_code("service registry",
                                    service_registry_contract_address)
        except AddressWrongContract:
            handle_contract_wrong_address("secret registry",
                                          service_registry_contract_address)

    # By now these should be set or Raiden aborted
    assert token_network_registry, "TokenNetworkRegistry needs to be set"
    assert secret_registry, "SecretRegistry needs to be set"

    if routing_mode == RoutingMode.PFS:
        check_pfs_configuration(
            service_registry=service_registry,
            pathfinding_service_address=pathfinding_service_address,
        )

        pfs_info = configure_pfs_or_exit(
            pfs_url=pathfinding_service_address,
            routing_mode=routing_mode,
            service_registry=service_registry,
            node_network_id=node_network_id,
            token_network_registry_address=token_network_registry.address,
            pathfinding_max_fee=config["services"]["pathfinding_max_fee"],
        )
        msg = "Eth address of selected pathfinding service is unknown."
        assert pfs_info.payment_address is not None, msg

        # Only check that PFS is registered in production mode
        if environment_type == Environment.PRODUCTION:
            check_pfs_for_production(service_registry=service_registry,
                                     pfs_info=pfs_info)

        config["pfs_config"] = PFSConfig(
            info=pfs_info,
            maximum_fee=config["services"]["pathfinding_max_fee"],
            iou_timeout=config["services"]["pathfinding_iou_timeout"],
            max_paths=config["services"]["pathfinding_max_paths"],
        )
    else:
        config["pfs_config"] = None

    proxies = Proxies(
        token_network_registry=token_network_registry,
        secret_registry=secret_registry,
        user_deposit=user_deposit,
        service_registry=service_registry,
    )
    return proxies