Merge branch 'dev' into carl-patch-1
This commit is contained in:
commit
6975288254
|
@ -208,16 +208,14 @@ These configurations are updated for releases, but may be out of sync during `de
|
||||||
| Name | Value |
|
| Name | Value |
|
||||||
| - | - |
|
| - | - |
|
||||||
| `GENESIS_FORK_VERSION` | `int_to_bytes4(0)` |
|
| `GENESIS_FORK_VERSION` | `int_to_bytes4(0)` |
|
||||||
| `GENESIS_SLOT` | `2**32` |
|
| `GENESIS_SLOT` | `0` |
|
||||||
| `GENESIS_EPOCH` | `slot_to_epoch(GENESIS_SLOT)` |
|
| `GENESIS_EPOCH` | `0` |
|
||||||
| `GENESIS_START_SHARD` | `0` |
|
| `GENESIS_START_SHARD` | `0` |
|
||||||
| `FAR_FUTURE_EPOCH` | `2**64 - 1` |
|
| `FAR_FUTURE_EPOCH` | `2**64 - 1` |
|
||||||
| `ZERO_HASH` | `int_to_bytes32(0)` |
|
| `ZERO_HASH` | `int_to_bytes32(0)` |
|
||||||
| `EMPTY_SIGNATURE` | `int_to_bytes96(0)` |
|
| `EMPTY_SIGNATURE` | `int_to_bytes96(0)` |
|
||||||
| `BLS_WITHDRAWAL_PREFIX_BYTE` | `int_to_bytes1(0)` |
|
| `BLS_WITHDRAWAL_PREFIX_BYTE` | `int_to_bytes1(0)` |
|
||||||
|
|
||||||
* `GENESIS_SLOT` should be at least as large in terms of time as the largest of the time parameters or state list lengths below (ie. it should be at least as large as any value measured in slots, and at least `SLOTS_PER_EPOCH` times as large as any value measured in epochs).
|
|
||||||
|
|
||||||
### Time parameters
|
### Time parameters
|
||||||
|
|
||||||
| Name | Value | Unit | Duration |
|
| Name | Value | Unit | Duration |
|
||||||
|
@ -425,8 +423,6 @@ The types are defined topologically to aid in facilitating an executable version
|
||||||
'aggregation_bitfield': 'bytes',
|
'aggregation_bitfield': 'bytes',
|
||||||
# Attestation data
|
# Attestation data
|
||||||
'data': AttestationData,
|
'data': AttestationData,
|
||||||
# Custody bitfield
|
|
||||||
'custody_bitfield': 'bytes',
|
|
||||||
# Inclusion slot
|
# Inclusion slot
|
||||||
'inclusion_slot': 'uint64',
|
'inclusion_slot': 'uint64',
|
||||||
}
|
}
|
||||||
|
@ -680,8 +676,10 @@ def slot_to_epoch(slot: Slot) -> Epoch:
|
||||||
def get_previous_epoch(state: BeaconState) -> Epoch:
|
def get_previous_epoch(state: BeaconState) -> Epoch:
|
||||||
"""`
|
"""`
|
||||||
Return the previous epoch of the given ``state``.
|
Return the previous epoch of the given ``state``.
|
||||||
|
Return the current epoch if it's genesis epoch.
|
||||||
"""
|
"""
|
||||||
return get_current_epoch(state) - 1
|
current_epoch = get_current_epoch(state)
|
||||||
|
return (current_epoch - 1) if current_epoch > GENESIS_EPOCH else current_epoch
|
||||||
```
|
```
|
||||||
|
|
||||||
### `get_current_epoch`
|
### `get_current_epoch`
|
||||||
|
@ -916,7 +914,7 @@ def get_block_root(state: BeaconState,
|
||||||
return state.latest_block_roots[slot % SLOTS_PER_HISTORICAL_ROOT]
|
return state.latest_block_roots[slot % SLOTS_PER_HISTORICAL_ROOT]
|
||||||
```
|
```
|
||||||
|
|
||||||
`get_block_root(_, s)` should always return `hash_tree_root` of the block in the beacon chain at slot `s`, and `get_crosslink_committees_at_slot(_, s)` should not change unless the [validator](#dfn-validator) registry changes.
|
`get_block_root(_, s)` should always return `signed_root` of the block in the beacon chain at slot `s`, and `get_crosslink_committees_at_slot(_, s)` should not change unless the [validator](#dfn-validator) registry changes.
|
||||||
|
|
||||||
### `get_state_root`
|
### `get_state_root`
|
||||||
|
|
||||||
|
@ -976,6 +974,7 @@ def get_beacon_proposer_index(state: BeaconState) -> ValidatorIndex:
|
||||||
Return the beacon proposer index at ``state.slot``.
|
Return the beacon proposer index at ``state.slot``.
|
||||||
"""
|
"""
|
||||||
current_epoch = get_current_epoch(state)
|
current_epoch = get_current_epoch(state)
|
||||||
|
|
||||||
first_committee, _ = get_crosslink_committees_at_slot(state, state.slot)[0]
|
first_committee, _ = get_crosslink_committees_at_slot(state, state.slot)[0]
|
||||||
i = 0
|
i = 0
|
||||||
while True:
|
while True:
|
||||||
|
@ -1262,14 +1261,13 @@ Note: All functions in this section mutate `state`.
|
||||||
#### `activate_validator`
|
#### `activate_validator`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def activate_validator(state: BeaconState, index: ValidatorIndex, is_genesis: bool) -> None:
|
def activate_validator(state: BeaconState, index: ValidatorIndex) -> None:
|
||||||
"""
|
"""
|
||||||
Activate the validator of the given ``index``.
|
Activate the validator of the given ``index``.
|
||||||
Note that this function mutates ``state``.
|
Note that this function mutates ``state``.
|
||||||
"""
|
"""
|
||||||
validator = state.validator_registry[index]
|
validator = state.validator_registry[index]
|
||||||
|
if state.slot == GENESIS_SLOT:
|
||||||
if is_genesis:
|
|
||||||
validator.activation_eligibility_epoch = GENESIS_EPOCH
|
validator.activation_eligibility_epoch = GENESIS_EPOCH
|
||||||
validator.activation_epoch = GENESIS_EPOCH
|
validator.activation_epoch = GENESIS_EPOCH
|
||||||
else:
|
else:
|
||||||
|
@ -1437,7 +1435,7 @@ def get_genesis_beacon_state(genesis_validator_deposits: List[Deposit],
|
||||||
# Finality
|
# Finality
|
||||||
previous_epoch_attestations=[],
|
previous_epoch_attestations=[],
|
||||||
current_epoch_attestations=[],
|
current_epoch_attestations=[],
|
||||||
previous_justified_epoch=GENESIS_EPOCH - 1,
|
previous_justified_epoch=GENESIS_EPOCH,
|
||||||
current_justified_epoch=GENESIS_EPOCH,
|
current_justified_epoch=GENESIS_EPOCH,
|
||||||
previous_justified_root=ZERO_HASH,
|
previous_justified_root=ZERO_HASH,
|
||||||
current_justified_root=ZERO_HASH,
|
current_justified_root=ZERO_HASH,
|
||||||
|
@ -1467,7 +1465,7 @@ def get_genesis_beacon_state(genesis_validator_deposits: List[Deposit],
|
||||||
# Process genesis activations
|
# Process genesis activations
|
||||||
for index in range(len(state.validator_registry)):
|
for index in range(len(state.validator_registry)):
|
||||||
if get_effective_balance(state, index) >= MAX_EFFECTIVE_BALANCE:
|
if get_effective_balance(state, index) >= MAX_EFFECTIVE_BALANCE:
|
||||||
activate_validator(state, index, is_genesis=True)
|
activate_validator(state, index)
|
||||||
|
|
||||||
genesis_active_index_root = hash_tree_root(get_active_validator_indices(state, GENESIS_EPOCH))
|
genesis_active_index_root = hash_tree_root(get_active_validator_indices(state, GENESIS_EPOCH))
|
||||||
for index in range(LATEST_ACTIVE_INDEX_ROOTS_LENGTH):
|
for index in range(LATEST_ACTIVE_INDEX_ROOTS_LENGTH):
|
||||||
|
@ -1490,7 +1488,7 @@ For a beacon chain block, `block`, to be processed by a node, the following cond
|
||||||
|
|
||||||
* The parent block with root `block.previous_block_root` has been processed and accepted.
|
* The parent block with root `block.previous_block_root` has been processed and accepted.
|
||||||
* An Ethereum 1.0 block pointed to by the `state.latest_eth1_data.block_hash` has been processed and accepted.
|
* An Ethereum 1.0 block pointed to by the `state.latest_eth1_data.block_hash` has been processed and accepted.
|
||||||
* The node's Unix time is greater than or equal to `state.genesis_time + (block.slot - GENESIS_SLOT) * SECONDS_PER_SLOT`. (Note that leap seconds mean that slots will occasionally last `SECONDS_PER_SLOT + 1` or `SECONDS_PER_SLOT - 1` seconds, possibly several times a year.)
|
* The node's Unix time is greater than or equal to `state.genesis_time + block.slot * SECONDS_PER_SLOT`. (Note that leap seconds mean that slots will occasionally last `SECONDS_PER_SLOT + 1` or `SECONDS_PER_SLOT - 1` seconds, possibly several times a year.)
|
||||||
|
|
||||||
If these conditions are not met, the client should delay processing the beacon block until the conditions are all satisfied.
|
If these conditions are not met, the client should delay processing the beacon block until the conditions are all satisfied.
|
||||||
|
|
||||||
|
@ -1577,17 +1575,17 @@ At every `slot > GENESIS_SLOT` run the following function:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def cache_state(state: BeaconState) -> None:
|
def cache_state(state: BeaconState) -> None:
|
||||||
previous_slot_state_root = hash_tree_root(state)
|
# Cache latest known state root (for previous slot)
|
||||||
|
latest_state_root = hash_tree_root(state)
|
||||||
|
state.latest_state_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = latest_state_root
|
||||||
|
|
||||||
# store the previous slot's post state transition root
|
# Store latest known state root (for previous slot) in latest_block_header if it is empty
|
||||||
state.latest_state_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = previous_slot_state_root
|
|
||||||
|
|
||||||
# cache state root in stored latest_block_header if empty
|
|
||||||
if state.latest_block_header.state_root == ZERO_HASH:
|
if state.latest_block_header.state_root == ZERO_HASH:
|
||||||
state.latest_block_header.state_root = previous_slot_state_root
|
state.latest_block_header.state_root = latest_state_root
|
||||||
|
|
||||||
# store latest known block for previous slot
|
# Cache latest known block root (for previous slot)
|
||||||
state.latest_block_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = signing_root(state.latest_block_header)
|
latest_block_root = signing_root(state.latest_block_header)
|
||||||
|
state.latest_block_roots[state.slot % SLOTS_PER_HISTORICAL_ROOT] = latest_block_root
|
||||||
```
|
```
|
||||||
|
|
||||||
### Per-epoch processing
|
### Per-epoch processing
|
||||||
|
@ -1682,7 +1680,11 @@ Run the following function:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def update_justification_and_finalization(state: BeaconState) -> None:
|
def update_justification_and_finalization(state: BeaconState) -> None:
|
||||||
antepenultimate_justified_epoch = state.previous_justified_epoch
|
if get_current_epoch(state) <= GENESIS_EPOCH + 1:
|
||||||
|
return
|
||||||
|
|
||||||
|
old_previous_justified_epoch = state.previous_justified_epoch
|
||||||
|
old_current_justified_epoch = state.current_justified_epoch
|
||||||
|
|
||||||
# Process justifications
|
# Process justifications
|
||||||
state.previous_justified_epoch = state.current_justified_epoch
|
state.previous_justified_epoch = state.current_justified_epoch
|
||||||
|
@ -1703,20 +1705,20 @@ def update_justification_and_finalization(state: BeaconState) -> None:
|
||||||
bitfield = state.justification_bitfield
|
bitfield = state.justification_bitfield
|
||||||
current_epoch = get_current_epoch(state)
|
current_epoch = get_current_epoch(state)
|
||||||
# The 2nd/3rd/4th most recent epochs are justified, the 2nd using the 4th as source
|
# The 2nd/3rd/4th most recent epochs are justified, the 2nd using the 4th as source
|
||||||
if (bitfield >> 1) % 8 == 0b111 and antepenultimate_justified_epoch == current_epoch - 3:
|
if (bitfield >> 1) % 8 == 0b111 and old_previous_justified_epoch == current_epoch - 3:
|
||||||
state.finalized_epoch = antepenultimate_justified_epoch
|
state.finalized_epoch = old_previous_justified_epoch
|
||||||
state.finalized_root = get_block_root(state, get_epoch_start_slot(state.finalized_epoch))
|
state.finalized_root = get_block_root(state, get_epoch_start_slot(state.finalized_epoch))
|
||||||
# The 2nd/3rd most recent epochs are justified, the 2nd using the 3rd as source
|
# The 2nd/3rd most recent epochs are justified, the 2nd using the 3rd as source
|
||||||
if (bitfield >> 1) % 4 == 0b11 and antepenultimate_justified_epoch == current_epoch - 2:
|
if (bitfield >> 1) % 4 == 0b11 and old_previous_justified_epoch == current_epoch - 2:
|
||||||
state.finalized_epoch = antepenultimate_justified_epoch
|
state.finalized_epoch = old_previous_justified_epoch
|
||||||
state.finalized_root = get_block_root(state, get_epoch_start_slot(state.finalized_epoch))
|
state.finalized_root = get_block_root(state, get_epoch_start_slot(state.finalized_epoch))
|
||||||
# The 1st/2nd/3rd most recent epochs are justified, the 1st using the 3rd as source
|
# The 1st/2nd/3rd most recent epochs are justified, the 1st using the 3rd as source
|
||||||
if (bitfield >> 0) % 8 == 0b111 and state.previous_justified_root == current_epoch - 2:
|
if (bitfield >> 0) % 8 == 0b111 and old_current_justified_epoch == current_epoch - 2:
|
||||||
state.finalized_epoch = state.previous_justified_root
|
state.finalized_epoch = old_current_justified_epoch
|
||||||
state.finalized_root = get_block_root(state, get_epoch_start_slot(state.finalized_epoch))
|
state.finalized_root = get_block_root(state, get_epoch_start_slot(state.finalized_epoch))
|
||||||
# The 1st/2nd most recent epochs are justified, the 1st using the 2nd as source
|
# The 1st/2nd most recent epochs are justified, the 1st using the 2nd as source
|
||||||
if (bitfield >> 0) % 4 == 0b11 and state.previous_justified_root == current_epoch - 1:
|
if (bitfield >> 0) % 4 == 0b11 and old_current_justified_epoch == current_epoch - 1:
|
||||||
state.finalized_epoch = state.previous_justified_root
|
state.finalized_epoch = old_current_justified_epoch
|
||||||
state.finalized_root = get_block_root(state, get_epoch_start_slot(state.finalized_epoch))
|
state.finalized_root = get_block_root(state, get_epoch_start_slot(state.finalized_epoch))
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1726,9 +1728,8 @@ Run the following function:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def process_crosslinks(state: BeaconState) -> None:
|
def process_crosslinks(state: BeaconState) -> None:
|
||||||
current_epoch = get_current_epoch(state)
|
previous_epoch = get_previous_epoch(state)
|
||||||
previous_epoch = max(current_epoch - 1, GENESIS_EPOCH)
|
next_epoch = get_current_epoch(state) + 1
|
||||||
next_epoch = current_epoch + 1
|
|
||||||
for slot in range(get_epoch_start_slot(previous_epoch), get_epoch_start_slot(next_epoch)):
|
for slot in range(get_epoch_start_slot(previous_epoch), get_epoch_start_slot(next_epoch)):
|
||||||
for crosslink_committee, shard in get_crosslink_committees_at_slot(state, slot):
|
for crosslink_committee, shard in get_crosslink_committees_at_slot(state, slot):
|
||||||
winning_root, participants = get_winning_root_and_participants(state, shard)
|
winning_root, participants = get_winning_root_and_participants(state, shard)
|
||||||
|
@ -1847,6 +1848,9 @@ Run the following:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def apply_rewards(state: BeaconState) -> None:
|
def apply_rewards(state: BeaconState) -> None:
|
||||||
|
if get_current_epoch(state) == GENESIS_EPOCH:
|
||||||
|
return
|
||||||
|
|
||||||
rewards1, penalties1 = get_justification_and_finalization_deltas(state)
|
rewards1, penalties1 = get_justification_and_finalization_deltas(state)
|
||||||
rewards2, penalties2 = get_crosslink_deltas(state)
|
rewards2, penalties2 = get_crosslink_deltas(state)
|
||||||
for i in range(len(state.validator_registry)):
|
for i in range(len(state.validator_registry)):
|
||||||
|
@ -2102,8 +2106,7 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None:
|
||||||
Process ``Attestation`` operation.
|
Process ``Attestation`` operation.
|
||||||
Note that this function mutates ``state``.
|
Note that this function mutates ``state``.
|
||||||
"""
|
"""
|
||||||
assert max(GENESIS_SLOT, state.slot - SLOTS_PER_EPOCH) <= attestation.data.slot
|
assert attestation.data.slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot <= attestation.data.slot + SLOTS_PER_EPOCH
|
||||||
assert attestation.data.slot <= state.slot - MIN_ATTESTATION_INCLUSION_DELAY
|
|
||||||
|
|
||||||
# Check target epoch, source epoch, and source root
|
# Check target epoch, source epoch, and source root
|
||||||
target_epoch = slot_to_epoch(attestation.data.slot)
|
target_epoch = slot_to_epoch(attestation.data.slot)
|
||||||
|
@ -2130,7 +2133,6 @@ def process_attestation(state: BeaconState, attestation: Attestation) -> None:
|
||||||
pending_attestation = PendingAttestation(
|
pending_attestation = PendingAttestation(
|
||||||
data=attestation.data,
|
data=attestation.data,
|
||||||
aggregation_bitfield=attestation.aggregation_bitfield,
|
aggregation_bitfield=attestation.aggregation_bitfield,
|
||||||
custody_bitfield=attestation.custody_bitfield,
|
|
||||||
inclusion_slot=state.slot
|
inclusion_slot=state.slot
|
||||||
)
|
)
|
||||||
if target_epoch == get_current_epoch(state):
|
if target_epoch == get_current_epoch(state):
|
||||||
|
@ -2153,7 +2155,7 @@ def process_proposer_attestation_rewards(state: BeaconState) -> None:
|
||||||
|
|
||||||
##### Deposits
|
##### Deposits
|
||||||
|
|
||||||
Verify that `len(block.body.deposits) == min(MAX_DEPOSITS, latest_eth1_data.deposit_count - state.deposit_index)`.
|
Verify that `len(block.body.deposits) == min(MAX_DEPOSITS, state.latest_eth1_data.deposit_count - state.deposit_index)`.
|
||||||
|
|
||||||
For each `deposit` in `block.body.deposits`, run the following function:
|
For each `deposit` in `block.body.deposits`, run the following function:
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,26 @@
|
||||||
**NOTICE**: This document is a work-in-progress for researchers and implementers.
|
**NOTICE**: This document is a work-in-progress for researchers and implementers.
|
||||||
|
|
||||||
### Constants
|
## Table of Contents
|
||||||
|
<!-- TOC -->
|
||||||
|
|
||||||
|
- [Table of Contents](#table-of-contents)
|
||||||
|
- [Constants](#constants)
|
||||||
|
- [Generalized Merkle tree index](#generalized-merkle-tree-index)
|
||||||
|
- [SSZ object to index](#ssz-object-to-index)
|
||||||
|
- [Merkle multiproofs](#merkle-multiproofs)
|
||||||
|
- [MerklePartial](#merklepartial)
|
||||||
|
- [`SSZMerklePartial`](#sszmerklepartial)
|
||||||
|
- [Proofs for execution](#proofs-for-execution)
|
||||||
|
|
||||||
|
<!-- /TOC -->
|
||||||
|
|
||||||
|
## Constants
|
||||||
|
|
||||||
| Name | Value |
|
| Name | Value |
|
||||||
| - | - |
|
| - | - |
|
||||||
| `LENGTH_FLAG` | `2**64 - 1` |
|
| `LENGTH_FLAG` | `2**64 - 1` |
|
||||||
|
|
||||||
### Generalized Merkle tree index
|
## Generalized Merkle tree index
|
||||||
|
|
||||||
In a binary Merkle tree, we define a "generalized index" of a node as `2**depth + index`. Visually, this looks as follows:
|
In a binary Merkle tree, we define a "generalized index" of a node as `2**depth + index`. Visually, this looks as follows:
|
||||||
|
|
||||||
|
@ -20,16 +34,16 @@ In a binary Merkle tree, we define a "generalized index" of a node as `2**depth
|
||||||
Note that the generalized index has the convenient property that the two children of node `k` are `2k` and `2k+1`, and also that it equals the position of a node in the linear representation of the Merkle tree that's computed by this function:
|
Note that the generalized index has the convenient property that the two children of node `k` are `2k` and `2k+1`, and also that it equals the position of a node in the linear representation of the Merkle tree that's computed by this function:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def merkle_tree(leaves):
|
def merkle_tree(leaves: List[Bytes32]) -> List[Bytes32]:
|
||||||
o = [0] * len(leaves) + leaves
|
o = [0] * len(leaves) + leaves
|
||||||
for i in range(len(leaves)-1, 0, -1):
|
for i in range(len(leaves) - 1, 0, -1):
|
||||||
o[i] = hash(o[i*2] + o[i*2+1])
|
o[i] = hash(o[i * 2] + o[i * 2 + 1])
|
||||||
return o
|
return o
|
||||||
```
|
```
|
||||||
|
|
||||||
We will define Merkle proofs in terms of generalized indices.
|
We will define Merkle proofs in terms of generalized indices.
|
||||||
|
|
||||||
### SSZ object to index
|
## SSZ object to index
|
||||||
|
|
||||||
We can describe the hash tree of any SSZ object, rooted in `hash_tree_root(object)`, as a binary Merkle tree whose depth may vary. For example, an object `{x: bytes32, y: List[uint64]}` would look as follows:
|
We can describe the hash tree of any SSZ object, rooted in `hash_tree_root(object)`, as a binary Merkle tree whose depth may vary. For example, an object `{x: bytes32, y: List[uint64]}` would look as follows:
|
||||||
|
|
||||||
|
@ -47,33 +61,33 @@ y_data_root len(y)
|
||||||
We can now define a concept of a "path", a way of describing a function that takes as input an SSZ object and outputs some specific (possibly deeply nested) member. For example, `foo -> foo.x` is a path, as are `foo -> len(foo.y)` and `foo -> foo.y[5].w`. We'll describe paths as lists, which can have two representations. In "human-readable form", they are `["x"]`, `["y", "__len__"]` and `["y", 5, "w"]` respectively. In "encoded form", they are lists of `uint64` values, in these cases (assuming the fields of `foo` in order are `x` then `y`, and `w` is the first field of `y[i]`) `[0]`, `[1, 2**64-1]`, `[1, 5, 0]`.
|
We can now define a concept of a "path", a way of describing a function that takes as input an SSZ object and outputs some specific (possibly deeply nested) member. For example, `foo -> foo.x` is a path, as are `foo -> len(foo.y)` and `foo -> foo.y[5].w`. We'll describe paths as lists, which can have two representations. In "human-readable form", they are `["x"]`, `["y", "__len__"]` and `["y", 5, "w"]` respectively. In "encoded form", they are lists of `uint64` values, in these cases (assuming the fields of `foo` in order are `x` then `y`, and `w` is the first field of `y[i]`) `[0]`, `[1, 2**64-1]`, `[1, 5, 0]`.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def path_to_encoded_form(obj: Any, path: List[str or int]) -> List[int]:
|
def path_to_encoded_form(obj: Any, path: List[Union[str, int]]) -> List[int]:
|
||||||
if len(path) == 0:
|
if len(path) == 0:
|
||||||
return []
|
return []
|
||||||
if isinstance(path[0], "__len__"):
|
elif isinstance(path[0], "__len__"):
|
||||||
assert len(path) == 1
|
assert len(path) == 1
|
||||||
return [LENGTH_FLAG]
|
return [LENGTH_FLAG]
|
||||||
elif isinstance(path[0], str) and hasattr(obj, "fields"):
|
elif isinstance(path[0], str) and hasattr(obj, "fields"):
|
||||||
return [list(obj.fields.keys()).index(path[0])] + path_to_encoded_form(getattr(obj, path[0]), path[1:])
|
return [list(obj.fields.keys()).index(path[0])] + path_to_encoded_form(getattr(obj, path[0]), path[1:])
|
||||||
elif isinstance(obj, (StaticList, DynamicList)):
|
elif isinstance(obj, (Vector, List)):
|
||||||
return [path[0]] + path_to_encoded_form(obj[path[0]], path[1:])
|
return [path[0]] + path_to_encoded_form(obj[path[0]], path[1:])
|
||||||
else:
|
else:
|
||||||
raise Exception("Unknown type / path")
|
raise Exception("Unknown type / path")
|
||||||
```
|
```
|
||||||
|
|
||||||
We can now define a function `get_generalized_indices(object: Any, path: List[int], root=1: int) -> int` that converts an object and a path to a set of generalized indices (note that for constant-sized objects, there is only one generalized index and it only depends on the path, but for dynamically sized objects the indices may depend on the object itself too). For dynamically-sized objects, the set of indices will have more than one member because of the need to access an array's length to determine the correct generalized index for some array access.
|
We can now define a function `get_generalized_indices(object: Any, path: List[int], root: int=1) -> List[int]` that converts an object and a path to a set of generalized indices (note that for constant-sized objects, there is only one generalized index and it only depends on the path, but for dynamically sized objects the indices may depend on the object itself too). For dynamically-sized objects, the set of indices will have more than one member because of the need to access an array's length to determine the correct generalized index for some array access.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def get_generalized_indices(obj: Any, path: List[int], root=1) -> List[int]:
|
def get_generalized_indices(obj: Any, path: List[int], root: int=1) -> List[int]:
|
||||||
if len(path) == 0:
|
if len(path) == 0:
|
||||||
return [root]
|
return [root]
|
||||||
elif isinstance(obj, StaticList):
|
elif isinstance(obj, Vector):
|
||||||
items_per_chunk = (32 // len(serialize(x))) if isinstance(x, int) else 1
|
items_per_chunk = (32 // len(serialize(x))) if isinstance(x, int) else 1
|
||||||
new_root = root * next_power_of_2(len(obj) // items_per_chunk) + path[0] // items_per_chunk
|
new_root = root * next_power_of_2(len(obj) // items_per_chunk) + path[0] // items_per_chunk
|
||||||
return get_generalized_indices(obj[path[0]], path[1:], new_root)
|
return get_generalized_indices(obj[path[0]], path[1:], new_root)
|
||||||
elif isinstance(obj, DynamicList) and path[0] == LENGTH_FLAG:
|
elif isinstance(obj, List) and path[0] == LENGTH_FLAG:
|
||||||
return [root * 2 + 1]
|
return [root * 2 + 1]
|
||||||
elif isinstance(obj, DynamicList) and isinstance(path[0], int):
|
elif isinstance(obj, List) and isinstance(path[0], int):
|
||||||
assert path[0] < len(obj)
|
assert path[0] < len(obj)
|
||||||
items_per_chunk = (32 // len(serialize(x))) if isinstance(x, int) else 1
|
items_per_chunk = (32 // len(serialize(x))) if isinstance(x, int) else 1
|
||||||
new_root = root * 2 * next_power_of_2(len(obj) // items_per_chunk) + path[0] // items_per_chunk
|
new_root = root * 2 * next_power_of_2(len(obj) // items_per_chunk) + path[0] // items_per_chunk
|
||||||
|
@ -86,7 +100,7 @@ def get_generalized_indices(obj: Any, path: List[int], root=1) -> List[int]:
|
||||||
raise Exception("Unknown type / path")
|
raise Exception("Unknown type / path")
|
||||||
```
|
```
|
||||||
|
|
||||||
### Merkle multiproofs
|
## Merkle multiproofs
|
||||||
|
|
||||||
We define a Merkle multiproof as a minimal subset of nodes in a Merkle tree needed to fully authenticate that a set of nodes actually are part of a Merkle tree with some specified root, at a particular set of generalized indices. For example, here is the Merkle multiproof for positions 0, 1, 6 in an 8-node Merkle tree (ie. generalized indices 8, 9, 14):
|
We define a Merkle multiproof as a minimal subset of nodes in a Merkle tree needed to fully authenticate that a set of nodes actually are part of a Merkle tree with some specified root, at a particular set of generalized indices. For example, here is the Merkle multiproof for positions 0, 1, 6 in an 8-node Merkle tree (ie. generalized indices 8, 9, 14):
|
||||||
|
|
||||||
|
@ -99,19 +113,12 @@ x x . . . . x *
|
||||||
|
|
||||||
. are unused nodes, * are used nodes, x are the values we are trying to prove. Notice how despite being a multiproof for 3 values, it requires only 3 auxiliary nodes, only one node more than would be required to prove a single value. Normally the efficiency gains are not quite that extreme, but the savings relative to individual Merkle proofs are still significant. As a rule of thumb, a multiproof for k nodes at the same level of an n-node tree has size `k * (n/k + log(n/k))`.
|
. are unused nodes, * are used nodes, x are the values we are trying to prove. Notice how despite being a multiproof for 3 values, it requires only 3 auxiliary nodes, only one node more than would be required to prove a single value. Normally the efficiency gains are not quite that extreme, but the savings relative to individual Merkle proofs are still significant. As a rule of thumb, a multiproof for k nodes at the same level of an n-node tree has size `k * (n/k + log(n/k))`.
|
||||||
|
|
||||||
Here is code for creating and verifying a multiproof. First a helper:
|
Here is code for creating and verifying a multiproof. First, a method for computing the generalized indices of the auxiliary tree nodes that a proof of a given set of generalized indices will require:
|
||||||
|
|
||||||
```python
|
|
||||||
def log2(x):
|
|
||||||
return 0 if x == 1 else 1 + log2(x//2)
|
|
||||||
```
|
|
||||||
|
|
||||||
First, a method for computing the generalized indices of the auxiliary tree nodes that a proof of a given set of generalized indices will require:
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def get_proof_indices(tree_indices: List[int]) -> List[int]:
|
def get_proof_indices(tree_indices: List[int]) -> List[int]:
|
||||||
# Get all indices touched by the proof
|
# Get all indices touched by the proof
|
||||||
maximal_indices = set({})
|
maximal_indices = set()
|
||||||
for i in tree_indices:
|
for i in tree_indices:
|
||||||
x = i
|
x = i
|
||||||
while x > 1:
|
while x > 1:
|
||||||
|
@ -119,7 +126,7 @@ def get_proof_indices(tree_indices: List[int]) -> List[int]:
|
||||||
x //= 2
|
x //= 2
|
||||||
maximal_indices = tree_indices + sorted(list(maximal_indices))[::-1]
|
maximal_indices = tree_indices + sorted(list(maximal_indices))[::-1]
|
||||||
# Get indices that cannot be recalculated from earlier indices
|
# Get indices that cannot be recalculated from earlier indices
|
||||||
redundant_indices = set({})
|
redundant_indices = set()
|
||||||
proof = []
|
proof = []
|
||||||
for index in maximal_indices:
|
for index in maximal_indices:
|
||||||
if index not in redundant_indices:
|
if index not in redundant_indices:
|
||||||
|
@ -130,35 +137,35 @@ def get_proof_indices(tree_indices: List[int]) -> List[int]:
|
||||||
break
|
break
|
||||||
index //= 2
|
index //= 2
|
||||||
return [i for i in proof if i not in tree_indices]
|
return [i for i in proof if i not in tree_indices]
|
||||||
````
|
```
|
||||||
|
|
||||||
Generating a proof is simply a matter of taking the node of the SSZ hash tree with the union of the given generalized indices for each index given by `get_proof_indices`, and outputting the list of nodes in the same order.
|
Generating a proof is simply a matter of taking the node of the SSZ hash tree with the union of the given generalized indices for each index given by `get_proof_indices`, and outputting the list of nodes in the same order.
|
||||||
|
|
||||||
Here is the verification function:
|
Here is the verification function:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def verify_multi_proof(root, indices, leaves, proof):
|
def verify_multi_proof(root: Bytes32, indices: List[int], leaves: List[Bytes32], proof: List[Bytes32]) -> bool:
|
||||||
tree = {}
|
tree = {}
|
||||||
for index, leaf in zip(indices, leaves):
|
for index, leaf in zip(indices, leaves):
|
||||||
tree[index] = leaf
|
tree[index] = leaf
|
||||||
for index, proofitem in zip(get_proof_indices(indices), proof):
|
for index, proof_item in zip(get_proof_indices(indices), proof):
|
||||||
tree[index] = proofitem
|
tree[index] = proof_item
|
||||||
indexqueue = sorted(tree.keys())[:-1]
|
index_queue = sorted(tree.keys())[:-1]
|
||||||
i = 0
|
i = 0
|
||||||
while i < len(indexqueue):
|
while i < len(index_queue):
|
||||||
index = indexqueue[i]
|
index = index_queue[i]
|
||||||
if index >= 2 and index^1 in tree:
|
if index >= 2 and index ^ 1 in tree:
|
||||||
tree[index//2] = hash(tree[index - index%2] + tree[index - index%2 + 1])
|
tree[index // 2] = hash(tree[index - index % 2] + tree[index - index % 2 + 1])
|
||||||
indexqueue.append(index//2)
|
index_queue.append(index // 2)
|
||||||
i += 1
|
i += 1
|
||||||
return (indices == []) or (1 in tree and tree[1] == root)
|
return (indices == []) or (1 in tree and tree[1] == root)
|
||||||
```
|
```
|
||||||
|
|
||||||
### MerklePartial
|
## MerklePartial
|
||||||
|
|
||||||
We define:
|
We define:
|
||||||
|
|
||||||
#### `MerklePartial`
|
### `SSZMerklePartial`
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
|
@ -170,8 +177,8 @@ We define:
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Proofs for execution
|
### Proofs for execution
|
||||||
|
|
||||||
We define `MerklePartial(f, arg1, arg2..., focus=0)` as being a `MerklePartial` object wrapping a Merkle multiproof of the set of nodes in the hash tree of the SSZ object `arg[focus]` that is needed to authenticate the parts of the object needed to compute `f(arg1, arg2...)`.
|
We define `MerklePartial(f, arg1, arg2..., focus=0)` as being a `SSZMerklePartial` object wrapping a Merkle multiproof of the set of nodes in the hash tree of the SSZ object `arg[focus]` that is needed to authenticate the parts of the object needed to compute `f(arg1, arg2...)`.
|
||||||
|
|
||||||
Ideally, any function which accepts an SSZ object should also be able to accept a `MerklePartial` object as a substitute.
|
Ideally, any function which accepts an SSZ object should also be able to accept a `SSZMerklePartial` object as a substitute.
|
||||||
|
|
|
@ -5,52 +5,78 @@ __NOTICE__: This document is a work-in-progress for researchers and implementers
|
||||||
## Table of Contents
|
## Table of Contents
|
||||||
|
|
||||||
<!-- TOC -->
|
<!-- TOC -->
|
||||||
|
|
||||||
- [Beacon Chain Light Client Syncing](#beacon-chain-light-client-syncing)
|
- [Beacon Chain Light Client Syncing](#beacon-chain-light-client-syncing)
|
||||||
- [Table of Contents](#table-of-contents)
|
- [Table of Contents](#table-of-contents)
|
||||||
- [Light client state](#light-client-state)
|
- [Preliminaries](#preliminaries)
|
||||||
- [Updating the shuffled committee](#updating-the-shuffled-committee)
|
- [Expansions](#expansions)
|
||||||
|
- [`get_active_validator_indices`](#get_active_validator_indices)
|
||||||
|
- [`MerklePartial`](#merklepartial)
|
||||||
|
- [`PeriodData`](#perioddata)
|
||||||
|
- [`get_earlier_start_epoch`](#get_earlier_start_epoch)
|
||||||
|
- [`get_later_start_epoch`](#get_later_start_epoch)
|
||||||
|
- [`get_period_data`](#get_period_data)
|
||||||
|
- [Light client state](#light-client-state)
|
||||||
|
- [Updating the shuffled committee](#updating-the-shuffled-committee)
|
||||||
- [Computing the current committee](#computing-the-current-committee)
|
- [Computing the current committee](#computing-the-current-committee)
|
||||||
- [Verifying blocks](#verifying-blocks)
|
- [Verifying blocks](#verifying-blocks)
|
||||||
|
|
||||||
<!-- /TOC -->
|
<!-- /TOC -->
|
||||||
|
|
||||||
|
## Preliminaries
|
||||||
|
|
||||||
### Preliminaries
|
### Expansions
|
||||||
|
|
||||||
We define an "expansion" of an object as an object where a field in an object that is meant to represent the `hash_tree_root` of another object is replaced by the object. Note that defining expansions is not a consensus-layer-change; it is merely a "re-interpretation" of the object. Particularly, the `hash_tree_root` of an expansion of an object is identical to that of the original object, and we can define expansions where, given a complete history, it is always possible to compute the expansion of any object in the history. The opposite of an expansion is a "summary" (eg. `BeaconBlockHeader` is a summary of `BeaconBlock`).
|
We define an "expansion" of an object as an object where a field in an object that is meant to represent the `hash_tree_root` of another object is replaced by the object. Note that defining expansions is not a consensus-layer-change; it is merely a "re-interpretation" of the object. Particularly, the `hash_tree_root` of an expansion of an object is identical to that of the original object, and we can define expansions where, given a complete history, it is always possible to compute the expansion of any object in the history. The opposite of an expansion is a "summary" (eg. `BeaconBlockHeader` is a summary of `BeaconBlock`).
|
||||||
|
|
||||||
We define two expansions:
|
We define two expansions:
|
||||||
|
|
||||||
* `ExtendedBeaconBlock`, which is identical to a `BeaconBlock` except `state_root` is replaced with the corresponding `state: ExtendedBeaconState`
|
* `ExtendedBeaconState`, which is identical to a `BeaconState` except `latest_active_index_roots: List[Bytes32]` is replaced by `latest_active_indices: List[List[ValidatorIndex]]`, where `BeaconState.latest_active_index_roots[i] = hash_tree_root(ExtendedBeaconState.latest_active_indices[i])`.
|
||||||
* `ExtendedBeaconState`, which is identical to a `BeaconState` except `latest_active_index_roots: List[Bytes32]` is replaced by `latest_active_indices: List[List[ValidatorIndex]]`, where `BeaconState.latest_active_index_roots[i] = hash_tree_root(ExtendedBeaconState.latest_active_indices[i])`
|
* `ExtendedBeaconBlock`, which is identical to a `BeaconBlock` except `state_root` is replaced with the corresponding `state: ExtendedBeaconState`.
|
||||||
|
|
||||||
|
### `get_active_validator_indices`
|
||||||
|
|
||||||
Note that there is now a new way to compute `get_active_validator_indices`:
|
Note that there is now a new way to compute `get_active_validator_indices`:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def get_active_validator_indices(state: BeaconState, epoch: Epoch) -> List[ValidatorIndex]:
|
def get_active_validator_indices(state: ExtendedBeaconState, epoch: Epoch) -> List[ValidatorIndex]:
|
||||||
return state.latest_active_indices[epoch % LATEST_ACTIVE_INDEX_ROOTS_LENGTH]
|
return state.latest_active_indices[epoch % LATEST_ACTIVE_INDEX_ROOTS_LENGTH]
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that it takes `state` instead of `state.validator_registry` as an argument. This does not affect its use in `get_shuffled_committee`, because `get_shuffled_committee` has access to the full `state` as one of its arguments.
|
Note that it takes `state` instead of `state.validator_registry` as an argument. This does not affect its use in `get_shuffled_committee`, because `get_shuffled_committee` has access to the full `state` as one of its arguments.
|
||||||
|
|
||||||
|
|
||||||
|
### `MerklePartial`
|
||||||
|
|
||||||
A `MerklePartial(f, *args)` is an object that contains a minimal Merkle proof needed to compute `f(*args)`. A `MerklePartial` can be used in place of a regular SSZ object, though a computation would return an error if it attempts to access part of the object that is not contained in the proof.
|
A `MerklePartial(f, *args)` is an object that contains a minimal Merkle proof needed to compute `f(*args)`. A `MerklePartial` can be used in place of a regular SSZ object, though a computation would return an error if it attempts to access part of the object that is not contained in the proof.
|
||||||
|
|
||||||
We add a data type `PeriodData` and four helpers:
|
### `PeriodData`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
{
|
{
|
||||||
'validator_count': 'uint64',
|
'validator_count': 'uint64',
|
||||||
'seed': 'bytes32',
|
'seed': 'bytes32',
|
||||||
'committee': [Validator]
|
'committee': [Validator],
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `get_earlier_start_epoch`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def get_earlier_start_epoch(slot: Slot) -> int:
|
def get_earlier_start_epoch(slot: Slot) -> int:
|
||||||
return slot - slot % PERSISTENT_COMMITTEE_PERIOD - PERSISTENT_COMMITTEE_PERIOD * 2
|
return slot - slot % PERSISTENT_COMMITTEE_PERIOD - PERSISTENT_COMMITTEE_PERIOD * 2
|
||||||
|
```
|
||||||
|
|
||||||
|
### `get_later_start_epoch`
|
||||||
|
|
||||||
|
```python
|
||||||
def get_later_start_epoch(slot: Slot) -> int:
|
def get_later_start_epoch(slot: Slot) -> int:
|
||||||
return slot - slot % PERSISTENT_COMMITTEE_PERIOD - PERSISTENT_COMMITTEE_PERIOD
|
return slot - slot % PERSISTENT_COMMITTEE_PERIOD - PERSISTENT_COMMITTEE_PERIOD
|
||||||
|
```
|
||||||
|
|
||||||
|
### `get_period_data`
|
||||||
|
|
||||||
|
```python
|
||||||
def get_period_data(block: ExtendedBeaconBlock, shard_id: Shard, later: bool) -> PeriodData:
|
def get_period_data(block: ExtendedBeaconBlock, shard_id: Shard, later: bool) -> PeriodData:
|
||||||
period_start = get_later_start_epoch(header.slot) if later else get_earlier_start_epoch(header.slot)
|
period_start = get_later_start_epoch(header.slot) if later else get_earlier_start_epoch(header.slot)
|
||||||
validator_count = len(get_active_validator_indices(state, period_start))
|
validator_count = len(get_active_validator_indices(state, period_start))
|
||||||
|
@ -59,7 +85,7 @@ def get_period_data(block: ExtendedBeaconBlock, shard_id: Shard, later: bool) ->
|
||||||
return PeriodData(
|
return PeriodData(
|
||||||
validator_count,
|
validator_count,
|
||||||
generate_seed(block.state, period_start),
|
generate_seed(block.state, period_start),
|
||||||
[block.state.validator_registry[i] for i in indices]
|
[block.state.validator_registry[i] for i in indices],
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -72,7 +98,7 @@ A light client will keep track of:
|
||||||
* `later_period_data = get_period_data(finalized_header, shard_id, later=True)`
|
* `later_period_data = get_period_data(finalized_header, shard_id, later=True)`
|
||||||
* `earlier_period_data = get_period_data(finalized_header, shard_id, later=False)`
|
* `earlier_period_data = get_period_data(finalized_header, shard_id, later=False)`
|
||||||
|
|
||||||
We use the struct `validator_memory` to keep track of these variables.
|
We use the struct `ValidatorMemory` to keep track of these variables.
|
||||||
|
|
||||||
### Updating the shuffled committee
|
### Updating the shuffled committee
|
||||||
|
|
||||||
|
@ -85,14 +111,13 @@ later_period_data = get_period_data(new_committee_proof, finalized_header, shard
|
||||||
|
|
||||||
The maximum size of a proof is `128 * ((22-7) * 32 + 110) = 75520` bytes for validator records and `(22-7) * 32 + 128 * 8 = 1504` for the active index proof (much smaller because the relevant active indices are all beside each other in the Merkle tree). This needs to be done once per `PERSISTENT_COMMITTEE_PERIOD` epochs (2048 epochs / 9 days), or ~38 bytes per epoch.
|
The maximum size of a proof is `128 * ((22-7) * 32 + 110) = 75520` bytes for validator records and `(22-7) * 32 + 128 * 8 = 1504` for the active index proof (much smaller because the relevant active indices are all beside each other in the Merkle tree). This needs to be done once per `PERSISTENT_COMMITTEE_PERIOD` epochs (2048 epochs / 9 days), or ~38 bytes per epoch.
|
||||||
|
|
||||||
### Computing the current committee
|
## Computing the current committee
|
||||||
|
|
||||||
Here is a helper to compute the committee at a slot given the maximal earlier and later committees:
|
Here is a helper to compute the committee at a slot given the maximal earlier and later committees:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def compute_committee(header: BeaconBlockHeader,
|
def compute_committee(header: BeaconBlockHeader,
|
||||||
validator_memory: ValidatorMemory):
|
validator_memory: ValidatorMemory) -> List[ValidatorIndex]:
|
||||||
|
|
||||||
earlier_validator_count = validator_memory.earlier_period_data.validator_count
|
earlier_validator_count = validator_memory.earlier_period_data.validator_count
|
||||||
later_validator_count = validator_memory.later_period_data.validator_count
|
later_validator_count = validator_memory.later_period_data.validator_count
|
||||||
maximal_earlier_committee = validator_memory.earlier_period_data.committee
|
maximal_earlier_committee = validator_memory.earlier_period_data.committee
|
||||||
|
@ -106,10 +131,12 @@ def compute_committee(header: BeaconBlockHeader,
|
||||||
later_validator_count // (SHARD_COUNT * TARGET_COMMITTEE_SIZE),
|
later_validator_count // (SHARD_COUNT * TARGET_COMMITTEE_SIZE),
|
||||||
) + 1
|
) + 1
|
||||||
|
|
||||||
def get_offset(count, end:bool):
|
def get_offset(count: int, end: bool) -> int:
|
||||||
return get_split_offset(count,
|
return get_split_offset(
|
||||||
SHARD_COUNT * committee_count,
|
count,
|
||||||
validator_memory.shard_id * committee_count + (1 if end else 0))
|
SHARD_COUNT * committee_count,
|
||||||
|
validator_memory.shard_id * committee_count + (1 if end else 0),
|
||||||
|
)
|
||||||
|
|
||||||
actual_earlier_committee = maximal_earlier_committee[
|
actual_earlier_committee = maximal_earlier_committee[
|
||||||
0:get_offset(earlier_validator_count, True) - get_offset(earlier_validator_count, False)
|
0:get_offset(earlier_validator_count, True) - get_offset(earlier_validator_count, False)
|
||||||
|
@ -119,31 +146,30 @@ def compute_committee(header: BeaconBlockHeader,
|
||||||
]
|
]
|
||||||
def get_switchover_epoch(index):
|
def get_switchover_epoch(index):
|
||||||
return (
|
return (
|
||||||
bytes_to_int(hash(validator_memory.earlier_period_data.seed + bytes3(index))[0:8]) %
|
bytes_to_int(hash(validator_memory.earlier_period_data.seed + int_to_bytes3(index))[0:8]) %
|
||||||
PERSISTENT_COMMITTEE_PERIOD
|
PERSISTENT_COMMITTEE_PERIOD
|
||||||
)
|
)
|
||||||
|
|
||||||
# Take not-yet-cycled-out validators from earlier committee and already-cycled-in validators from
|
# Take not-yet-cycled-out validators from earlier committee and already-cycled-in validators from
|
||||||
# later committee; return a sorted list of the union of the two, deduplicated
|
# later committee; return a sorted list of the union of the two, deduplicated
|
||||||
return sorted(list(set(
|
return sorted(list(set(
|
||||||
[i for i in earlier_committee if epoch % PERSISTENT_COMMITTEE_PERIOD < get_switchover_epoch(i)] +
|
[i for i in actual_earlier_committee if epoch % PERSISTENT_COMMITTEE_PERIOD < get_switchover_epoch(i)] +
|
||||||
[i for i in later_committee if epoch % PERSISTENT_COMMITTEE_PERIOD >= get_switchover_epoch(i)]
|
[i for i in actual_later_committee if epoch % PERSISTENT_COMMITTEE_PERIOD >= get_switchover_epoch(i)]
|
||||||
)))
|
)))
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that this method makes use of the fact that the committee for any given shard always starts and ends at the same validator index independently of the committee count (this is because the validator set is split into `SHARD_COUNT * committee_count` slices but the first slice of a shard is a multiple `committee_count * i`, so the start of the slice is `n * committee_count * i // (SHARD_COUNT * committee_count) = n * i // SHARD_COUNT`, using the slightly nontrivial algebraic identity `(x * a) // ab == x // b`).
|
Note that this method makes use of the fact that the committee for any given shard always starts and ends at the same validator index independently of the committee count (this is because the validator set is split into `SHARD_COUNT * committee_count` slices but the first slice of a shard is a multiple `committee_count * i`, so the start of the slice is `n * committee_count * i // (SHARD_COUNT * committee_count) = n * i // SHARD_COUNT`, using the slightly nontrivial algebraic identity `(x * a) // ab == x // b`).
|
||||||
|
|
||||||
### Verifying blocks
|
## Verifying blocks
|
||||||
|
|
||||||
If a client wants to update its `finalized_header` it asks the network for a `BlockValidityProof`, which is simply:
|
If a client wants to update its `finalized_header` it asks the network for a `BlockValidityProof`, which is simply:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
{
|
{
|
||||||
'header': BlockHeader,
|
'header': BeaconBlockHeader,
|
||||||
'shard_aggregate_signature': 'bytes96',
|
'shard_aggregate_signature': 'bytes96',
|
||||||
'shard_bitfield': 'bytes',
|
'shard_bitfield': 'bytes',
|
||||||
'shard_parent_block': ShardBlock
|
'shard_parent_block': ShardBlock,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -151,23 +177,23 @@ The verification procedure is as follows:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def verify_block_validity_proof(proof: BlockValidityProof, validator_memory: ValidatorMemory) -> bool:
|
def verify_block_validity_proof(proof: BlockValidityProof, validator_memory: ValidatorMemory) -> bool:
|
||||||
assert proof.shard_parent_block.beacon_chain_ref == hash_tree_root(proof.header)
|
assert proof.shard_parent_block.beacon_chain_root == hash_tree_root(proof.header)
|
||||||
committee = compute_committee(proof.header, validator_memory)
|
committee = compute_committee(proof.header, validator_memory)
|
||||||
# Verify that we have >=50% support
|
# Verify that we have >=50% support
|
||||||
support_balance = sum([c.high_balance for i, c in enumerate(committee) if get_bitfield_bit(proof.shard_bitfield, i) is True])
|
support_balance = sum([v.high_balance for i, v in enumerate(committee) if get_bitfield_bit(proof.shard_bitfield, i) is True])
|
||||||
total_balance = sum([c.high_balance for i, c in enumerate(committee)]
|
total_balance = sum([v.high_balance for i, v in enumerate(committee)])
|
||||||
assert support_balance * 2 > total_balance
|
assert support_balance * 2 > total_balance
|
||||||
# Verify shard attestations
|
# Verify shard attestations
|
||||||
group_public_key = bls_aggregate_pubkeys([
|
group_public_key = bls_aggregate_pubkeys([
|
||||||
v.pubkey for v, index in enumerate(committee) if
|
v.pubkey for v, index in enumerate(committee)
|
||||||
get_bitfield_bit(proof.shard_bitfield, i) is True
|
if get_bitfield_bit(proof.shard_bitfield, index) is True
|
||||||
])
|
])
|
||||||
assert bls_verify(
|
assert bls_verify(
|
||||||
pubkey=group_public_key,
|
pubkey=group_public_key,
|
||||||
message_hash=hash_tree_root(shard_parent_block),
|
message_hash=hash_tree_root(shard_parent_block),
|
||||||
signature=shard_aggregate_signature,
|
signature=proof.shard_aggregate_signature,
|
||||||
domain=get_domain(state, slot_to_epoch(shard_block.slot), DOMAIN_SHARD_ATTESTER)
|
domain=get_domain(state, slot_to_epoch(shard_block.slot), DOMAIN_SHARD_ATTESTER),
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
The size of this proof is only 200 (header) + 96 (signature) + 16 (bitfield) + 352 (shard block) = 664 bytes. It can be reduced further by replacing `ShardBlock` with `MerklePartial(lambda x: x.beacon_chain_ref, ShardBlock)`, which would cut off ~220 bytes.
|
The size of this proof is only 200 (header) + 96 (signature) + 16 (bitfield) + 352 (shard block) = 664 bytes. It can be reduced further by replacing `ShardBlock` with `MerklePartial(lambda x: x.beacon_chain_root, ShardBlock)`, which would cut off ~220 bytes.
|
||||||
|
|
|
@ -9,6 +9,7 @@ from eth2spec.phase0.spec import (
|
||||||
)
|
)
|
||||||
from tests.helpers import (
|
from tests.helpers import (
|
||||||
get_valid_attester_slashing,
|
get_valid_attester_slashing,
|
||||||
|
next_epoch,
|
||||||
)
|
)
|
||||||
|
|
||||||
# mark entire file as 'attester_slashing'
|
# mark entire file as 'attester_slashing'
|
||||||
|
@ -58,6 +59,8 @@ def test_success_double(state):
|
||||||
|
|
||||||
|
|
||||||
def test_success_surround(state):
|
def test_success_surround(state):
|
||||||
|
next_epoch(state)
|
||||||
|
state.current_justified_epoch += 1
|
||||||
attester_slashing = get_valid_attester_slashing(state)
|
attester_slashing = get_valid_attester_slashing(state)
|
||||||
|
|
||||||
# set attestion1 to surround attestation 2
|
# set attestion1 to surround attestation 2
|
||||||
|
|
|
@ -14,7 +14,6 @@ MINIMAL_CONFIG = {
|
||||||
"MIN_ATTESTATION_INCLUSION_DELAY": 2,
|
"MIN_ATTESTATION_INCLUSION_DELAY": 2,
|
||||||
"TARGET_COMMITTEE_SIZE": 4,
|
"TARGET_COMMITTEE_SIZE": 4,
|
||||||
"SLOTS_PER_EPOCH": 8,
|
"SLOTS_PER_EPOCH": 8,
|
||||||
"GENESIS_EPOCH": spec.GENESIS_SLOT // 8,
|
|
||||||
"SLOTS_PER_HISTORICAL_ROOT": 64,
|
"SLOTS_PER_HISTORICAL_ROOT": 64,
|
||||||
"LATEST_RANDAO_MIXES_LENGTH": 64,
|
"LATEST_RANDAO_MIXES_LENGTH": 64,
|
||||||
"LATEST_ACTIVE_INDEX_ROOTS_LENGTH": 64,
|
"LATEST_ACTIVE_INDEX_ROOTS_LENGTH": 64,
|
||||||
|
|
|
@ -35,6 +35,8 @@ from eth2spec.phase0.spec import (
|
||||||
get_empty_block,
|
get_empty_block,
|
||||||
get_epoch_start_slot,
|
get_epoch_start_slot,
|
||||||
get_genesis_beacon_state,
|
get_genesis_beacon_state,
|
||||||
|
get_previous_epoch,
|
||||||
|
get_shard_delta,
|
||||||
slot_to_epoch,
|
slot_to_epoch,
|
||||||
verify_merkle_branch,
|
verify_merkle_branch,
|
||||||
hash,
|
hash,
|
||||||
|
@ -51,6 +53,19 @@ pubkeys = [bls.privtopub(privkey) for privkey in privkeys]
|
||||||
pubkey_to_privkey = {pubkey: privkey for privkey, pubkey in zip(privkeys, pubkeys)}
|
pubkey_to_privkey = {pubkey: privkey for privkey, pubkey in zip(privkeys, pubkeys)}
|
||||||
|
|
||||||
|
|
||||||
|
def set_bitfield_bit(bitfield, i):
|
||||||
|
"""
|
||||||
|
Set the bit in ``bitfield`` at position ``i`` to ``1``.
|
||||||
|
"""
|
||||||
|
byte_index = i // 8
|
||||||
|
bit_index = i % 8
|
||||||
|
return (
|
||||||
|
bitfield[:byte_index] +
|
||||||
|
bytes([bitfield[byte_index] | (1 << bit_index)]) +
|
||||||
|
bitfield[byte_index+1:]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def create_mock_genesis_validator_deposits(num_validators, deposit_data_leaves=None):
|
def create_mock_genesis_validator_deposits(num_validators, deposit_data_leaves=None):
|
||||||
if not deposit_data_leaves:
|
if not deposit_data_leaves:
|
||||||
deposit_data_leaves = []
|
deposit_data_leaves = []
|
||||||
|
@ -134,24 +149,31 @@ def build_deposit_data(state, pubkey, privkey, amount):
|
||||||
def build_attestation_data(state, slot, shard):
|
def build_attestation_data(state, slot, shard):
|
||||||
assert state.slot >= slot
|
assert state.slot >= slot
|
||||||
|
|
||||||
block_root = build_empty_block_for_next_slot(state).previous_block_root
|
if slot == state.slot:
|
||||||
|
block_root = build_empty_block_for_next_slot(state).previous_block_root
|
||||||
|
else:
|
||||||
|
block_root = get_block_root(state, slot)
|
||||||
|
|
||||||
epoch_start_slot = get_epoch_start_slot(get_current_epoch(state))
|
current_epoch_start_slot = get_epoch_start_slot(get_current_epoch(state))
|
||||||
if epoch_start_slot == slot:
|
if slot < current_epoch_start_slot:
|
||||||
|
epoch_boundary_root = get_block_root(state, get_epoch_start_slot(get_previous_epoch(state)))
|
||||||
|
elif slot == current_epoch_start_slot:
|
||||||
epoch_boundary_root = block_root
|
epoch_boundary_root = block_root
|
||||||
else:
|
else:
|
||||||
get_block_root(state, epoch_start_slot)
|
epoch_boundary_root = get_block_root(state, current_epoch_start_slot)
|
||||||
|
|
||||||
if slot < epoch_start_slot:
|
if slot < current_epoch_start_slot:
|
||||||
|
justified_epoch = state.previous_justified_epoch
|
||||||
justified_block_root = state.previous_justified_root
|
justified_block_root = state.previous_justified_root
|
||||||
else:
|
else:
|
||||||
|
justified_epoch = state.current_justified_epoch
|
||||||
justified_block_root = state.current_justified_root
|
justified_block_root = state.current_justified_root
|
||||||
|
|
||||||
return AttestationData(
|
return AttestationData(
|
||||||
slot=slot,
|
slot=slot,
|
||||||
shard=shard,
|
shard=shard,
|
||||||
beacon_block_root=block_root,
|
beacon_block_root=block_root,
|
||||||
source_epoch=state.current_justified_epoch,
|
source_epoch=justified_epoch,
|
||||||
source_root=justified_block_root,
|
source_root=justified_block_root,
|
||||||
target_root=epoch_boundary_root,
|
target_root=epoch_boundary_root,
|
||||||
crosslink_data_root=spec.ZERO_HASH,
|
crosslink_data_root=spec.ZERO_HASH,
|
||||||
|
@ -256,7 +278,13 @@ def get_valid_attester_slashing(state):
|
||||||
def get_valid_attestation(state, slot=None):
|
def get_valid_attestation(state, slot=None):
|
||||||
if slot is None:
|
if slot is None:
|
||||||
slot = state.slot
|
slot = state.slot
|
||||||
shard = state.latest_start_shard
|
|
||||||
|
if slot_to_epoch(slot) == get_current_epoch(state):
|
||||||
|
shard = (state.latest_start_shard + slot) % spec.SLOTS_PER_EPOCH
|
||||||
|
else:
|
||||||
|
previous_shard_delta = get_shard_delta(state, get_previous_epoch(state))
|
||||||
|
shard = (state.latest_start_shard - previous_shard_delta + slot) % spec.SHARD_COUNT
|
||||||
|
|
||||||
attestation_data = build_attestation_data(state, slot, shard)
|
attestation_data = build_attestation_data(state, slot, shard)
|
||||||
|
|
||||||
crosslink_committee = get_crosslink_committee_for_attestation(state, attestation_data)
|
crosslink_committee = get_crosslink_committee_for_attestation(state, attestation_data)
|
||||||
|
@ -352,6 +380,12 @@ def get_attestation_signature(state, attestation_data, privkey, custody_bit=0b0)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def fill_aggregate_attestation(state, attestation):
|
||||||
|
crosslink_committee = get_crosslink_committee_for_attestation(state, attestation.data)
|
||||||
|
for i in range(len(crosslink_committee)):
|
||||||
|
attestation.aggregation_bitfield = set_bitfield_bit(attestation.aggregation_bitfield, i)
|
||||||
|
|
||||||
|
|
||||||
def next_slot(state):
|
def next_slot(state):
|
||||||
block = build_empty_block_for_next_slot(state)
|
block = build_empty_block_for_next_slot(state)
|
||||||
state_transition(state, block)
|
state_transition(state, block)
|
||||||
|
|
|
@ -39,9 +39,11 @@ from eth2spec.utils.merkle_minimal import (
|
||||||
from .helpers import (
|
from .helpers import (
|
||||||
build_deposit_data,
|
build_deposit_data,
|
||||||
build_empty_block_for_next_slot,
|
build_empty_block_for_next_slot,
|
||||||
|
fill_aggregate_attestation,
|
||||||
get_valid_attestation,
|
get_valid_attestation,
|
||||||
get_valid_attester_slashing,
|
get_valid_attester_slashing,
|
||||||
get_valid_proposer_slashing,
|
get_valid_proposer_slashing,
|
||||||
|
next_slot,
|
||||||
privkeys,
|
privkeys,
|
||||||
pubkeys,
|
pubkeys,
|
||||||
)
|
)
|
||||||
|
@ -51,6 +53,33 @@ from .helpers import (
|
||||||
pytestmark = pytest.mark.sanity
|
pytestmark = pytest.mark.sanity
|
||||||
|
|
||||||
|
|
||||||
|
def check_finality(state,
|
||||||
|
prev_state,
|
||||||
|
current_justified_changed,
|
||||||
|
previous_justified_changed,
|
||||||
|
finalized_changed):
|
||||||
|
if current_justified_changed:
|
||||||
|
assert state.current_justified_epoch > prev_state.current_justified_epoch
|
||||||
|
assert state.current_justified_root != prev_state.current_justified_root
|
||||||
|
else:
|
||||||
|
assert state.current_justified_epoch == prev_state.current_justified_epoch
|
||||||
|
assert state.current_justified_root == prev_state.current_justified_root
|
||||||
|
|
||||||
|
if previous_justified_changed:
|
||||||
|
assert state.previous_justified_epoch > prev_state.previous_justified_epoch
|
||||||
|
assert state.previous_justified_root != prev_state.previous_justified_root
|
||||||
|
else:
|
||||||
|
assert state.previous_justified_epoch == prev_state.previous_justified_epoch
|
||||||
|
assert state.previous_justified_root == prev_state.previous_justified_root
|
||||||
|
|
||||||
|
if finalized_changed:
|
||||||
|
assert state.finalized_epoch > prev_state.finalized_epoch
|
||||||
|
assert state.finalized_root != prev_state.finalized_root
|
||||||
|
else:
|
||||||
|
assert state.finalized_epoch == prev_state.finalized_epoch
|
||||||
|
assert state.finalized_root == prev_state.finalized_root
|
||||||
|
|
||||||
|
|
||||||
def test_slot_transition(state):
|
def test_slot_transition(state):
|
||||||
test_state = deepcopy(state)
|
test_state = deepcopy(state)
|
||||||
cache_state(test_state)
|
cache_state(test_state)
|
||||||
|
@ -115,6 +144,33 @@ def test_empty_epoch_transition_not_finalizing(state):
|
||||||
return state, [block], test_state
|
return state, [block], test_state
|
||||||
|
|
||||||
|
|
||||||
|
def test_full_attestations_finalizing(state):
|
||||||
|
test_state = deepcopy(state)
|
||||||
|
|
||||||
|
for slot in range(spec.MIN_ATTESTATION_INCLUSION_DELAY):
|
||||||
|
next_slot(test_state)
|
||||||
|
|
||||||
|
for epoch in range(5):
|
||||||
|
for slot in range(spec.SLOTS_PER_EPOCH):
|
||||||
|
print(test_state.slot)
|
||||||
|
attestation = get_valid_attestation(test_state, test_state.slot - spec.MIN_ATTESTATION_INCLUSION_DELAY)
|
||||||
|
fill_aggregate_attestation(test_state, attestation)
|
||||||
|
block = build_empty_block_for_next_slot(test_state)
|
||||||
|
block.body.attestations.append(attestation)
|
||||||
|
state_transition(test_state, block)
|
||||||
|
|
||||||
|
if epoch == 0:
|
||||||
|
check_finality(test_state, state, False, False, False)
|
||||||
|
elif epoch == 1:
|
||||||
|
check_finality(test_state, state, False, False, False)
|
||||||
|
elif epoch == 2:
|
||||||
|
check_finality(test_state, state, True, False, False)
|
||||||
|
elif epoch == 3:
|
||||||
|
check_finality(test_state, state, True, True, False)
|
||||||
|
elif epoch == 4:
|
||||||
|
check_finality(test_state, state, True, True, True)
|
||||||
|
|
||||||
|
|
||||||
def test_proposer_slashing(state):
|
def test_proposer_slashing(state):
|
||||||
test_state = deepcopy(state)
|
test_state = deepcopy(state)
|
||||||
proposer_slashing = get_valid_proposer_slashing(state)
|
proposer_slashing = get_valid_proposer_slashing(state)
|
||||||
|
|
Loading…
Reference in New Issue