Skip to content

What is RLP Serialization?

Published:  at  05:00 PM
Summary

Understanding RLP

  • RLP (Recursive Length Prefix) is an encoding method essential to Ethereum’s data management.
  • It efficiently handles arbitrarily nested arrays of binary data.
  • RLP ensures compact and effective data storage and transmission within the Ethereum network.

Prefix Details

  • Each item starts with a prefix byte indicating its type and length. - This prefix guides how the data should be interpreted and decoded.

RLP In Practice

  • RLP encodes transactions, blocks, and state data, making it integral to Ethereum’s operation.
  • Its role in the Ethereum Virtual Machine (EVM) underscores its importance in maintaining Ethereum’s performance and scalability.

RLP, or Recursive Length Prefix, might not be the most glamorous topic, but it’s the unsung hero behind Ethereum’s data efficiency. While JSON or XML are common in web development, RLP’s minimalist and efficient design is what makes Ethereum’s complex, nested data structures both compact and fast.

In Ethereum, RLP plays a crucial role by encoding transactions, blocks, and state data, contributing to the network’s efficiency and scalability. Its role is fundamental within the Ethereum Virtual Machine (EVM), ensuring that data is stored and transmitted in an optimal format.

The Basics

RLP stands for Recursive Length Prefix. It is a serialization method used in Ethereum to encode arbitrarily nested arrays of binary data. RLP is a binary encoding method that allows for efficient storage and transmission of data.

It has many uses, such as encoding transactions, blocks, and state data. RLP is used in Ethereum to encode data structures in a way that is efficient and easy to decode. It is a key component of the Ethereum protocol and is used extensively in the Ethereum Virtual Machine (EVM).

The Gory Details

What can be encoded with RLP?

RLP can encode an “item”, which is defined as one of:

More formally, an item can be described in TypeScript as:

type RLPItem = number | string | Uint8Array | RLPItem[];

What’s the “prefix” in RLP?

The “P” in RLP stands for “prefix”. Each item in RLP encoding starts with a single byte that indicates both the type of the item and its length, providing a way to determine how to decode the following data.

There are 5 types of values represented by the prefix byte:

The data that follows the prefix byte is the actual data being encoded.

These are a little hard to fully digest, so let’s look at an example for each.

Examples

Values less than or equal to 0x7f (127) are encoded as themselves:

If the value is between 0-55 bytes long (and does not match the above), it is encoded as 0x80 + the length of the byte array + the byte array:

Otherwise, if the value is longer than 55 bytes, it is encoded as 0xb7 + the length of the length + the length + the byte array:

However, if we’re encoding a list of items, we have a different prefix byte. If the total encoded length of the list is less than 56 bytes, it is encoded as 0xc0 + the length of the list + the items:

And finally, if the total encoded length of the list is greater than 55 bytes, it is encoded as 0xf7 + the length of the length + the length + the items:

Decoding RLP

I’ve created a handy tool that allows you to paste in an RLP-encoded string and see the decoded result. If you’re not quite sure about some of the examples above, or just want to play around, here’s your chance!

Invalid hex string (does it start with "0x"?)

Decoded Result

Failed to decode

Explanation

Failed to decode

RLP In Practice

There are many occurrences where RLP is used in Ethereum. Here are a few examples:

It is also a building block used in many other concepts, including Patricia Merkle Tries (which will be introduced in the next article - stay tuned!).

Conclusion

RLP Serialization is the left-pad of Ethereum—essential yet often underappreciated. It provides a robust mechanism for encoding complex data structures efficiently, a role in maintaining Ethereum’s performance and scalability that cannot be overstated.

By understanding RLP, you’re preparing to dive into many topics in Ethereum, such as the one we’ll cover next: Patricia Merkle Tries. Stay tuned for more!