Update simple-serialize.md

This commit is contained in:
Justin 2019-02-27 12:57:06 +01:00 committed by GitHub
parent 07922a63fb
commit e698d7e29b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 15 deletions

View File

@ -30,24 +30,24 @@ This is a **work in progress** describing typing, serialization and Merkleizatio
## Typing ## Typing
### Primitive types #### Primitive types
* `uintN`: `N`-bit unsigned integer (where `N in [8, 16, 32, 64, 128, 256]`) * `uintN`: `N`-bit unsigned integer (where `N in [8, 16, 32, 64, 128, 256]`)
* `bool`: 1-bit unsigned integer * `bool`: 1-bit unsigned integer
### Composite types #### Composite types
* **Container**: ordered heterogenous collection of values * **container**: ordered heterogenous collection of values
* **Tuple**: ordered fixed-length homogeneous collection of values * **tuple**: ordered fixed-length homogeneous collection of values
* **List**: ordered variable-length homogenous collection of values * **list**: ordered variable-length homogenous collection of values
### Notation #### Notation
* **Container**: key-pair curly braken notation `{}` (e.g. `{'key1': uint64, 'key2': bool}`) * **container**: key-pair curly braket notation `{}` (e.g. `{'key1': uint64, 'key2': bool}`)
* **Tuple**: angle braket notation `[N]` (e.g. `uint64[N]`) * **tuple**: angle braket notation `[N]` (e.g. `uint64[N]`)
* **List**: angle braket notation `[]` (e.g. `uint64[]`) * **list**: angle braket notation `[]` (e.g. `uint64[]`)
### Aliases #### Aliases
For convenience we alias: For convenience we alias:
@ -60,21 +60,21 @@ For convenience we alias:
We reccursively define the `serialize` function which consumes an object `object` (of the type specified) and returns a byte string of type `bytes`. We reccursively define the `serialize` function which consumes an object `object` (of the type specified) and returns a byte string of type `bytes`.
### `uintN` #### `uintN`
```python ```python
assert N in [8, 16, 32, 64, 128, 256] assert N in [8, 16, 32, 64, 128, 256]
return object.to_bytes(N / 8, 'little') return object.to_bytes(N / 8, 'little')
``` ```
### `bool` #### `bool`
```python ```python
assert object in (True, False) assert object in (True, False)
return b'\x01' if object is True else b'\x00' return b'\x01' if object is True else b'\x00'
``` ```
### Containers #### Containers
```python ```python
serialized_elements = [serialize(element) for element in object] serialized_elements = [serialize(element) for element in object]
@ -84,7 +84,7 @@ serialized_length = len(serialized_bytes).to_bytes(LENGTH_BYTES, 'little')
return serialized_length + serialized_bytes return serialized_length + serialized_bytes
``` ```
### Tuples #### Tuples
```python ```python
serialized_elements = [serialize(element) for element in object] serialized_elements = [serialize(element) for element in object]
@ -92,7 +92,7 @@ serialized_bytes = reduce(lambda x, y: x + y, serialized_elements)
return serialized_bytes return serialized_bytes
``` ```
### Lists #### Lists
```python ```python
serialized_elements = [serialize(element) for element in object] serialized_elements = [serialize(element) for element in object]