EIP-6475 SSZ Optional Value Type (SSZ Optional)
No call decision · Consensus & staking · EIP status Stagnant · created 2023-02-09
An EIP that adds a new Optional[T] type to SSZ, the consensus layer's serialization format. It is a type representing 'a value that may or may not be present.'
Why it is needed
SSZ currently has no dedicated type for expressing Optional (a state where a value may or may not be present). As a result, developers have mimicked it using variations of other types such as Union[None, T] or List[T, 1]. There are two problems. The Union type has never actually been used within SSZ and its design is not finalized, so using it just to express a single Optional pulls in an unfinished feature. List[T, 1] prepends an offset table recording item positions when holding a variable-length value, making the serialized data longer. With a dedicated Optional[T] type, code reads naturally like the Optional syntax found in programming languages, and the serialized result is also shorter.
What changes
Today
- SSZ lacks a dedicated Optional[T] type
- Presence or absence of a value is worked around using Union[None, T] or List[T, 1]
- The Union type's design is unfinalized and it has never been used in an actual spec
- List[T, 1] becomes longer due to the offset table attached for variable-length values
After EIP-6475
- Optional[T] is added as a basic SSZ type
- Serializes to an empty byte string if the value is absent, or to 0x01 followed by the value if present
- Merkleization is handled the same way as List[T, 1] (length 0 or 1)
- Can be handled in code as a natural type like Optional[T]
Who is affected
- Consensus layer client developers: Optional fields can be defined directly in SSZ structs, eliminating the need for workaround implementations.
- Spec authors and researchers: A standard means of expressing presence or absence of a value becomes available when designing SSZ structs going forward.
- Ordinary users: This is a refinement of the internal data format with no directly felt impact.
In depth
Type Definition and Default Value
Optional[T] is defined as a type that either holds a value of SSZ type T, or holds None, meaning no value is present. The default value of Optional[T] is None.
Serialization Rules
If the value is None, it returns an empty byte string (b""). If a value is present, it writes a single 0x01 byte followed directly by the serialization of T. In other words, the mere presence or absence of a serialized result itself acts as a flag indicating whether the value exists.
Deserialization Rules
Deserialization is determined by the input length. If the length is 0, it is None. If the length is nonzero, the first byte must be 0x01, and the remaining bytes are deserialized as T.
Merkleization Identical to List[T, 1]
Optional[T] is merkleized the same way as List[T, 1]. If the value is None, the list length is treated as 0; if a value is present, the list length is treated as 1, with the actual value placed in that single element to build the Merkle tree.
Why Not Use Union[None, T]
Union[None, T] leaves ambiguity, since the type could later expand to something like Union[None, T, U]. Also, the SSZ Union type itself is not currently used in any finalized spec and its design is not settled. If the sole purpose is to express Optional[T], a simpler Optional[T] type suffices, and general Union support can be deferred until it is genuinely needed. The design of Optional[T] could also serve as a foundation for a more general Union design later.
Why Not Use List[T, 1]
For a variable-length T, List[T, 1] results in a longer serialization than the Optional[T] approach because an offset table indicating the list length is prepended to the data.
Backward compatibility and current status
The source states explicitly that there is no backwards compatibility issue, since the current workarounds mimicking Optional[T] via Union[None, T] or List[T, 1] are not actually in use. Security considerations are also listed as None in the source. This EIP's status is Stagnant, meaning discussion has stalled and is not progressing further.