Exemplo n.º 1
0
    def clean_config(self):

        items = self.project_config.items(flatten=True)

        default_config = Config(load_default_config(version=self.project_config['version']))
        default_config.unref()
        default_project_keys = [x[0] for x in default_config.items(flatten=True)]

        for key, value in items:
            if self.user_config.get(key) == value and key not in default_project_keys:
                self.project_config.pop(key)
Exemplo n.º 2
0
    def clean_config(self):

        items = self.project_config.items(flatten=True)

        default_config = Config(
            load_default_config(version=self.project_config['version']))
        default_config.unref()
        default_project_keys = [
            x[0] for x in default_config.items(flatten=True)
        ]

        for key, value in items:
            if self.user_config.get(
                    key) == value and key not in default_project_keys:
                self.project_config.pop(key)
Exemplo n.º 3
0
def configure_chain(project, chain_name):
    """
    Interactive configuration of an existing or new chain.

    - is it external?
    - rpc or ipc?
    - rpc/ipc configuration
    - select default account (web3.eth.defaultAccount)
    """
    try:
        chain_config = project.get_chain_config(chain_name)
        is_existing_chain = True
    except KeyError:
        chain_config = Config({})
        is_existing_chain = False

    start_msg = "Configuring {status} chain: {chain_name}".format(
        status="existing" if is_existing_chain else "**new**",
        chain_name=chain_name,
    )
    logger = logging.getLogger('populus.cli.utils.configure_chain')
    logger.info(start_msg)
    logger.info('-' * len(start_msg))

    if is_existing_chain:
        current_configuration_msg = "\n".join(itertools.chain((
            "Current Configuration",
        ), (
            "  {key} = {value}".format(key=key, value=value)
            for key, value in chain_config.items()
        )))
        logger.info(current_configuration_msg)

    # Internal or External
    internal_or_external_msg = (
        "\n\nPopulus can run the blockchain client for you, including "
        "connecting to the public main and test networks.\n\n "
        "Should populus manage running this chain?"
    )
    is_internal = click.confirm(internal_or_external_msg, default=True)

    if not is_internal:
        chain_config['is_external'] = True

    # Web3 Provider
    web3_provider_msg = (
        "\n\nWeb3 Provider Choices:\n"
        "1) IPC socket (default)\n"
        "2) RPC via HTTP\n\n"
        "How should populus connect web3.py to this chain?"
    )
    provider = click.prompt(web3_provider_msg, default='ipc')

    if provider.lower() in {'ipc', '1'}:
        chain_config['web3.provider.class'] = 'web3.providers.ipc.IPCProvider'
    elif provider.lower() in {'rpc', '2'}:
        chain_config['web3.provider.class'] = 'web3.providers.rpc.HTTPProvider'
    else:
        unknown_provider_message = (
            "Invalid response.  Allowed responses are 1/2/ipc/rpc"
        )
        raise click.ClickException(unknown_provider_message)

    if chain_config['web3.provider.class'] == 'web3.providers.ipc.IPCProvider':
        custom_ipc_path_msg = (
            "\n\nWill this blockchain be running with a non-standard `geth.ipc`"
            "path?\n\n"
        )
        if click.confirm(custom_ipc_path_msg, default=False):
            ipc_path_msg = "Path to `geth.ipc` socket?"
            ipc_path = click.prompt(ipc_path_msg)
            chain_config['web3.providers.settings.ipc_path'] = ipc_path
        elif chain_name not in {"mainnet", "ropsten"}:
            chain_config['web3.providers.settings.ipc_path'] = get_geth_ipc_path(
                get_local_chain_datadir(project.project_dir, chain_name),
            )
    elif chain_config['web3.provider.class'] == 'web3.providers.rpc.HTTPProvider':
        custom_rpc_host = (
            "\n\nWill the RPC server be bound to `localhost`?"
        )
        if not click.confirm(custom_rpc_host, default=True):
            rpc_host_msg = "Hostname?"
            rpc_host = click.prompt(rpc_host_msg)
        else:
            rpc_host = 'localhost'

        custom_rpc_port = (
            "\n\nWill the RPC server be listening on port 8545?"
        )
        if not click.confirm(custom_rpc_port, default=True):
            rpc_port_msg = "Port?"
            rpc_port = click.prompt(rpc_port_msg)
        else:
            rpc_port = '8545'

        chain_config['web3.providers.settings.rpc_host'] = 'http://{0}:{1}'.format(
            rpc_host,
            rpc_port,
        )

    # Save config so that we can spin this chain up.
    project.config['chains'][chain_name] = chain_config
    project.write_config()
    project.load_config()

    if chain_config.is_external:
        is_chain_ready_msg = (
            "Populus needs to connect to the chain.  Press [Enter] when the "
            "chain is ready for populus"
        )
        click.prompt(is_chain_ready_msg)

    with project.get_chain(chain_name) as chain:
        web3 = chain.web3
        choose_default_account_msg = (
            "This chain will default to sending transactions from "
            "{0}.  Would you like to set a different default "
            "account?".format(web3.eth.defaultAccount or web3.eth.coinbase)
        )
        if click.confirm(choose_default_account_msg, default=True):
            default_account = select_account(chain)
            default_account_key = 'chains.{0}.web3.eth.default_account'.format(chain_name)
            project.config[default_account_key] = default_account

    logger.info("Writing project configuration ...")
    project.write_config()
    logger.info("Success!")