merge in fork choice. tests pass

This commit is contained in:
Danny Ryan 2019-06-24 21:43:05 -06:00
parent bacd4b1e89
commit b8c0985e60
No known key found for this signature in database
GPG Key ID: 2765A792E42CE07A
4 changed files with 17 additions and 25 deletions

View File

@ -1337,13 +1337,14 @@ def process_justification_and_finalization(state: BeaconState) -> None:
state, get_matching_target_attestations(state, previous_epoch) state, get_matching_target_attestations(state, previous_epoch)
) )
if previous_epoch_matching_target_balance * 3 >= get_total_active_balance(state) * 2: if previous_epoch_matching_target_balance * 3 >= get_total_active_balance(state) * 2:
state.current_justified_checkpoint = Checkpoint(previous_epoch, get_block_root(state, previous_epoch)) state.current_justified_checkpoint = Checkpoint(epoch=previous_epoch,
root=get_block_root(state, previous_epoch))
state.justification_bitfield |= (1 << 1) state.justification_bitfield |= (1 << 1)
current_epoch_matching_target_balance = get_attesting_balance( current_epoch_matching_target_balance = get_attesting_balance(
state, get_matching_target_attestations(state, current_epoch) state, get_matching_target_attestations(state, current_epoch)
) )
if current_epoch_matching_target_balance * 3 >= get_total_active_balance(state) * 2: if current_epoch_matching_target_balance * 3 >= get_total_active_balance(state) * 2:
state.current_justified_checkpoint = Checkpoint(current_epoch, get_block_root(state, current_epoch)) state.current_justified_checkpoint = Checkpoint(epoch=current_epoch, root=get_block_root(state, current_epoch))
state.justification_bitfield |= (1 << 0) state.justification_bitfield |= (1 << 0)
# Process finalizations # Process finalizations
@ -1352,7 +1353,7 @@ def process_justification_and_finalization(state: BeaconState) -> None:
if (bitfield >> 1) % 8 == 0b111 and old_previous_justified_checkpoint.epoch + 3 == current_epoch: if (bitfield >> 1) % 8 == 0b111 and old_previous_justified_checkpoint.epoch + 3 == current_epoch:
state.finalized_checkpoint = old_previous_justified_checkpoint state.finalized_checkpoint = old_previous_justified_checkpoint
# 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 old_previous_justified_checkpoint.epoch+ 2 == current_epoch: if (bitfield >> 1) % 4 == 0b11 and old_previous_justified_checkpoint.epoch + 2 == current_epoch:
state.finalized_checkpoint = old_previous_justified_checkpoint state.finalized_checkpoint = old_previous_justified_checkpoint
# 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 old_current_justified_checkpoint.epoch + 2 == current_epoch: if (bitfield >> 0) % 8 == 0b111 and old_current_justified_checkpoint.epoch + 2 == current_epoch:

View File

@ -55,15 +55,6 @@ The head block root associated with a `store` is defined as `get_head(store)`. A
### Helpers ### Helpers
#### `Checkpoint`
```python
@dataclass(eq=True, frozen=True)
class Checkpoint(object):
epoch: Epoch
root: Hash
```
#### `Store` #### `Store`
```python ```python
@ -84,8 +75,8 @@ class Store(object):
def get_genesis_store(genesis_state: BeaconState) -> Store: def get_genesis_store(genesis_state: BeaconState) -> Store:
genesis_block = BeaconBlock(state_root=hash_tree_root(genesis_state)) genesis_block = BeaconBlock(state_root=hash_tree_root(genesis_state))
root = signing_root(genesis_block) root = signing_root(genesis_block)
justified_checkpoint = Checkpoint(GENESIS_EPOCH, root) justified_checkpoint = Checkpoint(epoch=GENESIS_EPOCH, root=root)
finalized_checkpoint = Checkpoint(GENESIS_EPOCH, root) finalized_checkpoint = Checkpoint(epoch=GENESIS_EPOCH, root=root)
return Store( return Store(
time=genesis_state.genesis_time, time=genesis_state.genesis_time,
justified_checkpoint=justified_checkpoint, justified_checkpoint=justified_checkpoint,
@ -168,21 +159,21 @@ def on_block(store: Store, block: BeaconBlock) -> None:
store.block_states[signing_root(block)] = state store.block_states[signing_root(block)] = state
# Update justified checkpoint # Update justified checkpoint
if state.current_justified_epoch > store.justified_checkpoint.epoch: if state.current_justified_checkpoint.epoch > store.justified_checkpoint.epoch:
store.justified_checkpoint = Checkpoint(state.current_justified_epoch, state.current_justified_root) store.justified_checkpoint = state.current_justified_checkpoint
elif state.previous_justified_epoch > store.justified_checkpoint.epoch: elif state.previous_justified_checkpoint.epoch > store.justified_checkpoint.epoch:
store.justified_checkpoint = Checkpoint(state.previous_justified_epoch, state.previous_justified_root) store.justified_checkpoint = state.previous_justified_checkpoint
# Update finalized checkpoint # Update finalized checkpoint
if state.finalized_epoch > state.finalized_epoch: if state.finalized_checkpoint.epoch > store.finalized_checkpoint.epoch:
store.finalized_checkpoint = Checkpoint(state.finalized_epoch, state.finalized_root) store.finalized_checkpoint = state.finalized_checkpoint
``` ```
#### `on_attestation` #### `on_attestation`
```python ```python
def on_attestation(store: Store, attestation: Attestation) -> None: def on_attestation(store: Store, attestation: Attestation) -> None:
target = Checkpoint(attestation.data.target_epoch, attestation.data.target_root) target = attestation.data.target
# Cannot calculate the current shuffling if have not seen the target # Cannot calculate the current shuffling if have not seen the target
assert target.root in store.blocks assert target.root in store.blocks

View File

@ -21,8 +21,8 @@ def run_on_attestation(spec, state, store, attestation, valid=True):
assert ( assert (
store.latest_targets[indexed_attestation.custody_bit_0_indices[0]] == store.latest_targets[indexed_attestation.custody_bit_0_indices[0]] ==
spec.Checkpoint( spec.Checkpoint(
epoch=attestation.data.target_epoch, epoch=attestation.data.target.epoch,
root=attestation.data.target_root, root=attestation.data.target.root,
) )
) )

View File

@ -180,8 +180,8 @@ def test_invalid_current_source_root(spec, state):
state.slot = spec.SLOTS_PER_EPOCH * 5 state.slot = spec.SLOTS_PER_EPOCH * 5
state.finalized_epoch = 2 state.finalized_epoch = 2
state.previous_justified_checkpoint = spec.Checkpoint(epoch=3, root=b'\x01'*32) state.previous_justified_checkpoint = spec.Checkpoint(epoch=3, root=b'\x01' * 32)
state.current_justified_checkpoint = spec.Checkpoint(epoch=4, root=b'\x32'*32) state.current_justified_checkpoint = spec.Checkpoint(epoch=4, root=b'\x32' * 32)
attestation = get_valid_attestation(spec, state, slot=(spec.SLOTS_PER_EPOCH * 3) + 1) attestation = get_valid_attestation(spec, state, slot=(spec.SLOTS_PER_EPOCH * 3) + 1)
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY