SCALE codec is used to encode and decode data for Substrate runtimes. Why is it the top choice in Substrate and can I implement my own SCALE library?
There are a few reasons, some historical and others not. Main reasons:
- Simple to define.
- Not Rust-specific.
- Consensus critical/bijective; one value will always encode to one blob and that blob will only decode to that value.
- Supports a copy-free decode for basic types on LE architectures.
- Viable for useful APIs like
It doesn't aim to be self-descriptive (e.g. encoding field names) or versioned and doesn't expend design bandwidth on those features.
SCALE - Simple Concatenated Aggregate Little-Endian is a binary encoding. In contrast to encodings like JSON, it just stores the data without any information about the structure of the data. This has the advantage of saving a lot of space when it comes to storing the data. Storing data is expensive on blockchains (because every node needs to store the data), so the encoding should be as space efficient as possible. Besides that, SCALE also stores all data in little endian. As WASM is also using little endian, it makes the encoding and decoding more performant.
And yes, you can implement your own SCALE implementation. There exists multiple of them besides the one from Parity.
SCALE codec is an efficient serializer and deserializer built for Substrate chains. It is lightweight, platform agnostic and enables compact encoding of Rust types and data structures.
In order to understand why Substrate uses SCALE, it helps to think about the constrains we're working with in the runtime of the blockchains we're building. We have limited storage and decoding values needs to be fast and efficient. All types and custom data structures a runtime exposes need to be in the most compact and light weight form. So we use SCALE because it can give us those guarantees. What makes it suitable for this:
- It's built for little endian architectures, making it suitable for Wasm.
- It uses bijectiveness, making it efficient for checking equality between two functions.
- It guarantees that data is as light as possible, supporting compact encoding of values.
Thus, the SCALE specification is a critical piece enabling applications to interact with a runtime. For an application (such as a front-end, CLI or blockchain client) to send a transaction to a runtime, it will need to encode data for the runtime to process it. Similarly, when an application receives data from the runtime, it will need to decode it.
Applications built for Substrate chains can implement a codec library in any language so long as it follows the SCALE specification. You can find some implementations of SCALE in different languages here.
This page has some more details on the encoding of various types.