unaiverse.networking.p2p.lib_types¶
The native networking library of the SDK is a shared library loaded through ctypes (see unaiverse.networking.p2p). Its functions take C strings, C integers and byte buffers and most of them return a pointer to a C string that holds JSON and that Python must free. This module holds TypeInterface, the class that converts the arguments into those C types and reads the results back. P2P owns one shared instance of it and uses it around every call. The developer never calls it.
Two methods read the replies of the library. from_go_ptr_to_json reads a JSON reply and frees it. The batch of received messages has a binary layout of its own and from_go_pop_messages reads and frees that one. A JSON reply of the library has the shape {"state": "Success" | "Error" | "Empty", "message": ...} and some replies add a "code" or a "seq" field.
The standard logging.Logger of this module is named "LIB-TYPES" and has level WARNING. It logs a reply that cannot be read and, at level CRITICAL, a failure to free the memory of a reply.
TypeInterface
¶
Converts values between Python and the C types of the native networking library. The constructor takes the loaded library as libp2p_instance and keeps it as the attribute libp2p. The methods that free a reply call the free function of the library through that attribute.
In the SDK there is exactly one instance. P2P.setup_library builds it with TypeInterface(P2P.libp2p) and stores it as P2P._type_interface. The package unaiverse.networking.p2p also assigns the loaded library to the class attribute TypeInterface.libp2p at import time.
Most methods are stateless conversions and only the two readers of a reply free memory. The converters to_go_* serve the arguments of a call and the converters from_go_* read its results.
The loaded native library, an object of ctypes.CDLL typed as the stub class GoLibP2P. The package assigns it to the class at import time and the constructor assigns it again to the instance.
to_go_string
¶
Source code in unaiverse/networking/p2p/lib_types.py
Encodes a str to UTF-8 bytes, the value ctypes passes to an argument of type c_char_p. None becomes b"".
from_go_string
¶
Source code in unaiverse/networking/p2p/lib_types.py
Decodes UTF-8 bytes to a str. An empty or None value gives "". The method does not free anything and is not used by P2P.
to_go_int
¶
Wraps a Python int in a ctypes.c_int. P2P passes the instance index and the payload lengths through it. A value outside the range of a signed 32-bit integer wraps around without an error.
from_go_int
¶
Returns int(val). The method works on a plain Python int. Its annotation names ctypes.c_int even though int() raises ValueError on a c_int object in the current version (read val.value instead). Nothing in the SDK calls it.
to_go_float
¶
Wraps a Python float in a ctypes.c_float (single precision). Nothing in the SDK calls it.
from_go_float
¶
Source code in unaiverse/networking/p2p/lib_types.py
Returns float(val). As with from_go_int, it works on a plain Python number and raises ValueError on a ctypes.c_float object. Nothing in the SDK calls it.
to_go_bool
¶
Source code in unaiverse/networking/p2p/lib_types.py
Returns ctypes.c_int(1) for a true value and ctypes.c_int(0) otherwise. Nothing in the SDK calls it.
from_go_bool
¶
Source code in unaiverse/networking/p2p/lib_types.py
Returns val == 1. The comparison holds for the Python int 1. Because a ctypes.c_int object never compares equal to an int, the method returns False on such an object whatever its value. Nothing in the SDK calls it.
to_go_bytes
¶
Source code in unaiverse/networking/p2p/lib_types.py
Prepares a byte payload for an argument of type c_char_p without copying it into a ctypes buffer. A bytes object is returned as it is. A bytearray or a memoryview is turned into bytes (one copy) and None becomes b"". A payload may contain zero bytes because the library copies the buffer during the call using the length that it receives in a separate argument.
from_go_bytes
¶
Source code in unaiverse/networking/p2p/lib_types.py
Reads length bytes at the C pointer cptr with ctypes.string_at and returns them as bytes without freeing the memory. A null pointer or a non-positive length gives b"". Nothing in the SDK calls the method.
from_go_ptr_to_json
¶
Source code in unaiverse/networking/p2p/lib_types.py
Reads the JSON reply of a library call and frees it. This is the reader of every call of P2P except the pop of the received messages.
| Argument | Type | Meaning |
|---|---|---|
c_void_ptr_val |
int |
The address of a NUL-terminated C string that the library allocated. ctypes returns a c_void_p result in this form. |
The method reads the string, decodes it as UTF-8 and parses it with json.loads. It returns the parsed object, usually a dict with "state" and "message". Whatever happens in the reading, the finally block passes the pointer to FreeString of the library exactly once. A failure of the free is logged at level CRITICAL and is never raised. An error of the reading therefore propagates unchanged.
It raises a plain Exception (not a dedicated class) in three cases: (1) a null pointer, (2) a string that cannot be read or decoded and (3) a string that is not valid JSON. P2P wraps these into a P2PError or treats them as a failed call.
from_go_pop_messages
¶
Source code in unaiverse/networking/p2p/lib_types.py
Reads the reply of a pop of the received messages (PopMessages, PopNetworkMessages, PopStreamMessages) and frees it. P2P.pop_messages calls it at every cycle of the node.
The first byte of the reply tells which of two forms it has.
- A JSON status (
{"state": "Empty"}or{"state": "Error", "message": ...}) starts with{. The method returns the parsed dict. - A batch of messages starts with the byte
0xB1. The method returns alist[dict]with one dict per message.
The binary batch has the layout below. Every integer in it is a big-endian uint32.
[0xB1][total length][count]
then, count times:
[len][sender peer ID][len][protocol ID][len][data][len][files JSON]
Each returned dict has three keys.
"from"is the peer ID of the sender as the transport verified it."protocol"is the protocol ID: one of the Session protocols or the protocol of the chat messages."data"is amemoryviewof the batch (not a copy) that the binary message format parses as it is.
A file message adds "files". This key holds the parsed JSON list of the files that the library wrote to disk, each a dict with "key", "path" and "size" (see files).
The payloads of the batch may contain zero bytes and a read that stops at the first zero byte would truncate them. The method therefore reads the binary batch with an explicit length. The method frees the pointer in a finally block as from_go_ptr_to_json does and raises a plain Exception for a null pointer.
to_go_json
¶
Source code in unaiverse/networking/p2p/lib_types.py
Serializes a Python object with json.dumps and returns it as UTF-8 bytes. P2P passes every structured argument through it: the configuration of the library and of the node, the list of addresses of a dial and the relay servers of the browser transport. A value that JSON cannot represent raises TypeError.
>>> ti.to_go_json(["/ip4/203.0.113.7/tcp/4001/p2p/12D3KooWAbc"])
b'["/ip4/203.0.113.7/tcp/4001/p2p/12D3KooWAbc"]'
from_go_string_to_list
¶
Source code in unaiverse/networking/p2p/lib_types.py
Decodes bytes with from_go_string and parses them with json.loads. The method expects a JSON list but returns whatever the JSON holds. It frees nothing and nothing in the SDK calls it.