def chain_run(name, mine, verbosity, active): """ Run a geth node. """ project_dir = os.getcwd() data_dir = get_geth_data_dir(project_dir, name) logfile_path = get_geth_logfile_path(data_dir) ensure_account_exists(data_dir) kwargs = {"logfile": logfile_path, "verbosity": "%d" % verbosity} command, proc = run_geth_node(data_dir, mine=mine, **kwargs) click.echo("Running: '{0}'".format(' '.join(command))) if active: set_active_data_dir(project_dir, name) try: while True: out_line = proc.get_stdout_nowait() if out_line: click.echo(out_line, nl=False) err_line = proc.get_stderr_nowait() if err_line: click.echo(err_line, nl=False) if err_line is None and out_line is None: time.sleep(0.2) except KeyboardInterrupt: try: proc.send_signal(signal.SIGINT) # Give the subprocess a SIGINT and give it a few seconds to # cleanup. utils.wait_for_popen(proc) while not proc.stdout_queue.empty() or not proc.stderr_queue.empty( ): out_line = proc.get_stdout_nowait() if out_line: click.echo(out_line, nl=False) err_line = proc.get_stderr_nowait() if err_line: click.echo(err_line, nl=False) except: # Try a harder termination. proc.terminate() utils.wait_for_popen(proc, 2) if proc.poll() is None: # Force it to kill if it hasn't exited already. proc.kill() if proc.returncode: raise click.ClickException("Error shutting down geth process.")
def chain_run(name, mine, verbosity, active): """ Run a geth node. """ project_dir = os.getcwd() data_dir = get_geth_data_dir(project_dir, name) logfile_path = get_geth_logfile_path(data_dir) ensure_account_exists(data_dir) kwargs = {"logfile": logfile_path, "verbosity": "%d" % verbosity} command, proc = run_geth_node(data_dir, mine=mine, **kwargs) click.echo("Running: '{0}'".format(" ".join(command))) if active: set_active_data_dir(project_dir, name) try: while True: out_line = proc.get_stdout_nowait() if out_line: click.echo(out_line, nl=False) err_line = proc.get_stderr_nowait() if err_line: click.echo(err_line, nl=False) if err_line is None and out_line is None: time.sleep(0.2) except KeyboardInterrupt: try: proc.send_signal(signal.SIGINT) # Give the subprocess a SIGINT and give it a few seconds to # cleanup. utils.wait_for_popen(proc) while not proc.stdout_queue.empty() or not proc.stderr_queue.empty(): out_line = proc.get_stdout_nowait() if out_line: click.echo(out_line, nl=False) err_line = proc.get_stderr_nowait() if err_line: click.echo(err_line, nl=False) except: # Try a harder termination. proc.terminate() utils.wait_for_popen(proc, 2) if proc.poll() is None: # Force it to kill if it hasn't exited already. proc.kill() if proc.returncode: raise click.ClickException("Error shutting down geth process.")
def geth_node(request, populus_config): from populus.geth import ( run_geth_node, get_geth_data_dir, get_geth_logfile_path, ensure_account_exists, create_geth_account, reset_chain, wait_for_geth_to_start, ) from populus.utils import ( ensure_path_exists, kill_proc, ) project_dir = populus_config.get_value(request, 'geth_project_dir') chain_name = populus_config.get_value(request, 'geth_chain_name') data_dir = get_geth_data_dir(project_dir, chain_name) logfile_name_fmt = "geth-{0}-{{0}}.log".format(request.module.__name__) logfile_path = get_geth_logfile_path(data_dir, logfile_name_fmt) ensure_path_exists(data_dir) ensure_account_exists(data_dir) num_accounts = populus_config.get_value(request, 'geth_num_accounts') if num_accounts > 1: for _ in range(num_accounts - 1): create_geth_account(data_dir) should_reset_chain = populus_config.get_value(request, 'geth_reset_chain') if should_reset_chain: reset_chain(data_dir) rpc_port = populus_config.get_value(request, 'geth_rpc_port') rpc_host = populus_config.get_value(request, 'geth_rpc_host') geth_max_wait = int(populus_config.get_value(request, 'geth_max_wait')) command, proc = run_geth_node(data_dir, rpc_addr=rpc_host, rpc_port=rpc_port, logfile=logfile_path, verbosity="6") wait_for_geth_to_start(proc, max_wait=geth_max_wait) yield proc kill_proc(proc)
def deploy(dry_run, dry_run_chain_name, production, confirm, contracts_to_deploy): """ Deploys the specified contracts via the RPC client. """ if dry_run is None: # If we are doing a production deploy and dry_run was not specified, # then default to True dry_run = production client = Client("127.0.0.1", "8545") project_dir = os.getcwd() deploy_gas = None contracts = package_contracts(utils.load_contracts(project_dir)) if dry_run: dry_run_data_dir = get_geth_data_dir(project_dir, dry_run_chain_name) logfile_path = get_geth_logfile_path( dry_run_data_dir, logfile_name_fmt="deploy-dry-run-{0}.log", ) ensure_account_exists(dry_run_data_dir) _, dry_run_proc = run_geth_node(dry_run_data_dir, logfile=logfile_path) wait_for_geth_to_start(dry_run_proc) message = ( "======= Executing Dry Run Deploy ========\n" "Chain Name : {chain_name}\n" "Data Directory : {data_dir}\n" "Geth Logfile : {logfile_path}\n\n" "... (deploying)\n" ).format( chain_name=dry_run_chain_name, data_dir=dry_run_data_dir, logfile_path=logfile_path, ) click.echo(message) # Dry run deploy uses max_gas dry_run_contracts = deploy_contracts( deploy_client=client, contracts=contracts, deploy_at_block=1, max_wait_for_deploy=60, from_address=None, max_wait=60, contracts_to_deploy=contracts_to_deploy, dependencies=None, constructor_args=None, deploy_gas=None, ) validate_deployed_contracts(client, dry_run_contracts) echo_post_deploy_message(client, dry_run_contracts) dry_run_proc.send_signal(signal.SIGINT) # Give the subprocess a SIGINT and give it a few seconds to # cleanup. utils.wait_for_popen(dry_run_proc) def get_deploy_gas(contract_name, contract_class): max_gas = int(client.get_max_gas() * 0.98) receipt = dry_run_contracts._deploy_receipts.get(contract_name) if receipt is None: return max_gas gas_used = int(receipt['gasUsed'], 16) return min(max_gas, int(gas_used * 1.1)) deploy_gas = get_deploy_gas contracts = package_contracts(utils.load_contracts(project_dir)) if not production: data_dir = get_geth_data_dir(project_dir, "default") logfile_path = get_geth_logfile_path( data_dir, logfile_name_fmt="deploy-dry-run-{0}.log", ) ensure_account_exists(data_dir) _, deploy_proc = run_geth_node(data_dir, logfile=logfile_path) wait_for_geth_to_start(deploy_proc) elif confirm: message = ( "You are about to deploy contracts to a production environment. " "You must have an RPC server that is unlocked running for this to " "work.\n\n" "Would you like to proceed?" ) if not click.confirm(message): raise click.Abort() if not dry_run: message = ( "You are about to do a production deploy with no dry run. Without " "a dry run, it isn't feasible to know gas costs and thus deployment " "may fail due to long transaction times.\n\n" "Are you sure you would like to proceed?" ) if confirm and not click.confirm(message): raise click.Abort() message = ( "========== Executing Deploy ===========\n" "... (deploying)\n" "Chain Name : {chain_name}\n" "Data Directory : {data_dir}\n" "Geth Logfile : {logfile_path}\n\n" "... (deploying)\n" ).format( chain_name="production" if production else "default", data_dir="N/A" if production else data_dir, logfile_path="N/A" if production else logfile_path, ) click.echo(message) deployed_contracts = deploy_contracts( deploy_client=client, contracts=contracts, deploy_at_block=1, max_wait_for_deploy=120, from_address=None, max_wait=120, contracts_to_deploy=contracts_to_deploy, dependencies=None, constructor_args=None, deploy_gas=deploy_gas, ) validate_deployed_contracts(client, deployed_contracts) echo_post_deploy_message(client, deployed_contracts) if not production: deploy_proc.send_signal(signal.SIGINT) # Give the subprocess a SIGINT and give it a few seconds to # cleanup. utils.wait_for_popen(deploy_proc)
def deploy(dry_run, dry_run_chain_name, production, confirm, record, contracts_to_deploy): """ Deploys the specified contracts via the RPC client. """ if dry_run is None: # If we are doing a production deploy and dry_run was not specified, # then default to True dry_run = production client = Client("127.0.0.1", "8545") project_dir = os.getcwd() deploy_gas = None contracts = package_contracts(utils.load_contracts(project_dir)) if dry_run: dry_run_data_dir = get_geth_data_dir(project_dir, dry_run_chain_name) logfile_path = get_geth_logfile_path( dry_run_data_dir, logfile_name_fmt="deploy-dry-run-{0}.log", ) ensure_account_exists(dry_run_data_dir) _, dry_run_proc = run_geth_node(dry_run_data_dir, logfile=logfile_path) wait_for_geth_to_start(dry_run_proc) message = ("======= Executing Dry Run Deploy ========\n" "Chain Name : {chain_name}\n" "Data Directory : {data_dir}\n" "Geth Logfile : {logfile_path}\n\n" "... (deploying)\n").format( chain_name=dry_run_chain_name, data_dir=dry_run_data_dir, logfile_path=logfile_path, ) click.echo(message) # Dry run deploy uses max_gas dry_run_contracts = deploy_contracts( deploy_client=client, contracts=contracts, deploy_at_block=1, max_wait_for_deploy=60, from_address=None, max_wait=60, contracts_to_deploy=contracts_to_deploy, dependencies=None, constructor_args=None, deploy_gas=None, ) validate_deployed_contracts(client, dry_run_contracts) echo_post_deploy_message(client, dry_run_contracts) dry_run_proc.send_signal(signal.SIGINT) # Give the subprocess a SIGINT and give it a few seconds to # cleanup. utils.wait_for_popen(dry_run_proc) def get_deploy_gas(contract_name, contract_class): max_gas = int(client.get_max_gas() * 0.98) receipt = dry_run_contracts._deploy_receipts.get(contract_name) if receipt is None: return max_gas gas_used = int(receipt['gasUsed'], 16) return min(max_gas, int(gas_used * 1.1)) deploy_gas = get_deploy_gas contracts = package_contracts(utils.load_contracts(project_dir)) if not production: data_dir = get_geth_data_dir(project_dir, "default") logfile_path = get_geth_logfile_path( data_dir, logfile_name_fmt="deploy-dry-run-{0}.log", ) ensure_account_exists(data_dir) _, deploy_proc = run_geth_node(data_dir, logfile=logfile_path) wait_for_geth_to_start(deploy_proc) elif confirm: message = ( "You are about to deploy contracts to a production environment. " "You must have an RPC server that is unlocked running for this to " "work.\n\n" "Would you like to proceed?") if not click.confirm(message): raise click.Abort() if not dry_run: message = ( "You are about to do a production deploy with no dry run. Without " "a dry run, it isn't feasible to know gas costs and thus deployment " "may fail due to long transaction times.\n\n" "Are you sure you would like to proceed?") if confirm and not click.confirm(message): raise click.Abort() message = ("========== Executing Deploy ===========\n" "... (deploying)\n" "Chain Name : {chain_name}\n" "Data Directory : {data_dir}\n" "Geth Logfile : {logfile_path}\n\n" "... (deploying)\n").format( chain_name="production" if production else "default", data_dir="N/A" if production else data_dir, logfile_path="N/A" if production else logfile_path, ) click.echo(message) deployed_contracts = deploy_contracts( deploy_client=client, contracts=contracts, deploy_at_block=1, max_wait_for_deploy=120, from_address=None, max_wait=120, contracts_to_deploy=contracts_to_deploy, dependencies=None, constructor_args=None, deploy_gas=deploy_gas, ) validate_deployed_contracts(client, deployed_contracts) echo_post_deploy_message(client, deployed_contracts) if not production: if record: add_to_known_contracts(deployed_contracts, data_dir) deploy_proc.send_signal(signal.SIGINT) # Give the subprocess a SIGINT and give it a few seconds to # cleanup. utils.wait_for_popen(deploy_proc)