A python implementation of the canonical serialization for the Libra network.
Canonical serialization guarantees byte consistency when serializing an in-memory
data structure. It is useful for situations where two parties want to efficiently compare
data structures they independently maintain. It happens in consensus where
independent validators need to agree on the state they independently compute. A cryptographic
hash of the serialized data structure is what ultimately gets compared. In order for
this to work, the serialization of the same data structures must be identical when computed
by independent validators potentially running different implementations
of the same spec in different languages.
Installation
Require python 3.6 or above installed.
$ python3 -m pip install canoser
Usage
First define a data structure with Canoser, that is, write a class that inherits from "canoser.Struct", and then define the fields owned by the structure through the "_fields" array. This structure naturally has the ability to canonical serialize and deserialize types. For example, the following AccountResource defines a data structure of the same name in the Libra code:
#python code,define canoser data structure
from canoser import Struct, Uint8, Uint64
class AccountResource(Struct):
_fields = [
('authentication_key', [Uint8]),
('balance', Uint64),
('delegated_withdrawal_capability', bool),
('received_events', EventHandle),
('sent_events', EventHandle),
('sequence_number', Uint64)
]
Here is the code that defines this data structure and serialization in Libra code:
In the rust language used by Libra, it is necessary to manually write code to serialize/deserialize the data structure, and the order of the fields in the data structure and the order of serialization are not necessarily the same.
In Canoser, after defining the data structure, you don't need to write code to implement serialization and deserialization. Note that the order of the data structures in Canoser is defined in the order in which they are serialized in Libra.
Supported field types
field type
optionl sub type
description
canoser.Uint8
Unsigned 8-bit integer
canoser.Uint16
Unsigned 16-bit integer
canoser.Uint32
Unsigned 32-bit integer
canoser.Uint64
Unsigned 64-bit integer
canoser.Uint128
Unsigned 128-bit integer
canoser.Int8
Signed 8-bit integer
canoser.Int16
Signed 16-bit integer
canoser.Int32
Signed 32-bit integer
canoser.Int64
Signed 64-bit integer
canoser.Int128
Signed 128-bit integer
bool
Boolean
str
String
bytes
Binary String
[]
supported
Array Type
{}
supported
Map Type
()
supported
Tuple Type
canoser.Struct
Another structure nested (cannot be recycled)
RustEnum
Enum type
RustOptional
Optional type
About Array Type
The default data type (if not defined) in the array is Uint8. The following two definitions are equivalent:
class Arr1(Struct):
_fields = [(addr, [])]
class Arr2(Struct):
_fields = [(addr, [Uint8])]
Arrays can also define lengths to represent fixed length data. For example, the address in Libra is 256 bits, which is 32 bytes, so it can be defined as follows:
class Address(Struct):
_fields = [(addr, [Uint8, 32])]
When the fixed length data is serialized, you can skip the length information written to the output. For example, following code will generate 32 bytes without writing the length of the addr during serialization.
class Address(Struct):
_fields = [(addr, [Uint8, 32, False])]
About map type
The default data type (if not defined) in the map is an array of Uint8 in Libra, both of key and value.
But the python language dosn't support the array data type to be the key of a dict, so we change the key type from array of Uint8 to bytes in python, the type of value is unchanged.
The following two definitions are equivalent:
class Map1(Struct):
_fields = [(addr, {})]
class Map2(Struct):
_fields = [(addr, {bytes : [Uint8] })]
About enum type
In python and C, enum is just enumerated constants. But in Libra(Rust), a enum has a type constant and a optional Value. A rust enumeration is typically represented as:
To define a enum with Canoser, first write a class that inherits from "canoser.RustEnum", and then define the types owned by the enum through the "_enums" array.
For example, the following TransactionArgument defines a data structure of the same name in the Libra code. The argument of a transaction can be one of the four types: Uint64 or Address or String or IntArray.:
Every RustEnum object has an index property and a value property. After instantiated, the index can't be modified. You can only modify the value of an enum with correct data type.
You can also instantiate a RustEnum object by index and value.
e1 = Enum1.new(0, [5])
# which is equivalent to
e1 = Enum1('opt1', [5])
About optional type
An optional type in libra is a nullable data either exists in its full representation or does not. For example,
optional_data: Option(uint8); // Rust/Libra
uint8 *optional_data; // C
It has similar semantics meaning with the following enum type:
enum Option<uint8> {
Some(uint8),
None,
}
To define a optional with Canoser, first write a class that inherits from "canoser.RustOptional", and then define the types owned by RustOptional through the "_type" field. For example,
class OptionStr(RustOptional):
_type = str
class OptionTest(Struct):
_fields = [
('message', OptionStr)
]
def __init__(self, msg=None):
if msg is not None:
self.message = OptionStr(msg)
else:
self.message = OptionStr(None)
test = OptionTest('test_str')
assert test.message.value == 'test_str'
The RustOptional type in canoser is similar to typing.Optional in python. Note that this is not the same concept as an optional argument, which is one that has a default.
Nested data structure
The following is a complex example with three data structures:
This example refers to the test code from canonical serialization in libra.
Serialization and deserialization
After defining canoser.Struct, you don't need to implement serialization and deserialization code yourself, you can directly call the default implementation of the base class. Take the AccountResource structure as an example:
# serialize an object
obj = AccountResource(authentication_key=...,...)
bbytes = obj.serialize()
# deserialize an object from bytes
obj = AccountResource.deserialize(bbytes)
Json pretty print
For any canoser Struct, you can call the to_json method to get a formatted json string:
# serialize an object
print(obj.to_json())
Get field value from object
For all fields defined by the "_fields", the value of this field of an object can be obtained via field_name. such as: