def project_test05(monkeypatch): project_dir = os.path.join(PROJECTS_DIR, "test-05") monkeypatch.chdir(project_dir) default_chain_dir = get_geth_data_dir(project_dir, "default") if os.path.exists(default_chain_dir): shutil.rmtree(default_chain_dir) return project_dir
def deploy_client(request, populus_config): client_type = populus_config.get_value(request, 'deploy_client_type') if client_type == 'ethtester': from populus.ethtester_client import EthTesterClient client = EthTesterClient() elif client_type == 'rpc': from eth_rpc_client import Client rpc_host = populus_config.get_value(request, 'deploy_client_rpc_host') rpc_port = populus_config.get_value(request, 'deploy_client_rpc_port') client = Client(rpc_host, rpc_port) elif client_type == 'ipc': from eth_ipc_client import Client ipc_path = populus_config.get_value(request, 'ipc_path') if ipc_path is None: from populus.geth import get_geth_data_dir geth_project_dir = populus_config.get_value(request, 'geth_project_dir') geth_chain_name = populus_config.get_value(request, 'geth_chain_name') geth_data_dir = get_geth_data_dir(geth_project_dir, geth_chain_name) ipc_path = os.path.join(geth_data_dir, 'geth.ipc') client = Client(ipc_path) else: raise ValueError( "Unsupported client type '{0}'. Supported values are 'tester', " "'rpc', and 'ipc'" ) return client
def project_test05(monkeypatch): project_dir = os.path.join(PROJECTS_DIR, 'test-05') monkeypatch.chdir(project_dir) default_chain_dir = get_geth_data_dir(project_dir, 'default') if os.path.exists(default_chain_dir): shutil.rmtree(default_chain_dir) return project_dir
def test_multiple_accounts(project_test02): chain_dir = get_geth_data_dir(project_test02, 'default') accounts = get_geth_accounts(data_dir=chain_dir) assert accounts == ( '0xae71658b3ab452f7e4f03bda6f777b860b2e2ff2', '0xe8e085862a8d951dd78ec5ea784b3e22ee1ca9c6', '0x0da70f43a568e88168436be52ed129f4a9bbdaf5', )
def geth_data_dir(request, populus_config): from populus.geth import get_geth_data_dir 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) return data_dir
def chain_reset(name, confirm): """ Reset a test chain """ data_dir = get_geth_data_dir(os.getcwd(), name) if confirm and not click.confirm("Are you sure you want to reset blockchain '{0}': {1}".format(name, data_dir)): # NOQA raise click.Abort() reset_chain(data_dir)
def chain_reset(name, confirm): """ Reset a test chain """ data_dir = get_geth_data_dir(os.getcwd(), name) if confirm and not click.confirm( "Are you sure you want to reset blockchain '{0}': {1}".format( name, data_dir)): # NOQA raise click.Abort() reset_chain(data_dir)
def test_create_geth_account(project_test05): data_dir = get_geth_data_dir(project_test05, "default") assert not get_geth_accounts(data_dir) account_0 = create_geth_account(data_dir) account_1 = create_geth_account(data_dir) accounts = get_geth_accounts(data_dir) assert (account_0, account_1) == accounts
def test_running_node_without_mining(project_test04, open_port): data_dir = get_geth_data_dir(project_test04, 'default') command, proc = run_geth_node(data_dir, rpc_port=open_port, mine=False) wait_for_popen(proc) rpc_client = Client('127.0.0.1', port=open_port) coinbase = rpc_client.get_coinbase() proc.send_signal(signal.SIGINT) wait_for_popen(proc) assert coinbase == get_geth_accounts(data_dir)[0]
def test_create_geth_account(project_test05): data_dir = get_geth_data_dir(project_test05, 'default') assert not get_geth_accounts(data_dir) account_0 = create_geth_account(data_dir) account_1 = create_geth_account(data_dir) accounts = get_geth_accounts(data_dir) assert (account_0, account_1) == accounts
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 ipc_client(request, populus_config): from eth_ipc_client import Client ipc_path = populus_config.get_value(request, 'ipc_path') if ipc_path is None: from populus.geth import get_geth_data_dir geth_project_dir = populus_config.get_value(request, 'geth_project_dir') geth_chain_name = populus_config.get_value(request, 'geth_chain_name') geth_data_dir = get_geth_data_dir(geth_project_dir, geth_chain_name) ipc_path = os.path.join(geth_data_dir, 'geth.ipc') client = Client(ipc_path) return client
def project_test04(monkeypatch): project_dir = os.path.join(PROJECTS_DIR, 'test-04') monkeypatch.chdir(project_dir) data_dir = get_geth_data_dir(project_dir, 'default') ensure_path_exists(data_dir) reset_chain(data_dir) ensure_account_exists(data_dir) return project_dir
def geth_accounts(populus_config, request): from populus.geth import ( get_geth_data_dir, get_geth_accounts, ) geth_project_dir = populus_config.get_value(request, 'geth_project_dir') geth_chain_name = populus_config.get_value(request, 'geth_chain_name') geth_data_dir = get_geth_data_dir(geth_project_dir, geth_chain_name) accounts = get_geth_accounts(geth_data_dir) return accounts
def test_running_node_and_mining(project_test04, open_port): data_dir = get_geth_data_dir(project_test04, 'default') command, proc = run_geth_node(data_dir, rpc_port=open_port, mine=True) wait_for_popen(proc) rpc_client = Client('127.0.0.1', port=open_port) block_num = rpc_client.get_block_number() start = time.time() rpc_client.wait_for_block(block_num + 1, 60) assert block_num < rpc_client.get_block_number() proc.send_signal(signal.SIGINT) wait_for_popen(proc)
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 test_running_node_and_mining(project_test04, open_port): data_dir = get_geth_data_dir(project_test04, 'default') command, proc = run_geth_node(data_dir, rpc_port=open_port, mine=True) time.sleep(2) rpc_client = Client('127.0.0.1', port=open_port) block_num = rpc_client.get_block_number() start = time.time() while time.time() < start + 10: time.sleep(0.2) if rpc_client.get_block_number() > block_num: break assert block_num < rpc_client.get_block_number() proc.send_signal(signal.SIGINT) wait_for_popen(proc)
def geth_coinbase(request, populus_config): from populus.geth import ( get_geth_data_dir, ensure_account_exists, ) from populus.utils import ( ensure_path_exists, ) 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) ensure_path_exists(data_dir) geth_coinbase = ensure_account_exists(data_dir) return geth_coinbase
def test_running_node_and_mining(project_test04, open_port): data_dir = get_geth_data_dir(project_test04, 'default') command, proc = run_geth_node(data_dir, rpc_port=open_port, mine=True) wait_for_popen(proc) rpc_client = Client('127.0.0.1', port=open_port) block_num = rpc_client.get_block_number() start = time.time() while rpc_client.get_block_number() <= block_num: time.sleep(0.2) if time.time() > start + 20: raise Exception('Could not mine block within 20 seconds') assert block_num < rpc_client.get_block_number() proc.send_signal(signal.SIGINT) wait_for_popen(proc)
def test_single_account(project_test01): chain_dir = get_geth_data_dir(project_test01, 'default') accounts = get_geth_accounts(data_dir=chain_dir) assert accounts == ('0xae71658b3ab452f7e4f03bda6f777b860b2e2ff2', )
def test_no_accounts(project_test03): chain_dir = get_geth_data_dir(project_test03, 'default') accounts = get_geth_accounts(data_dir=chain_dir) assert accounts == tuple()
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)
def test_single_account(project_test01): chain_dir = get_geth_data_dir(project_test01, 'default') accounts = get_geth_accounts(data_dir=chain_dir) assert accounts == ('0xae71658b3ab452f7e4f03bda6f777b860b2e2ff2',)
def test_geth_coinbase(geth_coinbase, project_test01_dir): data_dir = get_geth_data_dir(project_test01_dir, 'default') actual_coinbase = ensure_account_exists(data_dir) assert geth_coinbase == actual_coinbase