unaiverse 0.1.11__cp311-cp311-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of unaiverse might be problematic. Click here for more details.

Files changed (50) hide show
  1. unaiverse/__init__.py +19 -0
  2. unaiverse/agent.py +2090 -0
  3. unaiverse/agent_basics.py +1948 -0
  4. unaiverse/clock.py +221 -0
  5. unaiverse/dataprops.py +1236 -0
  6. unaiverse/hsm.py +1892 -0
  7. unaiverse/modules/__init__.py +18 -0
  8. unaiverse/modules/cnu/__init__.py +17 -0
  9. unaiverse/modules/cnu/cnus.py +536 -0
  10. unaiverse/modules/cnu/layers.py +261 -0
  11. unaiverse/modules/cnu/psi.py +60 -0
  12. unaiverse/modules/hl/__init__.py +15 -0
  13. unaiverse/modules/hl/hl_utils.py +411 -0
  14. unaiverse/modules/networks.py +1509 -0
  15. unaiverse/modules/utils.py +710 -0
  16. unaiverse/networking/__init__.py +16 -0
  17. unaiverse/networking/node/__init__.py +18 -0
  18. unaiverse/networking/node/connpool.py +1308 -0
  19. unaiverse/networking/node/node.py +2499 -0
  20. unaiverse/networking/node/profile.py +446 -0
  21. unaiverse/networking/node/tokens.py +79 -0
  22. unaiverse/networking/p2p/__init__.py +187 -0
  23. unaiverse/networking/p2p/go.mod +127 -0
  24. unaiverse/networking/p2p/go.sum +548 -0
  25. unaiverse/networking/p2p/golibp2p.py +18 -0
  26. unaiverse/networking/p2p/golibp2p.pyi +135 -0
  27. unaiverse/networking/p2p/lib.go +2662 -0
  28. unaiverse/networking/p2p/lib.go.sha256 +1 -0
  29. unaiverse/networking/p2p/lib_types.py +312 -0
  30. unaiverse/networking/p2p/message_pb2.py +50 -0
  31. unaiverse/networking/p2p/messages.py +362 -0
  32. unaiverse/networking/p2p/mylogger.py +77 -0
  33. unaiverse/networking/p2p/p2p.py +871 -0
  34. unaiverse/networking/p2p/proto-go/message.pb.go +846 -0
  35. unaiverse/networking/p2p/unailib.cpython-311-darwin.so +0 -0
  36. unaiverse/stats.py +1481 -0
  37. unaiverse/streamlib/__init__.py +15 -0
  38. unaiverse/streamlib/streamlib.py +210 -0
  39. unaiverse/streams.py +776 -0
  40. unaiverse/utils/__init__.py +16 -0
  41. unaiverse/utils/lone_wolf.json +24 -0
  42. unaiverse/utils/misc.py +310 -0
  43. unaiverse/utils/sandbox.py +293 -0
  44. unaiverse/utils/server.py +435 -0
  45. unaiverse/world.py +335 -0
  46. unaiverse-0.1.11.dist-info/METADATA +367 -0
  47. unaiverse-0.1.11.dist-info/RECORD +50 -0
  48. unaiverse-0.1.11.dist-info/WHEEL +6 -0
  49. unaiverse-0.1.11.dist-info/licenses/LICENSE +43 -0
  50. unaiverse-0.1.11.dist-info/top_level.txt +1 -0
