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

Rename "tuple" to "vector" in SSZ spec #794

Merged
merged 1 commit into from
Mar 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions specs/simple-serialize.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This is a **work in progress** describing typing, serialization and Merkleizatio
- [Serialization](#serialization)
- [`"uintN"`](#uintn)
- [`"bool"`](#bool)
- [Tuples, containers, lists](#tuples-containers-lists)
- [Vectors, containers, lists](#vectors-containers-lists)
- [Deserialization](#deserialization)
- [Merkleization](#merkleization)
- [Self-signed containers](#self-signed-containers)
Expand All @@ -34,12 +34,14 @@ This is a **work in progress** describing typing, serialization and Merkleizatio
### Composite types

* **container**: ordered heterogenous collection of values
* key-pair curly bracket notation `{}`, e.g. `{'foo': "uint64", 'bar': "bool"}`
* **tuple**: ordered fixed-length homogeneous collection of values
* key-pair curly bracket notation `{}`, e.g. `{"foo": "uint64", "bar": "bool"}`
* **vector**: ordered fixed-length homogeneous collection of values
* angle bracket notation `[type, N]`, e.g. `["uint64", N]`
* **list**: ordered variable-length homogenous collection of values
* angle bracket notation `[type]`, e.g. `["uint64"]`

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".

### Aliases

For convenience we alias:
Expand All @@ -54,34 +56,34 @@ We recursively define the `serialize` function which consumes an object `value`

*Note*: In the function definitions below (`serialize`, `hash_tree_root`, `signed_root`, etc.) objects implicitly carry their type.

### `uintN`
### `"uintN"`

```python
assert N in [8, 16, 32, 64, 128, 256]
return value.to_bytes(N // 8, 'little')
return value.to_bytes(N // 8, "little")
```

### `bool`
### `"bool"`

```python
assert value in (True, False)
return b'\x01' if value is True else b'\x00'
return b"\x01" if value is True else b"\x00"
```

### Tuples, containers, lists
### Vectors, containers, lists

If `value` is fixed-length (i.e. does not embed a list):
If `value` is fixed-size:

```python
return ''.join([serialize(element) for element in value])
return "".join([serialize(element) for element in value])
```

If `value` is variable-length (i.e. embeds a list):
If `value` is variable-size:

```python
serialized_bytes = ''.join([serialize(element) for element in value])
serialized_bytes = "".join([serialize(element) for element in value])
assert len(serialized_bytes) < 2**(8 * BYTES_PER_LENGTH_PREFIX)
serialized_length = len(serialized_bytes).to_bytes(BYTES_PER_LENGTH_PREFIX, 'little')
serialized_length = len(serialized_bytes).to_bytes(BYTES_PER_LENGTH_PREFIX, "little")
return serialized_length + serialized_bytes
```

Expand All @@ -99,9 +101,9 @@ We first define helper functions:

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

* `merkleize(pack(value))` if `value` is a basic object or a tuple of basic objects
* `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 tuple of composite objects or a container
* `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

## Self-signed containers
Expand Down