def test_deploying_contract_with_error_during_deploy_sanity_check(project_dir, THROWER):
    """
    Just a sanity check that the `Thrower` contract can be successfully
    deployed.
    """
    project = Project()
    chain = project.get_chain("testrpc")

    exports = []

    with chain:
        Thrower = chain.web3.eth.contract(**THROWER)

        @click.command()
        def wrapper():
            math_contract = deploy_contract_and_verify(
                chain, contract_name="Thrower", base_contract_factory=Thrower, deploy_arguments=[False]
            )
            exports.append(math_contract)
            print("~~{0}~~".format(math_contract.address))

        runner = CliRunner()
        result = runner.invoke(wrapper, [])

    assert result.exit_code == 0
    assert "Verified contract bytecode" in result.output
    assert "No runtime available" not in result.output
Esempio n. 2
0
def test_initialized():
    """Crowdsale is properly initialized with given parameters."""

    project_dir = DATACOIN_PATH
    project = Project(project_dir, create_config_file=True)
    with project.get_chain('tester') as chain:
        beneficiary = chain.web3.eth.accounts[3]
        multisig = chain.web3.eth.accounts[4]

        # Initialize crowdsale
        args = [beneficiary, multisig, 0]
        crowdsale, _ = chain.provider.get_or_deploy_contract('Crowdsale',
                                                             deploy_args=args)

        # Initialize token
        args = [beneficiary]  # Owner set
        token, _ = chain.provider.get_or_deploy_contract('DataCoin',
                                                         deploy_args=args)
        assert crowdsale.call().tokenReward(
        ) == '0x0000000000000000000000000000000000000000'
        crowdsale.transact({"from": beneficiary}).setToken(token.address)
        assert crowdsale.call().tokenReward(
        ) != '0x0000000000000000000000000000000000000000'

        token.transact({
            "from": beneficiary
        }).approve(crowdsale.address, 440000000)

        assert token.call().balanceOf(beneficiary) == 500000000
        assert token.call().totalSupply() == 500000000
        assert token.call().owner().lower() == beneficiary
        assert token.call().allowance(beneficiary,
                                      crowdsale.address) == 440000000
        assert token.call().owner().lower() == crowdsale.call().beneficiary(
        ).lower()
Esempio n. 3
0
def test_project_directory_properties(project_dir):
    project = Project()

    contracts_dir = get_contracts_dir(project_dir)
    assert is_same_path(project.contracts_dir, contracts_dir)

    build_dir = get_build_dir(project_dir)
    assert is_same_path(project.build_dir, build_dir)

    compiled_contracts_file_path = get_compiled_contracts_file_path(
        project_dir)
    assert is_same_path(project.compiled_contracts_file_path,
                        compiled_contracts_file_path)

    blockchains_dir = get_blockchains_dir(project_dir)
    assert is_same_path(project.blockchains_dir, blockchains_dir)

    data_dir = get_data_dir(project_dir, 'some-test-chain-name')
    assert is_same_path(
        project.get_blockchain_data_dir('some-test-chain-name'), data_dir)

    chaindata_dir = get_chaindata_dir(data_dir)
    assert is_same_path(
        project.get_blockchain_chaindata_dir('some-test-chain-name'),
        chaindata_dir)

    geth_ipc_path = get_geth_ipc_path(data_dir)
    assert is_same_path(
        project.get_blockchain_ipc_path('some-test-chain-name'), geth_ipc_path)
def test_with_successful_deploy_sans_runtime_bytecode(project_dir,
                                                      MATH):
    project = Project()
    chain = project.get_chain('testrpc')

    exports = []

    with chain:
        Math = chain.web3.eth.contract(
            abi=MATH['abi'],
            code=MATH['code'],
        )
        assert not Math.code_runtime

        @click.command()
        def wrapper():
            math_contract = deploy_contract_and_verify(
                chain,
                contract_name='Math',
                base_contract_factory=Math,
            )
            exports.append(math_contract)
            print("~~{0}~~".format(math_contract.address))

        runner = CliRunner()
        result = runner.invoke(wrapper, [])

    assert result.exit_code == 0, str(result.output) + '\n' + str(result.exception)
    assert len(exports) == 1
    math_contract = exports[0]
    expected = "~~{0}~~".format(math_contract.address)
    assert expected in result.output
    assert "Verified contract bytecode" not in result.output
    assert "No runtime available" in result.output
Esempio n. 5
0
def project(request, pytestconfig):

    project_dir = get_populus_option(cmdline_option="--populus-project",
                                     ini_option="populus_project",
                                     environ_var="PYTEST_POPULUS_PROJECT",
                                     pytestconfig=pytestconfig,
                                     default=pytestconfig.args[0])

    if not os.path.exists(get_json_config_file_path(project_dir)):
        raise FileNotFoundError(
            "No populus project found for testing in {project_dir}".format(
                project_dir=project_dir))

    contracts = request.config.cache.get(CACHE_KEY_CONTRACTS, None)
    mtime = request.config.cache.get(CACHE_KEY_MTIME, None)

    project = Project(project_dir, create_config_file=True)

    project.fill_contracts_cache(contracts, mtime)
    request.config.cache.set(
        CACHE_KEY_CONTRACTS,
        normalize_object_for_json(project.compiled_contract_data),
    )
    request.config.cache.set(
        CACHE_KEY_MTIME,
        get_latest_mtime(project.get_all_source_file_paths()),
    )

    return project
