def test_short_data_directory_paths_use_local_geth_ipc_socket(): with tempdir() as data_dir: expected_path = os.path.abspath(os.path.join(data_dir, 'geth.ipc')) assert len(expected_path) < get_max_socket_path_length() chain_kwargs = construct_test_chain_kwargs(data_dir=data_dir) assert chain_kwargs['ipc_path'] == expected_path
def test_long_data_directory_paths_use_tempfile_geth_ipc_socket(): with tempdir() as temp_directory: data_dir = os.path.abspath(os.path.join( temp_directory, 'this-path-is-longer-than-the-maximum-unix-socket-path-lengh', 'and-thus-the-underlying-function-should-not-use-it-for-the', 'geth-ipc-path', )) data_dir_ipc_path = os.path.abspath(os.path.join(data_dir, 'geth.ipc')) assert len(data_dir_ipc_path) > get_max_socket_path_length() chain_kwargs = construct_test_chain_kwargs(data_dir=data_dir) assert chain_kwargs['ipc_path'] != data_dir_ipc_path
def new_local_chain(project_dir, chain_name): chains_path = os.path.join(project_dir, 'chains') ensure_path_exists(chains_path) chain_dir = os.path.join(chains_path, chain_name) data_dir = os.path.join(chain_dir, 'chain_data') overrides = { 'data_dir': data_dir, 'ws_port': '8546', 'rpc_port': '8545', 'port': '30303' } geth_kwargs = construct_test_chain_kwargs(**overrides) password = geth_kwargs.pop('password') data_dir = geth_kwargs.pop('data_dir') account = create_new_account(data_dir, password, **geth_kwargs) account = account.decode('ascii') genesis = GENESIS_BLOCK % ( account, account, ) genesis_path = os.path.join(chain_dir, 'genesis.json') with open(genesis_path, 'w+') as f: f.write(genesis) password_path = os.path.join(chain_dir, 'password') shutil.copyfile(password, password_path) ipc_path = geth_kwargs['ipc_path'] init = INIT_COMMAND.format(data_dir=data_dir, ipc_path=ipc_path, genesis_path=genesis_path) init_geth_path = os.path.join(chain_dir, 'init_chain.sh') with open(init_geth_path, 'w+') as f: f.write(SHEBANG) f.write('\n') f.write(init) chmod_plus_x(init_geth_path) run = RUN_COMMAND.format(data_dir=data_dir, ipc_path=ipc_path, password_path=password_path, account=account) run_geth_path = os.path.join(chain_dir, 'run_chain.sh') with open(run_geth_path, 'w+') as f: f.write(SHEBANG) f.write('\n') f.write(run) chmod_plus_x(run_geth_path)
def test_long_data_directory_paths_use_tempfile_geth_ipc_socket(): with tempdir() as temp_directory: data_dir = os.path.abspath( os.path.join( temp_directory, 'this-path-is-longer-than-the-maximum-unix-socket-path-lengh', 'and-thus-the-underlying-function-should-not-use-it-for-the', 'geth-ipc-path', )) data_dir_ipc_path = os.path.abspath(os.path.join(data_dir, 'geth.ipc')) assert len(data_dir_ipc_path) > get_max_socket_path_length() chain_kwargs = construct_test_chain_kwargs(data_dir=data_dir) assert chain_kwargs['ipc_path'] != data_dir_ipc_path
def __init__(self, chain_name, base_dir=None, overrides=None, genesis_data=None): if overrides is None: overrides = {} if genesis_data is None: genesis_data = {} if 'data_dir' in overrides: raise ValueError( "You cannot specify `data_dir` for a DevGethProcess") if base_dir is None: base_dir = get_default_base_dir() self.data_dir = get_chain_data_dir(base_dir, chain_name) geth_kwargs = construct_test_chain_kwargs(data_dir=self.data_dir, **overrides) # ensure that an account is present coinbase = ensure_account_exists(**geth_kwargs) # ensure that the chain is initialized genesis_file_path = get_genesis_file_path(self.data_dir) needs_init = all(( not os.path.exists(genesis_file_path), not is_live_chain(self.data_dir), not is_ropsten_chain(self.data_dir), )) if needs_init: genesis_data.setdefault( 'alloc', dict([ (coinbase, { "balance": "1000000000000000000000000000000" }), # 1 billion ether. ])) initialize_chain(genesis_data, **geth_kwargs) super(DevGethProcess, self).__init__(geth_kwargs)
def __init__(self, chain_name, base_dir=None, overrides=None, genesis_data=None): if overrides is None: overrides = {} if genesis_data is None: genesis_data = {} if 'data_dir' in overrides: raise ValueError("You cannot specify `data_dir` for a DevGethProcess") if base_dir is None: base_dir = get_default_base_dir() self.data_dir = get_chain_data_dir(base_dir, chain_name) geth_kwargs = construct_test_chain_kwargs( data_dir=self.data_dir, **overrides ) # ensure that an account is present coinbase = ensure_account_exists(**geth_kwargs) # ensure that the chain is initialized genesis_file_path = get_genesis_file_path(self.data_dir) needs_init = all(( not os.path.exists(genesis_file_path), not is_live_chain(self.data_dir), not is_ropsten_chain(self.data_dir), )) if needs_init: genesis_data.setdefault( 'alloc', dict([ (coinbase, {"balance": "1000000000000000000000000000000"}), # 1 billion ether. ]) ) initialize_chain(genesis_data, **geth_kwargs) super(DevGethProcess, self).__init__(geth_kwargs)
def __init__( self, base_directory: Path, hostname: str, port: int, mnemonic: str, number_of_accounts: int, chain_id: int = 1337, initial_balance: Union[str, int] = to_wei(10000, "ether"), ): self.data_dir = base_directory / "dev" self._hostname = hostname self._port = port geth_kwargs = construct_test_chain_kwargs( data_dir=self.data_dir, rpc_addr=hostname, rpc_port=port, network_id=chain_id, ) # Ensure a clean data-dir. self._clean() sealer = ensure_account_exists(**geth_kwargs).decode().replace("0x", "") accounts = generate_dev_accounts(mnemonic, number_of_accounts=number_of_accounts) genesis_data: Dict = { "overwrite": True, "coinbase": "0x0000000000000000000000000000000000000000", "difficulty": "0x0", "extraData": f"0x{'0' * 64}{sealer}{'0' * 130}", "config": { "chainId": chain_id, "gasLimit": 0, "homesteadBlock": 0, "difficulty": "0x0", "eip150Block": 0, "eip155Block": 0, "eip158Block": 0, "byzantiumBlock": 0, "constantinopleBlock": 0, "petersburgBlock": 0, "istanbulBlock": 0, "berlinBlock": 0, "londonBlock": 0, "clique": {"period": 0, "epoch": 30000}, }, "alloc": {a.address: {"balance": str(initial_balance)} for a in accounts}, } def make_logs_paths(stream_name: str): path = base_directory / "geth-logs" / f"{stream_name}_{self._port}" path.parent.mkdir(exist_ok=True, parents=True) return path initialize_chain(genesis_data, **geth_kwargs) super().__init__( geth_kwargs, stdout_logfile_path=make_logs_paths("stdout"), stderr_logfile_path=make_logs_paths("stderr"), )