Skip to content

Commit 971bc60

Browse files
authored
Fix so trinity respects --trinity-root-dir setting (#1268)
* Fix so trinity respects --trinity-root-dir setting * cleanup path utils * docstring cleanup
1 parent de4809f commit 971bc60

File tree

9 files changed

+129
-145
lines changed

9 files changed

+129
-145
lines changed

tests/trinity/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def xdg_trinity_root(monkeypatch, tmpdir):
3939

4040
assert not is_under_path(os.path.expandvars('$HOME'), get_xdg_trinity_root())
4141

42-
return str(dir_path)
42+
return Path(str(dir_path))
4343

4444

4545
@pytest.fixture(scope='session')

tests/trinity/core/chains-utils/test_chain_config_object.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from pathlib import Path
2+
13
import pytest
24

35
from eth_utils import (
@@ -7,6 +9,7 @@
79
from eth_keys import keys
810

911
from trinity.utils.chains import (
12+
get_data_dir_for_network_id,
1013
get_local_data_dir,
1114
get_nodekey_path,
1215
)
@@ -15,30 +18,42 @@
1518
DATABASE_DIR_NAME,
1619
)
1720
from trinity.utils.filesystem import (
18-
is_same_path,
21+
is_under_path,
1922
)
2023

2124

22-
def test_chain_config_computed_properties():
23-
data_dir = get_local_data_dir('muffin')
24-
chain_config = ChainConfig(network_id=1234, max_peers=1, data_dir=data_dir)
25+
def test_chain_config_computed_properties(xdg_trinity_root):
26+
data_dir = get_local_data_dir('muffin', xdg_trinity_root)
27+
chain_config = ChainConfig(network_id=1234, data_dir=data_dir)
2528

2629
assert chain_config.network_id == 1234
2730
assert chain_config.data_dir == data_dir
2831
assert chain_config.database_dir == data_dir / DATABASE_DIR_NAME / "full"
2932
assert chain_config.nodekey_path == get_nodekey_path(data_dir)
3033

3134

35+
def test_chain_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+
chain_config = ChainConfig(trinity_root_dir=alt_xdg_root, network_id=1)
41+
42+
assert chain_config.network_id == 1
43+
assert chain_config.data_dir == data_dir
44+
assert chain_config.database_dir == data_dir / DATABASE_DIR_NAME / "full"
45+
assert chain_config.nodekey_path == get_nodekey_path(data_dir)
46+
47+
3248
def test_chain_config_explicit_properties():
3349
chain_config = ChainConfig(
3450
network_id=1,
35-
max_peers=1,
3651
data_dir='./data-dir',
3752
nodekey_path='./nodekey'
3853
)
3954

40-
assert is_same_path(chain_config.data_dir, './data-dir')
41-
assert is_same_path(chain_config.nodekey_path, './nodekey')
55+
assert chain_config.data_dir == Path('./data-dir').resolve()
56+
assert chain_config.nodekey_path == Path('./nodekey').resolve()
4257

4358

4459
NODEKEY = '0xd18445cc77139cd8e09110e99c9384f0601bd2dfa5b230cda917df7e56b69949'
@@ -61,7 +76,6 @@ def nodekey_path(tmpdir, nodekey_bytes):
6176
def test_chain_config_nodekey_loading(nodekey_bytes, nodekey_path):
6277
chain_config = ChainConfig(
6378
network_id=1,
64-
max_peers=1,
6579
nodekey_path=nodekey_path,
6680
)
6781

@@ -72,7 +86,6 @@ def test_chain_config_nodekey_loading(nodekey_bytes, nodekey_path):
7286
def test_chain_config_explictely_provided_nodekey(nodekey_bytes, as_bytes):
7387
chain_config = ChainConfig(
7488
network_id=1,
75-
max_peers=1,
7689
nodekey=nodekey_bytes if as_bytes else keys.PrivateKey(nodekey_bytes),
7790
)
7891

tests/trinity/core/filesystem-utils/test_is_same_path.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

tests/trinity/core/filesystem-utils/test_is_under_path.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,3 @@
2626
def test_is_under_path(base_path, path, expected):
2727
actual = is_under_path(base_path, path)
2828
assert actual is expected
29-
30-
31-
@pytest.mark.parametrize(
32-
'base_path,path,expected',
33-
(
34-
# Same Path
35-
('foo', 'foo', True),
36-
('foo', 'foo/bar/..', True),
37-
# up a directory (or two)
38-
('foo', '..', False),
39-
('foo', 'foo/bar/../../', False),
40-
# relative and abs
41-
('foo', '/foo/bar', False),
42-
('foo', '/foo', False),
43-
# actually nested
44-
('foo', 'foo/bar.sol', True),
45-
('foo', 'foo/bar', True),
46-
('foo', 'foo/bar/../../foo/baz', True),
47-
),
48-
)
49-
def test_is_under_path_not_strict(base_path, path, expected):
50-
actual = is_under_path(base_path, path, strict=False)
51-
assert actual is expected

trinity/chains/__init__.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@
4242
AsyncHeaderDB,
4343
AsyncHeaderDBProxy,
4444
)
45+
from trinity.utils.filesystem import (
46+
is_under_path,
47+
)
4548
from trinity.utils.mp import (
4649
async_method,
4750
sync_method,
4851
)
49-
from trinity.utils.xdg import (
50-
is_under_xdg_trinity_root,
51-
)
52+
5253