def test_with_successful_deploy_sans_runtime_bytecode(project_dir, MATH):
    project = Project()
    chain = project.get_chain("testrpc")

    exports = []

    with chain:
        Math = chain.web3.eth.contract(abi=MATH["abi"], code=MATH["code"])
        assert not Math.code_runtime

        @click.command()
        def wrapper():
            math_contract = deploy_contract_and_verify(chain, contract_name="Math", base_contract_factory=Math)
            exports.append(math_contract)
            print("~~{0}~~".format(math_contract.address))

        runner = CliRunner()
        result = runner.invoke(wrapper, [])

    assert result.exit_code == 0, str(result.output) + "\n" + str(result.exception)
    assert len(exports) == 1
    math_contract = exports[0]
    expected = "~~{0}~~".format(math_contract.address)
    assert expected in result.output
    assert "Verified contract bytecode" not in result.output
    assert "No runtime available" in result.output
Esempio n. 7
0
def test_cli_select_chain_helper(project_dir, write_project_file, stdin,
                                 expected):
    project = Project()
    project.config['chains.local_a.chain.class'] = 'populus.chain.TesterChain'
    project.config[
        'chains.local_a.web3.provider.class'] = 'web3.providers.ipc.IPCProvider'
    project.config['chains.local_a.web3.provider.settings.ipc_path'] = (
        get_geth_ipc_path(
            get_local_chain_datadir(project.project_dir, 'local_a')))
    project.config['chains.local_b.chain.class'] = 'populus.chain.TesterChain'
    project.config[
        'chains.local_b.web3.provider.class'] = 'web3.providers.ipc.IPCProvider'
    project.config['chains.local_b.web3.provider.settings.ipc_path'] = (
        get_geth_ipc_path(
            get_local_chain_datadir(project.project_dir, 'local_b')))
    project.config['chains.local_c.chain.class'] = 'populus.chain.TesterChain'
    project.config[
        'chains.local_c.web3.provider.class'] = 'web3.providers.ipc.IPCProvider'
    project.config['chains.local_c.web3.provider.settings.ipc_path'] = (
        get_geth_ipc_path(
            get_local_chain_datadir(project.project_dir, 'local_c')))
    project.write_config()

    @click.command()
    def wrapper():
        chain_name = select_chain(project)
        print("~~{0}~~".format(chain_name))

    runner = CliRunner()
    result = runner.invoke(wrapper, [], input=stdin)

    assert result.exit_code == 0
    assert expected in result.output
def test_deploying_contract_with_error_during_deploy_sanity_check(project_dir,
                                                                  THROWER):
    """
    Just a sanity check that the `Thrower` contract can be successfully
    deployed.
    """
    project = Project()
    chain = project.get_chain('testrpc')

    exports = []

    with chain:
        Thrower = chain.web3.eth.contract(**THROWER)
        @click.command()
        def wrapper():
            math_contract = deploy_contract_and_verify(
                chain,
                contract_name='Thrower',
                base_contract_factory=Thrower,
                deploy_arguments=[False],
            )
            exports.append(math_contract)
            print("~~{0}~~".format(math_contract.address))

        runner = CliRunner()
        result = runner.invoke(wrapper, [])

    assert result.exit_code == 0
    assert "Verified contract bytecode" in result.output
    assert "No runtime available" not in result.output
Esempio n. 9
0
def test_project_testrpc_chain(project_dir):
    project = Project(project_dir, create_config_file=True)

    chain = project.get_chain('testrpc')

    with chain as running_tester_chain:
        web3 = running_tester_chain.web3
        assert web3.version.node.startswith('TestRPC')
Esempio n. 10
0
def test_project_tester_chain(project_dir):
    project = Project(project_dir)

    chain = project.get_chain('tester')

    with chain as running_tester_chain:
        web3 = running_tester_chain.web3
        assert web3.version.node.startswith('TestRPC')
Esempio n. 11
0
def test_project_tester_chain(project_dir):
    project = Project(project_dir)

    chain = project.get_chain('tester')

    with chain as running_tester_chain:
        web3 = running_tester_chain.web3
        assert web3.version.node.startswith('TestRPC')
Esempio n. 12
0
def test_project_tester_chain(project_dir):
    project = Project(project_dir)

    chain = project.get_chain('tester')

    with chain as running_tester_chain:
        web3 = running_tester_chain.web3
        assert web3.clientVersion.startswith('EthereumTester')
Esempio n. 13
0
def test_project_temp_chain(project_dir):
    project = Project()

    chain = project.get_chain('temp')

    with chain as running_temp_chain:
        web3 = running_temp_chain.web3
        assert hasattr(running_temp_chain, 'geth')
        assert web3.version.node.startswith('Geth')
def test_project_contract_factories_property(project_dir, write_project_file,
                                             MATH):
    write_project_file('contracts/Math.sol', MATH['source'])

    project = Project()
    with project.get_chain('testrpc') as chain:
        assert len(chain.contract_factories.Math.abi) == len(MATH['abi'])
        assert len(chain.contract_factories.Math.code) > 2
        assert len(chain.contract_factories.Math.code_runtime) > 2
Esempio n. 15
0
def test_project_temp_chain(project_dir):
    project = Project()

    chain = project.get_chain('temp')

    with chain as running_temp_chain:
        web3 = running_temp_chain.web3
        assert hasattr(running_temp_chain, 'geth')
        assert web3.version.node.startswith('Geth')
