Apply suggestions from code review

Co-Authored-By: JustinDrake <drakefjustin@gmail.com>
This commit is contained in:
Danny Ryan 2019-02-27 22:23:42 +01:00 committed by GitHub
parent bac352e70f
commit 8176cc4cf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -43,23 +43,23 @@ For convenience we alias:
## Serialization ## Serialization
We reccursively define the `serialize` function which consumes an object `value` (of the type specified) and returns a byte string of type `bytes`. We recursively define the `serialize` function which consumes an object `value` (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 value.to_bytes(N // 8, 'little') return value.to_bytes(N // 8, 'little')
``` ```
#### `bool` ### `bool`
```python ```python
assert value in (True, False) 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'
``` ```
#### Containers, tuples, lists ### Containers, tuples, lists
```python ```python
serialized_bytes = ''.join([serialize(element) for element in value]) serialized_bytes = ''.join([serialize(element) for element in value])
@ -79,7 +79,7 @@ We first define helper functions:
* `pack`: Given ordered objects of the same basic type, serialize them, pack them into 32-byte chunks, right-pad the last chunk with zero bytes, and return the chunks. * `pack`: Given ordered objects of the same basic type, serialize them, pack them into 32-byte chunks, right-pad the last chunk with zero bytes, and return the chunks.
* `merkleize`: Given ordered 32-byte chunks, right-pad them with zero chunks to the next power of two, Merkleize the chunks, and return the root. * `merkleize`: Given ordered 32-byte chunks, right-pad them with zero chunks to the next power of two, Merkleize the chunks, and return the root.
* `mix_in_length`: Given a Merkle root `root` and a length `length` (32-byte little-endian serialization) return `hash(root + length)`. * `mix_in_length`: Given a Merkle root `root` and a length `length` (`uint256` little-endian serialization) return `hash(root + length)`.
We now define Merkleization `hash_tree_root(value)` of an object `value` recursively: We now define Merkleization `hash_tree_root(value)` of an object `value` recursively: