|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from eth_utils import ( |
| 6 | + decode_hex, |
| 7 | +) |
| 8 | + |
| 9 | +from eth_keys import keys |
| 10 | + |
| 11 | +from trinity.utils.chains import ( |
| 12 | + get_data_dir_for_network_id, |
| 13 | + get_local_data_dir, |
| 14 | + get_nodekey_path, |
| 15 | +) |
| 16 | +from trinity.config import ( |
| 17 | + TrinityConfig, |
| 18 | + DATABASE_DIR_NAME, |
| 19 | +) |
| 20 | +from trinity.utils.filesystem import ( |
| 21 | + is_under_path, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def test_trinity_config_computed_properties(xdg_trinity_root): |
| 26 | + data_dir = get_local_data_dir('muffin', xdg_trinity_root) |
| 27 | + trinity_config = TrinityConfig(network_id=1, data_dir=data_dir) |
| 28 | + |
| 29 | + assert trinity_config.network_id == 1 |
| 30 | + assert trinity_config.data_dir == data_dir |
| 31 | + assert trinity_config.database_dir == data_dir / DATABASE_DIR_NAME / "full" |
| 32 | + assert trinity_config.nodekey_path == get_nodekey_path(data_dir) |
| 33 | + |
| 34 | + |
| 35 | +def test_trinity_config_computed_properties_custom_xdg(tmpdir, xdg_trinity_root): |
| 36 | + alt_xdg_root = tmpdir.mkdir('trinity-custom') |
| 37 | + assert not is_under_path(alt_xdg_root, xdg_trinity_root) |
| 38 | + |
| 39 | + data_dir = get_data_dir_for_network_id(1, alt_xdg_root) |
| 40 | + trinity_config = TrinityConfig(trinity_root_dir=alt_xdg_root, network_id=1) |
| 41 | + |
| 42 | + assert trinity_config.network_id == 1 |
| 43 | + assert trinity_config.data_dir == data_dir |
| 44 | + assert trinity_config.database_dir == data_dir / DATABASE_DIR_NAME / "full" |
| 45 | + assert trinity_config.nodekey_path == get_nodekey_path(data_dir) |
| 46 | + |
| 47 | + |
| 48 | +def test_trinity_config_explicit_properties(): |
| 49 | + trinity_config = TrinityConfig( |
| 50 | + network_id=1, |
| 51 | + data_dir='./data-dir', |
| 52 | + nodekey_path='./nodekey' |
| 53 | + ) |
| 54 | + |
| 55 | + assert trinity_config.data_dir == Path('./data-dir').resolve() |
| 56 | + assert trinity_config.nodekey_path == Path('./nodekey').resolve() |
| 57 | + |
| 58 | + |
| 59 | +NODEKEY = '0xd18445cc77139cd8e09110e99c9384f0601bd2dfa5b230cda917df7e56b69949' |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def nodekey_bytes(): |
| 64 | + _nodekey_bytes = decode_hex(NODEKEY) |
| 65 | + return _nodekey_bytes |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture |
| 69 | +def nodekey_path(tmpdir, nodekey_bytes): |
| 70 | + nodekey_file = tmpdir.mkdir('temp-nodekey-dir').join('nodekey') |
| 71 | + nodekey_file.write_binary(nodekey_bytes) |
| 72 | + |
| 73 | + return str(nodekey_file) |
| 74 | + |
| 75 | + |
| 76 | +def test_trinity_config_nodekey_loading(nodekey_bytes, nodekey_path): |
| 77 | + trinity_config = TrinityConfig( |
| 78 | + network_id=1, |
| 79 | + nodekey_path=nodekey_path, |
| 80 | + ) |
| 81 | + |
| 82 | + assert trinity_config.nodekey.to_bytes() == nodekey_bytes |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize('as_bytes', (True, False)) |
| 86 | +def test_trinity_config_explictely_provided_nodekey(nodekey_bytes, as_bytes): |
| 87 | + trinity_config = TrinityConfig( |
| 88 | + network_id=1, |
| 89 | + nodekey=nodekey_bytes if as_bytes else keys.PrivateKey(nodekey_bytes), |
| 90 | + ) |
| 91 | + |
| 92 | + assert trinity_config.nodekey.to_bytes() == nodekey_bytes |
0 commit comments