Esempio n. 16
0
def test_buy_tokens():
    """Sending ETH successfully buys tokens."""
    start = 1488294000
    end = 1490112000
    project_dir = DATACOIN_PATH
    project = Project(project_dir, create_config_file=True)
    with project.get_chain('tester') as chain:
        beneficiary = chain.web3.eth.accounts[3]
        multisig = chain.web3.eth.accounts[4]
        customer = chain.web3.eth.accounts[1]

        # Initialize crowdsale
        args = [beneficiary, multisig, 0]
        crowdsale, _ = chain.provider.get_or_deploy_contract('Crowdsale',
                                                             deploy_args=args)

        # Initialize token
        args = [beneficiary]  # Owner set
        token, _ = chain.provider.get_or_deploy_contract('DataCoin',
                                                         deploy_args=args)
        assert crowdsale.call().tokenReward(
        ) == '0x0000000000000000000000000000000000000000'
        crowdsale.transact({"from": beneficiary}).setToken(token.address)
        assert crowdsale.call().tokenReward(
        ) != '0x0000000000000000000000000000000000000000'

        token.transact({
            "from": beneficiary
        }).approve(crowdsale.address, 440000000)

        # Doing open crowdsale
        crowdsale.transact().setCurrent(start + 1)
        token.transact().setCurrent(start + 1)

        chain.web3.eth.sendTransaction({
            "from": customer,
            "to": crowdsale.address,
            "value": Web3.toWei(20, "ether"),
            "gas": 250000,
        })

        # We get ERC-20 event
        events = token.pastEvents("Transfer").get()
        assert len(events) == 1
        e = events[0]
        assert e["args"]["to"].lower() == customer
        assert e["args"]["from"].lower() == beneficiary
        assert e["args"]["value"] == 24000

        # We get crowdsale event
        events = crowdsale.pastEvents("FundTransfer").get()
        assert len(events) == 1
        e = events[0]
        assert e["args"]["backer"].lower() == customer
        assert e["args"]["amount"] == Web3.toWei(20, "ether")
        assert e["args"]["amountRaised"] == Web3.toWei(20, "ether")
Esempio n. 17
0
def local_chain(project_dir, write_project_file):
    write_project_file('populus.ini', '[chain:local]')

    project = Project()
    chain = project.get_chain('local')

    # create a new account
    create_new_account(chain.geth.data_dir, b'a-test-password')

    return chain
Esempio n. 18
0
def local_chain(project_dir):
    project = Project()
    project.config['chains.local.chain.class'] = 'populus.chain.LocalGethChain'
    project.config['chains.local.web3.provider.class'] = 'web3.providers.ipc.IPCProvider'
    project.config['chains.local.contracts.backends.Memory.$ref'] = 'contracts.backends.Memory'
    project.write_config()

    chain = project.get_chain('local')

    return chain
def local_chain(project_dir, write_project_file):
    write_project_file('populus.ini', '[chain:local]')

    project = Project()
    chain = project.get_chain('local')

    # create a new account
    create_new_account(chain.geth.data_dir, b'a-test-password')

    return chain
Esempio n. 20
0
def test_project_morden_chain(project_dir):
    project = Project()

    chain = project.get_chain('morden')

    with chain as running_morden_chain:
        web3 = running_morden_chain.web3
        assert web3.version.node.startswith('Geth')

        running_morden_chain.wait.for_block(block_number=1, timeout=180)

        block_1 = web3.eth.getBlock(1)
        assert block_1['hash'] == TESTNET_BLOCK_1_HASH
Esempio n. 21
0
def local_chain(project_dir):
    project = Project()
    project.config['chains.local.chain.class'] = 'populus.chain.LocalGethChain'
    project.config[
        'chains.local.web3.provider.class'] = 'web3.providers.ipc.IPCProvider'
    project.config['chains.local.web3.provider.settings.ipc_path'] = (
        get_geth_ipc_path(get_local_chain_datadir(project.project_dir,
                                                  'local')))
    project.write_config()

    chain = project.get_chain('local')

    return chain
Esempio n. 22
0
def project(request):
    # This should probably be configurable using the `request` fixture but it's
    # unclear what needs to be configurable.

    # use pytest cache to preset the sessions project to recently compiled contracts
    contracts = request.config.cache.get(CACHE_KEY_CONTRACTS, None)
    mtime = request.config.cache.get(CACHE_KEY_MTIME, None)
    project = Project()
    project.fill_contracts_cache(contracts, mtime)
    request.config.cache.set(CACHE_KEY_CONTRACTS, project.compiled_contracts)
    request.config.cache.set(CACHE_KEY_MTIME, project.get_source_modification_time())

    return project
Esempio n. 23
0
def test_project_morden_chain(project_dir):
    project = Project()

    chain = project.get_chain('morden')

    with chain as running_morden_chain:
        web3 = running_morden_chain.web3
        assert web3.version.node.startswith('Geth')

        running_morden_chain.wait.for_block(block_number=1, timeout=180)

        block_1 = web3.eth.getBlock(1)
        assert block_1['hash'] == TESTNET_BLOCK_1_HASH
Esempio n. 24
0
def project(request):
    contracts = request.config.cache.get(CACHE_KEY_CONTRACTS, None)
    mtime = request.config.cache.get(CACHE_KEY_MTIME, None)

    project = Project()

    project.fill_contracts_cache(contracts, mtime)
    request.config.cache.set(CACHE_KEY_CONTRACTS,
                             project.compiled_contract_data)
    request.config.cache.set(CACHE_KEY_MTIME,
                             project.get_source_modification_time())

    return project
Esempio n. 25
0
def test_select_account_with_invalid_option(project_dir, stdin):
    project = Project()
    chain = project.get_chain('temp')

    @click.command()
    def wrapper():
        account = select_account(chain)
        print("~~{0}~~".format(account))

    with chain:
        runner = CliRunner()
        result = runner.invoke(wrapper, [], input="{0}\n".format(stdin))

    assert result.exit_code != 0
def test_select_account_with_invalid_option(project_dir, stdin):
    project = Project()
    chain = project.get_chain('temp')

    @click.command()
    def wrapper():
        account = select_account(chain)
        print("~~{0}~~".format(account))

    with chain:
        runner = CliRunner()
        result = runner.invoke(wrapper, [], input="{0}\n".format(stdin))

    assert result.exit_code != 0
