EIP-6475 · Stagnant · Core · 2023-02-09
EIP-6475: SSZ Optional
Proposes a native Optional[T] type for SSZ, the consensus layer's serialization format, so a value can be explicitly absent without awkward list or union workarounds.
What problem does it solve?
Simple Serialize (SSZ) is the encoding and Merkleization scheme used by Ethereum's consensus layer. It has no way to say "this field may be missing". Spec writers have to fake it, typically with a List[T, 1] (zero or one element) or a Union[None, T]. Both work but are clumsy:
List[T, 1]needs an offset table for variable-sizeT, so it serializes larger than necessary.Uniontypes had no finalized design and were not used in any final specification, andUnion[None, T]hints that more variants might be added later.
EIP-6475 proposes a dedicated Optional[T] that maps cleanly onto Optional types in programming languages.
How it works
- Type:
Optional[T]holds either a value of SSZ typeT, orNone. The default isNone. - Serialization:
Noneserializes to an empty byte string. A present value serializes as0x01followed byserialize(value). - Deserialization: an empty input means
None; otherwise the first byte must be0x01and the rest is decoded asT. - Merkleization: identical to
List[T, 1]: length 0 forNone, length 1 with the value otherwise. So the hash tree root is the same as the list workaround, while the byte encoding is more compact.
Sample code
Python-style reference logic:
def serialize_optional(value, serialize_t) -> bytes:
if value is None:
return b""
return b"\x01" + serialize_t(value)
def deserialize_optional(data: bytes, deserialize_t):
if len(data) == 0:
return None
assert data[0] == 0x01, "invalid Optional prefix"
return deserialize_t(data[1:])
def hash_tree_root_optional(value, T):
# Merkleized exactly like List[T, 1]
return hash_tree_root(List[T, 1]([] if value is None else [value]))
# Example with T = uint64
assert serialize_optional(None, ser_u64) == b""
assert serialize_optional(5, ser_u64) == b"\x01" + (5).to_bytes(8, "little")
Things to know
- Status: Stagnant.
Optional[T]is not part of the SSZ specification used by Ethereum clients today. - Because the encoding of
Noneis empty, anOptionalis variable-size: inside a container it is placed behind an offset like other variable-size fields. - Later SSZ work has focused on other extensions, such as EIP-7495 (ProgressiveContainer) and EIP-7916 (ProgressiveList), which address forward-compatible containers and unbounded lists.