5354
from .header import (
5455
AsyncHeaderChain,
@@ -97,7 +98,11 @@ def is_database_initialized(chaindb: AsyncChainDB) -> bool:
9798

9899

99100
def initialize_data_dir(chain_config: ChainConfig) -> None:
100-
if not chain_config.data_dir.exists() and is_under_xdg_trinity_root(chain_config.data_dir):
101+
should_create_data_dir = (
102+
not chain_config.data_dir.exists() and
103+
is_under_path(chain_config.trinity_root_dir, chain_config.data_dir)
104+
)
105+
if should_create_data_dir:
101106
chain_config.data_dir.mkdir(parents=True, exist_ok=True)
102107
elif not chain_config.data_dir.exists():
103108
# we don't lazily create the base dir for non-default base directories.
@@ -109,9 +114,11 @@ def initialize_data_dir(chain_config: ChainConfig) -> None:
109114
)
110115

111116
# Logfile
112-
if (not chain_config.logdir_path.exists() and
113-
is_under_xdg_trinity_root(chain_config.logdir_path)):
114-
117+
should_create_logdir = (
118+
not chain_config.logdir_path.exists() and
119+
is_under_path(chain_config.trinity_root_dir, chain_config.logdir_path)
120+
)
121+
if should_create_logdir:
115122
chain_config.logdir_path.mkdir(parents=True, exist_ok=True)
116123
chain_config.logfile_path.touch()
117124
elif not chain_config.logdir_path.exists():

trinity/config.py

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
from trinity.utils.filesystem import (
4040
PidFile,
4141
)
42+
from trinity.utils.xdg import (
43+
get_xdg_trinity_root,
44+
)
45+
4246

4347
if TYPE_CHECKING:
4448
# avoid circular import
@@ -48,6 +52,7 @@
4852

4953

5054
class ChainConfig:
55+
_trinity_root_dir: Path = None
5156
_data_dir: Path = None
5257
_nodekey_path: Path = None
5358
_logfile_path: Path = None
@@ -61,7 +66,8 @@ class ChainConfig:
6166