Esempio n. 27
0
def test_dates():
    """Dates match given in the project material."""
    project_dir = DATACOIN_PATH
    project = Project(project_dir, create_config_file=True)
    with project.get_chain('tester') as chain:
        beneficiary = chain.web3.eth.accounts[3]
        multisig = chain.web3.eth.accounts[4]
        customer = chain.web3.eth.accounts[1]

        # Initialize crowdsale
        args = [beneficiary, multisig, 0]
        crowdsale, _ = chain.provider.get_or_deploy_contract('Crowdsale',
                                                             deploy_args=args)

        # Initialize token
        args = [beneficiary]  # Owner set
        token, _ = chain.provider.get_or_deploy_contract('DataCoin',
                                                         deploy_args=args)
        assert crowdsale.call().tokenReward(
        ) == '0x0000000000000000000000000000000000000000'
        crowdsale.transact({"from": beneficiary}).setToken(token.address)
        assert crowdsale.call().tokenReward(
        ) != '0x0000000000000000000000000000000000000000'

        token.transact({
            "from": beneficiary
        }).approve(crowdsale.address, 440000000)

        deadlines = [
            crowdsale.call().deadlines(0),
            crowdsale.call().deadlines(1),
            crowdsale.call().deadlines(2),
            crowdsale.call().deadlines(3)
        ]

        print("Start is {}".format(
            datetime.datetime.fromtimestamp(crowdsale.call().start(),
                                            tz=datetime.timezone.utc)))

        for idx, deadline in enumerate(deadlines):
            print("Deadline {} is {}".format(
                idx,
                datetime.datetime.fromtimestamp(deadline,
                                                tz=datetime.timezone.utc)))

        print("Token is transferable {}".format(
            datetime.datetime.fromtimestamp(token.call().startTime(),
                                            tz=datetime.timezone.utc)))

        assert token.call().startTime() == deadlines[-1]
Esempio n. 28
0
def project(request):
    # This should probably be configurable using the `request` fixture but it's
    # unclear what needs to be configurable.

    # use pytest cache to preset the sessions project to recently compiled contracts
    contracts = request.config.cache.get(CACHE_KEY_CONTRACTS, None)
    mtime = request.config.cache.get(CACHE_KEY_MTIME, None)
    project = Project()
    project.fill_contracts_cache(contracts, mtime)
    request.config.cache.set(CACHE_KEY_CONTRACTS, project.compiled_contracts)
    request.config.cache.set(CACHE_KEY_MTIME,
                             project.get_source_modification_time())

    return project
def test_project_fill_contracts_cache(write_project_file,
                                      MATH):
    write_project_file('contracts/Math.sol', MATH['source'])
    source_mtime = Project().get_source_modification_time()

    project = Project()
    compiled_contracts_object_id = id(project.compiled_contracts)

    # fill with code from the future -> no recompilation
    project.fill_contracts_cache(project.compiled_contracts, source_mtime + 10)
    assert id(project.compiled_contracts) == compiled_contracts_object_id

    # fill with code from the past -> recompilation
    project.fill_contracts_cache(project.compiled_contracts, source_mtime - 10)
    assert not id(project.compiled_contracts) == compiled_contracts_object_id
def test_cli_select_chain_helper(project_dir, write_project_file, stdin,
                                 expected):
    write_project_file(
        'populus.ini',
        '\n'.join((
            "[chain:local_a]",  # 0
            "",
            "[chain:local_b]",  # 1
            "",
            "[chain:local_c]",  # 2
            "",
            "[chain:mainnet]",  # 3
            "",
            "[chain:morden]",  # 4
            "",
            "[chain:testrpc]",  # 5
        )))
    project = Project()

    assert 'local_a' in project.config.chains
    assert 'local_b' in project.config.chains
    assert 'local_c' in project.config.chains

    @click.command()
    def wrapper():
        chain_name = select_chain(project)
        print("~~{0}~~".format(chain_name))

    runner = CliRunner()
    result = runner.invoke(wrapper, [], input=stdin)

    assert result.exit_code == 0
    assert expected in result.output
Esempio n. 31
0
def test_upgrade_to_user_config(project, from_legacy_version):

    shutil.copyfile(
        get_default_config_path(version=from_legacy_version),
        get_legacy_json_config_file_path(project_dir=project.project_dir))

    os.remove(project.config_file_path)

    logger = logging.getLogger("test.test_upgrade_to_user_config")
    upgrade_configs(project.project_dir, logger, FIRST_USER_CONFIG_VERSION)

    upgraded_project = Project(
        project_dir=project.project_dir,
        user_config_file_path=project.user_config_file_path)

    expected_user_config = Config(
        load_user_default_config(FIRST_USER_CONFIG_VERSION))
    expected_user_config.unref()

    expected_project_config = Config(
        load_default_config(FIRST_USER_CONFIG_VERSION))
    expected_project_config.unref()

    assert upgraded_project.legacy_config_path is None
    assert upgraded_project.config == expected_user_config
    assert upgraded_project.user_config == expected_user_config
    assert upgraded_project.project_config == expected_project_config
def test_select_project_contract_helper(project_dir, write_project_file, input,
                                        expected_name):
    write_project_file('contracts/ContractA.sol',
                       'contract A { function A() {}}')
    write_project_file('contracts/ContractB.sol',
                       'contract B { function B() {}}')
    write_project_file('contracts/ContractC.sol',
                       'contract C { function C() {}}')

    project = Project()

    assert 'A' in project.compiled_contract_data
    assert 'B' in project.compiled_contract_data
    assert 'C' in project.compiled_contract_data

    @click.command()
    def wrapper():
        contract_name = select_project_contract(project)
        print("~~{0}~~".format(contract_name))

    runner = CliRunner()
    result = runner.invoke(wrapper, [], input="{0}\n".format(input))

    assert result.exit_code == 0
    expected = "~~{0}~~".format(expected_name)
    assert expected in result.output
