Merge pull request #44 from ethereum/fix-recent_block_hashes

Fix `recent_block_hashes`
This commit is contained in:
Danny Ryan 2018-10-16 10:36:31 -05:00 committed by GitHub
commit f3ea9bf43c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -177,7 +177,7 @@ The `ActiveState` has the following fields:
'pending_attestations': [AttestationRecord], 'pending_attestations': [AttestationRecord],
# Specials not yet been processed # Specials not yet been processed
'pending_specials': [SpecialRecord] 'pending_specials': [SpecialRecord]
# Most recent 2 * CYCLE_LENGTH block hashes, older to newer # recent block hashes needed to process attestations, older to newer
'recent_block_hashes': ['hash32'], 'recent_block_hashes': ['hash32'],
# RANDAO state # RANDAO state
'randao_mix': 'hash32' 'randao_mix': 'hash32'
@ -454,8 +454,8 @@ def get_shards_and_committees_for_slot(crystallized_state: CrystallizedState,
def get_block_hash(active_state: ActiveState, def get_block_hash(active_state: ActiveState,
current_block: BeaconBlock, current_block: BeaconBlock,
slot: int) -> Hash32: slot: int) -> Hash32:
earliest_slot_in_array = current_block.slot - CYCLE_LENGTH * 2 earliest_slot_in_array = current_block.slot - len(active_state.recent_block_hashes)
assert earliest_slot_in_array <= slot < earliest_slot_in_array + CYCLE_LENGTH * 2 assert earliest_slot_in_array <= slot < current_block.slot
return active_state.recent_block_hashes[slot - earliest_slot_in_array] return active_state.recent_block_hashes[slot - earliest_slot_in_array]
``` ```
@ -605,15 +605,15 @@ This procedure should be carried out every block.
First, set `recent_block_hashes` to the output of the following, where `parent_hash` is the hash of the immediate previous block (ie. must be equal to `ancestor_hashes[0]`): First, set `recent_block_hashes` to the output of the following, where `parent_hash` is the hash of the immediate previous block (ie. must be equal to `ancestor_hashes[0]`):
```python ```python
def get_new_recent_block_hashes(old_block_hashes: List[Hash32], def append_to_recent_block_hashes(old_block_hashes: List[Hash32],
parent_slot: int, parent_slot: int,
current_slot: int, current_slot: int,
parent_hash: Hash32) -> List[Hash32]: parent_hash: Hash32) -> List[Hash32]:
d = current_slot - parent_slot d = current_slot - parent_slot
return old_block_hashes[d:] + [parent_hash] * min(d, len(old_block_hashes)) return old_block_hashes + [parent_hash] * d
``` ```
The output of `get_block_hash` should not change, except that it will no longer throw for `current_slot - 1`, and will now throw for `current_slot - CYCLE_LENGTH * 2 - 1`. Also, check that the block's `ancestor_hashes` array was correctly updated, using the following algorithm: The output of `get_block_hash` should not change, except that it will no longer throw for `current_slot - 1`. Also, check that the block's `ancestor_hashes` array was correctly updated, using the following algorithm:
```python ```python
def update_ancestor_hashes(parent_ancestor_hashes: List[Hash32], def update_ancestor_hashes(parent_ancestor_hashes: List[Hash32],
@ -716,6 +716,7 @@ For each `SpecialRecord` `obj` in `active_state.pending_specials`:
* Set `crystallized_state.last_state_recalculation_slot += CYCLE_LENGTH` * Set `crystallized_state.last_state_recalculation_slot += CYCLE_LENGTH`
* Remove all attestation records older than slot `crystallized_state.last_state_recalculation_slot` * Remove all attestation records older than slot `crystallized_state.last_state_recalculation_slot`
* Empty the `active_state.pending_specials` list * Empty the `active_state.pending_specials` list
* Set `active_state.recent_block_hashes = active_state.recent_block_hashes[CYCLE_LENGTH:]`
* Set `shard_and_committee_for_slots[:CYCLE_LENGTH] = shard_and_committee_for_slots[CYCLE_LENGTH:]` * Set `shard_and_committee_for_slots[:CYCLE_LENGTH] = shard_and_committee_for_slots[CYCLE_LENGTH:]`
### Validator set change ### Validator set change