Beispiel #1
0
def get_contracts_deployment_info(
    chain_id: ChainID,
    version: Optional[str] = None,
    module: DeploymentModule = DeploymentModule.ALL,
) -> Optional[DeployedContracts]:
    """Reads the deployment data. Returns None if the file is not found.

    Parameter:
        module The name of the module. ALL means deployed contracts from all modules that are
        available for the version.
    """
    if not isinstance(module, DeploymentModule):
        raise ValueError(
            f"Unknown module {module} given to get_contracts_deployment_info()"
        )

    def module_chosen(to_be_added: DeploymentModule) -> bool:
        return module == to_be_added or module == DeploymentModule.ALL

    files: List[Path] = []

    if module_chosen(DeploymentModule.RAIDEN):
        files.append(
            contracts_deployed_path(chain_id=chain_id,
                                    version=version,
                                    services=False))

    if module == DeploymentModule.SERVICES and not contracts_version_provides_services(
            version):
        raise ValueError(
            f"SERVICES module queried for version {version}, but {version} "
            "does not provide service contracts.")

    if module_chosen(DeploymentModule.SERVICES
                     ) and contracts_version_provides_services(version):
        files.append(
            contracts_deployed_path(chain_id=chain_id,
                                    version=version,
                                    services=True))

    deployment_data: Optional[DeployedContracts] = {}

    for f in files:
        j = load_json_from_path(f)
        if j is None:
            continue
        assert deployment_data is not None
        deployment_data = merge_deployment_data(
            deployment_data,
            DeployedContracts({
                "chain_id": j["chain_id"],
                "contracts": j["contracts"],
                "contracts_version": j["contracts_version"],
            }),
        )

    if not deployment_data:
        deployment_data = None
    return deployment_data
Beispiel #2
0
    def deploy_raiden_contracts(
        self,
        max_num_of_token_networks: Optional[int],
        reuse_secret_registry_from_deploy_file: Optional[Path],
        settle_timeout_min: int,
        settle_timeout_max: int,
    ) -> DeployedContracts:
        """ Deploy all required raiden contracts and return a dict of contract_name:address

        Args:
            max_num_of_token_networks (Optional[int]): The max number of tokens that can be
            registered to the TokenNetworkRegistry. If None, the argument is omitted from
            the call to the constructor of TokenNetworkRegistry.
        """

        deployed_contracts: DeployedContracts = {
            "contracts_version": self.contract_manager.contracts_version,
            "chain_id": self.web3.eth.chainId,
            "contracts": {},
        }

        if reuse_secret_registry_from_deploy_file:
            reused_doc = DeployedContracts(  # type: ignore
                load_json_from_path(reuse_secret_registry_from_deploy_file))
            if not reused_doc:
                raise RuntimeError(
                    f"{reuse_secret_registry_from_deploy_file} does not contain deployment data."
                )
            reused_contracts = reused_doc["contracts"]

            secret_registry = self.contract_instance_from_deployment_data(
                reused_doc, CONTRACT_SECRET_REGISTRY)

            deployed_contracts["contracts"][
                CONTRACT_SECRET_REGISTRY] = deepcopy(
                    reused_contracts[CONTRACT_SECRET_REGISTRY])
        else:
            secret_registry = self._deploy_and_remember(
                contract_name=CONTRACT_SECRET_REGISTRY,
                arguments=[],
                deployed_contracts=deployed_contracts,
            )
        token_network_registry_args = [
            secret_registry.address,
            deployed_contracts["chain_id"],
            settle_timeout_min,
            settle_timeout_max,
        ]
        if max_num_of_token_networks:
            token_network_registry_args.append(max_num_of_token_networks)
        self._deploy_and_remember(
            contract_name=CONTRACT_TOKEN_NETWORK_REGISTRY,
            arguments=token_network_registry_args,
            deployed_contracts=deployed_contracts,
        )

        return deployed_contracts
Beispiel #3
0
def test_load_json_from_corrupt_file() -> None:
    with tempfile.NamedTemporaryFile() as f:
        f.write(b"not a JSON")
        with pytest.raises(ValueError):
            load_json_from_path(Path(f.name))
Beispiel #4
0
    def deploy_service_contracts(
        self,
        token_address: HexAddress,
        user_deposit_whole_balance_limit: int,
        service_registry_controller: HexAddress,
        initial_service_deposit_price: int,
        service_deposit_bump_numerator: int,
        service_deposit_bump_denominator: int,
        decay_constant: int,
        min_price: int,
        registration_duration: int,
        token_network_registry_address: HexAddress,
        reuse_service_registry_from_deploy_file: Optional[Path],
    ) -> DeployedContracts:
        """Deploy 3rd party service contracts"""
        if not contracts_version_monitoring_service_takes_token_network_registry(
                self.contract_manager.contracts_version):
            raise RuntimeError(
                "Deployment of older service contracts is not suppported.")

        chain_id = self.web3.eth.chain_id
        deployed_contracts: DeployedContracts = {
            "contracts_version": self.contract_manager.contracts_version,
            "chain_id": chain_id,
            "contracts": {},
        }

        if reuse_service_registry_from_deploy_file:
            reused_doc = DeployedContracts(  # type: ignore
                load_json_from_path(reuse_service_registry_from_deploy_file))
            if not reused_doc:
                raise RuntimeError(
                    f"{reuse_service_registry_from_deploy_file} does not contain deployment data."
                )
            reused_contracts = reused_doc["contracts"]

            service_registry = self.contract_instance_from_deployment_data(
                reused_doc, CONTRACT_SERVICE_REGISTRY)

            deployed_contracts["contracts"][
                CONTRACT_SERVICE_REGISTRY] = deepcopy(
                    reused_contracts[CONTRACT_SERVICE_REGISTRY])
        else:
            service_registry = self._deploy_and_remember(
                contract_name=CONTRACT_SERVICE_REGISTRY,
                arguments=[
                    token_address,
                    service_registry_controller,
                    initial_service_deposit_price,
                    service_deposit_bump_numerator,
                    service_deposit_bump_denominator,
                    decay_constant,
                    min_price,
                    registration_duration,
                ],
                deployed_contracts=deployed_contracts,
            )

        user_deposit = self._deploy_and_remember(
            contract_name=CONTRACT_USER_DEPOSIT,
            arguments=[token_address, user_deposit_whole_balance_limit],
            deployed_contracts=deployed_contracts,
        )

        monitoring_service_constructor_args = [
            token_address,
            deployed_contracts["contracts"][CONTRACT_SERVICE_REGISTRY]
            ["address"],
            deployed_contracts["contracts"][CONTRACT_USER_DEPOSIT]["address"],
            token_network_registry_address,
        ]
        msc = self._deploy_and_remember(
            contract_name=CONTRACT_MONITORING_SERVICE,
            arguments=monitoring_service_constructor_args,
            deployed_contracts=deployed_contracts,
        )

        one_to_n = self._deploy_and_remember(
            contract_name=CONTRACT_ONE_TO_N,
            arguments=[
                user_deposit.address, chain_id, service_registry.address
            ],
            deployed_contracts=deployed_contracts,
        )

        # Tell the UserDeposit instance about other contracts.
        LOG.debug("Calling UserDeposit.init() with "
                  f"msc_address={msc.address} "
                  f"one_to_n_address={one_to_n.address}")
        self.transact(
            user_deposit.functions.init(_msc_address=msc.address,
                                        _one_to_n_address=one_to_n.address))

        return deployed_contracts