SCALE Encoding
Substrate uses a lightweight and efficient encoding and decoding program to optimize how data is sent and received over the network. The program used to serialize and deserialize data is called the SCALE codec, with SCALE being an acronym for simple concatenated aggregate little-endian.
The SCALE codec is a critical component for communication between the runtime and the outer node.
It is designed for high-performance, copy-free encoding and decoding of data in resource-constrained execution environments like the Substrate WebAssembly runtime.
The SCALE codec is not self-describing in any way. It assumes the decoding context has all type knowledge about the encoded data. Front-end libraries maintained by Parity use the parity-scale-codec
crate—which is a Rust implementation of the SCALE codec—to encode and decode interactions between RPCs and the runtime.
SCALE codec is advantageous for Substrate and blockchain systems because:
It is lightweight relative to generic serialization frameworks like serde, which add significant boilerplate that can bloat the size of the binary.
It does not use Rust
libstd
making it compatible withno_std
environments that compile to Wasm, such as the Substrate runtime.It is built to have great support in Rust for deriving codec logic for new types using:
#[derive(Encode, Decode)]
.
It's important to define the encoding scheme used in Substrate rather than reuse an existing Rust codec library because this codec needs to be re-implemented on other platforms and languages that want to support interoperability among Substrate blockchains.
The following table shows how the Rust implementation of the Parity SCALE codec encodes different types.
SCALE codec examples of different types
Type | Description | Example SCALE decoded value | SCALE encoded value |
---|---|---|---|
Fixed-width integers | Basic integers are encoded using a fixed-width little-endian (LE) format. |
|
|
|
| ||
|
| ||
Compact/general | A "compact" or general integer encoding is sufficient for encoding large integers (up to 2**536) and is more efficient at encoding most values than the fixed-width version. (Though for single-byte values, the fixed-width integer is never worse.) |
|
|
|
| ||
|
| ||
|
| ||
|
| ||
|
| ||
Boolean | Boolean values are encoded using the least significant bit of a single byte. |
|
|
|
| ||
Results are commonly used enumerations which indicate whether certain operations were successful or unsuccessful. |
|
| |
|
| ||
Options | One or zero values of a particular type. |
|
|
|
| ||
Vectors (lists, series, sets) | A collection of same-typed values is encoded, prefixed with a compact encoding of the number of items, followed by each item's encoding concatenated in turn. | Vector of unsigned 16-bit integers: |
|
Strings | Strings are Vectors of bytes ( | ||
Tuples | A fixed-size series of values, each with a possibly different but predetermined and fixed type. This is simply the concatenation of each encoded value. | Tuple of compact unsigned integer and boolean: |
|
Structs | For structures, the values are named, but that is irrelevant for the encoding (names are ignored - only order matters). All containers store elements consecutively. The order of the elements is not fixed, depends on the container, and cannot be relied on at decoding. This implicitly means that decoding some byte-array into a specified structure that enforces an order and then re-encoding it could result in a different byte array than the original that was decoded. | A |
|
Enumerations (tagged-unions) | A fixed number of variants, each mutually exclusive and potentially implying a further value or series of values. Encoded as the first byte identifying the index of the variant that the value is. Any further bytes are used to encode any data that the variant implies. Thus, no more than 256 variants are supported. |
|
|
SCALE Codec has been implemented in other languages, including:
Python:
polkascan/py-scale-codec
Golang:
itering/scale.go
JavaScript:
polkadot-js/api
TypeScript:
parity-scale-codec-ts
,scale-ts
,soramitsu/scale-codec-js-library
AssemblyScript:
LimeChain/as-scale-codec
Haskell:
airalab/hs-web3
Java:
emeraldpay/polkaj
Ruby:
wuminzhe/scale_rb
Last updated