Esempio n. 33
0
def main(ctx, config):
    """
    Populus
    """
    if not config and check_if_ini_config_file_exists():
        click.echo("Attempting to upgrade legacy `populus.ini` config file")
        try:
            backup_ini_config_file_path = upgrade_legacy_config_file(os.getcwd())
        except:
            click.echo(
                "The following error occured while trying to upgrade the legacy "
                "`populus.ini` config file:"
            )
            raise
        else:
            click.echo(
                "Project configuration upgraded.  New config file "
                "`populus.json` has been written.  Old config file was renamed "
                "to `{0}`".format(backup_ini_config_file_path)
            )

    project = Project(config)

    if not any(is_same_path(p, project.project_dir) for p in sys.path):
        # ensure that the project directory is in the sys.path
        sys.path.insert(0, project.project_dir)

    ctx.obj = {}
    ctx.obj['PROJECT'] = project
Esempio n. 34
0
def test_upgrade_custom_key(project):
    legacy_config_file_path = get_legacy_json_config_file_path(
        project_dir=project.project_dir)
    shutil.copyfile(get_default_config_path(version=V3),
                    legacy_config_file_path)
    os.remove(project.config_file_path)

    legacy_key = 'compilation.import_remapping'
    legacy_value = ['import-path-from-legacy=contracts']
    upgraded_key = 'compilation.import_remappings'

    legacy_config = Config(load_config(legacy_config_file_path))
    legacy_config[legacy_key] = legacy_value
    write_config(legacy_config, legacy_config_file_path)

    logger = logging.getLogger("test.test_upgrade_custom_key")
    upgrade_configs(project.project_dir, logger, FIRST_USER_CONFIG_VERSION)

    upgraded_project = Project(
        project_dir=project.project_dir,
        user_config_file_path=project.user_config_file_path)

    assert upgraded_project.config.get(upgraded_key) == legacy_value
    assert upgraded_project.project_config.get(upgraded_key) == legacy_value

    default_user_config = Config(
        load_user_default_config(version=FIRST_USER_CONFIG_VERSION))
    assert upgraded_project.user_config.get(
        upgraded_key) == default_user_config.get(upgraded_key)
Esempio n. 35
0
def test_reset_chain_on_empty_project_dir(project_dir, write_project_file):
    project = Project()

    data_dir = project.get_blockchain_data_dir('test-chain')
    ensure_path_exists(data_dir)

    chaindata_dir = project.get_blockchain_chaindata_dir('test-chain')
    dapp_dir = project.get_blockchain_dapp_dir('test-chain')
    nodekey_path = project.get_blockchain_nodekey_path('test-chain')
    geth_ipc_path = project.get_blockchain_ipc_path('test-chain')

    # sanity check
    assert os.path.exists(data_dir)
    assert not os.path.exists(chaindata_dir)
    assert not os.path.exists(dapp_dir)
    assert not os.path.exists(nodekey_path)
    assert not os.path.exists(geth_ipc_path)

    reset_chain(data_dir)

    assert os.path.exists(data_dir)
    assert not os.path.exists(chaindata_dir)
    assert not os.path.exists(dapp_dir)
    assert not os.path.exists(nodekey_path)
    assert not os.path.exists(geth_ipc_path)
def test_project_directory_properties(project_dir):
    project = Project()

    contracts_source_dir = get_contracts_source_dir(project_dir)
    assert is_same_path(project.contracts_source_dir, contracts_source_dir)
    with pytest.warns(DeprecationWarning):
        assert is_same_path(project.contracts_dir, contracts_source_dir)

    build_asset_dir = get_build_asset_dir(project_dir)
    assert is_same_path(project.build_asset_dir, build_asset_dir)
    with pytest.warns(DeprecationWarning):
        assert is_same_path(project.build_dir, build_asset_dir)

    compiled_contracts_asset_path = get_compiled_contracts_asset_path(
        build_asset_dir)
    assert is_same_path(project.compiled_contracts_asset_path,
                        compiled_contracts_asset_path)
    with pytest.warns(DeprecationWarning):
        assert is_same_path(project.compiled_contracts_file_path,
                            compiled_contracts_asset_path)

    base_blockchain_storage_dir = get_base_blockchain_storage_dir(project_dir)
    assert is_same_path(project.base_blockchain_storage_dir,
                        base_blockchain_storage_dir)
    with pytest.warns(DeprecationWarning):
        assert is_same_path(project.blockchains_dir,
                            base_blockchain_storage_dir)

    data_dir = get_data_dir(project_dir, 'some-test-chain-name')
    with pytest.warns(DeprecationWarning):
        assert is_same_path(
            project.get_blockchain_data_dir('some-test-chain-name'), data_dir)

    chaindata_dir = get_chaindata_dir(data_dir)
    with pytest.warns(DeprecationWarning):
        assert is_same_path(
            project.get_blockchain_chaindata_dir('some-test-chain-name'),
            chaindata_dir)

    geth_ipc_path = get_geth_ipc_path(data_dir)
    with pytest.warns(DeprecationWarning):
        assert is_same_path(
            project.get_blockchain_ipc_path('some-test-chain-name'),
            geth_ipc_path)
Esempio n. 37
0
def test_project_local_chain_rpc(project_dir, write_project_file):
    write_project_file('populus.ini', '\n'.join((
        '[chain:custom-chain]',
        'provider=web3.providers.rpc.RPCProvider',
    )))
    project = Project()

    chain = project.get_chain('custom-chain')

    with chain as running_local_chain:
        web3 = running_local_chain.web3
        assert web3.version.node.startswith('Geth')

        running_local_chain.wait.for_block(block_number=1, timeout=180)

        block_1 = web3.eth.getBlock(1)
        assert block_1['hash'] != MAINNET_BLOCK_1_HASH
        assert block_1['hash'] != TESTNET_BLOCK_1_HASH
        assert block_1['miner'] == web3.eth.coinbase
