Skip to content

Commit

Permalink
Add (shard, shard_root) to LatestMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed May 5, 2020
1 parent fca1bbc commit 79b1b4b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
32 changes: 31 additions & 1 deletion specs/phase1/fork-choice.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

- [Introduction](#introduction)
- [Fork choice](#fork-choice)
- [Helpers](#helpers)
- [Extended `LatestMessage`](#extended-latestmessage)
- [Updated `update_latest_messages`](#updated-update_latest_messages)
- [Handlers](#handlers)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
Expand All @@ -25,6 +28,33 @@ Due to the changes in the structure of `IndexedAttestation` in Phase 1, `on_atte

The rest of the fork choice remains stable.

### Helpers

#### Extended `LatestMessage`

```python
@dataclass(eq=True, frozen=True)
class LatestMessage(object):
epoch: Epoch
root: Root
shard: Shard
shard_root: Root
```

#### Updated `update_latest_messages`

```python
def update_latest_messages(store: Store, attesting_indices: Sequence[ValidatorIndex], attestation: Attestation) -> None:
target = attestation.data.target
beacon_block_root = attestation.data.beacon_block_root
shard = get_shard(store.block_states[beacon_block_root], attestation)
for i in attesting_indices:
if i not in store.latest_messages or target.epoch > store.latest_messages[i].epoch:
store.latest_messages[i] = LatestMessage(
epoch=target.epoch, root=beacon_block_root, shard=shard, shard_root=attestation.data.head_shard_root
)
```

### Handlers

```python
Expand All @@ -49,4 +79,4 @@ def on_attestation(store: Store, attestation: Attestation) -> None:
if attestation.aggregation_bits[i]
]
update_latest_messages(store, attesting_indices, attestation)
```
```
13 changes: 10 additions & 3 deletions tests/core/pyspec/eth2spec/test/fork_choice/test_on_attestation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@ def run_on_attestation(spec, state, store, attestation, valid=True):

if spec.fork == PHASE0:
sample_index = indexed_attestation.attesting_indices[0]
latest_message = spec.LatestMessage(
epoch=attestation.data.target.epoch,
root=attestation.data.beacon_block_root,
)
else:
attesting_indices = [
index for i, index in enumerate(indexed_attestation.committee)
if attestation.aggregation_bits[i]
]
sample_index = attesting_indices[0]
assert (
store.latest_messages[sample_index] ==
spec.LatestMessage(
latest_message = spec.LatestMessage(
epoch=attestation.data.target.epoch,
root=attestation.data.beacon_block_root,
shard=spec.get_shard(state, attestation),
shard_root=attestation.data.head_shard_root,
)

assert (
store.latest_messages[sample_index] == latest_message
)


Expand Down

0 comments on commit 79b1b4b

Please sign in to comment.