Serialization Primitives
How AvalancheGo encodes primitive types in its binary format.
AvalancheGo uses one simple binary format for P-Chain and X-Chain data, such as transactions, blocks and UTXOs. It also uses this format for C-Chain atomic (import and export) transactions, ProposerVM block headers and Warp messages. This document describes how this format encodes primitive types. Transactions are encoded in terms of these primitive types. Other data uses other formats. For example, P2P messages use Protocol Buffers, and C-Chain EVM blocks and transactions use RLP.
Byte
Bytes are packed as-is into the message payload.
Example:
Packing:
0x01
Results in:
[0x01]Short
Shorts are packed in BigEndian format into the message payload.
Example:
Packing:
0x0102
Results in:
[0x01, 0x02]Integer
Integers are 32-bit values packed in BigEndian format into the message payload.
Example:
Packing:
0x01020304
Results in:
[0x01, 0x02, 0x03, 0x04]Long Integers
Long integers are 64-bit values packed in BigEndian format into the message payload.
Example:
Packing:
0x0102030405060708
Results in:
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]IP Addresses
IP addresses are represented in the 16-byte IPv6 format, with the port appended as a Short. An IPv4 address uses the IPv4-mapped IPv6 form: 10 bytes of 0x00, then 2 bytes of 0xff, then the 4 bytes of the IPv4 address. AvalancheGo uses this format in the bytes that a node signs to claim its IP. P2P messages carry the IP as a protobuf bytes field instead.
IPv4 example:
Packing:
"127.0.0.1:9650"
Results in:
[
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01,
0x25, 0xb2,
]IPv6 example:
Packing:
"[2001:0db8:ac10:fe01::]:12345"
Results in:
[
0x20, 0x01, 0x0d, 0xb8, 0xac, 0x10, 0xfe, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x30, 0x39,
]Fixed-Length Array
Fixed-length arrays, whose length is known ahead of time and by context, are packed in order.
Byte array example:
Packing:
[0x01, 0x02]
Results in:
[0x01, 0x02]Integer array example:
Packing:
[0x03040506]
Results in:
[0x03, 0x04, 0x05, 0x06]Variable Length Array
The length of the array is prefixed in Integer format, followed by the packing of the array contents in Fixed Length Array format.
Byte array example:
Packing:
[0x01, 0x02]
Results in:
[0x00, 0x00, 0x00, 0x02, 0x01, 0x02]Int array example:
Packing:
[0x03040506]
Results in:
[0x00, 0x00, 0x00, 0x01, 0x03, 0x04, 0x05, 0x06]String
A String is packed similarly to a variable-length byte array. However, the length prefix is a short rather than an int. Strings are encoded in UTF-8 format.
Example:
Packing:
"Avax"
Results in:
[0x00, 0x04, 0x41, 0x76, 0x61, 0x78]Is this guide helpful?