merge in fork choice. tests pass
This commit is contained in:
parent
bacd4b1e89
commit
b8c0985e60
|
@ -1337,13 +1337,14 @@ def process_justification_and_finalization(state: BeaconState) -> None:
|
|||
state, get_matching_target_attestations(state, previous_epoch)
|
||||
)
|
||||
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)
|
||||
current_epoch_matching_target_balance = get_attesting_balance(
|
||||
state, get_matching_target_attestations(state, current_epoch)
|
||||
)
|
||||
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)
|
||||
|
||||
# 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:
|
||||
state.finalized_checkpoint = old_previous_justified_checkpoint
|
||||
# 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
|
||||
# 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:
|
||||
|
|
|
@ -55,15 +55,6 @@ The head block root associated with a `store` is defined as `get_head(store)`. A
|
|||
|
||||
### Helpers
|
||||
|
||||
#### `Checkpoint`
|
||||
|
||||
```python
|
||||
@dataclass(eq=True, frozen=True)
|
||||
class Checkpoint(object):
|
||||
epoch: Epoch
|
||||
root: Hash
|
||||
```
|
||||
|
||||
#### `Store`
|
||||
|
||||
```python
|
||||
|
@ -84,8 +75,8 @@ class Store(object):
|
|||
def get_genesis_store(genesis_state: BeaconState) -> Store:
|
||||
genesis_block = BeaconBlock(state_root=hash_tree_root(genesis_state))
|
||||
root = signing_root(genesis_block)
|
||||
justified_checkpoint = Checkpoint(GENESIS_EPOCH, root)
|
||||
finalized_checkpoint = Checkpoint(GENESIS_EPOCH, root)
|
||||
justified_checkpoint = Checkpoint(epoch=GENESIS_EPOCH, root=root)
|
||||
finalized_checkpoint = Checkpoint(epoch=GENESIS_EPOCH, root=root)
|
||||
return Store(
|
||||
time=genesis_state.genesis_time,
|
||||
justified_checkpoint=justified_checkpoint,
|
||||
|
@ -168,21 +159,21 @@ def on_block(store: Store, block: BeaconBlock) -> None:
|
|||
store.block_states[signing_root(block)] = state
|
||||
|
||||
# Update justified checkpoint
|
||||
if state.current_justified_epoch > store.justified_checkpoint.epoch:
|
||||
store.justified_checkpoint = Checkpoint(state.current_justified_epoch, state.current_justified_root)
|
||||
elif state.previous_justified_epoch > store.justified_checkpoint.epoch:
|
||||
store.justified_checkpoint = Checkpoint(state.previous_justified_epoch, state.previous_justified_root)
|
||||
if state.current_justified_checkpoint.epoch > store.justified_checkpoint.epoch:
|
||||
store.justified_checkpoint = state.current_justified_checkpoint
|
||||
elif state.previous_justified_checkpoint.epoch > store.justified_checkpoint.epoch:
|
||||
store.justified_checkpoint = state.previous_justified_checkpoint
|
||||
|
||||
# Update finalized checkpoint
|
||||
if state.finalized_epoch > state.finalized_epoch:
|
||||
store.finalized_checkpoint = Checkpoint(state.finalized_epoch, state.finalized_root)
|
||||
if state.finalized_checkpoint.epoch > store.finalized_checkpoint.epoch:
|
||||
store.finalized_checkpoint = state.finalized_checkpoint
|
||||
```
|
||||
|
||||
#### `on_attestation`
|
||||
|
||||
```python
|
||||
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
|
||||
assert target.root in store.blocks
|
||||
|
|
|
@ -21,8 +21,8 @@ def run_on_attestation(spec, state, store, attestation, valid=True):
|
|||
assert (
|
||||
store.latest_targets[indexed_attestation.custody_bit_0_indices[0]] ==
|
||||
spec.Checkpoint(
|
||||
epoch=attestation.data.target_epoch,
|
||||
root=attestation.data.target_root,
|
||||
epoch=attestation.data.target.epoch,
|
||||
root=attestation.data.target.root,
|
||||
)
|
||||
)
|
||||
|
||||
|
|
|
@ -180,8 +180,8 @@ def test_invalid_current_source_root(spec, state):
|
|||
state.slot = spec.SLOTS_PER_EPOCH * 5
|
||||
state.finalized_epoch = 2
|
||||
|
||||
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.previous_justified_checkpoint = spec.Checkpoint(epoch=3, root=b'\x01' * 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)
|
||||
state.slot += spec.MIN_ATTESTATION_INCLUSION_DELAY
|
||||
|
|
Loading…
Reference in New Issue