6267
def __init__(self,
6368
network_id: int,
64-
max_peers: int,
69+
max_peers: int=25,
70+
trinity_root_dir: str=None,
6571
data_dir: str=None,
6672
nodekey_path: str=None,
6773
logfile_path: str=None,
@@ -75,6 +81,9 @@ def __init__(self,
7581
self.sync_mode = sync_mode
7682
self.port = port
7783

84+
if trinity_root_dir is not None:
85+
self.trinity_root_dir = trinity_root_dir
86+
7887
if not preferred_nodes and network_id in DEFAULT_PREFERRED_NODES:
7988
self.preferred_nodes = DEFAULT_PREFERRED_NODES[self.network_id]
8089
else:
@@ -92,32 +101,28 @@ def __init__(self,
92101
else:
93102
self.bootstrap_nodes = bootstrap_nodes
94103

95-
# validation
96-
if nodekey is not None and nodekey_path is not None:
97-
raise ValueError("It is invalid to provide both a `nodekey` and a `nodekey_path`")
98-
99-
# set values
100104
if data_dir is not None:
101105
self.data_dir = data_dir
102-
else:
103-
self.data_dir = get_data_dir_for_network_id(self.network_id)
104106

105-
if nodekey_path is not None:
107+
if nodekey is not None and nodekey_path is not None:
108+
raise ValueError("It is invalid to provide both a `nodekey` and a `nodekey_path`")
109+
elif nodekey_path is not None:
106110
self.nodekey_path = nodekey_path
107111
elif nodekey is not None:
108112
self.nodekey = nodekey
109113

110114
if logfile_path is not None:
111115
self.logfile_path = logfile_path
112-
else:
113-
self.logfile_path = get_logfile_path(self.data_dir)
114116

115117
@property
116118
def logfile_path(self) -> Path:
117119
"""
118120
Return the path to the log file.
119121
"""
120-
return self._logfile_path
122+
if self._logfile_path is not None:
123+
return self._logfile_path
124+
else:
125+
return get_logfile_path(self.data_dir)
121126

122127
@logfile_path.setter
123128
def logfile_path(self, value: Path) -> None:
@@ -130,21 +135,49 @@ def logdir_path(self) -> Path:
130135
"""
131136
return self.logfile_path.parent
132137

138+
@property
139+
def trinity_root_dir(self) -> Path:
140+
"""
141+
The trinity_root_dir is the base directory that all trinity data is
142+
stored under.
143+
144+
The default ``data_dir`` path will be resolved relative to this
145+
directory.
146+
"""
147+
if self._trinity_root_dir is not None:
148+
return self._trinity_root_dir
149+
else:
150+
return get_xdg_trinity_root()
151+
152+
@trinity_root_dir.setter
153+
def trinity_root_dir(self, value: str) -> None:
154+
self._trinity_root_dir = Path(value).resolve()
155+
133156
@property
134157
def data_dir(self) -> Path:
135158
"""
136159
The data_dir is the base directory that all chain specific information
137-
for a given chain is stored. All other chain directories are by
138-
default relative to this directory.
160+
for a given chain is stored.
161+
162+
All defaults for chain directories are resolved relative to this
163+
directory.
139164
"""
140-
return self._data_dir
165+
if self._data_dir is not None:
166+
return self._data_dir
167+
else:
168+
return get_data_dir_for_network_id(self.network_id, self.trinity_root_dir)
141169

142170
@data_dir.setter
143171
def data_dir(self, value: str) -> None:
144172
self._data_dir = Path(value).resolve()
145173

146174
@property
147175
def database_dir(self) -> Path:
176+
"""
177+
Path where the chain database will be stored.
178+
179+
This is resolved relative to the ``data_dir``
180+
"""
148181
if self.sync_mode == SYNC_FULL:
149182
return self.data_dir / DATABASE_DIR_NAME / "full"
150183
elif self.sync_mode == SYNC_LIGHT:
@@ -154,14 +187,23 @@ def database_dir(self) -> Path:
154187

155188
@property
156189
def database_ipc_path(self) -> Path:
190+
"""
191+
Path for the database IPC socket connection.
192+
"""
157193
return get_database_socket_path(self.data_dir)
158194

159195
@property
160196
def jsonrpc_ipc_path(self) -> Path:
197+
"""
198+
Path for the JSON-RPC server IPC socket.
199+
"""
161200
return get_jsonrpc_socket_path(self.data_dir)
162201

163202
@property
164203
def nodekey_path(self) -> Path:
204+
"""
205+
Path where the nodekey is stored
206+
"""
165207
if self._nodekey_path is None:
166208
if self._nodekey is not None:
167209
return None
@@ -203,11 +245,18 @@ def nodekey(self, value: Union[bytes, PrivateKey]) -> None:
203245

204246
@classmethod
205247
def from_parser_args(cls, parser_args: argparse.Namespace) -> 'ChainConfig':
248+
"""
249+
Helper function for initializing from the namespace object produced by
250+
an ``argparse.ArgumentParser``
251+
"""
206252
constructor_kwargs = construct_chain_config_params(parser_args)
207253
return cls(**constructor_kwargs)
208254

209255
@property
210256
def node_class(self) -> Type['Node']:
257+
"""
258+
The ``Node`` class that trinity will use.
259+
"""
211260
from trinity.nodes.mainnet import (
212261
MainnetFullNode,
213262
MainnetLightNode,

0 commit comments

Comments
 (0)