Skip to content

Commit e9121a7

Browse files
committed
review changes
1 parent efab80b commit e9121a7

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

hathor/cli/main.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ def __init__(self) -> None:
6666
'Run a merged mining coordinator (hathor and bitcoin nodes required)')
6767
self.add_cmd('mining', 'run_stratum_miner', stratum_mining, 'Run a mining process (running node required)')
6868
self.add_cmd('hathor', 'run_node', run_node, 'Run a node')
69-
self.add_cmd('hathor', 'side_dag', side_dag, 'Run a side-dag')
7069
self.add_cmd('hathor', 'gen_peer_id', peer_id, 'Generate a new random peer-id')
7170
if sys.platform != 'win32':
7271
from . import top
7372
self.add_cmd('hathor', 'top', top, 'CPU profiler viewer')
73+
self.add_cmd('side-dag', 'run_node_with_side_dag', side_dag, 'Run a side-dag')
7474
self.add_cmd('docs', 'generate_openapi_json', openapi_json, 'Generate OpenAPI json for API docs')
7575
self.add_cmd('multisig', 'gen_multisig_address', multisig_address, 'Generate a new multisig address')
7676
self.add_cmd('multisig', 'spend_multisig_output', multisig_spend, 'Generate tx that spends a multisig output')
@@ -155,8 +155,13 @@ def execute_from_command_line(self):
155155

156156
pre_setup_logging = getattr(module, 'PRE_SETUP_LOGGING', True)
157157
if pre_setup_logging:
158-
debug, logging_output, sentry = process_logging_options(sys.argv)
159-
setup_logging(debug=debug, capture_stdout=capture_stdout, logging_output=logging_output, sentry=sentry)
158+
options = process_logging_options(sys.argv)
159+
setup_logging(
160+
debug=options.debug,
161+
capture_stdout=capture_stdout,
162+
logging_output=options.logging_output,
163+
sentry=options.sentry,
164+
)
160165
module.main()
161166
else:
162167
module.main(capture_stdout=capture_stdout)

hathor/cli/side_dag.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class SideDagProcessTerminated:
5858
pass
5959

6060

61-
class SideDag(RunNode):
61+
class SideDagRunNode(RunNode):
6262
env_vars_prefix = 'hathor_side_dag_'
6363

6464

@@ -88,18 +88,23 @@ def main(capture_stdout: bool) -> None:
8888
In this example, Hathor testnet logs would be disabled, while side-dag logs would be outputted to stdout as json.
8989
"""
9090
from hathor.cli.util import process_logging_options, setup_logging
91-
hathor_node_argv, side_dag_argv = _process_argv(sys.argv[1:])
91+
hathor_node_argv, side_dag_argv = _partition_argv(sys.argv[1:])
9292
conn1, conn2 = Pipe()
9393
hathor_node_process = _start_hathor_node_process(hathor_node_argv, capture_stdout=capture_stdout, conn=conn1)
9494

95-
debug, logging_output, sentry = process_logging_options(side_dag_argv)
96-
setup_logging(debug=debug, capture_stdout=capture_stdout, logging_output=logging_output, sentry=sentry)
95+
log_options = process_logging_options(side_dag_argv)
96+
setup_logging(
97+
debug=log_options.debug,
98+
capture_stdout=capture_stdout,
99+
logging_output=log_options.logging_output,
100+
sentry=log_options.sentry,
101+
)
97102
logger.info('starting nodes', hathor_node_pid=hathor_node_process.pid, side_dag_pid=os.getpid())
98103

99104
_run_side_dag_node(side_dag_argv, hathor_node_process=hathor_node_process, conn=conn2)
100105

101106

102-
def _process_argv(argv: list[str]) -> tuple[list[str], list[str]]:
107+
def _partition_argv(argv: list[str]) -> tuple[list[str], list[str]]:
103108
"""Partition arguments into hathor node args and side-dag args, based on the `--side-dag` prefix."""
104109
hathor_node_argv: list[str] = []
105110
side_dag_argv: list[str] = []
@@ -148,7 +153,7 @@ def _run_side_dag_node(argv: list[str], *, hathor_node_process: Process, conn: '
148153
logger.info('starting side-dag node...')
149154

150155
try:
151-
side_dag = SideDag(argv=argv)
156+
side_dag = SideDagRunNode(argv=argv)
152157
except (BaseException, Exception):
153158
logger.critical('terminating hathor node...')
154159
conn.send(SideDagProcessTerminated())
@@ -185,8 +190,13 @@ def _run_hathor_node(argv: list[str], run_node_cmd: type[RunNode], capture_stdou
185190
# We don't terminate via SIGINT directly, instead the side-dag process will terminate us.
186191
signal.signal(signal.SIGINT, lambda _, __: None)
187192
try:
188-
debug, logging_output, sentry = process_logging_options(argv)
189-
setup_logging(debug=debug, capture_stdout=capture_stdout, logging_output=logging_output, sentry=sentry)
193+
log_options = process_logging_options(argv)
194+
setup_logging(
195+
debug=log_options.debug,
196+
capture_stdout=capture_stdout,
197+
logging_output=log_options.logging_output,
198+
sentry=log_options.sentry,
199+
)
190200
hathor_node = run_node_cmd(argv=argv)
191201
except (BaseException, Exception):
192202
conn.send(HathorProcessInitFail(traceback.format_exc()))

hathor/cli/util.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from collections import OrderedDict
2020
from datetime import datetime
2121
from enum import IntEnum, auto
22-
from typing import Any
22+
from typing import Any, NamedTuple
2323

2424
import configargparse
2525
import structlog
@@ -129,7 +129,14 @@ class LoggingOutput(IntEnum):
129129
JSON = auto()
130130

131131

132-
def process_logging_options(argv: list[str]) -> tuple[bool, LoggingOutput, bool]:
132+
class LoggingOptions(NamedTuple):
133+
debug: bool
134+
logging_output: LoggingOutput
135+
sentry: bool
136+
137+
138+
def process_logging_options(argv: list[str]) -> LoggingOptions:
139+
"""Extract logging-specific options that are processed before argv parsing."""
133140
debug = '--debug' in argv
134141
if debug:
135142
argv.remove('--debug')
@@ -151,7 +158,7 @@ def process_logging_options(argv: list[str]) -> tuple[bool, LoggingOutput, bool]
151158

152159
sentry = '--sentry-dsn' in argv
153160

154-
return debug, logging_output, sentry
161+
return LoggingOptions(debug=debug, logging_output=logging_output, sentry=sentry)
155162

156163

157164
def setup_logging(

tests/cli/test_side_dag.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
HathorProcessInitSuccess,
2323
HathorProcessTerminated,
2424
SideDagProcessTerminated,
25-
_process_argv,
25+
_partition_argv,
2626
_run_hathor_node,
2727
_run_side_dag_node,
2828
main,
@@ -54,7 +54,7 @@ def test_process_argv(
5454
expected_hathor_node_argv: list[str],
5555
expected_side_dag_argv: list[str]
5656
) -> None:
57-
hathor_node_argv, side_dag_argv = _process_argv(argv)
57+
hathor_node_argv, side_dag_argv = _partition_argv(argv)
5858

5959
assert hathor_node_argv == expected_hathor_node_argv
6060
assert side_dag_argv == expected_side_dag_argv

0 commit comments

Comments
 (0)