Skip to content

Commit acd9374

Browse files
committed
Add support for trinity-beacon command
This is going to be the main entry point for the beacon node for the beginning.
1 parent e0c1739 commit acd9374

File tree

4 files changed

+118
-2
lines changed

4 files changed

+118
-2
lines changed

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@
131131
],
132132
# trinity
133133
entry_points={
134-
'console_scripts': ['trinity=trinity:main'],
134+
'console_scripts': [
135+
'trinity=trinity:main',
136+
'trinity-beacon=trinity:main_beacon'
137+
],
135138
},
136139
)

setup_trinity.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
python_requires=">=3.6,<4",
3333
# trinity
3434
entry_points={
35-
'console_scripts': ['trinity=trinity:main'],
35+
'console_scripts': [
36+
'trinity=trinity:main',
37+
'trinity-beacon=trinity:main_beacon'
38+
],
3639
},
3740
)

trinity/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ def is_uvloop_supported() -> bool:
2929
from .main import ( # noqa: F401
3030
main,
3131
)
32+
33+
from .main_beacon import ( # noqa: F401
34+
main_beacon,
35+
)

trinity/main_beacon.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
from argparse import ArgumentParser, Namespace
2+
import asyncio
3+
import logging
4+
import signal
5+
from typing import (
6+
Any,
7+
Dict,
8+
)
9+
10+
from lahja import (
11+
EventBus,
12+
Endpoint,
13+
)
14+
15+
from eth.db.backends.level import LevelDB
16+
17+
from trinity.bootstrap import (
18+
kill_trinity_gracefully,
19+
main_entry,
20+
run_database_process,
21+
)
22+
from trinity.config import (
23+
TrinityConfig,
24+
)
25+
from trinity.events import (
26+
ShutdownRequest
27+
)
28+
from trinity.extensibility import (
29+
PluginManager,
30+
)
31+
from trinity.utils.ipc import (
32+
wait_for_ipc,
33+
kill_process_gracefully,
34+
)
35+
from trinity.utils.mp import (
36+
ctx,
37+
)
38+
39+
40+
def main_beacon() -> None:
41+
main_entry(trinity_boot)
42+
43+
44+
def trinity_boot(args: Namespace,
45+
trinity_config: TrinityConfig,
46+
extra_kwargs: Dict[str, Any],
47+
plugin_manager: PluginManager,
48+
listener: logging.handlers.QueueListener,
49+
event_bus: EventBus,
50+
main_endpoint: Endpoint,
51+
logger: logging.Logger) -> None:
52+
# start the listener thread to handle logs produced by other processes in
53+
# the local logger.
54+
listener.start()
55+
56+
event_bus.start()
57+
58+
# First initialize the database process.
59+
database_server_process = ctx.Process(
60+
name="DB",
61+
target=run_database_process,
62+
args=(
63+
trinity_config,
64+
LevelDB,
65+
),
66+
kwargs=extra_kwargs,
67+
)
68+
69+
# start the processes
70+
database_server_process.start()
71+
logger.info("Started DB server process (pid=%d)", database_server_process.pid)
72+
73+
# networking process needs the IPC socket file provided by the database process
74+
try:
75+
wait_for_ipc(trinity_config.database_ipc_path)
76+
except TimeoutError as e:
77+
logger.error("Timeout waiting for database to start. Exiting...")
78+
kill_process_gracefully(database_server_process, logger)
79+
ArgumentParser().error(message="Timed out waiting for database start")
80+
81+
def kill_trinity_with_reason(reason: str) -> None:
82+
kill_trinity_gracefully(
83+
logger,
84+
(database_server_process,),
85+
plugin_manager,
86+
main_endpoint,
87+
event_bus,
88+
reason=reason
89+
)
90+
91+
main_endpoint.subscribe(
92+
ShutdownRequest,
93+
lambda ev: kill_trinity_with_reason(ev.reason)
94+
)
95+
96+
plugin_manager.prepare(args, trinity_config, extra_kwargs)
97+
98+
kill_trinity_with_reason("No beacon support yet. SOON!")
99+
100+
try:
101+
loop = asyncio.get_event_loop()
102+
loop.add_signal_handler(signal.SIGTERM, lambda: kill_trinity_with_reason("SIGTERM"))
103+
loop.run_forever()
104+
loop.close()
105+
except KeyboardInterrupt:
106+
kill_trinity_with_reason("CTRL+C / Keyboard Interrupt")

0 commit comments

Comments
 (0)