-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Alternative crosslink data construction, take 1 #1278
Changes from 5 commits
f4de621
457908d
dcd0dc5
467d7b4
2af45b4
102473a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
- [Misc](#misc) | ||
- [Initial values](#initial-values) | ||
- [Time parameters](#time-parameters) | ||
- [Signature domains](#signature-domains) | ||
- [Signature domain types](#signature-domain-types) | ||
- [TODO PLACEHOLDER](#todo-placeholder) | ||
- [Data structures](#data-structures) | ||
- [`ShardBlockBody`](#shardblockbody) | ||
|
@@ -27,6 +27,8 @@ | |
- [`get_shard_proposer_index`](#get_shard_proposer_index) | ||
- [`get_shard_header`](#get_shard_header) | ||
- [`verify_shard_attestation_signature`](#verify_shard_attestation_signature) | ||
- [`flatten_block`](#flatten_block) | ||
- [`compute_crosslink_data`](#compute_crosslink_data) | ||
- [`compute_crosslink_data_root`](#compute_crosslink_data_root) | ||
- [Object validity](#object-validity) | ||
- [Shard blocks](#shard-blocks) | ||
|
@@ -46,6 +48,7 @@ This document describes the shard data layer and the shard fork choice rule in P | |
|
||
| Name | Value | | ||
| - | - | | ||
| `BYTES_PER_SHARD_BLOCK_HEADER` | `2**9` (= 512) | | ||
| `BYTES_PER_SHARD_BLOCK_BODY` | `2**14` (= 16,384) | | ||
| `MAX_SHARD_ATTESTIONS` | `2**4` (= 16) | | ||
|
||
|
@@ -76,7 +79,7 @@ The following types are defined, mapping into `DomainType` (little endian): | |
|
||
| Name | Value | | ||
| - | - | | ||
| `PLACEHOLDER` | `2**32` | | ||
| `PLACEHOLDER` | `2**3` | | ||
|
||
## Data structures | ||
|
||
|
@@ -107,7 +110,7 @@ class ShardBlock(Container): | |
shard: Shard | ||
beacon_chain_root: Bytes32 | ||
parent_root: Bytes32 | ||
data: ShardBlockBody | ||
body: ShardBlockBody | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also hotfix in #1298. |
||
state_root: Bytes32 | ||
attestations: List[ShardAttestation, PLACEHOLDER] | ||
signature: BLSSignature | ||
|
@@ -247,36 +250,37 @@ def verify_shard_attestation_signature(state: BeaconState, | |
) | ||
``` | ||
|
||
### `flatten_block` | ||
|
||
```python | ||
def flatten_block(block: ShardBlock) -> Bytes: | ||
vbuterin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return zpad(serialize(get_shard_header(block)), BYTES_PER_SHARD_BLOCK_HEADER) + serialize(block.body) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added |
||
``` | ||
|
||
### `compute_crosslink_data` | ||
|
||
```python | ||
def compute_crosslink_data(blocks: Sequence[ShardBlock]) -> bytes: | ||
""" | ||
Flattens a series of blocks. Designed to be equivalent to SSZ serializing a list of | ||
flattened blocks with one empty block at the end as a termination marker. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you elaborate what do you mean by "one empty block at the end" here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you elaborate what's the main advantage of this one over just using SSZ class CrosslinkingBlocks:
data: List[ShardBlock, SHARD_SLOTS_PER_EPOCH * MAX_EPOCHS_PER_CROSSLINK]
assert len(blocks) <= SHARD_SLOTS_PER_EPOCH * MAX_EPOCHS_PER_CROSSLINK
crosslinking_blocks = CrosslinkingBlocks(data=blocks)
return serialize(crosslinking_blocks) ? |
||
""" | ||
positions = [4 * len(blocks)] | ||
bodies = [] | ||
for block in blocks: | ||
bodies.append(flatten_block(block)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't flattening the block here reduce our ability to construct proofs about components of a block through the crosslink root? Still possible but not as clean due to flattening to bytes |
||
positions.append(positions[-1] + len(bodies[-1])) | ||
return b''.join([int_to_bytes(pos, 4) for pos in positions]) + b''.join(bodies) | ||
``` | ||
|
||
### `compute_crosslink_data_root` | ||
|
||
```python | ||
def compute_crosslink_data_root(blocks: Sequence[ShardBlock]) -> Bytes32: | ||
def is_power_of_two(value: uint64) -> bool: | ||
return (value > 0) and (value & (value - 1) == 0) | ||
|
||
def pad_to_power_of_2(values: MutableSequence[bytes]) -> Sequence[bytes]: | ||
while not is_power_of_two(len(values)): | ||
values.append(b'\x00' * BYTES_PER_SHARD_BLOCK_BODY) | ||
return values | ||
|
||
def hash_tree_root_of_bytes(data: bytes) -> bytes: | ||
return hash_tree_root([data[i:i + 32] for i in range(0, len(data), 32)]) | ||
|
||
def zpad(data: bytes, length: uint64) -> bytes: | ||
return data + b'\x00' * (length - len(data)) | ||
|
||
return hash( | ||
# TODO untested code. | ||
# Need to either pass a typed list to hash-tree-root, or merkleize_chunks(values, pad_to=2**x) | ||
hash_tree_root(pad_to_power_of_2([ | ||
hash_tree_root_of_bytes( | ||
zpad(serialize(get_shard_header(block)), BYTES_PER_SHARD_BLOCK_BODY) | ||
) for block in blocks | ||
])) | ||
+ hash_tree_root(pad_to_power_of_2([ | ||
hash_tree_root_of_bytes(block.body) for block in blocks | ||
])) | ||
) | ||
MAXLEN = ( | ||
BYTES_PER_SHARD_BLOCK_HEADER + BYTES_PER_SHARD_BLOCK_BODY | ||
) * SHARD_SLOTS_PER_EPOCH * MAX_EPOCHS_PER_CROSSLINK | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return hash_tree_root(BytesN[MAXLEN](zpad(compute_crosslink_data(blocks), MAXLEN))) | ||
``` | ||
|
||
## Object validity | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to
2**3
for testing.