From 9cf88fda9ae4d873d660c26d1f506c5be494267a Mon Sep 17 00:00:00 2001 From: Jason Carver Date: Wed, 25 May 2022 16:35:03 -0700 Subject: [PATCH] Use existing trin node if it is already running Also, a convenient log to run trin yourself, if you want to. --- eth_portal/trin.py | 46 +++++++++++++++++++++++------------- newsfragments/14.feature.rst | 2 ++ 2 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 newsfragments/14.feature.rst diff --git a/eth_portal/trin.py b/eth_portal/trin.py index e9bf41c..39eb200 100644 --- a/eth_portal/trin.py +++ b/eth_portal/trin.py @@ -1,4 +1,5 @@ from contextlib import contextmanager +from pathlib import Path import signal import subprocess @@ -26,25 +27,38 @@ def launch_trin(private_key: bytes, port: int): ipc_path = f"/tmp/trin-jsonrpc-{node_id_hex[:20]}.ipc" private_key_hex = remove_0x_prefix(private_key.hex()) - print(f"Launching trin with node ID {node_id_hex[:10]}...") - try: + short_node_id = node_id_hex[:10] + trin_args = [ + "./trin", + # fmt: off + "--discovery-port", str(port), + "--unsafe-private-key", private_key_hex, + "--web3-ipc-path", ipc_path, + "--kb", "20000", + "--networks", "history", + "--bootnodes", "default", + # fmt: on + ] + + if Path(ipc_path).exists(): + print(f"trin already running for {short_node_id}, skipping launch...") + trin_proc = None + else: + print(f"Launching trin with node ID {short_node_id} using...") + print(" ".join(trin_args)) trin_proc = subprocess.Popen( - [ - "./trin", - # fmt: off - "--discovery-port", str(port), - "--unsafe-private-key", private_key_hex, - "--web3-ipc-path", ipc_path, - "--kb", "20000", - "--networks", "history", - "--bootnodes", "default", - # fmt: on - ], + trin_args, env={"TRIN_INFURA_PROJECT_ID": "1"}, ) + try: yield Web3(Web3.IPCProvider(ipc_path)) finally: - print(f"Exiting trin with node ID {node_id_hex[:10]}...") - trin_proc.send_signal(signal.SIGINT) - trin_proc.wait() + if trin_proc: + print(f"Exiting trin with node ID {node_id_hex[:10]}...") + trin_proc.send_signal(signal.SIGINT) + trin_proc.wait() + else: + print( + f"Did not launch trin with node ID {node_id_hex[:10]}, so cannot exit." + ) diff --git a/newsfragments/14.feature.rst b/newsfragments/14.feature.rst new file mode 100644 index 0000000..ae9ae91 --- /dev/null +++ b/newsfragments/14.feature.rst @@ -0,0 +1,2 @@ +In bridge launcher, detect if trin "injector" node is already running, then use it. One benefit is +being able to observe the logs on trin during bridge operation.