15

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?

3 Answers 3

12

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 MaxEncodedLen and TypeInfo.

It doesn't aim to be self-descriptive (e.g. encoding field names) or versioned and doesn't expend design bandwidth on those features.

13

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.

10

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.

3
  • I am curious, is the WASM interpreter that comes with substrate understand SCALE encoded rust structs and enums specifically , meaning was the wasm interpreter in substrate built custom for substrate to include SCALE support? Feb 17, 2022 at 7:59
  • 1
    The WASM execution isn't aware of SCALE and is also not using any SCALE encoded data (on the engine level) when executing the runtime.
    – bkchr
    Feb 17, 2022 at 8:32
  • 1
    So this answer needs rectifying to put things more simply and say: runtimes only understand byte code and SCALE helps applications efficiently decode and encode data for them. Runtime data needs to be as light as possible, a capability that SCALE provides. And its performance is owed to it being built for LE architectures, which is compatible with Wasm environments. Feb 17, 2022 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.