@@ -0,0 +1,871 @@
1
+ """
2
+ █████ █████ ██████ █████ █████ █████ █████ ██████████ ███████████ █████████ ██████████
3
+ ░░███ ░░███ ░░██████ ░░███ ░░███ ░░███ ░░███ ░░███░░░░░█░░███░░░░░███ ███░░░░░███░░███░░░░░█
4
+ ░███ ░███ ░███░███ ░███ ██████ ░███ ░███ ░███ ░███ █ ░ ░███ ░███ ░███ ░░░ ░███ █ ░
5
+ ░███ ░███ ░███░░███░███ ░░░░░███ ░███ ░███ ░███ ░██████ ░██████████ ░░█████████ ░██████
6
+ ░███ ░███ ░███ ░░██████ ███████ ░███ ░░███ ███ ░███░░█ ░███░░░░░███ ░░░░░░░░███ ░███░░█
7
+ ░███ ░███ ░███ ░░█████ ███░░███ ░███ ░░░█████░ ░███ ░ █ ░███ ░███ ███ ░███ ░███ ░ █
8
+ ░░████████ █████ ░░█████░░████████ █████ ░░███ ██████████ █████ █████░░█████████ ██████████
9
+ ░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░ ░░░░░ ░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░░░
10
+ A Collectionless AI Project (https://collectionless.ai)
11
+ Registration/Login: https://unaiverse.io
12
+ Code Repositories: https://github.com/collectionlessai/
13
+ Main Developers: Stefano Melacci (Project Leader), Christian Di Maio, Tommaso Guidi
14
+ """
15
+ import os
16
+ import time
17
+ import logging
18
+ import threading
19
+ from .messages import Msg
20
+ from .lib_types import TypeInterface
21
+ from typing import Optional, List, Dict, Any, TYPE_CHECKING
22
+
23
+ # Conditional import for type hinting to avoid circular dependencies
24
+ if TYPE_CHECKING:
25
+ try:
26
+ from unaiverse.networking.p2p.golibp2p import GoLibP2P # Assuming this class loads the library
27
+ except ImportError:
28
+ pass
29
+
30
+ logger = logging.getLogger('P2P')
31
+
32
+
33
+ class P2PError(Exception):
34
+ """Custom exception class for P2P library errors."""
35
+ pass
36
+
37
+
38
+ class P2P:
39
+ """
40
+ Python wrapper for the Go libp2p shared library.
41
+
42
+ This class initializes a libp2p node, provides methods to interact with the
43
+ p2p network (connect, send/receive messages, pubsub, relay), and manages
44
+ the lifecycle of the underlying Go node.
45
+
46
+ Attributes:
47
+ libp2p (LibP2P): Static class attribute holding the loaded Go library instance.
48
+ Must be set before instantiating P2P. Example: P2P.libp2p = LibP2P()
49
+ Properties:
50
+ peer_id (str): The Peer ID of the initialized local node.
51
+ addresses (Optional[List[str]]): List of multiaddresses the local node is listening on.
52
+ is_public (bool): Whether the node is publicly reachable.
53
+ peer_map (Dict[str, Any]): A dictionary to potentially store information about connected peers (managed manually or by polling thread).
54
+ """
55
+
56
+ # --- Class-level state ---
57
+ libp2p: 'GoLibP2P' # Static variable for the loaded Go library
58
+ _type_interface: 'TypeInterface' # Shared type interface for all instances
59
+
60
+ # --- Config class variables for configuration ---
61
+ _MAX_INSTANCES = 32
62
+ _MAX_NUM_CAHNNELS = 100
63
+ _MAX_QUEUE_PER_CHANNEL = 50
64
+ _MAX_MESSAGE_SIZE = 50 * 1024 * 1024 # 50 MB
65
+
66
+ # --- Class-level tracking ---
67
+ _library_initialized = False
68
+ _initialize_lock = threading.Lock()
69
+ _instance_ids = [False, ] * _MAX_INSTANCES
70
+ _instance_lock = threading.Lock()
71
+
72
+ @classmethod
73
+ def setup_library(cls,
74
+ max_instances: Optional[int] = None,
75
+ max_channels: Optional[int] = None,
76
+ max_queue_per_channel: Optional[int] = None,
77
+ max_message_size: Optional[int] = None,
78
+ enable_logging: bool = False) -> None:
79
+ """
80
+ Initializes the underlying Go library. Must be called once. This is called automatically.
81
+ """
82
+ with cls._initialize_lock:
83
+ if cls._library_initialized:
84
+ logger.warning("P2P library is already initialized. Skipping setup.")
85
+ return
86
+
87
+ if not hasattr(cls, 'libp2p') or cls.libp2p is None:
88
+ raise P2PError("Library not loaded before setup. Check package __init__.py")
89
+
90
+ # Configure Python logging based on the flag
91
+ if not enable_logging:
92
+ logger.setLevel(logging.CRITICAL)
93
+ _log_config = {}
94
+ else:
95
+ logger.setLevel(logging.INFO)
96
+ _log_config = {
97
+ 'net/identify': 'info',
98
+ 'unailib': 'info',
99
+ 'autotls': 'info',
100
+ 'p2p-forge': 'info',
101
+ 'nat': 'info',
102
+ 'basichost': 'info',
103
+ 'p2p-circuit': 'info',
104
+ 'relay': 'info',
105
+ 'p2p-holepunch': 'info',
106
+ }
107
+
108
+ logger.info("🐍 Setting up and initializing P2P library core with user settings...")
109
+ cls._type_interface = TypeInterface(cls.libp2p)
110
+
111
+ # Use provided arguments or fall back to class defaults
112
+ _max_instances = max_instances if max_instances is not None else cls._MAX_INSTANCES
113
+ _max_channels = max_channels if max_channels is not None else cls._MAX_NUM_CAHNNELS
114
+ _max_queue = max_queue_per_channel if max_queue_per_channel is not None else cls._MAX_QUEUE_PER_CHANNEL
115
+ _max_msg_size = max_message_size if max_message_size is not None else cls._MAX_MESSAGE_SIZE
116
+
117
+ # Update class attributes if they were overridden
118
+ cls._MAX_INSTANCES = _max_instances
119
+ cls._instance_ids = [False, ] * _max_instances # Resize the tracking list
120
+
121
+ # Call the Go function to set up its internal state
122
+ logger.info("🐍 Initializing Go library core...")
123
+ cls.libp2p.InitializeLibrary(
124
+ cls._type_interface.to_go_int(_max_instances),
125
+ cls._type_interface.to_go_int(_max_channels),
126
+ cls._type_interface.to_go_int(_max_queue),
127
+ cls._type_interface.to_go_int(_max_msg_size),
128
+ cls._type_interface.to_go_json(_log_config)
129
+ )
130
+
131
+ cls._library_initialized = True
132
+ logger.info("✅ Go library initialized successfully.")
133
+
134
+ def __init__(self,
135
+ identity_dir: str,
136
+ port: int = 0,
137
+ ips: List[str] = None,
138
+ enable_relay_client: bool = True,
139
+ enable_relay_service: bool = False,
140
+ knows_is_public: bool = False,
141
+ max_connections: int = 1000,
142
+ enable_tls: bool = False,
143
+ domain_name: Optional[str] = None,
144
+ tls_cert_path: Optional[str] = None,
145
+ tls_key_path: Optional[str] = None,
146
+ dht_enabled: bool = False,
147
+ dht_mode: str = "client",
148
+ dht_keep: bool = True
149
+ ) -> None:
150
+ """
151
+ Initializes and starts a new libp2p node.
152
+
153
+ Args:
154
+ identity_dir: Directory path to load/store the node's private key and certificates.
155
+ port: The (first) TCP port to listen on (0 for random).
156
+ ips: A list of specific IP addresses to listen on. Defaults to ["0.0.0.0"].
157
+ enable_relay_client: Enable listening to relayed connections for this node.
158
+ enable_relay_service: Enable relay service capabilities for this node.
159
+ knows_is_public: If you already know that the node is public this forces its public reachability. Otherwise it tries every possible attempt to make the node publicly reachable (UPnP, HolePunching, AutoNat via DHT...).
160
+ max_connections: Maximum number of connections this node can handle.
161
+ enable_tls: Whether to enable AutoTLS certificate management (requires internet access).
162
+ domain_name: Optional domain name for TLS certificate (required if enable_tls is True).
163
+ tls_cert_path: Optional path to a custom TLS certificate file (PEM format).
164
+ tls_key_path: Optional path to a custom TLS private key file (PEM format).
165
+
166
+ Raises:
167
+ P2PError: If the node creation fails in the Go library.
168
+ AttributeError: If P2P.libp2p has not been set before instantiation.
169
+ """
170
+
171
+ # --- CRITICAL: Check if library is initialized ---
172
+ if not P2P._library_initialized:
173
+ raise P2PError("P2P library not set up. Call P2P.setup_library() before creating an instance.")
174
+
175
+ # Assign instance ID
176
+ assigned_instance_id = -1
177
+ with P2P._instance_lock:
178
+ for _instance_id, i in enumerate(self._instance_ids):
179
+ if not i:
180
+ self._instance_ids[_instance_id] = True
181
+ assigned_instance_id = _instance_id
182
+ break
183
+ if assigned_instance_id == -1:
184
+ raise P2PError(
185
+ f"Cannot create new P2P instance: Maximum number of instances "
186
+ f"({P2P._MAX_INSTANCES})."
187
+ )
188
+
189
+ self._instance: int = assigned_instance_id
190
+ logger.info(f"🚀 Attempting to initialize P2P Node with auto-assigned Instance ID: {self._instance}")
191
+
192
+ os.makedirs(identity_dir, exist_ok=True)
193
+
194
+ self._enable_relay_client = enable_relay_client or enable_relay_service
195
+ self._peer_id: Optional[str] = None
196
+
197
+ # TLS Validation Logic
198
+ has_custom_tls_args = (tls_cert_path is not None) or (tls_key_path is not None) or (domain_name is not None)
199
+ if has_custom_tls_args:
200
+ if domain_name is None or tls_cert_path is None or tls_key_path is None:
201
+ raise ValueError("Custom TLS requires 'domain_name', 'tls_cert_path' and 'tls_key_path'.")
202
+
203
+ use_auto_tls = enable_tls and not has_custom_tls_args
204
+
205
+ # --- Build Configuration JSON ---
206
+ node_config = {
207
+ "identity_dir": identity_dir,
208
+ "predefined_port": port,
209
+ "listen_ips": ips,
210
+ "max_connections": max_connections,
211
+ "relay": {
212
+ "enable_client": self._enable_relay_client,
213
+ "enable_service": enable_relay_service,
214
+ },
215
+ "tls": {
216
+ "auto_tls": use_auto_tls,
217
+ "domain": domain_name if domain_name else "",
218
+ "cert_path": tls_cert_path if tls_cert_path else "",
219
+ "key_path": tls_key_path if tls_key_path else "",
220
+ },
221
+ "network": {
222
+ "force_public": knows_is_public,
223
+ },
224
+ "dht": {
225
+ "enabled": dht_enabled,
226
+ "mode": dht_mode if dht_enabled else "",
227
+ "keep": dht_keep
228
+ }
229
+ }
230
+
231
+ logger.info(f"🐍 Creating Node (Instance ID: {self._instance})...")
232
+ try:
233
+
234
+ # Call the Go function
235
+ result_ptr = P2P.libp2p.CreateNode(
236
+ P2P._type_interface.to_go_int(self._instance),
237
+ P2P._type_interface.to_go_json(node_config),
238
+ )
239
+ result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
240
+
241
+ if result is None:
242
+ err_msg = "Received null result from Go CreateNode."
243
+ logger.error(f"[Instance {self._instance}] {err_msg}")
244
+ raise P2PError(f"[Instance {self._instance}] {err_msg}")
245
+ if result.get('state') == "Error":
246
+ err_msg = result.get('message', 'Unknown Go error on CreateNode')
247
+ logger.error(f"[Instance {self._instance}] Go error: {err_msg}")
248
+ raise P2PError(f"[Instance {self._instance}] Failed to create node: {err_msg}")
249
+
250
+ message_data = result.get('message')
251
+ initial_addresses = message_data.get("addresses", [])
252
+ self._is_public = message_data.get("isPublic", False)
253
+
254
+ # Check teh returned data
255
+ if not isinstance(initial_addresses, list) or not initial_addresses:
256
+ err_msg = "Received empty or invalid addresses list from Go CreateNode."
257
+ logger.error(f"[Instance {self._instance}] {err_msg}")
258
+ raise P2PError(f"[Instance {self._instance}] {err_msg}")
259
+
260
+ self._peer_id = initial_addresses[0].split("/")[-1]
261
+
262
+ logger.info(f"✅ [Instance {self._instance}] Node created with ID: {self._peer_id}")
263
+ logger.info(f"👂 [Instance {self._instance}] Listening on: {initial_addresses}")
264
+ logger.info(f"🌐 [Instance {self._instance}] Publicly reachable: {self._is_public}")
265
+
266
+ logger.info(f"🎉 [Instance {self._instance}] Node initialized successfully.")
267
+
268
+ except Exception as e:
269
+ logger.error(f"❌ [Instance {self._instance}] Node creation failed: {e}")
270
+
271
+ # Reclaim the instance ID using the _instance_ids list
272
+ if self._instance != -1: # Check if an ID was actually assigned
273
+ with P2P._instance_lock:
274
+ P2P._instance_ids[self._instance] = False
275
+ logger.info(f"[Instance {self._instance}] Reclaimed instance ID {self._instance} due to creation failure.")
276
+ raise # Re-raise the exception that caused the failure
277
+
278
+ logger.info("🎉 Node created successfully and background polling started.")
279
+
280
+ # --- Core P2P Operations ---
281
+
282
+ def connect_to(self, multiaddrs: list[str]) -> Dict[str, Any]:
283
+ """
284
+ Establishes a connection with a remote peer.
285
+
286
+ Args:
287
+ multiaddrs: The list of multiaddress strings of the peer to try to connect to.
288
+
289
+ Returns:
290
+ A dictionary containing the connected peer's AddrInfo (ID and Addrs).
291
+
292
+ Raises:
293
+ P2PError: If the connection fails.
294
+ ValueError: If the multiaddr is invalid.
295
+ """
296
+ if not multiaddrs or not isinstance(multiaddrs, list):
297
+ logger.error("Invalid multiaddr provided.")
298
+ raise ValueError("Invalid multiaddr provided.")
299
+ dest_peer_id = multiaddrs[0].split('/')[-1]
300
+ logger.info(f"📞 Attempting to connect to: {dest_peer_id}...")
301
+ try:
302
+ result_ptr = P2P.libp2p.ConnectTo(
303
+ P2P._type_interface.to_go_int(self._instance),
304
+ P2P._type_interface.to_go_json(multiaddrs)
305
+ )
306
+ result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
307
+
308
+ if result is None:
309
+ logger.error("Failed to connect to peer, received null result.")
310
+ raise P2PError("Failed to connect to peer, received null result.")
311
+ if result.get('state') == "Error":
312
+ logger.error(f"Failed to connect to peer '{dest_peer_id}': {result.get('message', 'Unknown Go error')}")
313
+ raise P2PError(f"Failed to connect to peer '{dest_peer_id}': {result.get('message', 'Unknown Go error')}")
314
+
315
+ peer_info = result.get('message', {})
316
+ logger.info(f"✅ Connection initiated to peer: {peer_info.get('ID', dest_peer_id)}") # Use ID if available
317
+
318
+ return peer_info
319
+
320
+ except Exception as e:
321
+ logger.error(f"❌ Connection to {dest_peer_id} failed: {e}")
322
+ raise P2PError(f"Connection to {dest_peer_id} failed") from e
323
+
324
+ def disconnect_from(self, peer_id: str) -> None:
325
+ """
326
+ Closes connections to a specific peer and removes tracking.
327
+
328
+ Args:
329
+ peer_id: The Peer ID string of the peer to disconnect from.
330
+
331
+ Raises:
332
+ P2PError: If disconnecting fails.
333
+ ValueError: If the peer_id is invalid.
334
+ """
335
+ if not peer_id or not isinstance(peer_id, str):
336
+ logger.error("Invalid Peer ID provided.")
337
+ raise ValueError("Invalid Peer ID provided.")
338
+
339
+ # Basic peer ID format check (Qm... or 12D3...)
340
+ if not (peer_id.startswith("Qm") or peer_id.startswith("12D3")):
341
+ logger.warning(f"⚠️ Warning: Peer ID '{peer_id}' does not look like a standard v0 or v1 ID.")
342
+
343
+ logger.info(f"🔌 Attempting to disconnect from peer: {peer_id}...")
344
+ try:
345
+ result_ptr = P2P.libp2p.DisconnectFrom(
346
+ P2P._type_interface.to_go_int(self._instance),
347
+ P2P._type_interface.to_go_string(peer_id)
348
+ )
349
+ result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
350
+
351
+ if result is None:
352
+ logger.error("Failed to disconnect from peer, received null result.")
353
+ raise P2PError("Failed to disconnect from peer, received null result.")
354
+
355
+ if result.get('state') == "Error":
356
+ logger.error(f"Failed to disconnect from peer '{peer_id}': {result.get('message', 'Unknown Go error')}")
357
+ raise P2PError(f"Failed to disconnect from peer '{peer_id}': {result.get('message', 'Unknown Go error')}")
358
+
359
+ logger.info(f"✅ Successfully disconnected from {peer_id}")
360
+
361
+ except Exception as e:
362
+ logger.error(f"❌ Disconnection from {peer_id} failed: {e}")
363
+ raise P2PError(f"Disconnection from {peer_id} failed") from e
364
+
365
+ def send_message_to_peer(self, channel: str, msg_bytes: bytes) -> None:
366
+ """
367
+ Sends a direct message to a specific peer.
368
+
369
+ Args:
370
+ channel: The string identifying the channel for the communication.
371
+ msg_bytes: The message to send (bytes).
372
+
373
+ Raises:
374
+ P2PError: If message sending fails (based on return code).
375
+ ValueError: If inputs are invalid.
376
+ TypeError: If data is not bytes.
377
+ """
378
+ if not channel or not isinstance(channel, str):
379
+ logger.error("Invalid channel provided.")
380
+ raise ValueError("Invalid channel provided.")
381
+
382
+ # Serialize the entire message object to bytes using Protobuf.
383
+ payload_len = len(msg_bytes)
384
+ peer_id = channel.split("::dm:")[1].split('-')[0] # Extract Peer ID from channel format
385
+
386
+ # Call the Go function
387
+ try:
388
+ result_ptr = P2P.libp2p.SendMessageToPeer(
389
+ P2P._type_interface.to_go_int(self._instance),
390
+ P2P._type_interface.to_go_string(channel),
391
+ P2P._type_interface.to_go_bytes(msg_bytes), # Pass bytes directly
392
+ P2P._type_interface.to_go_int(payload_len),
393
+ )
394
+ result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
395
+
396
+ if result is None:
397
+ logger.error(f"Failed to send direct message to {peer_id}, received null result.")
398
+ raise P2PError(f"Failed to send direct message to {peer_id}, received null result.")
399
+
400
+ if result.get('state') == "Error":
401
+ logger.error(f"Failed to send direct message to '{peer_id}': {result.get('message', 'Unknown Go error')}")
402
+ raise P2PError(f"Failed to send direct message to '{peer_id}': {result.get('message', 'Unknown Go error')}")
403
+
404
+ logger.info(f"✅ Successfully sent direct message to {peer_id[-5:]}.")
405
+
406
+ except Exception as e:
407
+ logger.error(f"❌ Sending direct message to {peer_id} failed: {e}")
408
+ raise P2PError(f"Sending direct message to {peer_id} failed") from e
409
+
410
+ def broadcast_message(self, channel: str, msg_bytes: bytes) -> None:
411
+ """
412
+ Broadcasts a message using PubSub to the node's own topic.
413
+ Peers subscribed to this node's Peer ID topic will receive it.
414
+
415
+ Args:
416
+ channel: The Channel for this topic (e.g., owner_peer_id::ps:topic_name).
417
+ msg_bytes: The message to send (bytes).
418
+
419
+ Raises:
420
+ P2PError: If broadcasting fails.
421
+ ValueError: If inputs are invalid.
422
+ TypeError: If data is not bytes.
423
+ """
424
+ if not channel or not isinstance(channel, str):
425
+ raise ValueError("Invalid channel provided.")
426
+
427
+ # Serialize the entire message object to bytes using Protobuf.
428
+ payload_len = len(msg_bytes)
429
+
430
+ # Call SendMessageToPeer with an empty peer_id string for broadcast
431
+ try:
432
+ result_ptr = P2P.libp2p.SendMessageToPeer(
433
+ P2P._type_interface.to_go_int(self._instance),
434
+ P2P._type_interface.to_go_string(channel),
435
+ P2P._type_interface.to_go_bytes(msg_bytes),
436
+ P2P._type_interface.to_go_int(payload_len),
437
+ )
438
+
439
+ result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
440
+
441
+ if result is None:
442
+ logger.error(f"Failed to broadcast message on channel {channel}, received null result.")
443
+ raise P2PError(f"Failed to broadcast message on channel {channel}, received null result.")
444
+
445
+ if result.get('state') == "Error":
446
+ logger.error(f"Failed to broadcast message on channel '{channel}': {result.get('message', 'Unknown Go error')}")
447
+ raise P2PError(f"Failed to broadcast message on channel '{channel}': {result.get('message', 'Unknown Go error')}")
448
+
449
+ except Exception as e:
450
+ logger.error(f"❌ Broadcasting to {channel} failed: {e}")
451
+ raise P2PError(f"Broadcasting to {channel} failed") from e
452
+
453
+ logger.info(f"✅ Successfully broadcasted message on channel {channel}.")
454
+
455
+ def pop_messages(self) -> List[bytes]:
456
+ """
457
+ Retrieves and removes the first message from the queue of each channel for this node instance.
458
+
459
+ Returns:
460
+ A list of byte arrays (messages). Returns an empty list if no messages were available.
461
+
462
+ Raises:
463
+ P2PError: If popping messages failed internally in Go, or if data
464
+ conversion fails for any message.
465
+ """
466
+ logger.debug(f"[Instance {self._instance}] Popping message(s)...")
467
+ try:
468
+ go_instance_c = P2P._type_interface.to_go_int(self._instance)
469
+
470
+ result_ptr = P2P.libp2p.PopMessages(go_instance_c)
471
+
472
+ # From_go_ptr_to_json should handle freeing result_ptr
473
+ raw_result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
474
+
475
+ if raw_result is None:
476
+
477
+ # This indicates an issue with the C call or JSON conversion in TypeInterface
478
+ logger.error(f"[Instance {self._instance}] PopMessages: Received null/invalid result from TypeInterface.")
479
+ raise P2PError(f"[Instance {self._instance}] PopMessages: Failed to get valid JSON response.")
480
+
481
+ # Check for Go-side error or empty states first
482
+ if isinstance(raw_result, dict):
483
+ state = raw_result.get('state')
484
+ if state == "Empty":
485
+ logger.debug(f"[Instance {self._instance}] PopMessages: Queue is empty.")
486
+ return [] # No messages available
487
+ if state == "Error":
488
+ error_message = raw_result.get('message', 'Unknown Go error during PopMessages')
489
+ logger.error(f"[Instance {self._instance}] PopMessages: {error_message}")
490
+ raise P2PError(f"[Instance {self._instance}] PopMessages: {error_message}")
491
+
492
+ # If it's a dict but not a known state, it's unexpected
493
+ logger.warning(f"[Instance {self._instance}] PopMessages: Unexpected dictionary format: {raw_result}")
494
+ raise P2PError(f"[Instance {self._instance}] PopMessages: Unexpected dictionary response format.")
495
+
496
+ # Expecting a list of messages if not an error/empty dict
497
+ if not isinstance(raw_result, list):
498
+ # This also covers the case where n=0 and Go returns "[]" which json.loads makes a list
499
+ # If it's not a list at this point, it's an unexpected format.
500
+ logger.error(f"[Instance {self._instance}] PopMessages: Unexpected response format, expected a list or "
501
+ f"specific state dictionary. Got: {type(raw_result)}")
502
+ raise P2PError(f"[Instance {self._instance}] PopMessages: Unexpected response format.")
503
+
504
+ return raw_result
505
+
506
+ except P2PError: # Re-raise P2PError directly
507
+ raise
508
+ except Exception as e:
509
+
510
+ # Catch potential JSON parsing errors from TypeInterface or other unexpected errors
511
+ logger.error(f"[Instance {self._instance}] ❌ Error during pop_message: {e}")
512
+ raise P2PError(f"[Instance {self._instance}] Unexpected error during pop_message: {e}") from e
513
+
514
+ # --- PubSub Operations ---
515
+
516
+ def subscribe_to_topic(self, channel: str) -> None:
517
+ """
518
+ Subscribes to a PubSub topic to receive messages.
519
+
520
+ Args:
521
+ channel: The Channel for this topic (e.g., owner_peer_id::ps:topic_name).
522
+
523
+ Raises:
524
+ P2PError: If subscribing fails.
525
+ ValueError: If topic_name is invalid.
526
+ """
527
+ if not channel or not isinstance(channel, str):
528
+ logger.error("Invalid topic name provided.")
529
+ raise ValueError("Invalid topic name provided.")
530
+ logger.info(f"<sub> Subscribing to topic: {channel}...")
531
+ try:
532
+ result_ptr = P2P.libp2p.SubscribeToTopic(
533
+ P2P._type_interface.to_go_int(self._instance),
534
+ P2P._type_interface.to_go_string(channel)
535
+ )
536
+ result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
537
+
538
+ if result is None:
539
+ logger.error("Failed to subscribe to topic, received null result.")
540
+ raise P2PError("Failed to subscribe to topic, received null result.")
541
+ if result.get('state') == "Error":
542
+ logger.error(f"Failed to subscribe to topic '{channel}': {result.get('message', 'Unknown Go error')}")
543
+ raise P2PError(f"Failed to subscribe to topic '{channel}': {result.get('message', 'Unknown Go error')}")
544
+
545
+ logger.info(f"✅ Successfully subscribed to {channel}")
546
+
547
+ except Exception as e:
548
+ logger.error(f"❌ Subscription to {channel} failed: {e}")
549
+ raise P2PError(f"Subscription to {channel} failed") from e
550
+
551
+ def unsubscribe_from_topic(self, channel: str) -> None:
552
+ """
553
+ Unsubscribes from a PubSub topic.
554
+
555
+ Args:
556
+ channel: The Channel for this topic (e.g., owner_peer_id::ps:topic_name).
557
+
558
+ Raises:
559
+ P2PError: If unsubscribing fails.
560
+ ValueError: If topic_name is invalid.
561
+ """
562
+ if not channel or not isinstance(channel, str):
563
+ logger.error("Invalid topic name provided.")
564
+ raise ValueError("Invalid topic name provided.")
565
+ logger.info(f"</sub> Unsubscribing from topic: {channel}...")
566
+ try:
567
+ result_ptr = P2P.libp2p.UnsubscribeFromTopic(
568
+ P2P._type_interface.to_go_int(self._instance),
569
+ P2P._type_interface.to_go_string(channel)
570
+ )
571
+ result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
572
+
573
+ if result is None:
574
+ logger.error("Failed to unsubscribe from topic, received null result.")
575
+ raise P2PError("Failed to unsubscribe from topic, received null result.")
576
+ if result.get('state') == "Error":
577
+ logger.error(f"Failed to unsubscribe from topic '{channel}': {result.get('message', 'Unknown Go error')}")
578
+ raise P2PError(f"Failed to unsubscribe from topic '{channel}': {result.get('message', 'Unknown Go error')}")
579
+
580
+ logger.info(f"✅ Successfully unsubscribed from {channel}")
581
+
582
+ except Exception as e:
583
+ logger.error(f"❌ Unsubscription from {channel} failed: {e}")
584
+ raise P2PError(f"Unsubscription from {channel} failed") from e
585
+
586
+ # --- Relay Operations ---
587
+
588
+ def reserve_on_relay(self, relay_peer_id: str) -> str:
589
+ """
590
+ Attempts to reserve a slot on a specified relay node.
591
+
592
+ Args:
593
+ relay_peer_id: The peerID of the relay node
594
+
595
+ Returns:
596
+ The UTC expiration timestamp of the reservation as an ISO 8601 string.
597
+
598
+ Raises:
599
+ P2PError: If the reservation fails.
600
+ ValueError: If the relay_multiaddr is invalid.
601
+ """
602
+ if not relay_peer_id or not isinstance(relay_peer_id, str):
603
+ logger.error("Invalid relay multiaddr provided.")
604
+ raise ValueError("Invalid relay multiaddr provided.")
605
+ logger.info(f"🅿️ Attempting to reserve slot on relay: {relay_peer_id}...")
606
+ try:
607
+ result_ptr = P2P.libp2p.ReserveOnRelay(
608
+ P2P._type_interface.to_go_int(self._instance),
609
+ P2P._type_interface.to_go_string(relay_peer_id)
610
+ )
611
+ result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
612
+
613
+ if result is None:
614
+ logger.error("Failed to reserve on relay, received null result.")
615
+ raise P2PError("Failed to reserve on relay, received null result.")
616
+ if result.get('state') == "Error":
617
+ logger.error(f"Failed to reserve on relay '{relay_peer_id}': {result.get('message', 'Unknown Go error')}")
618
+ raise P2PError(f"Failed to reserve on relay '{relay_peer_id}': {result.get('message', 'Unknown Go error')}")
619
+
620
+ expiration_utc = result.get('message', {})
621
+ if not isinstance(expiration_utc, str):
622
+ raise P2PError(f"Expected expiration timestamp string, but got {type(expiration_utc)}")
623
+
624
+ logger.info(f"✅ Reservation successful. Expires at: {expiration_utc}")
625
+ return expiration_utc
626
+
627
+ except Exception as e:
628
+ logger.error(f"❌ Reservation on {relay_peer_id} failed: {e}")
629
+ raise P2PError(f"Reservation on {relay_peer_id} failed") from e
630
+
631
+ # --- Node Information ---
632
+
633
+ @property
634
+ def peer_id(self) -> Optional[str]:
635
+ """Returns the Peer ID of the local node."""
636
+ return self._peer_id
637
+
638
+ @property
639
+ def addresses(self) -> List[str]:
640
+ """
641
+ Returns the LIVE list of multiaddresses from the Go engine.
642
+ Since Go caches this via events, this call is instant O(1).
643
+ """
644
+ try:
645
+ return self.get_node_addresses()
646
+ except P2PError as e:
647
+ logger.warning(f"Failed to fetch addresses: {e}")
648
+ return []
649
+
650
+ @property
651
+ def is_public(self) -> Optional[bool]:
652
+ """Returns a boolean stating whether the local node is publicly reachable."""
653
+ return self._is_public
654
+
655
+ @property
656
+ def relay_is_enabled(self) -> bool:
657
+ """Returns whether the relay client functionality is enabled for this node."""
658
+ return self._enable_relay_client
659
+
660
+ def get_node_addresses(self, peer_id: str = "") -> List[str]:
661
+ """
662
+ Gets the known multiaddresses for the local node or a specific peer.
663
+
664
+ Args:
665
+ peer_id: The Peer ID string of the target peer. If empty, gets
666
+ addresses for the local node.
667
+
668
+ Returns:
669
+ A list of multiaddress strings (including the /p2p/PeerID suffix).
670
+
671
+ Raises:
672
+ P2PError: If fetching addresses fails.
673
+ """
674
+ target = "local node" if not peer_id else f"peer {peer_id}"
675
+ logger.info(f"ℹ️ Fetching addresses for {target}...")
676
+ try:
677
+ result_ptr = P2P.libp2p.GetNodeAddresses(
678
+ P2P._type_interface.to_go_int(self._instance),
679
+ P2P._type_interface.to_go_string(peer_id)
680
+ )
681
+ result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
682
+
683
+ if result is None:
684
+ logger.error("Failed to get node addresses, received null result.")
685
+ raise P2PError("Failed to get node addresses, received null result.")
686
+ if result.get('state') == "Error":
687
+ logger.error(f"Failed to get addresses for '{target}': {result.get('message', 'Unknown Go error')}")
688
+ raise P2PError(f"Failed to get addresses for '{target}': {result.get('message', 'Unknown Go error')}")
689
+
690
+ addr_list = result.get('message', [])
691
+ logger.info(f"✅ Found addresses for {target}: {addr_list}")
692
+ return addr_list
693
+
694
+ except Exception as e:
695
+ logger.error(f"❌ Failed to get addresses for {target}: {e}")
696
+ raise P2PError(f"Failed to get addresses for {target}") from e
697
+
698
+ def get_connected_peers_info(self) -> List[Dict[str, Any]]:
699
+ """
700
+ Gets information about currently connected peers from the Go library.
701
+
702
+ Returns:
703
+ A list of dictionaries, each representing a connected peer with
704
+ keys like 'addr_info' (containing 'ID', 'Addrs'), 'connected_at', 'direction', and 'misc'.
705
+
706
+ Raises:
707
+ P2PError: If fetching connected peers fails.
708
+ """
709
+
710
+ # Logger.info("ℹ️ Fetching connected peers info...") # Can be noisy
711
+ try:
712
+
713
+ # GetConnectedPeers takes no arguments in Go
714
+ result_ptr = P2P.libp2p.GetConnectedPeers(P2P._type_interface.to_go_int(self._instance))
715
+ result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
716
+
717
+ if result is None:
718
+ logger.error("Failed to get connected peers, received null result.")
719
+ raise P2PError("Failed to get connected peers, received null result.")
720
+ if result.get('state') == "Error":
721
+ logger.error(f"Failed to get connected peers: {result.get('message', 'Unknown Go error')}")
722
+ raise P2PError(f"Failed to get connected peers: {result.get('message', 'Unknown Go error')}")
723
+
724
+ peers_list = result.get('message', [])
725
+
726
+ # Update internal map (optional)
727
+ # logger.info(f" Connected peers count: {len(peers_list)}") # Can be noisy
728
+ return peers_list
729
+
730
+ except Exception as e:
731
+
732
+ # Avoid crashing the polling thread, just log the error
733
+ logger.error(f"❌ Error fetching connected peers info: {e}")
734
+
735
+ # Optionally raise P2PError(f"Failed to get connected peers info") from e if called directly
736
+ return [] # Return empty list on error during polling
737
+
738
+ def get_rendezvous_peers_info(self) -> Dict[str, Any] | None:
739
+ """
740
+ Gets the full rendezvous state from the Go library, including peers and metadata.
741
+
742
+ Returns:
743
+ - A dictionary representing the RendezvousState (containing 'peers',
744
+ 'update_count', 'last_updated') if an update has been received.
745
+ - None if no rendezvous topic is active or no updates have arrived yet.
746
+
747
+ Raises:
748
+ P2PError: If fetching the state fails in Go.
749
+ """
750
+ try:
751
+ result_ptr = P2P.libp2p.GetRendezvousPeers(P2P._type_interface.to_go_int(self._instance))
752
+ result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
753
+
754
+ if result is None:
755
+ logger.error("Failed to get rendezvous peers, received null result.")
756
+ raise P2PError("Failed to get rendezvous peers, received null result.")
757
+
758
+ state = result.get('state')
759
+ if state == "Empty":
760
+ logger.debug(f"[Instance {self._instance}] GetRendezvousPeers: No rendezvous messages received yet.")
761
+ return None # Return None for the "empty" state
762
+ elif state == "Error":
763
+ error_msg = result.get('message', 'Unknown Go error')
764
+ logger.error(f"Failed to get rendezvous peers: {error_msg}")
765
+ raise P2PError(f"Failed to get rendezvous peers: {error_msg}")
766
+ elif state == "Success":
767
+
768
+ # The message payload is the full RendezvousState object
769
+ rendezvous_state = result.get('message', {})
770
+ return rendezvous_state
771
+ else:
772
+ logger.error(f"[Instance {self._instance}] GetRendezvousPeers: Received invalid state '{state}'.")
773
+ raise P2PError(f"[Instance {self._instance}] GetRendezvousPeers: Received invalid state.")
774
+
775
+ except Exception as e:
776
+
777
+ # Avoid crashing the polling thread, just log the error
778
+ logger.error(f"❌ Error fetching rendezvous peers info: {e}")
779
+
780
+ # Optionally raise P2PError(f"Failed to get rendezvous peers info") from e if called directly
781
+ return [] # Return empty list on error during polling
782
+
783
+ def get_message_queue_length(self) -> int:
784
+ """
785
+ Gets the current number of messages in the incoming queue.
786
+
787
+ Returns:
788
+ The number of messages waiting.
789
+
790
+ Raises:
791
+ P2PError: If querying the length fails (should be rare).
792
+ """
793
+ try:
794
+
795
+ # Call Go function, returns C.int directly
796
+ length_cint = P2P.libp2p.MessageQueueLength(P2P._type_interface.to_go_int(self._instance))
797
+ length = P2P._type_interface.from_go_int(length_cint)
798
+
799
+ # Print(f" Current Message Queue Len: {length}") # Can be noisy
800
+ return length
801
+ except Exception as e:
802
+
803
+ # Avoid crashing polling thread
804
+ logger.error(f"❌ Error fetching message queue length: {e}")
805
+ return -1 # Indicate error
806
+
807
+ # --- Lifecycle Management ---
808
+
809
+ def close(self, close_all: bool = False) -> None:
810
+ """
811
+ Gracefully shuts down the libp2p node and stops background threads.
812
+
813
+ Args:
814
+ close_all: If True, closes all instances of the node. Default is False.
815
+ """
816
+ logger.info("🛑 Closing node...")
817
+
818
+ # 1. Signal background threads to stop
819
+ logger.info(" - Stopping background threads...")
820
+
821
+ # 2. Wait briefly for threads to finish (optional, they are daemons)
822
+ # self._get_connected_peers_thread.join(timeout=2)
823
+ # self._check_message_queue_thread.join(timeout=2)
824
+ # print(" - Background threads signaled.")
825
+
826
+ # 3. Call the Go CloseNode function
827
+ try:
828
+ if close_all:
829
+ result_ptr = P2P.libp2p.CloseNode(P2P._type_interface.to_go_int(-1))
830
+ else:
831
+ result_ptr = P2P.libp2p.CloseNode(P2P._type_interface.to_go_int(self._instance))
832
+ result = P2P._type_interface.from_go_ptr_to_json(result_ptr)
833
+
834
+ if result is None:
835
+ logger.error("Node closure failed: received null result.")
836
+ raise P2PError("Node closure failed: received null result.")
837
+ if result.get('state') == "Error":
838
+ logger.error(f"Node closure failed: {result.get('message', 'Unknown Go error')}")
839
+ raise P2PError(f"Node closure failed: {result.get('message', 'Unknown Go error')}")
840
+
841
+ close_msg = f"Node closed successfully ({'all instances' if close_all else f'instance {str(self._instance)}'})."
842
+ logger.info(f"✅ {close_msg}")
843
+
844
+ except Exception as e:
845
+ logger.error(f"❌ Error closing node: {e}")
846
+ raise P2PError(f"Error closing node: {e}") from e
847
+
848
+ # 4. Clear internal state
849
+ self._peer_id = None
850
+ with P2P._instance_lock:
851
+ if close_all:
852
+
853
+ # Also apply the lock here and use the corrected logic
854
+ P2P._instance_ids = [False] * P2P._MAX_INSTANCES
855
+ logger.info("🐍 All instance slots have been marked as free.")
856
+ else:
857
+ if self._instance != -1: # Ensure instance was set
858
+ P2P._instance_ids[self._instance] = False
859
+ logger.info(f"🐍 Instance slot {self._instance} has been marked as free.")
860
+
861
+ logger.info("🐍 Python P2P object state cleared.")
862
+
863
+ return close_msg
864
+
865
+ def __enter__(self):
866
+ """Enter context manager."""
867
+ return self
868
+
869
+ def __exit__(self, exc_type, exc_val, exc_tb):
870
+ """Exit context manager, ensuring node closure."""
871
+ self.close()