Merge pull request #1283 from ethereum/n_zero
Clarify the illegal SSZ types
This commit is contained in:
commit
ab156f31b1
|
@ -49,7 +49,7 @@ from eth2spec.utils.ssz.ssz_impl import (
|
||||||
hash_tree_root,
|
hash_tree_root,
|
||||||
signing_root,
|
signing_root,
|
||||||
serialize,
|
serialize,
|
||||||
is_empty,
|
is_zero,
|
||||||
)
|
)
|
||||||
from eth2spec.utils.ssz.ssz_typing import (
|
from eth2spec.utils.ssz.ssz_typing import (
|
||||||
bit, boolean, Container, List, Vector, Bytes, uint64,
|
bit, boolean, Container, List, Vector, Bytes, uint64,
|
||||||
|
|
|
@ -328,7 +328,7 @@ def get_reveal_period(state: BeaconState, validator_index: ValidatorIndex, epoch
|
||||||
```python
|
```python
|
||||||
def replace_empty_or_append(list: MutableSequence[Any], new_element: Any) -> int:
|
def replace_empty_or_append(list: MutableSequence[Any], new_element: Any) -> int:
|
||||||
for i in range(len(list)):
|
for i in range(len(list)):
|
||||||
if is_empty(list[i]):
|
if is_zero(list[i]):
|
||||||
list[i] = new_element
|
list[i] = new_element
|
||||||
return i
|
return i
|
||||||
list.append(new_element)
|
list.append(new_element)
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
- [Variable-size and fixed-size](#variable-size-and-fixed-size)
|
- [Variable-size and fixed-size](#variable-size-and-fixed-size)
|
||||||
- [Aliases](#aliases)
|
- [Aliases](#aliases)
|
||||||
- [Default values](#default-values)
|
- [Default values](#default-values)
|
||||||
- [`is_empty`](#is_empty)
|
- [`is_zero`](#is_zero)
|
||||||
- [Illegal types](#illegal-types)
|
- [Illegal types](#illegal-types)
|
||||||
- [Serialization](#serialization)
|
- [Serialization](#serialization)
|
||||||
- [`uintN`](#uintn)
|
- [`uintN`](#uintn)
|
||||||
|
@ -75,19 +75,21 @@ For convenience we alias:
|
||||||
* `bit` to `boolean`
|
* `bit` to `boolean`
|
||||||
* `byte` to `uint8` (this is a basic type)
|
* `byte` to `uint8` (this is a basic type)
|
||||||
* `BytesN` to `Vector[byte, N]` (this is *not* a basic type)
|
* `BytesN` to `Vector[byte, N]` (this is *not* a basic type)
|
||||||
* `null`: `{}`, i.e. the empty container
|
* `null`: `{}`
|
||||||
|
|
||||||
### Default values
|
### 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.
|
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
|
### 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
|
## Serialization
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ def deserialize_basic(value, typ: BasicType):
|
||||||
raise Exception(f"Type not supported: {typ}")
|
raise Exception(f"Type not supported: {typ}")
|
||||||
|
|
||||||
|
|
||||||
def is_empty(obj: SSZValue):
|
def is_zero(obj: SSZValue):
|
||||||
return type(obj).default() == obj
|
return type(obj).default() == obj
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue