Esempio n. 1
0
def join_sources(source_module: str, contract_name: str):
    """ Use join-contracts.py to concatenate all imported Solidity files.

    Args:
        source_module: a module name to look up contracts_source_path()
        contract_name: 'TokenNetworkRegistry', 'SecretRegistry' etc.
    """
    joined_file = Path(__file__).parent.joinpath('joined.sol')
    remapping = {
        module: str(path)
        for module, path in contracts_source_path().items()
    }
    command = [
        './utils/join-contracts.py',
        '--import-map',
        json.dumps(remapping),
        str(contracts_source_path()[source_module].joinpath(contract_name +
                                                            '.sol')),
        str(joined_file),
    ]
    working_dir = Path(__file__).parent.parent
    try:
        subprocess.check_call(command, cwd=working_dir)
    except subprocess.CalledProcessError as ex:
        print(
            f'cd {str(working_dir)}; {subprocess.list2cmdline(command)} failed.'
        )
        raise ex

    return joined_file.read_text()
Esempio n. 2
0
def test_verification_contracts_checksums():
    """ Tamper with the contract checksums and see failures in verify_precompiled_checksums() """
    manager = ContractSourceManager(contracts_source_path())
    manager.checksum_contracts()
    manager.verify_precompiled_checksums(contracts_precompiled_path())

    assert manager.contracts_checksums
    for contract, checksum in manager.contracts_checksums.items():
        manager.contracts_checksums[contract] += '2'
        with pytest.raises(ContractSourceManagerVerificationError):
            manager.verify_precompiled_checksums(contracts_precompiled_path())

        manager.contracts_checksums[contract] = None  # type: ignore
        with pytest.raises(ContractSourceManagerVerificationError):
            manager.verify_precompiled_checksums(contracts_precompiled_path())

        manager.contracts_checksums[contract] = ''
        with pytest.raises(ContractSourceManagerVerificationError):
            manager.verify_precompiled_checksums(contracts_precompiled_path())

        checksum_fail = list(checksum)
        # Replace the first char with a different one
        checksum_fail[0] = 'a' if checksum_fail[0] == '2' else '2'
        manager.contracts_checksums[contract] = ''.join(checksum_fail)
        with pytest.raises(ContractSourceManagerVerificationError):
            manager.verify_precompiled_checksums(contracts_precompiled_path())

        manager.contracts_checksums[contract] = checksum
        manager.verify_precompiled_checksums(contracts_precompiled_path())
Esempio n. 3
0
def test_verification_overall_checksum():
    """ Tamper with the overall checksum and see failures in verify_precompiled_checksums() """
    manager = ContractSourceManager(contracts_source_path())
    manager.checksum_contracts()
    manager.verify_precompiled_checksums(contracts_precompiled_path())

    assert manager.overall_checksum
    original_checksum = manager.overall_checksum

    # We change the source code overall checksum
    manager.overall_checksum += '2'
    # Now the verification should fail
    with pytest.raises(ContractSourceManagerVerificationError):
        manager.verify_precompiled_checksums(contracts_precompiled_path())

    manager.overall_checksum = None  # type: ignore
    with pytest.raises(ContractSourceManagerVerificationError):
        manager.verify_precompiled_checksums(contracts_precompiled_path())

    manager.overall_checksum = ''
    with pytest.raises(ContractSourceManagerVerificationError):
        manager.verify_precompiled_checksums(contracts_precompiled_path())

    checksum_fail = list(original_checksum)
    # Replace the first char with a different one
    checksum_fail[0] = 'a' if checksum_fail[0] == '2' else '2'
    manager.overall_checksum = ''.join(checksum_fail)
    with pytest.raises(ContractSourceManagerVerificationError):
        manager.verify_precompiled_checksums(contracts_precompiled_path())

    manager.overall_checksum = original_checksum
    manager.verify_precompiled_checksums(contracts_precompiled_path())
Esempio n. 4
0
def test_contract_manager_json(tmpdir):
    """ Check the ABI in contracts.json """
    precompiled_path = Path(str(tmpdir)).joinpath('contracts.json')
    ContractSourceManager(
        contracts_source_path()).compile_contracts(precompiled_path)
    # try to load contracts from a precompiled file
    contract_manager_meta(precompiled_path, source=False)
def test_contract_manager_json(tmpdir: LocalPath) -> None:
    """ Check the ABI in contracts.json """
    precompiled_path = Path(tmpdir).joinpath("contracts.json")
    ContractSourceManager(
        contracts_source_path()).compile_contracts(precompiled_path)
    # try to load contracts from a precompiled file
    contract_manager_meta(precompiled_path)
def test_current_development_version() -> None:
    """ contracts_source_path() exists and contains the expected files """
    contracts_version = CONTRACTS_VERSION
    contract_names = [
        "Utils",
        "SecretRegistry",
        "TokenNetworkRegistry",
        "TokenNetwork",
        "MonitoringService",
        "ServiceRegistry",
    ]

    manager = ContractManager(contracts_precompiled_path(contracts_version))
    assert manager.contracts_version == contracts_version
    check_precompiled_content(manager, contract_names, PRECOMPILED_DATA_FIELDS)

    for _, source_path in contracts_source_path().items():
        assert source_path.exists()
    assert contracts_precompiled_path().exists()

    # deployment files exist
    assert contracts_deployed_path(NETWORKNAME_TO_ID["rinkeby"]).exists()
    assert contracts_deployed_path(NETWORKNAME_TO_ID["ropsten"]).exists()
    assert contracts_deployed_path(NETWORKNAME_TO_ID["kovan"]).exists()
    # deployment files for service contracts also exist
    assert contracts_deployed_path(NETWORKNAME_TO_ID["rinkeby"],
                                   services=True).exists()
    assert contracts_deployed_path(NETWORKNAME_TO_ID["ropsten"],
                                   services=True).exists()
    assert contracts_deployed_path(NETWORKNAME_TO_ID["kovan"],
                                   services=True).exists()
Esempio n. 7
0
def join_sources(source_module: DeploymentModule, contract_name: str) -> str:
    """ Use join_contracts.py to concatenate all imported Solidity files.

    Args:
        source_module: a module name to look up contracts_source_path()
        contract_name: 'TokenNetworkRegistry', 'SecretRegistry' etc.
    """
    joined_file = Path(__file__).parent.joinpath("joined.sol")
    remapping = {
        module: str(path)
        for module, path in contracts_source_path(
            contracts_version=None).items()
    }
    command = [
        "./utils/join_contracts.py",
        "--import-map",
        json.dumps(remapping),
        str(
            contracts_source_path_of_deployment_module(source_module).joinpath(
                contract_name + ".sol")),
        str(joined_file),
    ]
    working_dir = Path(__file__).parent.parent
    try:
        subprocess.check_call(command, cwd=working_dir)
    except subprocess.CalledProcessError as ex:
        print(
            f"cd {str(working_dir)}; {subprocess.list2cmdline(command)} failed."
        )
        raise ex

    return joined_file.read_text()
    def __init__(
        self,
        web3: Web3,
        private_key: str,
        gas_limit: int,
        gas_price: int = 1,
        wait: int = 10,
        contracts_version: Optional[str] = None,
    ):
        # pylint: disable=E1101
        super(ContractDeployer, self).__init__(web3=web3, contracts_version=contracts_version)
        self.wait = wait
        self.owner = private_key_to_address(private_key)
        self.transaction = {"from": self.owner, "gas": gas_limit}
        if gas_price != 0:
            self.transaction["gasPrice"] = gas_price * int(units["gwei"])

        self.web3.middleware_stack.add(construct_sign_and_send_raw_middleware(private_key))

        # Check that the precompiled data matches the source code
        # Only for current version, because this is the only one with source code
        if self.contracts_version in [None, CONTRACTS_VERSION]:
            contract_manager_source = ContractSourceManager(contracts_source_path())
            contract_manager_source.checksum_contracts()
            contract_manager_source.verify_precompiled_checksums(self.precompiled_path)
        else:
            LOG.info("Skipped checks against the source code because it is not available.")
Esempio n. 9
0
    def run(self) -> None:  # pylint: disable=no-self-use
        from raiden_contracts.contract_manager import contracts_precompiled_path
        from raiden_contracts.contract_source_manager import (
            ContractSourceManager,
            contracts_source_path,
        )

        contract_manager = ContractSourceManager(contracts_source_path(contracts_version=None))
        contract_manager.compile_contracts(contracts_precompiled_path())
Esempio n. 10
0
    def run(self):
        from raiden_contracts.contract_manager import contracts_precompiled_path
        from raiden_contracts.contract_source_manager import (
            ContractSourceManager,
            contracts_source_path,
        )

        contract_manager = ContractSourceManager(contracts_source_path())
        contract_manager.compile_contracts(contracts_precompiled_path())
Esempio n. 11
0
 def run(self):
     from raiden_contracts.contract_manager import (
         contracts_precompiled_path, )
     from raiden_contracts.contract_source_manager import (
         ContractSourceManager,
         contracts_source_path,
     )
     manager = ContractSourceManager(contracts_source_path())
     manager.verify_precompiled_checksums(contracts_precompiled_path())
Esempio n. 12
0
def test_contract_manager_compile():
    """ Check the ABI in the sources """
    contract_manager_meta(contracts_source_path(), source=True)
def test_verification_without_checksum() -> None:
    """ Call ContractSourceManager.verify_precompiled_checksums() before computing the checksum """
    manager = ContractSourceManager(contracts_source_path())
    with pytest.raises(AttributeError):
        manager.verify_precompiled_checksums(contracts_precompiled_path())
def test_contract_manager_compile() -> None:
    """ Check the ABI in the sources """
    contract_source_manager_meta(contracts_source_path())
Esempio n. 15
0
def contract_source_manager() -> ContractSourceManager:
    return ContractSourceManager(contracts_source_path())
Esempio n. 16
0
def contract_source_manager() -> ContractSourceManager:
    return ContractSourceManager(contracts_source_path(contracts_version=None))