unaiverse 0.1.0__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.

Potentially problematic release.


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

Files changed (45) hide show
  1. unaiverse/__init__.py +19 -0
  2. unaiverse/agent.py +2008 -0
  3. unaiverse/agent_basics.py +1844 -0
  4. unaiverse/clock.py +186 -0
  5. unaiverse/dataprops.py +1209 -0
  6. unaiverse/hsm.py +1880 -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 +680 -0
  16. unaiverse/networking/__init__.py +16 -0
  17. unaiverse/networking/node/__init__.py +18 -0
  18. unaiverse/networking/node/connpool.py +1265 -0
  19. unaiverse/networking/node/node.py +2203 -0
  20. unaiverse/networking/node/profile.py +446 -0
  21. unaiverse/networking/node/tokens.py +79 -0
  22. unaiverse/networking/p2p/__init__.py +259 -0
  23. unaiverse/networking/p2p/golibp2p.py +18 -0
  24. unaiverse/networking/p2p/golibp2p.pyi +135 -0
  25. unaiverse/networking/p2p/lib.go +2495 -0
  26. unaiverse/networking/p2p/lib_types.py +312 -0
  27. unaiverse/networking/p2p/message_pb2.py +63 -0
  28. unaiverse/networking/p2p/messages.py +265 -0
  29. unaiverse/networking/p2p/mylogger.py +77 -0
  30. unaiverse/networking/p2p/p2p.py +963 -0
  31. unaiverse/streamlib/__init__.py +15 -0
  32. unaiverse/streamlib/streamlib.py +210 -0
  33. unaiverse/streams.py +763 -0
  34. unaiverse/utils/__init__.py +16 -0
  35. unaiverse/utils/ask_lone_wolf.json +27 -0
  36. unaiverse/utils/lone_wolf.json +19 -0
  37. unaiverse/utils/misc.py +305 -0
  38. unaiverse/utils/sandbox.py +293 -0
  39. unaiverse/utils/server.py +435 -0
  40. unaiverse/world.py +175 -0
  41. unaiverse-0.1.0.dist-info/METADATA +363 -0
  42. unaiverse-0.1.0.dist-info/RECORD +45 -0
  43. unaiverse-0.1.0.dist-info/WHEEL +5 -0
  44. unaiverse-0.1.0.dist-info/licenses/LICENSE +43 -0
  45. unaiverse-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1265 @@
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 math
16
+ from unaiverse.networking.p2p.messages import Msg
17
+ from unaiverse.networking.p2p.p2p import P2P, P2PError
18
+ from unaiverse.networking.node.tokens import TokenVerifier
19
+
20
+
21
+ class ConnectionPools:
22
+ DEBUG = True
23
+
24
+ def __init__(self, max_connections: int, pool_name_to_p2p_name_and_ratio: dict[str, [str, float]],
25
+ p2p_name_to_p2p: dict[str, P2P], public_key: str | None = None, token: str | None = None):
26
+ """Initializes a new instance of the ConnectionPools class.
27
+
28
+ Args:
29
+ max_connections: The maximum total number of connections allowed across all pools.
30
+ pool_name_to_p2p_name_and_ratio: A dictionary mapping pool names to a list containing the associated P2P network name and its connection ratio.
31
+ p2p_name_to_p2p: A dictionary mapping P2P network names to their corresponding P2P objects.
32
+ public_key: An optional public key for token verification.
33
+ token: An optional initial token for authentication.
34
+
35
+ Returns:
36
+ None.
37
+
38
+ """
39
+ # Common terms: a "pool triple" is [pool_contents, max_connections_in_such_a_pool, p2p_object_of_the_pool
40
+ self.max_con = max_connections
41
+ self.pool_count = len(pool_name_to_p2p_name_and_ratio)
42
+ self.pool_names = list(pool_name_to_p2p_name_and_ratio.keys())
43
+ self.pool_ratios = [p2p_name_and_ratio[1] for p2p_name_and_ratio in pool_name_to_p2p_name_and_ratio.values()]
44
+
45
+ # Indices involving the P2P object or its name
46
+ self.p2p_name_to_p2p = p2p_name_to_p2p
47
+ self.p2p_name_and_pool_name_to_pool_triple = {}
48
+ self.p2p_to_pool_names = {}
49
+
50
+ # Indices rooted around the pool name
51
+ self.pool_name_to_pool_triple = {}
52
+ self.pool_name_to_added_in_last_update = {}
53
+ self.pool_name_to_removed_in_last_update = {}
54
+ self.pool_name_to_peer_infos = {p: {} for p in self.pool_names}
55
+
56
+ # Indices rooted around the peer ID
57
+ self.peer_id_to_pool_name = {}
58
+ self.peer_id_to_p2p = {}
59
+ self.peer_id_to_misc = {}
60
+ self.peer_id_to_token = {}
61
+
62
+ # Token-related stuff, super private
63
+ self.__token = token if token is not None else ""
64
+ self.__token_verifier = TokenVerifier(public_key) if public_key is not None else None
65
+
66
+ # Checking
67
+ for p2p_name_and_ratio in pool_name_to_p2p_name_and_ratio.values():
68
+ assert p2p_name_and_ratio[0] in self.p2p_name_to_p2p, f"Cannot find p2p named {p2p_name_and_ratio[0]} "
69
+ assert self.max_con >= len(self.pool_names), "Too small number of max connections"
70
+ assert sum([x for x in self.pool_ratios if x > 0]) == 1.0, "Pool ratios must sum to 1.0"
71
+
72
+ # Preparing the pool triples
73
+ self.pool_name_to_pool_triple = \
74
+ {k: [set(), 0, self.p2p_name_to_p2p[pool_name_to_p2p_name_and_ratio[k][0]]] for k in self.pool_names}
75
+ num_zero_ratio_pools = len([x for x in self.pool_ratios if x == 0])
76
+ assert num_zero_ratio_pools <= self.max_con, "Cannot create pools given the provided max connection count"
77
+
78
+ # Edit: to solve the teacher not engaging with more than two students.
79
+ pools_max_sizes = {k: max(math.floor(self.pool_ratios[i] * (self.max_con - num_zero_ratio_pools)),
80
+ 1 if self.pool_ratios[i] >= 0. else 0)
81
+ for i, k in enumerate(self.pool_names)}
82
+
83
+ # Pools_max_sizes = {k: self.max_con for k in self.pool_names}
84
+
85
+ # Fixing sizes
86
+ tot = 0
87
+ for i, (k, v) in enumerate(pools_max_sizes.items()):
88
+ assert v > 0 or self.pool_ratios[i] < 0, "Cannot create pools given the provided max connection count"
89
+
90
+ # Edit: to solve the teacher not engaging with more than two students.
91
+ tot += v
92
+ assert tot <= self.max_con, \
93
+ "Cannot create pools given the provided max connection count"
94
+ pools_max_sizes[self.pool_names[-1]] += (self.max_con - tot)
95
+
96
+ # Storing fixed sizes in the previously created pool triples & building additional index
97
+ for pool_name, pool_contents_max_con_and_p2p in self.pool_name_to_pool_triple.items():
98
+ pool_contents_max_con_and_p2p[1] = pools_max_sizes[pool_name] # Fixing the second element of the triple
99
+
100
+ pool, _, p2p = pool_contents_max_con_and_p2p
101
+ p2p_name = None
102
+ for k, v in self.p2p_name_to_p2p.items():
103
+ if v == p2p:
104
+ p2p_name = k
105
+ break
106
+ if p2p_name not in self.p2p_name_and_pool_name_to_pool_triple:
107
+ self.p2p_name_and_pool_name_to_pool_triple[p2p_name] = {}
108
+ self.p2p_to_pool_names[p2p] = []
109
+ self.p2p_name_and_pool_name_to_pool_triple[p2p_name][pool_name] = (
110
+ pool_contents_max_con_and_p2p)
111
+ self.p2p_to_pool_names[p2p].append(pool_name)
112
+
113
+ def __str__(self):
114
+ """Returns a human-readable string representation of the connection pools' status.
115
+
116
+ Args:
117
+ None.
118
+
119
+ Returns:
120
+ A formatted string showing the number of connections and peer IDs in each pool.
121
+ """
122
+ max_len = max(len(s) for s in self.pool_names)
123
+ s = f"[ConnectionPool] max_con={self.max_con}, pool_count={self.pool_count}"
124
+ for k, (pool, max_con, _) in self.pool_name_to_pool_triple.items():
125
+ kk = k + ","
126
+ s += f"\n\tpool={kk.ljust(max_len + 1)}\tmax_con={max_con},\tcurrent_con={len(pool)},\tpeer_ids="
127
+ ss = "("
128
+ first = True
129
+ for c in pool:
130
+ if not first:
131
+ ss += ", "
132
+ ss += c
133
+ first = False
134
+ ss += ")"
135
+ s += ss
136
+ return s
137
+
138
+ def __getitem__(self, p2p_name):
139
+ """Retrieves a P2P object by its name.
140
+
141
+ Args:
142
+ p2p_name: The name of the P2P network.
143
+
144
+ Returns:
145
+ The P2P object.
146
+ """
147
+ return self.p2p_name_to_p2p[p2p_name]
148
+
149
+ def conn_routing_fcn(self, connected_peer_infos: list, p2p: P2P):
150
+ """A placeholder function that must be implemented to route connected peers to the correct pool.
151
+
152
+ Args:
153
+ connected_peer_infos: A list of dictionaries containing information about connected peers.
154
+ p2p: The P2P network object from which the peers were connected.
155
+
156
+ Returns:
157
+ A dictionary mapping pool names to a dictionary of peer IDs and their information.
158
+ """
159
+ raise NotImplementedError("You must implement conn_routing_fcn!")
160
+
161
+ @staticmethod
162
+ def __connect(p2p: P2P, addresses: list[str]):
163
+ """Establishes a connection to a peer via a P2P network.
164
+
165
+ Args:
166
+ p2p: The P2P network object to use for the connection.
167
+ addresses: A list of addresses of the peer to connect to.
168
+
169
+ Returns:
170
+ A tuple containing the peer ID and a boolean indicating if the connection was established through a relay.
171
+ """
172
+ if ConnectionPools.DEBUG:
173
+ print(f"[DEBUG CONNECTIONS-POOL] Connecting to {addresses}")
174
+
175
+ try:
176
+ a = []
177
+ for address in addresses:
178
+ if "/tcp/" not in address:
179
+ a.append(address)
180
+ addresses = a
181
+
182
+ winning_addr_info_dict = p2p.connect_to(addresses)
183
+ peer_id = winning_addr_info_dict.get('ID')
184
+ connected_addr_str = winning_addr_info_dict.get('Addrs')[0]
185
+ through_relay = '/p2p-circuit/' in connected_addr_str
186
+ if ConnectionPools.DEBUG:
187
+ print(f"[DEBUG CONNECTIONS-POOL] Connected to peer {peer_id} via {connected_addr_str} "
188
+ f"(through relay: {through_relay})")
189
+
190
+ return peer_id, through_relay
191
+ except P2PError:
192
+ if ConnectionPools.DEBUG:
193
+ print(f"[DEBUG CONNECTIONS-POOL] Connection failed!")
194
+ return None, False
195
+
196
+ @staticmethod
197
+ def disconnect(p2p: P2P, peer_id: str):
198
+ """Disconnects from a specific peer on a P2P network.
199
+
200
+ Args:
201
+ p2p: The P2P network object to use for disconnection.
202
+ peer_id: The peer ID to disconnect from.
203
+
204
+ Returns:
205
+ True if the disconnection is successful, otherwise False.
206
+ """
207
+ try:
208
+ p2p.disconnect_from(peer_id)
209
+ except P2PError:
210
+ return False
211
+ return True
212
+
213
+ def set_token(self, token: str):
214
+ """Sets the authentication token for the connection pools.
215
+
216
+ Args:
217
+ token: The new token string.
218
+ """
219
+ self.__token = token
220
+
221
+ def verify_token(self, token: str, peer_id: str):
222
+ """Verifies a received token using the provided public key.
223
+
224
+ Args:
225
+ token: The token string to verify.
226
+ peer_id: The peer ID associated with the token.
227
+
228
+ Returns:
229
+ A tuple containing the node ID and CV hash if the token is valid, otherwise None.
230
+ """
231
+ if self.__token_verifier is None:
232
+ return None
233
+ else:
234
+ node_id, cv_hash = self.__token_verifier.verify_token(token, p2p_peer=peer_id)
235
+ return node_id, cv_hash # If the verification fails, this is None, None
236
+
237
+ def connect(self, addresses: list[str], p2p_name: str):
238
+ """Connects to a peer on a specified P2P network.
239
+
240
+ Args:
241
+ addresses: A list of addresses of the peer to connect to.
242
+ p2p_name: The name of the P2P network to use.
243
+
244
+ Returns:
245
+ A tuple containing the peer ID of the connected peer and a boolean indicating if a relay was used.
246
+ """
247
+ p2p = self.p2p_name_to_p2p[p2p_name]
248
+
249
+ # Connecting
250
+ peer_id, through_relay = ConnectionPools.__connect(p2p, addresses)
251
+ return peer_id, through_relay
252
+
253
+ def add(self, peer_info: dict, pool_name: str):
254
+ """Adds a connected peer to a specified connection pool.
255
+
256
+ Args:
257
+ peer_info: A dictionary containing information about the peer.
258
+ pool_name: The name of the pool to add the peer to.
259
+
260
+ Returns:
261
+ True if the peer is successfully added, otherwise False.
262
+ """
263
+ peer_id = peer_info['id']
264
+ pool, max_size, p2p = self.pool_name_to_pool_triple[pool_name]
265
+ if len(pool) < max_size:
266
+
267
+ # "hoping" peer IDs are unique, and stopping duplicate cases
268
+ if peer_id in self.peer_id_to_pool_name and self.peer_id_to_pool_name[peer_id] != pool_name:
269
+ return False
270
+
271
+ self.peer_id_to_pool_name[peer_id] = pool_name
272
+ self.peer_id_to_p2p[peer_id] = p2p
273
+
274
+ # Setting 'misc' field (default is 0, where 0 means public)
275
+ peer_info['misc'] = self.peer_id_to_misc.get(peer_id, 0)
276
+
277
+ # Storing (only)
278
+ pool.add(peer_id)
279
+ self.pool_name_to_peer_infos[pool_name][peer_id] = peer_info
280
+ return True
281
+ else:
282
+ return False
283
+
284
+ def remove(self, peer_id: str):
285
+ """Removes a peer from its connection pool and disconnects from it.
286
+
287
+ Args:
288
+ peer_id: The peer ID to remove.
289
+
290
+ Returns:
291
+ True if the peer is successfully removed, otherwise False.
292
+ """
293
+ if peer_id in self.peer_id_to_pool_name:
294
+ pool_name = self.peer_id_to_pool_name[peer_id]
295
+ pool, _, p2p = self.pool_name_to_pool_triple[pool_name]
296
+
297
+ # Disconnecting
298
+ disc = ConnectionPools.disconnect(p2p, peer_id)
299
+ pool.remove(peer_id)
300
+ del self.pool_name_to_peer_infos[pool_name][peer_id]
301
+ del self.peer_id_to_pool_name[peer_id]
302
+ del self.peer_id_to_p2p[peer_id]
303
+ if peer_id in self.peer_id_to_misc:
304
+ del self.peer_id_to_misc[peer_id]
305
+ if peer_id in self.peer_id_to_token:
306
+ del self.peer_id_to_token[peer_id]
307
+ return disc
308
+ else:
309
+ return False
310
+
311
+ def get_all_connected_peer_infos(self, pool_name: str):
312
+ """Retrieves a list of peer information dictionaries for a given pool.
313
+
314
+ Args:
315
+ pool_name: The name of the pool to query.
316
+
317
+ Returns:
318
+ A list of dictionaries, each containing information about a peer in the pool.
319
+ """
320
+ return list(self.pool_name_to_peer_infos[pool_name].values())
321
+
322
+ def get_pool_status(self):
323
+ """Returns a dictionary showing the set of peer IDs in each pool.
324
+
325
+ Args:
326
+ None.
327
+
328
+ Returns:
329
+ A dictionary mapping pool names to the set of peer IDs in that pool.
330
+ """
331
+ return {k: v[0] for k, v in self.pool_name_to_pool_triple.items()}
332
+
333
+ def get_all_connected_peer_ids(self):
334
+ """Retrieves a list of all peer IDs currently connected across all pools.
335
+
336
+ Args:
337
+ None.
338
+
339
+ Returns:
340
+ A list of all connected peer IDs.
341
+ """
342
+ return list(self.peer_id_to_pool_name.keys())
343
+
344
+ def update(self):
345
+ """Refreshes the connection pools by checking for new and lost connections.
346
+
347
+ Args:
348
+ None.
349
+
350
+ Returns:
351
+ A tuple containing two dictionaries: one for newly added peers and one for removed peers, both keyed by
352
+ pool name.
353
+ """
354
+ self.pool_name_to_added_in_last_update = {}
355
+ self.pool_name_to_removed_in_last_update = {}
356
+
357
+ for p2p_name, p2p in self.p2p_name_to_p2p.items():
358
+ connected_peer_infos = p2p.get_connected_peers_info()
359
+
360
+ if connected_peer_infos is not None:
361
+
362
+ # Routing to the right queue / filtering
363
+ pool_name_and_peer_ids_to_peer_info = self.conn_routing_fcn(connected_peer_infos, p2p)
364
+
365
+ # Parsing the generated index
366
+ for pool_name, connected_peer_ids_to_connected_peer_infos \
367
+ in pool_name_and_peer_ids_to_peer_info.items():
368
+ pool, _, pool_p2p = self.p2p_name_and_pool_name_to_pool_triple[p2p_name][pool_name]
369
+ connected_peer_ids = connected_peer_ids_to_connected_peer_infos.keys()
370
+ new_peer_ids = connected_peer_ids - pool
371
+ lost_peer_ids = pool - connected_peer_ids
372
+
373
+ # Clearing disconnected agents
374
+ for lost_peer_id in lost_peer_ids:
375
+ self.pool_name_to_removed_in_last_update.setdefault(pool_name, set()).add(lost_peer_id)
376
+
377
+ # Adding new agents
378
+ for new_peer_id in new_peer_ids:
379
+ peer_info = connected_peer_ids_to_connected_peer_infos[new_peer_id]
380
+ if not self.add(peer_info, pool_name=pool_name):
381
+ break
382
+ self.pool_name_to_added_in_last_update.setdefault(pool_name, set()).add(new_peer_id)
383
+
384
+ return self.pool_name_to_added_in_last_update, self.pool_name_to_removed_in_last_update
385
+
386
+ def get_messages(self, p2p_name: str, allowed_not_connected_peers: set | None = None):
387
+ """Retrieves and verifies all messages from a specified P2P network.
388
+
389
+ Args:
390
+ p2p_name: The name of the P2P network to fetch messages from.
391
+ allowed_not_connected_peers: An optional set of peer IDs to allow messages from, even if they are not in the pools.
392
+
393
+ Returns:
394
+ A list of verified and processed message objects.
395
+ """
396
+ # Pop all messages
397
+ messages: list[Msg] = self[p2p_name].pop_messages() # Pop all messages (list of messages - list[Msg])
398
+ ret = []
399
+ for m in messages:
400
+ if (m.sender in self.peer_id_to_pool_name or # Check if expected sender
401
+ (allowed_not_connected_peers is not None and m.sender in allowed_not_connected_peers)):
402
+ try:
403
+ token_with_inspector_final_bit = m.piggyback
404
+ token = token_with_inspector_final_bit[0:-1]
405
+ inspector_mode = token_with_inspector_final_bit[-1]
406
+ node_id, _ = self.verify_token(token, m.sender)
407
+ if node_id is not None:
408
+
409
+ # Replacing piggyback with the node ID and the flag telling if it is inspector
410
+ m.piggyback = node_id + inspector_mode
411
+ ret.append(m)
412
+ if m.sender in self.peer_id_to_pool_name:
413
+ self.peer_id_to_token[m.sender] = token
414
+ else:
415
+ print("Received a message missing expected info in the token payload (discarding it)")
416
+ except Exception as e:
417
+ print(f"Received a message with an invalid piggyback token! (discarding it) [{e}]")
418
+ else:
419
+ if ConnectionPools.DEBUG:
420
+ print("Received a message from a unknown sender (discarding it)")
421
+ return ret
422
+
423
+ def get_added_after_updating(self, pool_name: str | None = None):
424
+ """Retrieves the peers that were added in the last update cycle.
425
+
426
+ Args:
427
+ pool_name: The name of a specific pool to query. If None, returns data for all pools.
428
+
429
+ Returns:
430
+ A set of added peer IDs for the specified pool, or a dictionary of sets for all pools.
431
+ """
432
+ if pool_name is not None:
433
+ return self.pool_name_to_added_in_last_update[pool_name]
434
+ else:
435
+ return self.pool_name_to_added_in_last_update
436
+
437
+ def get_removed_after_updating(self, pool_name: str | None = None):
438
+ """Retrieves the peers that were removed in the last update cycle.
439
+
440
+ Args:
441
+ pool_name: The name of a specific pool to query. If None, returns data for all pools.
442
+
443
+ Returns:
444
+ A set of removed peer IDs for the specified pool, or a dictionary of sets for all pools.
445
+ """
446
+ if pool_name is not None:
447
+ return self.pool_name_to_removed_in_last_update[pool_name]
448
+ else:
449
+ return self.pool_name_to_removed_in_last_update
450
+
451
+ def get_last_token(self, peer_id):
452
+ """Retrieves the last known token for a given peer.
453
+
454
+ Args:
455
+ peer_id: The peer ID to query.
456
+
457
+ Returns:
458
+ The token string if found, otherwise None.
459
+ """
460
+ return self.peer_id_to_token[peer_id] if peer_id in self.peer_id_to_token else None
461
+
462
+ def is_connected(self, peer_id: str, pool_name: str | None = None):
463
+ """Checks if a peer is currently connected, optionally in a specific pool.
464
+
465
+ Args:
466
+ peer_id: The peer ID to check.
467
+ pool_name: An optional pool name to check within.
468
+
469
+ Returns:
470
+ True if the peer is connected, otherwise False.
471
+ """
472
+ if pool_name is None:
473
+ return peer_id in self.peer_id_to_pool_name
474
+ else:
475
+ return peer_id in self.peer_id_to_pool_name and pool_name == self.peer_id_to_pool_name[peer_id]
476
+
477
+ def get_pool_of(self, peer_id: str):
478
+ """Gets the pool name for a given connected peer.
479
+
480
+ Args:
481
+ peer_id: The peer ID to query.
482
+
483
+ Returns:
484
+ The name of the pool the peer is in.
485
+ """
486
+ return self.peer_id_to_pool_name[peer_id]
487
+
488
+ def size(self, pool_name: str | None = None):
489
+ """Returns the number of connections in a specific pool or the total number across all pools.
490
+
491
+ Args:
492
+ pool_name: An optional pool name to get the size of. If None, returns the total size.
493
+
494
+ Returns:
495
+ The size of the pool or the total number of connections.
496
+ """
497
+ if pool_name is not None:
498
+ return len(self.pool_name_to_pool_triple[pool_name])
499
+ else:
500
+ c = 0
501
+ for v in self.pool_name_to_pool_triple.values():
502
+ c += len(v)
503
+ return c
504
+
505
+ def send(self, peer_id: str, channel_trail: str | None,
506
+ content_type: str, content: bytes | dict | None = None, p2p: P2P | None = None):
507
+ """Sends a direct message to a specific peer.
508
+
509
+ Args:
510
+ peer_id: The peer ID to send the message to.
511
+ channel_trail: An optional string to append to the channel name.
512
+ content_type: The type of content in the message.
513
+ content: The message content.
514
+ p2p: An optional P2P object to use for sending. If None, it is derived from the peer_id.
515
+
516
+ Returns:
517
+ True if the message is sent successfully, otherwise False.
518
+ """
519
+ # Getting the right p2p object
520
+ if p2p is None:
521
+ p2p = self.peer_id_to_p2p[peer_id] if peer_id in self.peer_id_to_p2p else None
522
+ if p2p is None:
523
+ if ConnectionPools.DEBUG:
524
+ print("[DEBUG CONNECTIONS-POOL] P2P non found for peer id: " + str(peer_id))
525
+ return False
526
+
527
+ # Defining channel
528
+ if channel_trail is not None and len(channel_trail) > 0:
529
+ channel = f"{p2p.peer_id}::dm:{peer_id}-{content_type}~{channel_trail}"
530
+ else:
531
+ channel = f"{p2p.peer_id}::dm:{peer_id}-{content_type}"
532
+
533
+ # Adding sender info here
534
+ msg = Msg(sender=p2p.peer_id,
535
+ content_type=content_type,
536
+ content=content,
537
+ channel=channel,
538
+ piggyback=self.__token + "0") # Adding inspector-mode bit (dummy bit here)
539
+ if ConnectionPools.DEBUG:
540
+ print("[DEBUG CONNECTIONS-POOL] Sending message: " + str(msg))
541
+
542
+ # Sending direct message
543
+ try:
544
+ p2p.send_message_to_peer(channel, msg)
545
+
546
+ # If the line above executes without raising an error, it was successful.
547
+ return True
548
+ except P2PError as e:
549
+
550
+ # If send_message_to_peer fails, it will raise a P2PError. We catch it here.
551
+ if ConnectionPools.DEBUG:
552
+ print("[DEBUG CONNECTIONS-POOL] Sending error is: " + str(e))
553
+ return False
554
+
555
+ def subscribe(self, peer_id: str, channel: str, default_p2p_name: str | None = None):
556
+ """Subscribes to a topic/channel on a P2P network.
557
+
558
+ Args:
559
+ peer_id: The peer ID associated with the topic/channel.
560
+ channel: The name of the channel to subscribe to.
561
+ default_p2p_name: An optional P2P network name to use if the peer's network is unknown.
562
+
563
+ Returns:
564
+ True if the subscription is successful, otherwise False.
565
+ """
566
+
567
+ # Getting the right p2p object
568
+ p2p = None
569
+ for _p2p in self.p2p_to_pool_names.keys():
570
+ if _p2p.peer_id == peer_id:
571
+ p2p = _p2p
572
+ break
573
+ if p2p is None and peer_id in self.peer_id_to_p2p:
574
+ p2p = self.peer_id_to_p2p[peer_id]
575
+ if p2p is None:
576
+ if default_p2p_name is not None:
577
+ p2p = self.p2p_name_to_p2p[default_p2p_name]
578
+ else:
579
+ return False
580
+
581
+ try:
582
+ p2p.subscribe_to_topic(channel)
583
+ except (P2PError, ValueError) as e:
584
+ return False
585
+ return True
586
+
587
+ def unsubscribe(self, peer_id: str, channel: str):
588
+ """Unsubscribes from a topic/channel on a P2P network.
589
+
590
+ Args:
591
+ peer_id: The peer ID associated with the topic/channel.
592
+ channel: The name of the channel to unsubscribe from.
593
+
594
+ Returns:
595
+ True if the unsubscription is successful, otherwise False.
596
+ """
597
+
598
+ # Getting the right p2p object
599
+ p2p = None
600
+ for _p2p in self.p2p_to_pool_names.keys():
601
+ if _p2p.peer_id == peer_id:
602
+ p2p = _p2p
603
+ break
604
+ if p2p is None and peer_id in self.peer_id_to_p2p:
605
+ p2p = self.peer_id_to_p2p[peer_id]
606
+ if p2p is None:
607
+ return False
608
+
609
+ try:
610
+ p2p.unsubscribe_from_topic(channel)
611
+ except (P2PError, ValueError):
612
+ return False
613
+ return True
614
+
615
+ def publish(self, peer_id: str, channel: str,
616
+ content_type: str, content: bytes | dict | tuple | None = None):
617
+ """Publishes a message to a topic/channel on a P2P network.
618
+
619
+ Args:
620
+ peer_id: The peer ID associated with the topic/channel.
621
+ channel: The name of the channel to publish to.
622
+ content_type: The type of content in the message.
623
+ content: The message content.
624
+
625
+ Returns:
626
+ True if the message is published successfully, otherwise False.
627
+ """
628
+
629
+ # Getting the right p2p object
630
+ p2p = None
631
+ for _p2p in self.p2p_to_pool_names.keys():
632
+ if _p2p.peer_id == peer_id:
633
+ p2p = _p2p
634
+ break
635
+ if p2p is None:
636
+ p2p = self.peer_id_to_p2p[peer_id]
637
+ if p2p is None:
638
+ return False
639
+
640
+ # Adding sender info here
641
+ msg = Msg(sender=p2p.peer_id,
642
+ content_type=content_type,
643
+ content=content,
644
+ channel=channel,
645
+ piggyback=self.__token + "0") # Adding inspector-mode bit (dummy bit here)
646
+ if ConnectionPools.DEBUG:
647
+ print("[DEBUG CONNECTIONS-POOL] Sending (publish) message: " + str(msg))
648
+
649
+ # Sending message via GossipSub
650
+ try:
651
+ p2p.broadcast_message(channel, msg)
652
+
653
+ # If the line above executes without raising an error, it was successful.
654
+ return True
655
+ except P2PError as e:
656
+
657
+ # If send_message_to_peer fails, it will raise a P2PError. We catch it here.
658
+ return False
659
+
660
+
661
+ class NodeConn(ConnectionPools):
662
+
663
+ # Basic name
664
+ __ALL_UNIVERSE = "all_universe"
665
+ __WORLD_AGENTS_ONLY = "world_agents"
666
+ __WORLD_NODE_ONLY = "world_node"
667
+ __WORLD_MASTERS_ONLY = "world_masters"
668
+
669
+ # Suffixes
670
+ __PUBLIC_NET = "_public"
671
+ __PRIVATE_NET = "_private"
672
+
673
+ # Prefixes
674
+ __INBOUND = "in_"
675
+ __OUTBOUND = "out_"
676
+
677
+ # P2p names
678
+ P2P_PUBLIC = "p2p_public"
679
+ P2P_WORLD = "p2p_world"
680
+
681
+ # All pools (prefix + basic name + suffix)
682
+ IN_PUBLIC = __INBOUND + __ALL_UNIVERSE + __PUBLIC_NET
683
+ OUT_PUBLIC = __OUTBOUND + __ALL_UNIVERSE + __PUBLIC_NET
684
+ IN_WORLD_AGENTS = __INBOUND + __WORLD_AGENTS_ONLY + __PRIVATE_NET
685
+ OUT_WORLD_AGENTS = __OUTBOUND + __WORLD_AGENTS_ONLY + __PRIVATE_NET
686
+ IN_WORLD_NODE = __INBOUND + __WORLD_NODE_ONLY + __PRIVATE_NET
687
+ OUT_WORLD_NODE = __OUTBOUND + __WORLD_NODE_ONLY + __PRIVATE_NET
688
+ IN_WORLD_MASTERS = __INBOUND + __WORLD_MASTERS_ONLY + __PRIVATE_NET
689
+ OUT_WORLD_MASTERS = __OUTBOUND + __WORLD_MASTERS_ONLY + __PRIVATE_NET
690
+
691
+ # Aggregated pools
692
+ PUBLIC = {IN_PUBLIC, OUT_PUBLIC}
693
+ WORLD_NODE = {IN_WORLD_NODE, OUT_WORLD_NODE}
694
+ WORLD_AGENTS = {IN_WORLD_AGENTS, OUT_WORLD_AGENTS}
695
+ WORLD_MASTERS = {IN_WORLD_MASTERS, OUT_WORLD_MASTERS}
696
+ WORLD = WORLD_NODE | WORLD_AGENTS | WORLD_MASTERS
697
+ ALL = PUBLIC | WORLD
698
+ OUTGOING = {OUT_PUBLIC, OUT_WORLD_NODE, OUT_WORLD_AGENTS, OUT_WORLD_MASTERS}
699
+ INCOMING = {IN_PUBLIC, IN_WORLD_NODE, IN_WORLD_AGENTS, IN_WORLD_MASTERS}
700
+
701
+ def __init__(self, max_connections: int, p2p_u: P2P, p2p_w: P2P,
702
+ is_world_node: bool, public_key: str, token: str):
703
+ """Initializes a new instance of the NodeConn class.
704
+
705
+ Args:
706
+ max_connections: The total number of connections the node can handle.
707
+ p2p_u: The P2P object for the public network.
708
+ p2p_w: The P2P object for the world/private network.
709
+ is_world_node: A boolean flag indicating if this node is a world node.
710
+ public_key: The public key for token verification.
711
+ token: The node's authentication token.
712
+ """
713
+ super().__init__(max_connections=max_connections,
714
+ p2p_name_to_p2p={
715
+ NodeConn.P2P_PUBLIC: p2p_u,
716
+ NodeConn.P2P_WORLD: p2p_w,
717
+ },
718
+ pool_name_to_p2p_name_and_ratio={
719
+ NodeConn.IN_PUBLIC: [NodeConn.P2P_PUBLIC, 0.25 / 2. if not is_world_node else 0.25 / 2.],
720
+ NodeConn.OUT_PUBLIC: [NodeConn.P2P_PUBLIC, 0.25 / 2. if not is_world_node else 0.25 / 2.],
721
+ NodeConn.IN_WORLD_AGENTS: [NodeConn.P2P_WORLD, .75 / 2 if not is_world_node else 0.5 / 2],
722
+ NodeConn.OUT_WORLD_AGENTS: [NodeConn.P2P_WORLD, .75 / 2 if not is_world_node else 0.5 / 2],
723
+ NodeConn.IN_WORLD_NODE: [NodeConn.P2P_WORLD, 0. if not is_world_node else -1.],
724
+ NodeConn.OUT_WORLD_NODE: [NodeConn.P2P_WORLD, 0. if not is_world_node else -1],
725
+ NodeConn.IN_WORLD_MASTERS: [NodeConn.P2P_WORLD, 0. if not is_world_node else 0.25 / 2.],
726
+ NodeConn.OUT_WORLD_MASTERS: [NodeConn.P2P_WORLD, 0. if not is_world_node else 0.25 / 2.]
727
+ },
728
+ public_key=public_key, token=token)
729
+
730
+ # Just for convenience
731
+ self.p2p_public = p2p_u
732
+ self.p2p_world = p2p_w
733
+
734
+ # These are the list of all the possible agents that might try to connect when we are in world
735
+ self.world_agents_list = set()
736
+ self.world_masters_list = set()
737
+ self.world_agents_and_world_masters_list = set()
738
+ self.world_node_peer_id = None
739
+ self.inspector_peer_id = None
740
+ self.role_to_peer_ids = {}
741
+ self.peer_id_to_addrs = {}
742
+
743
+ # Rendezvous
744
+ self.rendezvous_tag = -1
745
+
746
+ def reset_rendezvous_tag(self):
747
+ """Resets the rendezvous tag to its initial state."""
748
+ self.rendezvous_tag = -1
749
+
750
+ def conn_routing_fcn(self, connected_peer_infos: list, p2p: P2P):
751
+ """Routes connected peers to the correct connection pool based on their network and role.
752
+
753
+ Args:
754
+ connected_peer_infos: A list of dictionaries with information about connected peers.
755
+ p2p: The P2P network object where the connections were found.
756
+
757
+ Returns:
758
+ A dictionary mapping pool names to a dictionary of peer IDs and their information.
759
+ """
760
+ pool_name_and_peer_id_to_peer_info = {k: {} for k in self.p2p_to_pool_names[p2p]}
761
+ public = p2p == self.p2p_public
762
+
763
+ for c in connected_peer_infos:
764
+ inbound = c['direction'] == "incoming"
765
+ outbound = c['direction'] == "outgoing"
766
+ peer_id = c['id'] # Other fields are: c['addrs'], c['connected_at']
767
+
768
+ if public:
769
+ if inbound:
770
+ pool_name_and_peer_id_to_peer_info[NodeConn.IN_PUBLIC][peer_id] = c
771
+ elif outbound:
772
+ pool_name_and_peer_id_to_peer_info[NodeConn.OUT_PUBLIC][peer_id] = c
773
+ else:
774
+ raise ValueError(f"Connection direction is undefined: {c['direction']}")
775
+ else:
776
+ is_world_agent = peer_id in self.world_agents_list
777
+ is_world_master = peer_id in self.world_masters_list
778
+ is_world_node = self.world_node_peer_id is not None and peer_id == self.world_node_peer_id
779
+ is_inspector = self.inspector_peer_id is not None and peer_id == self.inspector_peer_id
780
+ if not is_world_node and not is_world_master and not is_world_agent and not is_inspector:
781
+ if ConnectionPools.DEBUG:
782
+ print("[DEBUG CONNECTIONS-POOL] World agents list: " + str(self.world_agents_list))
783
+ print("[DEBUG CONNECTIONS-POOL] World masters list: " + str(self.world_masters_list))
784
+ print("[DEBUG CONNECTIONS-POOL] World node peer id: " + str(self.world_node_peer_id))
785
+ print("[DEBUG CONNECTIONS-POOL] Inspector peer id: " + str(self.inspector_peer_id))
786
+ print(f"[DEBUG CONNECTIONS-POOL] Unable to determine the peer type for {peer_id}: "
787
+ f"cannot say if world agent, master, world node, inspector (disconnecting it)")
788
+ ConnectionPools.disconnect(p2p, peer_id)
789
+ continue
790
+
791
+ if inbound:
792
+ pool_name_and_peer_id_to_peer_info[NodeConn.IN_WORLD_AGENTS if is_world_agent else (
793
+ NodeConn.IN_WORLD_NODE if is_world_node else
794
+ NodeConn.IN_WORLD_MASTERS)][peer_id] = c
795
+ elif outbound:
796
+ pool_name_and_peer_id_to_peer_info[NodeConn.OUT_WORLD_AGENTS if is_world_agent else (
797
+ NodeConn.OUT_WORLD_NODE if is_world_node else
798
+ NodeConn.OUT_WORLD_MASTERS)][peer_id] = c
799
+ else:
800
+ raise ValueError(f"Connection direction is undefined: {c}")
801
+
802
+ return pool_name_and_peer_id_to_peer_info
803
+
804
+ def set_world(self, world_peer_id: str | None):
805
+ """Sets the peer ID of the world node.
806
+
807
+ Args:
808
+ world_peer_id: The peer ID of the world node, or None to clear it.
809
+ """
810
+ self.world_node_peer_id = world_peer_id
811
+
812
+ def set_inspector(self, inspector_peer_id: str | None):
813
+ """Sets the peer ID of the inspector.
814
+
815
+ Args:
816
+ inspector_peer_id: The peer ID of the inspector node.
817
+ """
818
+ self.inspector_peer_id = inspector_peer_id
819
+
820
+ def get_world_peer_id(self):
821
+ """Returns the peer ID of the world node.
822
+
823
+ Args:
824
+ None.
825
+
826
+ Returns:
827
+ The world node's peer ID.
828
+ """
829
+ return self.world_node_peer_id
830
+
831
+ def set_addresses_in_peer_info(self, peer_id, addresses):
832
+ """Updates the list of addresses for a given peer.
833
+
834
+ Args:
835
+ peer_id: The peer ID to update.
836
+ addresses: A new list of addresses for the peer.
837
+ """
838
+ if self.in_connection_queues(peer_id):
839
+ addrs = self.pool_name_to_peer_infos[self.get_pool_of(peer_id)][peer_id]['addrs']
840
+ addrs.clear() # Warning: do not allocate a new list, keep the current one (it is referenced by others)
841
+ for _addrs in addresses:
842
+ addrs.append(_addrs)
843
+
844
+ def set_role(self, peer_id, new_role: int):
845
+ """Updates the role of a peer and its associated role-based lists.
846
+
847
+ Args:
848
+ peer_id: The peer ID to update.
849
+ new_role: The new role for the peer.
850
+ """
851
+ cur_role = self.get_role(peer_id)
852
+
853
+ # Updating
854
+ self.peer_id_to_misc[peer_id] = new_role
855
+
856
+ if self.in_connection_queues(peer_id):
857
+ self.pool_name_to_peer_infos[self.get_pool_of(peer_id)][peer_id]['misc'] = new_role
858
+
859
+ # Updating
860
+ if cur_role in self.role_to_peer_ids:
861
+ if peer_id in self.role_to_peer_ids[cur_role]:
862
+ self.role_to_peer_ids[cur_role].remove(peer_id)
863
+ if len(self.role_to_peer_ids[cur_role]) == 0:
864
+ del self.role_to_peer_ids[cur_role]
865
+ if new_role not in self.role_to_peer_ids:
866
+ self.role_to_peer_ids[new_role] = set()
867
+ self.role_to_peer_ids[new_role].add(peer_id)
868
+
869
+ def set_world_agents_list(self, world_agents_list_peer_infos: list[dict] | None):
870
+ """Sets the list of all world agents based on a provided list of peer information.
871
+
872
+ Args:
873
+ world_agents_list_peer_infos: A list of dictionaries containing peer information for world agents.
874
+ """
875
+
876
+ # Clearing previous information
877
+ to_remove = []
878
+ for peer_id, misc in self.peer_id_to_misc.items():
879
+ if misc & 1 == 1 and misc & 2 == 0:
880
+ to_remove.append((peer_id, misc))
881
+
882
+ for peer_id, misc in to_remove:
883
+ del self.peer_id_to_misc[peer_id]
884
+ del self.peer_id_to_addrs[peer_id]
885
+ self.role_to_peer_ids[misc].remove(peer_id)
886
+
887
+ # Setting new information
888
+ if world_agents_list_peer_infos is not None and len(world_agents_list_peer_infos) > 0:
889
+ self.world_agents_list = {x['id'] for x in world_agents_list_peer_infos}
890
+ for x in world_agents_list_peer_infos:
891
+ self.peer_id_to_addrs[x['id']] = x['addrs']
892
+ self.set_role(x['id'], x['misc'])
893
+ else:
894
+ self.world_agents_list = set()
895
+
896
+ self.world_agents_and_world_masters_list = self.world_agents_list | self.world_masters_list
897
+
898
+ def set_world_masters_list(self, world_masters_list_peer_infos: list[dict] | None):
899
+ """Sets the list of all world masters based on a provided list of peer information.
900
+
901
+ Args:
902
+ world_masters_list_peer_infos: A list of dictionaries containing peer information for world masters.
903
+ """
904
+
905
+ # Clearing previous information
906
+ to_remove = []
907
+ for peer_id, misc in self.peer_id_to_misc.items():
908
+ if misc & 1 == 1 and misc & 2 == 2:
909
+ to_remove.append((peer_id, misc))
910
+
911
+ for peer_id, misc in to_remove:
912
+ del self.peer_id_to_misc[peer_id]
913
+ del self.peer_id_to_addrs[peer_id]
914
+ self.role_to_peer_ids[misc].remove(peer_id)
915
+
916
+ # Setting new information
917
+ if world_masters_list_peer_infos is not None and len(world_masters_list_peer_infos) > 0:
918
+ self.world_masters_list = {x['id'] for x in world_masters_list_peer_infos}
919
+ for x in world_masters_list_peer_infos:
920
+ self.peer_id_to_addrs[x['id']] = x['addrs']
921
+ self.set_role(x['id'], x['misc'])
922
+ else:
923
+ self.world_masters_list = set()
924
+
925
+ self.world_agents_and_world_masters_list = self.world_agents_list | self.world_masters_list
926
+
927
+ def add_to_world_agents_list(self, peer_id: str, addrs: list[str], role: int = -1):
928
+ """Adds a new world agent to the list.
929
+
930
+ Args:
931
+ peer_id: The peer ID of the new agent.
932
+ addrs: A list of addresses for the new agent.
933
+ role: The role assigned to the agent.
934
+ """
935
+ self.world_agents_list.add(peer_id)
936
+
937
+ # This assumes that the WORLD MASTER/AGENT BIT is the first one
938
+ assert role & 1 == 1, "Expecting the first bit of the role to be 1 for world agents"
939
+ assert role & 2 == 0, "Expecting the second bit of the role to be 0 for world agents"
940
+ self.peer_id_to_addrs[peer_id] = addrs
941
+ self.set_role(peer_id, role)
942
+ self.world_agents_and_world_masters_list = self.world_agents_list | self.world_masters_list
943
+
944
+ def add_to_world_masters_list(self, peer_id: str, addrs: list[str], role: int = -1):
945
+ """Adds a new world master to the list.
946
+
947
+ Args:
948
+ peer_id: The peer ID of the new master.
949
+ addrs: A list of addresses for the new master.
950
+ role: The role assigned to the master.
951
+ """
952
+ self.world_masters_list.add(peer_id)
953
+
954
+ # This assumes that the WORLD MASTER/AGENT BIT is the first one
955
+ assert role & 1 == 1, "Expecting the first bit of the role to be 1 for world masters"
956
+ assert role & 2 == 2, "Expecting the second bit of the role to be 1 for world masters"
957
+ self.peer_id_to_addrs[peer_id] = addrs
958
+ self.set_role(peer_id, role)
959
+ self.world_agents_and_world_masters_list = self.world_agents_list | self.world_masters_list
960
+
961
+ def get_added_after_updating(self, pool_names: list[str] | None = None):
962
+ """Retrieves the set of peers added after the last update cycle for specified pools.
963
+
964
+ Args:
965
+ pool_names: A list of pool names to check. If None, checks all pools.
966
+
967
+ Returns:
968
+ A dictionary mapping pool names to sets of added peer IDs, or a single set if only one pool is specified.
969
+ """
970
+ if pool_names is not None:
971
+ ret = {}
972
+ for p in pool_names:
973
+ ret[p] = super().get_added_after_updating(p)
974
+ return ret
975
+ else:
976
+ return super().get_added_after_updating()
977
+
978
+ def get_removed_after_updating(self, pool_names: list[str] | None = None):
979
+ """Retrieves the set of peers removed after the last update cycle for specified pools.
980
+
981
+ Args:
982
+ pool_names: A list of pool names to check. If None, checks all pools.
983
+
984
+ Returns:
985
+ A dictionary mapping pool names to sets of removed peer IDs, or a single set if only one pool is specified.
986
+ """
987
+ if pool_names is not None:
988
+ ret = {}
989
+ for p in pool_names:
990
+ ret[p] = super().get_removed_after_updating(p)
991
+ return ret
992
+ else:
993
+ return super().get_removed_after_updating()
994
+
995
+ def size(self, pool_names: list[str] | None = None):
996
+ """Returns the total number of connections across all specified pools.
997
+
998
+ Args:
999
+ pool_names: A list of pool names to sum the size of. If None, returns the total size of all pools.
1000
+
1001
+ Returns:
1002
+ The total number of connections.
1003
+ """
1004
+ if pool_names is not None:
1005
+ return super().size()
1006
+ else:
1007
+ c = 0
1008
+ for p in self.pool_names:
1009
+ c += super().size(p)
1010
+ return c
1011
+
1012
+ def is_connected(self, peer_id: str, pool_names: list[str] | None = None):
1013
+ """Checks if a peer is connected in any of the specified pools.
1014
+
1015
+ Args:
1016
+ peer_id: The peer ID to check.
1017
+ pool_names: A list of pool names to search within. If None, searches all pools.
1018
+
1019
+ Returns:
1020
+ True if the peer is found in any of the pools, otherwise False.
1021
+ """
1022
+ if pool_names is None:
1023
+ return super().is_connected(peer_id)
1024
+ else:
1025
+ for p in pool_names:
1026
+ if super().is_connected(peer_id, p):
1027
+ return True
1028
+ return False
1029
+
1030
+ def is_public(self, peer_id):
1031
+ """Checks if a peer is connected via the public network.
1032
+
1033
+ Args:
1034
+ peer_id: The peer ID to check.
1035
+
1036
+ Returns:
1037
+ True if the peer is in a public pool, otherwise False.
1038
+ """
1039
+ pool_name = self.get_pool_of(peer_id)
1040
+ return pool_name in NodeConn.PUBLIC
1041
+
1042
+ def is_world_master(self, peer_id):
1043
+ """Checks if a peer is a world master.
1044
+
1045
+ Args:
1046
+ peer_id: The peer ID to check.
1047
+
1048
+ Returns:
1049
+ True if the peer is in a world master pool, otherwise False.
1050
+ """
1051
+ pool_name = self.get_pool_of(peer_id)
1052
+ return pool_name in NodeConn.WORLD_MASTERS
1053
+
1054
+ def is_world_node(self, peer_id):
1055
+ """Checks if a peer is the world node.
1056
+
1057
+ Args:
1058
+ peer_id: The peer ID to check.
1059
+
1060
+ Returns:
1061
+ True if the peer is in a world node pool, otherwise False.
1062
+ """
1063
+ pool_name = self.get_pool_of(peer_id)
1064
+ return pool_name in NodeConn.WORLD_NODE
1065
+
1066
+ def is_in_world(self, peer_id):
1067
+ """Checks if a peer is connected to the world network.
1068
+
1069
+ Args:
1070
+ peer_id: The peer ID to check.
1071
+
1072
+ Returns:
1073
+ True if the peer is in any world pool, otherwise False.
1074
+ """
1075
+ pool_name = self.get_pool_of(peer_id)
1076
+ return pool_name in NodeConn.WORLD
1077
+
1078
+ def get_role(self, peer_id):
1079
+ """Retrieves the role of a given peer.
1080
+
1081
+ Args:
1082
+ peer_id: The peer ID to query.
1083
+
1084
+ Returns:
1085
+ The integer role of the peer.
1086
+ """
1087
+ role = self.peer_id_to_misc.get(peer_id, 0) # 0 means public
1088
+ assert role >= 0, "Expecting role to be >= 0"
1089
+ assert role & 1 != 0 or role == 0, "Expecting public role to be zero (all-zero-bits)"
1090
+ return role
1091
+
1092
+ def get_addrs(self, peer_id):
1093
+ """Retrieves the list of addresses for a given peer.
1094
+
1095
+ Args:
1096
+ peer_id: The peer ID to query.
1097
+
1098
+ Returns:
1099
+ A list of addresses for the peer.
1100
+ """
1101
+ return self.peer_id_to_addrs.get(peer_id)
1102
+
1103
+ def in_connection_queues(self, peer_id):
1104
+ """Checks if a peer ID exists in any connection pool.
1105
+
1106
+ Args:
1107
+ peer_id: The peer ID to check.
1108
+
1109
+ Returns:
1110
+ True if the peer is found in any pool, otherwise False.
1111
+ """
1112
+ return peer_id in self.peer_id_to_pool_name
1113
+
1114
+ def find_addrs_by_role(self, role, return_peer_ids_too: bool = False):
1115
+ """Finds all addresses of peers with a specific role.
1116
+
1117
+ Args:
1118
+ role: The integer role to search for.
1119
+ return_peer_ids_too: A boolean to also return the peer IDs.
1120
+
1121
+ Returns:
1122
+ A list of lists of addresses, and optionally a list of peer IDs.
1123
+ """
1124
+ if role in self.role_to_peer_ids:
1125
+ peer_ids = self.role_to_peer_ids[role]
1126
+ else:
1127
+ if not return_peer_ids_too:
1128
+ return []
1129
+ else:
1130
+ return [], []
1131
+ ret_addrs = []
1132
+ ret_peer_ids = []
1133
+ for peer_id in peer_ids:
1134
+ addrs = self.get_addrs(peer_id)
1135
+ if addrs is not None:
1136
+ ret_addrs.append(addrs)
1137
+ ret_peer_ids.append(peer_id)
1138
+ if not return_peer_ids_too:
1139
+ return ret_addrs
1140
+ else:
1141
+ return ret_addrs, ret_peer_ids
1142
+
1143
+ def count_by_role(self, role: int):
1144
+ """Counts the number of peers with a specific role.
1145
+
1146
+ Args:
1147
+ role: The integer role to count.
1148
+
1149
+ Returns:
1150
+ The number of peers with that role.
1151
+ """
1152
+ if role in self.role_to_peer_ids:
1153
+ return len(self.role_to_peer_ids[role])
1154
+ else:
1155
+ return 0
1156
+
1157
+ def get_all_connected_peer_infos(self, pool_names: list[str] | set[str]):
1158
+ """Retrieves a list of all peer info dictionaries for the specified pools.
1159
+
1160
+ Args:
1161
+ pool_names: A list or set of pool names to query.
1162
+
1163
+ Returns:
1164
+ A list of dictionaries containing peer information.
1165
+ """
1166
+ ret = []
1167
+ for p in pool_names:
1168
+ ret += super().get_all_connected_peer_infos(p)
1169
+ return ret
1170
+
1171
+ def set_world_agents_and_world_masters_lists_from_rendezvous(self):
1172
+ """Updates the lists of world agents and masters using data from the rendezvous topic."""
1173
+ rendezvous_state = self.p2p_world.get_rendezvous_peers_info()
1174
+
1175
+ if rendezvous_state is not None:
1176
+ tag = rendezvous_state.get('update_count', -1)
1177
+
1178
+ if tag > self.rendezvous_tag:
1179
+ self.rendezvous_tag = tag
1180
+ rendezvous_peer_infos = rendezvous_state.get('peers', [])
1181
+
1182
+ world_agents_peer_infos = []
1183
+ world_masters_peer_infos = []
1184
+
1185
+ if ConnectionPools.DEBUG:
1186
+ print(f"[DEBUG CONNECTIONS-POOL] Rendezvous peer infos (tag: {tag}, peers: "
1187
+ f"{len(rendezvous_peer_infos)} peers)")
1188
+
1189
+ for c in rendezvous_peer_infos:
1190
+ if c['addrs'] is None or len(c['addrs']) == 0:
1191
+ print(f"[DEBUG CONNECTIONS-POOL] Skipping a peer with missing addrs: {c}")
1192
+ continue
1193
+ if (c['misc'] & 1) == 1 and (c['misc'] & 2) == 0:
1194
+ world_agents_peer_infos.append(c)
1195
+ elif (c['misc'] & 1) == 1 and (c['misc'] & 2) == 2:
1196
+ world_masters_peer_infos.append(c)
1197
+ else:
1198
+ raise ValueError("Unexpected value of the 'misc' field: " + str(c))
1199
+
1200
+ # Updating lists
1201
+ self.set_world_agents_list(world_agents_peer_infos)
1202
+ self.set_world_masters_list(world_masters_peer_infos)
1203
+
1204
+ def get_cv_hash_from_last_token(self, peer_id):
1205
+ """Retrieves the CV hash from the last token received from a peer.
1206
+
1207
+ Args:
1208
+ peer_id: The peer ID to query.
1209
+
1210
+ Returns:
1211
+ The CV hash string, or None if not found.
1212
+ """
1213
+ token = self.get_last_token(peer_id)
1214
+ if token is not None:
1215
+ _, cv_hash = self.verify_token(token, peer_id)
1216
+ return cv_hash
1217
+ else:
1218
+ return None
1219
+
1220
+ def remove(self, peer_id: str):
1221
+ """Removes a peer and its associated information from all lists and pools.
1222
+
1223
+ Args:
1224
+ peer_id: The peer ID to remove.
1225
+ """
1226
+ super().remove(peer_id)
1227
+ if peer_id in self.peer_id_to_addrs:
1228
+ del self.peer_id_to_addrs[peer_id]
1229
+
1230
+ def remove_all_world_agents(self):
1231
+ """Removes all connected world agents from the pools and role lists."""
1232
+ peer_infos = self.get_all_connected_peer_infos(NodeConn.WORLD)
1233
+ for c in peer_infos:
1234
+ peer_id = c['id']
1235
+ self.remove(peer_id)
1236
+ for role, peer_ids in self.role_to_peer_ids.items():
1237
+ if role & 1 == NodeConn.WORLD:
1238
+ peer_ids.remove(peer_id)
1239
+
1240
+ def subscribe(self, peer_id: str, channel: str, default_p2p_name: str | None = None):
1241
+ """Subscribes to a channel, defaulting to the world P2P network if a network is not specified.
1242
+
1243
+ Args:
1244
+ peer_id: The peer ID associated with the channel.
1245
+ channel: The channel to subscribe to.
1246
+ default_p2p_name: An optional P2P name to use for the subscription.
1247
+
1248
+ Returns:
1249
+ True if successful, False otherwise.
1250
+ """
1251
+ return super().subscribe(peer_id, channel,
1252
+ default_p2p_name=NodeConn.P2P_WORLD if default_p2p_name is None else default_p2p_name)
1253
+
1254
+ def get_messages(self, p2p_name: str, allowed_not_connected_peers: set | None = None):
1255
+ """Retrieves messages, allowing for messages from known world agents and masters even if not in a connection pool.
1256
+
1257
+ Args:
1258
+ p2p_name: The name of the P2P network to get messages from.
1259
+ allowed_not_connected_peers: This parameter is ignored in this implementation.
1260
+
1261
+ Returns:
1262
+ A list of verified and processed message objects.
1263
+ """
1264
+ assert allowed_not_connected_peers is None, "This param (allowed_not_connected_peers is ignored in NodeConn"
1265
+ return super().get_messages(p2p_name, allowed_not_connected_peers=self.world_agents_and_world_masters_list)