eth-rlp
eth-rlp provides RLP (Recursive Length Prefix) serialization definitions for common Ethereum objects in Python. It's used to encode arbitrarily nested arrays of binary data into a space-efficient format, crucial for data transfer between Ethereum nodes and object serialization within the Ethereum execution layer. The library is currently at version 2.2.0 and receives updates to align with Ethereum protocol specifications.
Warnings
- breaking Ethereum's EIP-7934 introduced a strict upper limit on the size of RLP-encoded execution blocks (10 MiB plus a safety margin). Applications using `eth-rlp` to create large transactions or data structures must ensure they adhere to these new size constraints, as oversized blocks are non-backward compatible and will be rejected by the network.
- gotcha RLP is designed specifically for encoding arbitrarily nested arrays of binary data. It does not inherently handle all Python data types (e.g., floats, complex objects beyond basic lists/bytes/integers) directly. Higher-order protocols or libraries (like `eth-rlp` using dataclasses) are required to map complex Python objects to RLP-compatible structures. Positive integers must be represented in big-endian binary form with no leading zeros.
- gotcha Ethereum's execution layer predominantly uses RLP for serialization, while the newer consensus layer (Beacon Chain) has transitioned to Simple Serialize (SSZ). Developers interacting with both layers must be aware of these distinct serialization standards, as they are not interchangeable and can introduce complexity when building unified clients or applications.
- gotcha The RLP encoding structure for EIP-1559 transactions has been refined (e.g., to avoid an 'extra empty byte' for legacy gas price fields). Applications relying on a precise RLP byte-level representation of EIP-1559 transactions, especially older implementations, might encounter discrepancies with current specifications if not updated.
Install
-
pip install eth-rlp
Imports
- encode
from ethereum_rlp import encode
- decode_to
from ethereum_rlp import decode_to
Quickstart
from dataclasses import dataclass
from ethereum_rlp import encode, decode_to
from ethereum_types.numeric import Uint
from typing import List
@dataclass
class Stuff:
toggle: bool
number: Uint
sequence: List["Stuff"]
# Encode an object
original_stuff = Stuff(toggle=True, number=Uint(3), sequence=[])
encoded_data = encode(original_stuff)
print(f"Encoded: {encoded_data.hex()}")
# Decode back to the object type
decoded_stuff = decode_to(Stuff, encoded_data)
print(f"Decoded number: {decoded_stuff.number}")
assert decoded_stuff.number == Uint(3)