Skip to content

Commit 968b96c

Browse files
committed
feat(p2p): add ability to update peer_id.json with SIGUSR1
1 parent 74b7251 commit 968b96c

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

hathor/builder/cli_builder.py

+2-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import getpass
16-
import json
1716
import os
1817
import platform
1918
import sys
@@ -96,8 +95,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
9695
self.log = logger.new()
9796
self.reactor = reactor
9897

99-
peer_id = self.create_peer_id()
100-
98+
peer_id = PeerId.create_from_json_path(self._args.peer) if self._args.peer else PeerId()
10199
python = f'{platform.python_version()}-{platform.python_implementation()}'
102100

103101
self.log.info(
@@ -322,6 +320,7 @@ def create_manager(self, reactor: Reactor) -> HathorManager:
322320
verification_service=verification_service,
323321
cpu_mining_service=cpu_mining_service,
324322
execution_manager=execution_manager,
323+
peer_id_json_path=self._args.peer,
325324
)
326325

327326
if self._args.x_ipython_kernel:
@@ -397,14 +396,6 @@ def get_hostname(self) -> Optional[str]:
397396
print('Hostname discovered and set to {}'.format(hostname))
398397
return hostname
399398

400-
def create_peer_id(self) -> PeerId:
401-
if not self._args.peer:
402-
peer_id = PeerId()
403-
else:
404-
data = json.load(open(self._args.peer, 'r'))
405-
peer_id = PeerId.create_from_json(data)
406-
return peer_id
407-
408399
def create_wallet(self) -> BaseWallet:
409400
if self._args.wallet == 'hd':
410401
kwargs: dict[str, Any] = {

hathor/cli/run_node.py

+1
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ def signal_usr1_handler(self, sig: int, frame: Any) -> None:
267267
self.log.warn('USR1 received. Killing all connections...')
268268
if self.manager and self.manager.connections:
269269
self.manager.connections.disconnect_all_peers(force=True)
270+
self.manager.update_peer_id()
270271
except Exception:
271272
# see: https://docs.python.org/3/library/signal.html#note-on-signal-handlers-and-exceptions
272273
self.log.error('prevented exception from escaping the signal handler', exc_info=True)

hathor/manager.py

+34-25
Original file line numberDiff line numberDiff line change
@@ -89,31 +89,34 @@ class UnhealthinessReason(str, Enum):
8989
# This is the interval to be used by the task to check if the node is synced
9090
CHECK_SYNC_STATE_INTERVAL = 30 # seconds
9191

92-
def __init__(self,
93-
reactor: Reactor,
94-
*,
95-
settings: HathorSettings,
96-
pubsub: PubSubManager,
97-
consensus_algorithm: ConsensusAlgorithm,
98-
daa: DifficultyAdjustmentAlgorithm,
99-
peer_id: PeerId,
100-
tx_storage: TransactionStorage,
101-
p2p_manager: ConnectionsManager,
102-
event_manager: EventManager,
103-
feature_service: FeatureService,
104-
bit_signaling_service: BitSignalingService,
105-
verification_service: VerificationService,
106-
cpu_mining_service: CpuMiningService,
107-
network: str,
108-
execution_manager: ExecutionManager,
109-
hostname: Optional[str] = None,
110-
wallet: Optional[BaseWallet] = None,
111-
capabilities: Optional[list[str]] = None,
112-
checkpoints: Optional[list[Checkpoint]] = None,
113-
rng: Optional[Random] = None,
114-
environment_info: Optional[EnvironmentInfo] = None,
115-
full_verification: bool = False,
116-
enable_event_queue: bool = False):
92+
def __init__(
93+
self,
94+
reactor: Reactor,
95+
*,
96+
settings: HathorSettings,
97+
pubsub: PubSubManager,
98+
consensus_algorithm: ConsensusAlgorithm,
99+
daa: DifficultyAdjustmentAlgorithm,
100+
peer_id: PeerId,
101+
tx_storage: TransactionStorage,
102+
p2p_manager: ConnectionsManager,
103+
event_manager: EventManager,
104+
feature_service: FeatureService,
105+
bit_signaling_service: BitSignalingService,
106+
verification_service: VerificationService,
107+
cpu_mining_service: CpuMiningService,
108+
network: str,
109+
execution_manager: ExecutionManager,
110+
hostname: Optional[str] = None,
111+
wallet: Optional[BaseWallet] = None,
112+
capabilities: Optional[list[str]] = None,
113+
checkpoints: Optional[list[Checkpoint]] = None,
114+
rng: Optional[Random] = None,
115+
environment_info: Optional[EnvironmentInfo] = None,
116+
full_verification: bool = False,
117+
enable_event_queue: bool = False,
118+
peer_id_json_path: str | None = None,
119+
) -> None:
117120
"""
118121
:param reactor: Twisted reactor which handles the mainloop and the events.
119122
:param peer_id: Id of this node.
@@ -135,6 +138,7 @@ def __init__(self,
135138
self._settings = settings
136139
self.daa = daa
137140
self._cmd_path: Optional[str] = None
141+
self._peer_id_json_path = peer_id_json_path
138142

139143
self.log = logger.new()
140144

@@ -1173,6 +1177,11 @@ def get_cmd_path(self) -> Optional[str]:
11731177
"""Return the cmd path. If no cmd path is set, returns None."""
11741178
return self._cmd_path
11751179

1180+
def update_peer_id(self) -> None:
1181+
"""Recreate this manager's PeerId from the json file, that may have changed."""
1182+
if self._peer_id_json_path:
1183+
self.my_peer = PeerId.create_from_json_path(self._peer_id_json_path)
1184+
11761185

11771186
class ParentTxs(NamedTuple):
11781187
""" Tuple where the `must_include` hash, when present (at most 1), must be included in a pair, and a list of hashes

hathor/p2p/peer_id.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import base64
1616
import hashlib
17+
import json
1718
from enum import Enum
1819
from math import inf
1920
from typing import TYPE_CHECKING, Any, Optional, cast
@@ -159,9 +160,15 @@ def verify_signature(self, signature: bytes, data: bytes) -> bool:
159160
else:
160161
return True
161162

163+
@classmethod
164+
def create_from_json_path(cls, path: str) -> 'PeerId':
165+
"""Create a new PeerId from a JSON file."""
166+
data = json.load(open(path, 'r'))
167+
return PeerId.create_from_json(data)
168+
162169
@classmethod
163170
def create_from_json(cls, data: dict[str, Any]) -> 'PeerId':
164-
""" Create a new PeerId from a JSON.
171+
""" Create a new PeerId from JSON data.
165172
166173
It is used both to load a PeerId from disk and to create a PeerId
167174
from a peer connection.

0 commit comments

Comments
 (0)