Skip to content

Commit

Permalink
Added consistency check for FFG & LMD vote in validate_on_atttestatio…
Browse files Browse the repository at this point in the history
…n(), fixes #1636, fixes #1456, fixes #1408
  • Loading branch information
adiasg committed Apr 22, 2020
1 parent 956a3e7 commit 4c6be40
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion specs/phase0/fork-choice.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [`get_latest_attesting_balance`](#get_latest_attesting_balance)
- [`filter_block_tree`](#filter_block_tree)
- [`get_filtered_block_tree`](#get_filtered_block_tree)
- [`is_descendant_block`](#is_descendant_block)
- [`get_head`](#get_head)
- [`should_update_justified_checkpoint`](#should_update_justified_checkpoint)
- [`on_attestation` helpers](#on_attestation-helpers)
Expand Down Expand Up @@ -162,7 +163,7 @@ def get_latest_attesting_balance(store: Store, root: Root) -> Gwei:
active_indices = get_active_validator_indices(state, get_current_epoch(state))
return Gwei(sum(
state.validators[i].effective_balance for i in active_indices
if (i in store.latest_messages
if (i in store.latest_messages
and get_ancestor(store, store.latest_messages[i].root, store.blocks[root].slot) == root)
))
```
Expand Down Expand Up @@ -220,6 +221,28 @@ def get_filtered_block_tree(store: Store) -> Dict[Root, BeaconBlock]:
return blocks
```

#### `is_descendant_block`

```python
def is_descendant_block(store: Store, base_root: Root, descendant_root: Root) -> bool:
"""
Checks if the block with root ``descendant_root`` is a descendant of the block with root ``base_root``
"""
descendants = [base_root]

# Traverse the descendants block tree and check if ``descendant_root`` is encountered
while(descendants):
if descendants[0] == descendant_root:
return True
descendants.extend([
root for root in store.blocks.keys()
if store.blocks[root].parent_root == descendants[0]
])
descendants = descendants[1:]

return False
```

#### `get_head`

```python
Expand Down Expand Up @@ -286,6 +309,9 @@ def validate_on_attestation(store: Store, attestation: Attestation) -> None:
# Attestations must not be for blocks in the future. If not, the attestation should not be considered
assert store.blocks[attestation.data.beacon_block_root].slot <= attestation.data.slot

# FFG and LMD vote must be consistent with each other
assert is_descendant_block(store, target.root, attestation.data.beacon_block_root)

# Attestations can only affect the fork choice of subsequent slots.
# Delay consideration in the fork choice until their slot is in the past.
assert get_current_slot(store) >= attestation.data.slot + 1
Expand Down

0 comments on commit 4c6be40

Please sign in to comment.