Esempio n. 38
0
def test_initializing_no_choices(project_dir, write_project_file):
    write_project_file('populus.ini', "[chain:local_a]")
    project = Project()

    with project.get_chain('local_a') as chain:
        project.config.set('chain:local_a', 'deploy_from', chain.web3.eth.coinbase)
        project.write_config()

    runner = CliRunner()

    result = runner.invoke(
        main,
        ['chain', 'init', 'local_a'],
    )

    assert result.exit_code == 0, result.output + str(result.exception)

    updated_project = Project()
    assert 'registrar' in updated_project.config.chains['local_a']
    assert 'deploy_from' in updated_project.config.chains['local_a']
Esempio n. 39
0
def test_select_account_helper_with_indexes(project_dir, write_project_file,
                                            stdin, expected_config):
    write_project_file('populus.ini', '[chain:local]')
    project = Project()

    @click.command()
    def wrapper():
        configure_chain(project, 'local')

    runner = CliRunner()
    result = runner.invoke(wrapper, [], input=stdin)

    assert result.exit_code == 0

    project.reload_config()

    chain_config = project.config.chains['local']
    for key, value in expected_config.items():
        assert key in chain_config
        assert chain_config[key] == value
def test_get_unlocked_deploy_from_address_with_no_config(local_chain):
    project = Project()
    chain = local_chain

    with chain:
        @click.command()
        def wrapper():
            account = get_unlocked_deploy_from_address(chain)
            print("~~{0}~~".format(account))

        runner = CliRunner()
        result = runner.invoke(wrapper, [], input="1\ny\na-test-password\n")

        deploy_from = chain.web3.eth.accounts[1]
        assert result.exit_code == 0
        expected = "~~{0}~~".format(deploy_from)
        assert expected in result.output

        project.reload_config()
        assert project.config.chains['local']['deploy_from'] == deploy_from
def test_chain_web3_is_preconfigured_with_default_from(project_dir,
                                                       write_project_file):
    write_project_file('populus.ini', '[chain:local]')

    project = Project()

    chain = project.get_chain('local')

    default_account = force_text(
        create_new_account(chain.geth.data_dir, b'a-test-password'))

    project.config.set('chain:local', 'default_account', default_account)
    project.write_config()

    with chain:
        web3 = chain.web3

        assert len(web3.eth.accounts) == 2
        assert web3.eth.defaultAccount == default_account
        assert web3.eth.coinbase != default_account
def test_chain_web3_is_preconfigured_with_default_from(project_dir,
                                                       write_project_file):
    write_project_file('populus.ini', '[chain:local]')

    project = Project()

    chain = project.get_chain('local')

    default_account = force_text(
        create_new_account(chain.geth.data_dir, b'a-test-password')
    )

    project.config.set('chain:local', 'default_account', default_account)
    project.write_config()

    with chain:
        web3 = chain.web3

        assert len(web3.eth.accounts) == 2
        assert web3.eth.defaultAccount == default_account
        assert web3.eth.coinbase != default_account
def test_request_account_unlock_with_bad_password(project_dir):
    project = Project()
    chain = project.get_chain('temp')

    # create 3 new accounts
    account = force_text(
        create_new_account(chain.geth.data_dir, b'a-test-password')
    )

    @click.command()
    def wrapper():
        request_account_unlock(chain, account, None)

    with chain:
        assert is_account_locked(chain.web3, account)

        runner = CliRunner()
        result = runner.invoke(wrapper, [], input="bad-password\n")

        assert result.exit_code != 0
        assert is_account_locked(chain.web3, account)
def test_deploying_contract_with_error_during_deploy(project_dir, THROWER):
    project = Project()
    chain = project.get_chain("testrpc")

    exports = []

    with chain:
        Thrower = chain.web3.eth.contract(**THROWER)

        @click.command()
        def wrapper():
            math_contract = deploy_contract_and_verify(
                chain, contract_name="Thrower", base_contract_factory=Thrower, deploy_arguments=[True]
            )
            exports.append(math_contract)
            print("~~{0}~~".format(math_contract.address))

        runner = CliRunner()
        result = runner.invoke(wrapper, [])

    assert result.exit_code != 0
Esempio n. 45
0
def test_reset_chain(project_dir, write_project_file):
    project = Project()

    data_dir = project.get_blockchain_data_dir('test-chain')
    ensure_path_exists(data_dir)

    chaindata_dir = project.get_blockchain_chaindata_dir('test-chain')
    dapp_dir = project.get_blockchain_dapp_dir('test-chain')
    nodekey_path = project.get_blockchain_nodekey_path('test-chain')
    geth_ipc_path = project.get_blockchain_ipc_path('test-chain')

    ensure_path_exists(chaindata_dir)
    ensure_path_exists(dapp_dir)
    write_project_file(nodekey_path)
    write_project_file(geth_ipc_path)

    # sanity check
    assert os.path.exists(data_dir)
    assert os.path.exists(chaindata_dir)
    assert os.path.exists(dapp_dir)
    assert os.path.exists(nodekey_path)
    assert os.path.exists(geth_ipc_path)

    reset_chain(data_dir)

    assert os.path.exists(data_dir)
    assert not os.path.exists(chaindata_dir)
    assert not os.path.exists(dapp_dir)
    assert not os.path.exists(nodekey_path)
    assert not os.path.exists(geth_ipc_path)
def test_select_account_helper_with_indexes(project_dir,
                                            write_project_file,
                                            stdin,
                                            expected_config):
    write_project_file('populus.ini', '[chain:local]')
    project = Project()

    @click.command()
    def wrapper():
        configure_chain(project, 'local')

    runner = CliRunner()
    result = runner.invoke(wrapper, [], input=stdin)

    assert result.exit_code == 0

    project.reload_config()

    chain_config = project.config.chains['local']
    for key, value in expected_config.items():
        assert key in chain_config
        assert chain_config[key] == value
