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