Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add union type and null to SSZ #893

Merged
merged 14 commits into from
May 7, 2019
34 changes: 31 additions & 3 deletions specs/simple-serialize.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ This is a **work in progress** describing typing, serialization and Merkleizatio
- [Typing](#typing)
- [Basic types](#basic-types)
- [Composite types](#composite-types)
- [Illegal empty composites](#illegal-empty-composites)
- [Aliases](#aliases)
- [Default values](#default-values)
- [Serialization](#serialization)
- [`"uintN"`](#uintn)
- [`"bool"`](#bool)
- [Containers, vectors, lists](#containers-vectors-lists)
- [Vectors, containers, lists, unions](#vectors-containers-lists-unions)
- [Deserialization](#deserialization)
- [Merkleization](#merkleization)
- [Self-signed containers](#self-signed-containers)
Expand Down Expand Up @@ -41,8 +42,14 @@ This is a **work in progress** describing typing, serialization and Merkleizatio
* angle bracket notation `[type, N]`, e.g. `["uint64", N]`
* **list**: ordered variable-length homogeneous collection of values
* angle bracket notation `[type]`, e.g. `["uint64"]`
* **union**: union type containing one of the given subtypes
* round bracket notation `(type1, type2, ...)`, e.g. `("uint64", "null")`

We recursively define "variable-size" types to be lists and all types that contains a variable-size type. All other types are said to be "fixed-size".
We recursively define "variable-size" types to be lists and unions and all types that contain a variable-size type. All other types are said to be "fixed-size".
dankrad marked this conversation as resolved.
Show resolved Hide resolved

#### Illegal empty composites

The empty container `{}` (except as the `"null"` type inside a union, see below) and the empty fixed length list `[type, 0]` are **not** legal types.

### Aliases

Expand All @@ -51,6 +58,9 @@ For convenience we alias:
* `"byte"` to `"uint8"` (this is a basic type)
* `"bytes"` to `["byte"]` (this is *not* a basic type)
* `"bytesN"` to `["byte", N]` (this is *not* a basic type)
* `"null"`: `{}`, i.e. the empty container

The `"null"` type is only legal as a union sub-type.

### Default values
JustinDrake marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -77,7 +87,13 @@ assert value in (True, False)
return b"\x01" if value is True else b"\x00"
```

### Containers, vectors, lists
### `"null"`

```python
return b""
JustinDrake marked this conversation as resolved.
Show resolved Hide resolved
```

### Vectors, containers, lists, unions

```python
# Reccursively serialize
Expand All @@ -97,6 +113,16 @@ fixed_parts = [part if part != None else variable_offsets[i] for i, part in enum
return b"".join(fixed_parts + variable_parts)
```

If `value` is an union type:
dankrad marked this conversation as resolved.
Show resolved Hide resolved

Define value as an object that has properties `value.value` with the contained value, and `value.type_index` which indexes the type.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO maybe too many "value"s in these sentences and code. Perhaps using value.contained_value would be more readable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we use object instead? (That's what these things are after all, objects.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean value.object or object.value @JustinDrake ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking object.value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to go with that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh but 'object' is a type in python :(


```python
serialized_bytes = serialize(value.value)
serialized_type_index = value.type_index.to_bytes(BYTES_PER_LENGTH_PREFIX, "little")
dankrad marked this conversation as resolved.
Show resolved Hide resolved
return serialized_type_index + serialized_bytes
```

## Deserialization

Because serialization is an injective function (i.e. two distinct objects of the same type will serialize to different values) any bytestring has at most one object it could deserialize to. Efficient algorithms for computing this object can be found in [the implementations](#implementations).
Expand All @@ -108,13 +134,15 @@ We first define helper functions:
* `pack`: Given ordered objects of the same basic type, serialize them, pack them into `BYTES_PER_CHUNK`-byte chunks, right-pad the last chunk with zero bytes, and return the chunks.
* `merkleize`: Given ordered `BYTES_PER_CHUNK`-byte chunks, if necessary append zero chunks so that the number of chunks is a power of two, Merkleize the chunks, and return the root.
* `mix_in_length`: Given a Merkle root `root` and a length `length` (`"uint256"` little-endian serialization) return `hash(root + length)`.
* `mix_in_type`: Given a Merkle root `root` and a type_index `type_index` (`"uint256"` little-endian serialization) return `hash(root + type_index)`.

We now define Merkleization `hash_tree_root(value)` of an object `value` recursively:

* `merkleize(pack(value))` if `value` is a basic object or a vector of basic objects
* `mix_in_length(merkleize(pack(value)), len(value))` if `value` is a list of basic objects
* `merkleize([hash_tree_root(element) for element in value])` if `value` is a vector of composite objects or a container
* `mix_in_length(merkleize([hash_tree_root(element) for element in value]), len(value))` if `value` is a list of composite objects
* `mix_in_type(merkleize(value.value), value.type_index)` if `value` is of union type

## Self-signed containers

Expand Down