Esempio n. 47
0
def test_initializing_local_chain(project_dir, write_project_file):
    write_project_file('populus.ini', "[chain:local_a]")
    project = Project()

    runner = CliRunner()

    chain = project.get_chain('local_a')

    deploy_from = force_text(
        create_new_account(chain.geth.data_dir, b'a-test-password')
    )

    with chain:
        chain.wait.for_unlock(chain.web3.eth.coinbase, timeout=30)
        funding_txn_hash = chain.web3.eth.sendTransaction({
            'from': chain.web3.eth.coinbase,
            'to': deploy_from,
            'value': int(chain.web3.toWei(10, 'ether')),
        })
        chain.wait.for_receipt(funding_txn_hash, timeout=60)

    result = runner.invoke(
        main,
        ['chain', 'init'],
        input=((
            "local_a\n"          # choose chain.
            "{0}\n"              # pick deploy account.
            "Y\n"                # set account as default
            "a-test-password\n"  # unlock account
            "".format(deploy_from)
        ))
    )

    assert result.exit_code == 0, result.output + str(result.exception)

    updated_project = Project()
    assert 'registrar' in updated_project.config.chains['local_a']
    assert 'deploy_from' in updated_project.config.chains['local_a']
def test_project_directory_properties(project_dir):
    project = Project()

    contracts_dir = get_contracts_dir(project_dir)
    assert is_same_path(project.contracts_dir, contracts_dir)

    build_dir = get_build_dir(project_dir)
    assert is_same_path(project.build_dir, build_dir)

    compiled_contracts_file_path = get_compiled_contracts_file_path(project_dir)
    assert is_same_path(project.compiled_contracts_file_path, compiled_contracts_file_path)

    blockchains_dir = get_blockchains_dir(project_dir)
    assert is_same_path(project.blockchains_dir, blockchains_dir)

    data_dir = get_data_dir(project_dir, "some-test-chain-name")
    assert is_same_path(project.get_blockchain_data_dir("some-test-chain-name"), data_dir)

    chaindata_dir = get_chaindata_dir(data_dir)
    assert is_same_path(project.get_blockchain_chaindata_dir("some-test-chain-name"), chaindata_dir)

    geth_ipc_path = get_geth_ipc_path(data_dir)
    assert is_same_path(project.get_blockchain_ipc_path("some-test-chain-name"), geth_ipc_path)
Esempio n. 49
0
def test_external_rpc_chain(project_dir, write_project_file):
    project = Project()

    with project.get_chain('testrpc') as chain:
        web3 = chain.web3
        registrar = chain.registrar

        ini_contents = '\n'.join((
            "[chain:external]",
            "is_external=True",
            "provider=web3.providers.rpc.RPCProvider",
            "port={port}".format(port=chain.port),
            "registrar={registrar}".format(registrar=registrar.address),
        ))
        write_project_file('populus.ini', ini_contents)

        project.reload_config()

        with project.get_chain('external') as external_chain:
            ext_web3 = external_chain.web3
            ext_registrar = external_chain.registrar

            assert ext_web3.eth.coinbase == web3.eth.coinbase
            assert registrar.address == ext_registrar.address
Esempio n. 50
0
def test_external_ipc_chain(project_dir, write_project_file):
    project = Project()

    with project.get_chain('temp') as chain:
        web3 = chain.web3
        chain.wait.for_unlock(timeout=30)
        registrar = chain.registrar

        ini_contents = '\n'.join((
            "[chain:external]",
            "is_external=True",
            "ipc_path={ipc_path}".format(ipc_path=chain.geth.ipc_path),
            "registrar={registrar}".format(registrar=registrar.address),
        ))
        write_project_file('populus.ini', ini_contents)

        project.reload_config()

        with project.get_chain('external') as external_chain:
            ext_web3 = external_chain.web3
            ext_registrar = external_chain.registrar

            assert ext_web3.eth.coinbase == web3.eth.coinbase
            assert registrar.address == ext_registrar.address
def test_select_account_helper_with_accounts(project_dir, account_index):
    project = Project()
    chain = project.get_chain('temp')

    # create 3 new accounts
    create_new_account(chain.geth.data_dir, b'a-test-password')
    create_new_account(chain.geth.data_dir, b'a-test-password')
    create_new_account(chain.geth.data_dir, b'a-test-password')


    @click.command()
    def wrapper():
        account = select_account(chain)
        print("~~{0}~~".format(account))

    with chain:
        account = chain.web3.eth.accounts[account_index]

        runner = CliRunner()
        result = runner.invoke(wrapper, [], input="{0}\n".format(account))

    assert result.exit_code == 0
    expected = "~~{0}~~".format(account)
    assert expected in result.output
Esempio n. 52
0
def test_migrate_cmd(project_dir, write_project_file, MATH):
    class MigrationA(migrations.Migration):
        migration_id = '0001_deploy_math'
        dependencies = []

        operations = [
            migrations.DeployContract('Math'),
        ]

        compiled_contracts = {
            'Math': MATH,
        }

    class MigrationB(migrations.Migration):
        migration_id = '0002_increment'
        dependencies = ['0001_deploy_math']

        operations = [
            migrations.TransactContract(
                contract_name='Math',
                method_name='increment',
                arguments=[3],
                contract_address=migrations.Address.defer(key='contract/Math'),
                timeout=30,
            ),
        ]

        compiled_contracts = {
            'Math': MATH,
        }


    write_project_file('contracts/Math.sol', MATH['source'])
    write_project_file('migrations/__init__.py')

    migration_0001_path = os.path.join(
        project_dir, 'migrations', '0001_deploy_math.py',
    )
    with open(migration_0001_path, 'w') as migration_0001:
        write_migration(migration_0001, MigrationA)

    migration_0002_path = os.path.join(
        project_dir, 'migrations', '0002_increment.py',
    )
    with open(migration_0002_path, 'w') as migration_0002:
        write_migration(migration_0002, MigrationB)

    write_project_file('populus.ini', '[chain:local_a]')

    project = Project()

    # sanity
    assert len(project.migrations) == 2

    with project.get_chain('local_a') as chain:
        chain.wait.for_unlock(chain.web3.eth.coinbase, timeout=30)
        project.config.set('chain:local_a', 'deploy_from', chain.web3.eth.coinbase)
        RegistrarFactory = get_registrar(web3=chain.web3)
        deploy_transaction_hash = RegistrarFactory.deploy()
        registrar_address = chain.wait.for_contract_address(deploy_transaction_hash, timeout=60)
        project.config.set('chain:local_a', 'registrar', registrar_address)
        project.write_config()

    runner = CliRunner()
    result = runner.invoke(main, ['migrate', 'local_a'])

    assert result.exit_code == 0, result.output + str(result.exception)

    with project.get_chain('local_a') as chain:
        registrar = chain.registrar
        math_address = registrar.call().getAddress('contract/Math')
        Math = chain.contract_factories.Math(address=math_address)
        assert Math.call().counter() == 3
