networking.p2p
p2p
¶
Modules:
| Name | Description |
|---|---|
golibp2p |
|
lib_types |
|
messages |
|
mylogger |
|
p2p |
|
Classes:
| Name | Description |
|---|---|
P2P |
Python wrapper for the Go libp2p shared library. |
P2PError |
Custom exception class for P2P library errors. |
TypeInterface |
Helper class for converting between Python types and Go types using ctypes. |
Classes¶
P2P
¶
P2P(port: int = 0, ips: List[str] = None, enable_relay_client: bool = True, enable_relay_service: bool = False, wait_public_reachability: bool = False, max_connections: int = 1000)
Python wrapper for the Go libp2p shared library.
This class initializes a libp2p node, provides methods to interact with the p2p network (connect, send/receive messages, pubsub, relay), and manages the lifecycle of the underlying Go node.
Attributes:
| Name | Type | Description |
|---|---|---|
libp2p |
LibP2P
|
Static class attribute holding the loaded Go library instance. Must be set before instantiating P2P. Example: P2P.libp2p = LibP2P() |
peer_id |
str
|
The Peer ID of the initialized local node. |
addresses |
Optional[List[str]]
|
List of multiaddresses the local node is listening on. |
is_public |
bool
|
Whether the node is publicly reachable. |
peer_map |
Dict[str, Any]
|
A dictionary to potentially store information about connected peers (managed manually or by polling thread). |
Initializes and starts a new libp2p node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
port
|
int
|
The (first) TCP port to listen on (0 for random). |
0
|
ips
|
List[str]
|
A list of specific IP addresses to listen on. Defaults to ["0.0.0.0"]. |
None
|
enable_relay_client
|
bool
|
Enable listening to relayed connections for this node. |
True
|
enable_relay_service
|
bool
|
Enable relay service capabilities for this node. |
False
|
wait_public_reachability
|
bool
|
Tries every possible attempt to make the node publicly reachable (UPnP, HolePunching, AutoNat via DHT...). |
False
|
max_connections
|
int
|
Maximum number of connections this node can handle. |
1000
|
Raises:
| Type | Description |
|---|---|
P2PError
|
If the node creation fails in the Go library. |
AttributeError
|
If P2P.libp2p has not been set before instantiation. |
Methods:
| Name | Description |
|---|---|
setup_library |
Initializes the underlying Go library. Must be called once. This is called automatically. |
connect_to |
Establishes a connection with a remote peer. |
disconnect_from |
Closes connections to a specific peer and removes tracking. |
send_message_to_peer |
Sends a direct message to a specific peer. |
broadcast_message |
Broadcasts a message using PubSub to the node's own topic. |
pop_messages |
Retrieves and removes the first message from the queue of each channel for this node instance. |
subscribe_to_topic |
Subscribes to a PubSub topic to receive messages. |
unsubscribe_from_topic |
Unsubscribes from a PubSub topic. |
reserve_on_relay |
Attempts to reserve a slot on a specified relay node. |
get_node_addresses |
Gets the known multiaddresses for the local node or a specific peer. |
get_connected_peers_info |
Gets information about currently connected peers from the Go library. |
get_rendezvous_peers_info |
Gets the full rendezvous state from the Go library, including peers and metadata. |
get_message_queue_length |
Gets the current number of messages in the incoming queue. |
close |
Gracefully shuts down the libp2p node and stops background threads. |
Source code in unaiverse/networking/p2p/p2p.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
Attributes¶
addresses
property
¶
Returns the list of multiaddresses the local node is listening on. This property queries the Go layer directly, caching the result for performance.
is_public
property
¶
Returns a boolean stating whether the local node is publicly reachable.
relay_is_enabled
property
¶
Returns whether the relay client functionality is enabled for this node.
Methods:¶
setup_library
classmethod
¶
setup_library(max_instances: Optional[int] = None, max_channels: Optional[int] = None, max_queue_per_channel: Optional[int] = None, max_message_size: Optional[int] = None, enable_logging: bool = False) -> None
Initializes the underlying Go library. Must be called once. This is called automatically.
Source code in unaiverse/networking/p2p/p2p.py
connect_to
¶
Establishes a connection with a remote peer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
multiaddrs
|
list[str]
|
The list of multiaddress strings of the peer to try to connect to. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary containing the connected peer's AddrInfo (ID and Addrs). |
Raises:
| Type | Description |
|---|---|
P2PError
|
If the connection fails. |
ValueError
|
If the multiaddr is invalid. |
Source code in unaiverse/networking/p2p/p2p.py
disconnect_from
¶
Closes connections to a specific peer and removes tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
peer_id
|
str
|
The Peer ID string of the peer to disconnect from. |
required |
Raises:
| Type | Description |
|---|---|
P2PError
|
If disconnecting fails. |
ValueError
|
If the peer_id is invalid. |
Source code in unaiverse/networking/p2p/p2p.py
send_message_to_peer
¶
send_message_to_peer(channel: str, msg: Msg) -> None
Sends a direct message to a specific peer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel
|
str
|
The string identifying the channel for the communication. |
required |
msg
|
Msg
|
The message to send (Msg). |
required |
Raises:
| Type | Description |
|---|---|
P2PError
|
If message sending fails (based on return code). |
ValueError
|
If inputs are invalid. |
TypeError
|
If data is not bytes. |
Source code in unaiverse/networking/p2p/p2p.py
broadcast_message
¶
broadcast_message(channel: str, msg: Msg) -> None
Broadcasts a message using PubSub to the node's own topic. Peers subscribed to this node's Peer ID topic will receive it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel
|
str
|
The Channel for this topic (e.g., owner_peer_id: |
required |
msg
|
Msg
|
The message to send (Msg). |
required |
Raises:
| Type | Description |
|---|---|
P2PError
|
If broadcasting fails. |
ValueError
|
If inputs are invalid. |
TypeError
|
If data is not bytes. |
Source code in unaiverse/networking/p2p/p2p.py
pop_messages
¶
pop_messages() -> List[Msg]
Retrieves and removes the first message from the queue of each channel for this node instance.
Returns:
| Type | Description |
|---|---|
List[Msg]
|
A list of Msg objects. Returns an empty list if no messages were available. |
Raises:
| Type | Description |
|---|---|
P2PError
|
If popping messages failed internally in Go, or if data conversion fails for any message. |
Source code in unaiverse/networking/p2p/p2p.py
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 | |
subscribe_to_topic
¶
Subscribes to a PubSub topic to receive messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel
|
str
|
The Channel for this topic (e.g., owner_peer_id: |
required |
Raises:
| Type | Description |
|---|---|
P2PError
|
If subscribing fails. |
ValueError
|
If topic_name is invalid. |
Source code in unaiverse/networking/p2p/p2p.py
unsubscribe_from_topic
¶
Unsubscribes from a PubSub topic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel
|
str
|
The Channel for this topic (e.g., owner_peer_id: |
required |
Raises:
| Type | Description |
|---|---|
P2PError
|
If unsubscribing fails. |
ValueError
|
If topic_name is invalid. |
Source code in unaiverse/networking/p2p/p2p.py
reserve_on_relay
¶
Attempts to reserve a slot on a specified relay node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
relay_peer_id
|
str
|
The peerID of the relay node |
required |
Returns:
| Type | Description |
|---|---|
str
|
The UTC expiration timestamp of the reservation as an ISO 8601 string. |
Raises:
| Type | Description |
|---|---|
P2PError
|
If the reservation fails. |
ValueError
|
If the relay_multiaddr is invalid. |
Source code in unaiverse/networking/p2p/p2p.py
get_node_addresses
¶
Gets the known multiaddresses for the local node or a specific peer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
peer_id
|
str
|
The Peer ID string of the target peer. If empty, gets addresses for the local node. |
''
|
Returns:
| Type | Description |
|---|---|
List[str]
|
A list of multiaddress strings (including the /p2p/PeerID suffix). |
Raises:
| Type | Description |
|---|---|
P2PError
|
If fetching addresses fails. |
Source code in unaiverse/networking/p2p/p2p.py
get_connected_peers_info
¶
Gets information about currently connected peers from the Go library.
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
A list of dictionaries, each representing a connected peer with |
List[Dict[str, Any]]
|
keys like 'addr_info' (containing 'ID', 'Addrs'), 'connected_at', 'direction', and 'misc'. |
Raises:
| Type | Description |
|---|---|
P2PError
|
If fetching connected peers fails. |
Source code in unaiverse/networking/p2p/p2p.py
get_rendezvous_peers_info
¶
Gets the full rendezvous state from the Go library, including peers and metadata.
Returns:
| Type | Description |
|---|---|
Dict[str, Any] | None
|
|
Dict[str, Any] | None
|
|
Raises:
| Type | Description |
|---|---|
P2PError
|
If fetching the state fails in Go. |
Source code in unaiverse/networking/p2p/p2p.py
get_message_queue_length
¶
Gets the current number of messages in the incoming queue.
Returns:
| Type | Description |
|---|---|
int
|
The number of messages waiting. |
Raises:
| Type | Description |
|---|---|
P2PError
|
If querying the length fails (should be rare). |
Source code in unaiverse/networking/p2p/p2p.py
close
¶
Gracefully shuts down the libp2p node and stops background threads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
close_all
|
bool
|
If True, closes all instances of the node. Default is False. |
False
|
Source code in unaiverse/networking/p2p/p2p.py
P2PError
¶
Bases: Exception
Custom exception class for P2P library errors.
TypeInterface
¶
Helper class for converting between Python types and Go types using ctypes.
Methods:
| Name | Description |
|---|---|
to_go_string |
Converts a Python string to a UTF-8 encoded Python 'bytes' object. |
from_go_string |
Converts a C char pointer (Go string) to a Python string. |
to_go_int |
Converts a Python integer to a Go-compatible ctypes integer. |
from_go_int |
Converts a ctypes.c_int from Go to a Python integer. |
to_go_float |
Converts a Python float to a Go-compatible ctypes float. |
from_go_float |
Converts a ctypes.c_float from Go to a Python float. |
to_go_bool |
Converts a Python boolean to a Go-compatible integer (1 if True, 0 if False). |
from_go_bool |
Converts a Go-compatible integer (ctypes.c_int) to a Python boolean. |
to_go_bytes |
Converts a Python bytes object to a Go-compatible C char pointer. |
from_go_bytes |
Converts a Go pointer representing a byte array to a Python bytes object. |
from_go_ptr_to_json |
Converts a C void* pointer (returned by Go as int) pointing to a |
to_go_json |
Encodes a Python object to a JSON string, returning a UTF-8 encoded |
from_go_string_to_list |
Decodes a JSON-encoded list from a Go C char pointer into a Python list. |
Source code in unaiverse/networking/p2p/lib_types.py
Methods:¶
to_go_string
¶
Converts a Python string to a UTF-8 encoded Python 'bytes' object.
This 'bytes' object is suitable for direct use with ctypes when passing to a C function expecting a 'char*' (ctypes.c_char_p), as ctypes will automatically pass a pointer to the byte string's data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The Python string. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
A Python 'bytes' object containing the UTF-8 encoded string. |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_string
¶
Converts a C char pointer (Go string) to a Python string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cstr
|
bytes
|
The C char pointer. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The decoded Python string. |
Source code in unaiverse/networking/p2p/lib_types.py
to_go_int
¶
Converts a Python integer to a Go-compatible ctypes integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
i
|
int
|
The Python integer. |
required |
Returns:
| Type | Description |
|---|---|
c_int
|
A ctypes.c_int equivalent. |
from_go_int
¶
Converts a ctypes.c_int from Go to a Python integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
c_int
|
The ctypes.c_int value. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The corresponding Python integer. |
to_go_float
¶
Converts a Python float to a Go-compatible ctypes float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
float
|
The Python float. |
required |
Returns:
| Type | Description |
|---|---|
c_float
|
A ctypes.c_float equivalent. |
from_go_float
¶
Converts a ctypes.c_float from Go to a Python float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
c_float
|
The ctypes.c_float value. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The corresponding Python float. |
Source code in unaiverse/networking/p2p/lib_types.py
to_go_bool
¶
Converts a Python boolean to a Go-compatible integer (1 if True, 0 if False).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
bool
|
The Python boolean. |
required |
Returns:
| Type | Description |
|---|---|
c_int
|
A ctypes.c_int (1 or 0). |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_bool
¶
Converts a Go-compatible integer (ctypes.c_int) to a Python boolean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
val
|
c_int
|
The ctypes.c_int value. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the value equals 1, False otherwise. |
Source code in unaiverse/networking/p2p/lib_types.py
to_go_bytes
¶
Converts a Python bytes object to a Go-compatible C char pointer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
b
|
bytes
|
The Python bytes. |
required |
Returns:
| Type | Description |
|---|---|
c_char_p
|
A ctypes.c_char_p pointing to the byte data. |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_bytes
¶
Converts a Go pointer representing a byte array to a Python bytes object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cptr
|
c_char_p
|
The C pointer to the byte array. |
required |
length
|
int
|
The number of bytes to read. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
A Python bytes object containing the read data. |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_ptr_to_json
¶
Converts a C void* pointer (returned by Go as int) pointing to a null-terminated C string containing JSON into a Python object.
It reads the string, parses it as JSON, and crucially frees the C memory using the provided FreeString function from the Go library.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c_void_ptr_val
|
int
|
The integer value representing the C pointer address. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The parsed Python object from the JSON string. |
Raises:
| Type | Description |
|---|---|
GoLibError
|
If the pointer is NULL, reading/decoding fails, or JSON parsing fails. |
TypeError
|
When go_lib is not a valid ctypes library object. |
Source code in unaiverse/networking/p2p/lib_types.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
to_go_json
¶
Encodes a Python object to a JSON string, returning a UTF-8 encoded Python 'bytes' object.
This 'bytes' object is suitable for direct use with ctypes when passing to a C function expecting a 'char*' (ctypes.c_char_p).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
The Python object (e.g., dict, list) to encode. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
A Python 'bytes' object containing the JSON string, UTF-8 encoded. |
Source code in unaiverse/networking/p2p/lib_types.py
from_go_string_to_list
¶
Decodes a JSON-encoded list from a Go C char pointer into a Python list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cstr
|
c_char_p
|
The Go string (C char pointer) containing a JSON list. |
required |
Returns:
| Type | Description |
|---|---|
List[Any]
|
A Python list representing the JSON data. |