Skip to content

Commit

Permalink
Merge pull request #1283 from ethereum/n_zero
Browse files Browse the repository at this point in the history
Clarify the illegal SSZ types
  • Loading branch information
dankrad authored Jul 29, 2019
2 parents d791e9f + f336e7f commit ab156f3
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion scripts/build_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
hash_tree_root,
signing_root,
serialize,
is_empty,
is_zero,
)
from eth2spec.utils.ssz.ssz_typing import (
bit, boolean, Container, List, Vector, Bytes, uint64,
Expand Down
2 changes: 1 addition & 1 deletion specs/core/1_custody-game.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def get_reveal_period(state: BeaconState, validator_index: ValidatorIndex, epoch
```python
def replace_empty_or_append(list: MutableSequence[Any], new_element: Any) -> int:
for i in range(len(list)):
if is_empty(list[i]):
if is_zero(list[i]):
list[i] = new_element
return i
list.append(new_element)
Expand Down
12 changes: 7 additions & 5 deletions specs/simple-serialize.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- [Variable-size and fixed-size](#variable-size-and-fixed-size)
- [Aliases](#aliases)
- [Default values](#default-values)
- [`is_empty`](#is_empty)
- [`is_zero`](#is_zero)
- [Illegal types](#illegal-types)
- [Serialization](#serialization)
- [`uintN`](#uintn)
Expand Down Expand Up @@ -75,19 +75,21 @@ For convenience we alias:
* `bit` to `boolean`
* `byte` to `uint8` (this is a basic type)
* `BytesN` to `Vector[byte, N]` (this is *not* a basic type)
* `null`: `{}`, i.e. the empty container
* `null`: `{}`

### Default values

The default value of a type upon initialization is recursively defined using `0` for `uintN`, `False` for `boolean` and the elements of `Bitvector`, and `[]` for lists and `Bitlist`. Unions default to the first type in the union (with type index zero), which is `null` if present in the union.

#### `is_empty`
#### `is_zero`

An SSZ object is called empty (and thus, `is_empty(object)` returns true) if it is equal to the default value for that type.
An SSZ object is called zeroed (and thus, `is_zero(object)` returns true) if it is equal to the default value for that type.

### Illegal types

Empty vector types (i.e. `[subtype, 0]` for some `subtype`) are not legal. The `null` type is only legal as the first type in a union subtype (i.e. with type index zero).
- Empty vector types (`Vector[type, 0]`, `Bitvector[0]`) are illegal.
- Containers with no fields are illegal.
- The `null` type is only legal as the first type in a union subtype (i.e. with type index zero).

## Serialization

Expand Down
2 changes: 1 addition & 1 deletion test_libs/pyspec/eth2spec/utils/ssz/ssz_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def deserialize_basic(value, typ: BasicType):
raise Exception(f"Type not supported: {typ}")


def is_empty(obj: SSZValue):
def is_zero(obj: SSZValue):
return type(obj).default() == obj


Expand Down

0 comments on commit ab156f3

Please sign in to comment.