Esempio n. 53
0
def web3() -> Web3:
    """A py.test fixture to get a Web3 interface to a temporary geth instance.

    This is session scoped fixture.
    Geth is launched only once during the beginning of the test run.

    Geth will have huge instant balance on its coinbase account.
    Geth will also mine our transactions on artificially
    low difficulty level.

    :yield: :py:class:`web3.Web3` instance
    """

    project = Project()

    # Project is configured using populus.config.Config class
    # which is a subclass of Python config parser.
    # Instead of reading .ini file, here we dynamically
    # construct the configuration.
    project.config = Config()

    # Settings come for [populus] section of the config.
    project.config.add_section("populus")

    # Configure where Populus can find our contracts.json
    build_dir = os.path.join(os.getcwd(), "websauna", "wallet", "ethereum")
    project.config.set("populus", "build_dir", build_dir)

    chain_kwargs = {

        # Force RPC provider instead of default IPC one
        "provider": RPCProvider,
        "wait_for_dag_timeout": 20*60,
        "overrides": {
            "jitvm": "false",
        }
    }

    # This returns
    with project.get_chain("temp", **chain_kwargs) as geth_proc:

        web3 = geth_proc.web3

        # Use compatible web3.py version
        assert web3._requestManager.provider.network_timeout

        web3._requestManager.provider.network_timeout = 10
        web3._requestManager.provider.connection_timeout = 10

        # Allow access to sendTransaction() to use coinbase balance
        # to deploy contracts. Password is from py-geth
        # default_blockchain_password file. Assume we don't
        # run tests for more than 9999 seconds
        coinbase = web3.eth.coinbase
        success = web3.personal.unlockAccount(
            coinbase,
            passphrase="this-is-not-a-secure-password",
            duration=9999)

        assert success, "Could not unlock test geth coinbase account"

        yield web3
def test_show_chain_sync_progress():
    project = Project()

    main_chain = project.get_chain('temp',
                                   no_discover=False,
                                   max_peers=None,
                                   port=str(get_open_port()),
                                   rpc_enabled=False,
                                   ws_enabled=False)
    with main_chain:
        sync_chain = project.get_chain('temp',
                                       no_discover=False,
                                       max_peers=None,
                                       mine=False,
                                       miner_threads=None,
                                       port=str(get_open_port()),
                                       rpc_enabled=False,
                                       ws_enabled=False)

        main_chain_data_dir = main_chain.geth.data_dir
        sync_chain_data_dir = sync_chain.geth.data_dir

        main_genesis_file = os.path.join(main_chain_data_dir, 'genesis.json')
        sync_genesis_file = os.path.join(sync_chain_data_dir, 'genesis.json')

        main_chaindata_dir = os.path.join(main_chain_data_dir, 'chaindata')
        sync_chaindata_dir = os.path.join(sync_chain_data_dir, 'chaindata')

        os.remove(sync_genesis_file)
        shutil.rmtree(sync_chaindata_dir)

        shutil.copyfile(main_genesis_file, sync_genesis_file)
        shutil.copytree(main_chaindata_dir, sync_chaindata_dir)

        block_numbers = []

        runner = CliRunner()

        @click.command()
        def wrapper():
            block_numbers.append(sync_chain.web3.eth.blockNumber)
            show_chain_sync_progress(sync_chain)
            block_numbers.append(sync_chain.web3.eth.blockNumber)

        with sync_chain:
            main_node_info = main_chain.web3.admin.nodeInfo
            main_enode = "enode://{node_id}@127.0.0.1:{node_port}".format(
                node_id=main_node_info['id'],
                node_port=main_node_info['ports']['listener'],
            )
            sync_node_info = sync_chain.web3.admin.nodeInfo
            sync_enode = "enode://{node_id}@127.0.0.1:{node_port}".format(
                node_id=sync_node_info['id'],
                node_port=sync_node_info['ports']['listener'],
            )

            main_chain.wait.for_block(BLOCK_DELTA, timeout=BLOCK_DELTA * 4)

            main_chain_start_block = main_chain.web3.eth.blockNumber
            sync_chain_start_block = sync_chain.web3.eth.blockNumber

            assert main_chain_start_block - sync_chain_start_block >= BLOCK_DELTA // 2

            assert sync_chain.web3.net.peerCount == 0

            sync_chain.web3.admin.addPeer(main_enode)
            main_chain.web3.admin.addPeer(sync_enode)

            sync_chain.wait.for_peers(timeout=60)

            result = runner.invoke(wrapper, [])

            assert result.exit_code == 0
            assert len(block_numbers) == 2

            start_block = block_numbers[0]
            end_block = block_numbers[1]

            assert start_block <= 1
            assert end_block >= 20