def test_trinity_config_computed_properties(xdg_trinity_root): data_dir = get_local_data_dir('muffin', xdg_trinity_root) trinity_config = TrinityConfig(network_id=1, data_dir=data_dir) assert trinity_config.network_id == 1 assert trinity_config.data_dir == data_dir assert trinity_config.nodekey_path == get_nodekey_path(data_dir)
def test_trinity_config_explicit_properties(): trinity_config = TrinityConfig(network_id=1, data_dir='./data-dir', nodekey_path='./nodekey') assert trinity_config.data_dir == Path('./data-dir').resolve() assert trinity_config.nodekey_path == Path('./nodekey').resolve()
def test_trinity_config_nodekey_loading(nodekey_bytes, nodekey_path): trinity_config = TrinityConfig( network_id=1, nodekey_path=nodekey_path, ) assert trinity_config.nodekey.to_bytes() == nodekey_bytes
def test_trinity_config_explictely_provided_nodekey(nodekey_bytes, as_bytes): trinity_config = TrinityConfig( network_id=1, nodekey=nodekey_bytes if as_bytes else keys.PrivateKey(nodekey_bytes), ) assert trinity_config.nodekey.to_bytes() == nodekey_bytes
def database_server_ipc_path(): core_db = AtomicDB() core_db[b'key-a'] = b'value-a' chaindb = ChainDB(core_db) # TODO: use a custom chain class only for testing. chaindb.persist_header(ROPSTEN_GENESIS_HEADER) with tempfile.TemporaryDirectory() as temp_dir: trinity_config = TrinityConfig( network_id=ROPSTEN_NETWORK_ID, data_dir=temp_dir, ) manager = get_chaindb_manager(trinity_config, core_db) chaindb_server_process = multiprocessing.Process( target=serve_chaindb, args=(manager, ), ) chaindb_server_process.start() wait_for_ipc(trinity_config.database_ipc_path) try: yield trinity_config.database_ipc_path finally: kill_process_gracefully(chaindb_server_process, logging.getLogger())
def test_trinity_config_sub_configs(): trinity_config = TrinityConfig(network_id=1) trinity_config.initialize_app_configs(None, (BeaconAppConfig,)) assert trinity_config.has_app_config(BeaconAppConfig) beacon_config = trinity_config.get_app_config(BeaconAppConfig) assert type(beacon_config) is BeaconAppConfig
def test_chain_config_computed_properties(xdg_trinity_root): data_dir = get_local_data_dir('muffin', xdg_trinity_root) chain_config = TrinityConfig(network_id=1234, data_dir=data_dir) assert chain_config.network_id == 1234 assert chain_config.data_dir == data_dir assert chain_config.database_dir == data_dir / DATABASE_DIR_NAME / "full" assert chain_config.nodekey_path == get_nodekey_path(data_dir)
def test_trinity_config_computed_properties_custom_xdg(tmpdir, xdg_trinity_root): alt_xdg_root = tmpdir.mkdir('trinity-custom') assert not is_under_path(alt_xdg_root, xdg_trinity_root) data_dir = get_data_dir_for_network_id(1, alt_xdg_root) trinity_config = TrinityConfig(trinity_root_dir=alt_xdg_root, network_id=1) assert trinity_config.network_id == 1 assert trinity_config.data_dir == data_dir assert trinity_config.nodekey_path == get_nodekey_path(data_dir)
def test_sync_mode_effect_on_db_and_node_type(sync_mode, expected_full_db, expected_node_class): trinity_config = TrinityConfig(network_id=1) eth1_app_config = Eth1AppConfig(trinity_config, sync_mode) assert eth1_app_config.sync_mode == sync_mode assert eth1_app_config.node_class == expected_node_class if expected_full_db: assert eth1_app_config.database_mode is Eth1DbMode.FULL else: assert eth1_app_config.database_mode is Eth1DbMode.LIGHT
def test_chain_config_computed_properties_custom_xdg(tmpdir, xdg_trinity_root): alt_xdg_root = tmpdir.mkdir('trinity-custom') assert not is_under_path(alt_xdg_root, xdg_trinity_root) data_dir = get_data_dir_for_network_id(1, alt_xdg_root) chain_config = TrinityConfig(trinity_root_dir=alt_xdg_root, network_id=1) assert chain_config.network_id == 1 assert chain_config.data_dir == data_dir assert chain_config.database_dir == data_dir / DATABASE_DIR_NAME / "full" assert chain_config.nodekey_path == get_nodekey_path(data_dir)
def test_full_initialized_data_dir_with_custom_nodekey(): trinity_config = TrinityConfig(network_id=1, nodekey=NODEKEY) os.makedirs(trinity_config.data_dir, exist_ok=True) os.makedirs(trinity_config.database_dir, exist_ok=True) os.makedirs(trinity_config.logfile_path, exist_ok=True) trinity_config.logfile_path.touch() assert trinity_config.nodekey_path is None assert trinity_config.nodekey is not None assert is_data_dir_initialized(trinity_config)
def test_trinity_config_app_identifier(xdg_trinity_root, app_identifier, expected_suffix): data_dir = get_local_data_dir('muffin', xdg_trinity_root) trinity_config = TrinityConfig(network_id=1, data_dir=data_dir, app_identifier=app_identifier) assert trinity_config.network_id == 1 assert trinity_config.data_dir == data_dir assert trinity_config.logfile_path == data_dir / (LOG_DIR + expected_suffix) / LOG_FILE assert trinity_config.jsonrpc_ipc_path == data_dir / (IPC_DIR + expected_suffix) / JSONRPC_SOCKET_FILENAME # noqa: E501 assert trinity_config.database_ipc_path == data_dir / (IPC_DIR + expected_suffix) / DATABASE_SOCKET_FILENAME # noqa: E501 assert trinity_config.pid_dir == data_dir / (PID_DIR + expected_suffix) assert trinity_config.nodekey_path == get_nodekey_path(data_dir)
async def setup_rpc_server(event_bus, chain_fixture, fixture_path): chain = MainnetFullChain(None) trinity_config = TrinityConfig(app_identifier="eth1", network_id=1) rpc = RPCServer(initialize_eth1_modules(chain, event_bus, trinity_config), chain, event_bus) setup_result, setup_error = await call_rpc(rpc, 'evm_resetToGenesisFixture', [chain_fixture]) # We need to advance the event loop for modules to be able to pickup the new chain await asyncio.sleep(0) assert setup_error is None and setup_result is True, f"cannot load chain for {fixture_path}" return rpc
async def test_access_control(event_bus, api, param, disabled_modules, expected_result, expected_error): chain = MainnetFullChain(None) trinity_config = TrinityConfig(app_identifier="eth1", network_id=1) rpc = RPCServer(initialize_eth1_modules(chain, event_bus, trinity_config), chain, event_bus) request = build_request(api, param) response = await rpc.execute_with_access_control(disabled_modules, request) result, error = result_from_response(response) assert result == expected_result assert error == expected_error
async def test_logger_configuration(command, expected_stderr_logs, unexpected_stderr_logs, expected_file_logs, unexpected_file_logs, unused_tcp_port): command = amend_command_for_unused_port(command, unused_tcp_port) def contains_substring(iterable, substring): return any(substring in x for x in iterable) # Saw occasional (<25%, >5%) failures in CI at 30s because of slow machines or bad luck async with AsyncProcessRunner.run(command, timeout_sec=45) as runner: stderr_logs = [] # Collect logs up to the point when the sync begins so that we have enough logs # for assertions marker_seen_at = 0 async for line in runner.stderr: if marker_seen_at != 0 and time.time() - marker_seen_at > 3: break if "DiscoveryService" in line: marker_seen_at = time.time() stderr_logs.append(line) else: stderr_logs.append(line) for log in expected_stderr_logs: if not contains_substring(stderr_logs, log): raise AssertionError( f"Log should contain `{log}` but does not") for log in unexpected_stderr_logs: if contains_substring(stderr_logs, log): raise AssertionError( f"Log should not contain `{log}` but does") log_dir = TrinityConfig(app_identifier="eth1", network_id=1).log_dir log_file_path = max(log_dir.glob('*'), key=os.path.getctime) with open(log_file_path) as log_file: file_content = log_file.read() for log in expected_file_logs: if log not in file_content: raise AssertionError( f"Logfile should contain `{log}` but does not") for log in unexpected_file_logs: if log in file_content: raise AssertionError( f"Logfile should not contain `{log}` but does")
async def ipc_server(monkeypatch, event_bus, jsonrpc_ipc_pipe_path, event_loop, chain_with_block_validation): """ This fixture runs a single RPC server over IPC over the course of all tests. It yields the IPC server only for monkeypatching purposes """ trinity_config = TrinityConfig(app_identifier="eth1", network_id=1) rpc = RPCServer( initialize_eth1_modules(chain_with_block_validation, event_bus, trinity_config), chain_with_block_validation, event_bus, ) ipc_server = IPCServer(rpc, jsonrpc_ipc_pipe_path) async with background_asyncio_service(ipc_server): yield ipc_server
async def test_logger(async_process_runner, command, expected_stderr_logs, unexpected_stderr_logs, expected_file_logs, unexpected_file_logs): def contains_substring(iterable, substring): return any(substring in x for x in iterable) # Saw occasional (<25%, >5%) failures in CI at 30s because of slow machines or bad luck await async_process_runner.run(command, timeout_sec=45) stderr_logs = [] # Collect logs up to the point when the sync begins so that we have enough logs for assertions marker_seen_at = 0 async for line in async_process_runner.stderr: if marker_seen_at != 0 and time.time() - marker_seen_at > 3: break if "DiscoveryProtocol" in line: marker_seen_at = time.time() stderr_logs.append(line) else: stderr_logs.append(line) for log in expected_stderr_logs: if not contains_substring(stderr_logs, log): assert False, f"Log should contain `{log}` but does not" for log in unexpected_stderr_logs: if contains_substring(stderr_logs, log): assert False, f"Log should not contain `{log}` but does" log_file_path = TrinityConfig(app_identifier="eth1", network_id=1).logfile_path with open(log_file_path) as log_file: file_content = log_file.read() for log in expected_file_logs: if log not in file_content: assert False, f"Logfile should contain `{log}` but does not" for log in unexpected_file_logs: if log in file_content: assert False, f"Logfile should not contain `{log}` but does"
async def test_logger(async_process_runner, command, expected_stderr_logs, unexpected_stderr_logs, expected_file_logs, unexpected_file_logs): def contains_substring(iterable, substring): return any(substring in x for x in iterable) await async_process_runner.run(command, timeout_sec=30) stderr_logs = [] # Collect logs up to the point when the sync begins so that we have enough logs for assertions async for line in async_process_runner.stderr: if "FastChainBodySyncer" in line: break else: stderr_logs.append(line) for log in expected_stderr_logs: if not contains_substring(stderr_logs, log): assert False, f"Log should contain `{log}` but does not" for log in unexpected_stderr_logs: if contains_substring(stderr_logs, log): assert False, f"Log should not contain `{log}` but does" log_file_path = TrinityConfig(app_identifier="eth1", network_id=1).logfile_path with open(log_file_path) as log_file: file_content = log_file.read() for log in expected_file_logs: if log not in file_content: assert False, f"Logfile should contain `{log}` but does not" for log in unexpected_file_logs: if log in file_content: assert False, f"Logfile should not contain `{log}` but does"
def test_computed_database_dir(app_identifier, sync_mode, expected_db_path, expected_db_name): trinity_config = TrinityConfig(network_id=1, app_identifier=app_identifier) eth1_app_config = Eth1AppConfig(trinity_config, sync_mode) assert eth1_app_config.database_dir == trinity_config.data_dir / expected_db_path / expected_db_name # noqa: E501
def trinity_config(): _trinity_config = TrinityConfig(network_id=1) initialize_data_dir(_trinity_config) return _trinity_config
def trinity_config(): return TrinityConfig(network_id=1)
def trinity_config(xdg_trinity_root): data_dir = get_local_data_dir('mainnet', xdg_trinity_root) return TrinityConfig( network_id=1, data_dir=data_dir, )
from eth.db.chain import ChainDB from eth.db.backends.level import LevelDB from trinity.config import TrinityConfig from trinity.constants import ( MAINNET_NETWORK_ID, ROPSTEN_NETWORK_ID, SYNC_FULL, SYNC_LIGHT, ) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('-ropsten', action='store_true') parser.add_argument('-light', action='store_true') args = parser.parse_args() network_id = MAINNET_NETWORK_ID if args.ropsten: network_id = ROPSTEN_NETWORK_ID sync_mode = SYNC_FULL if args.light: sync_mode = SYNC_LIGHT cfg = TrinityConfig(network_id, sync_mode=sync_mode) chaindb = ChainDB(LevelDB(cfg.database_dir)) head = chaindb.get_canonical_head() print("Head #%d; hash: %s, state_root: %s" % (head.block_number, head.hex_hash, encode_hex(head.state_root)))