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 ) 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, V8) 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=V8)) assert upgraded_project.user_config.get(upgraded_key) == default_user_config.get(upgraded_key)
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)
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, ) click.echo(start_msg) click.echo('-' * 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()))) click.echo(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 if chain_config.get('is_external', False): is_chain_ready_msg = ( "Populus needs to connect to the chain. Press [Enter] when the " "chain is ready for populus") click.confirm(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)) 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 click.echo("Writing project configuration ...") project.write_config() click.echo("Success!")