test_process_registry_updates.py
1 import eth2spec.phase0.spec as spec 2 3 from eth2spec.phase0.spec import ( 4 get_current_epoch, 5 is_active_validator, 6 process_registry_updates 7 ) 8 from eth2spec.phase0.state_transition import state_transition 9 from eth2spec.test.helpers.block import build_empty_block_for_next_slot, sign_block 10 from eth2spec.test.helpers.state import next_epoch 11 from eth2spec.test.context import spec_state_test 12 13 14 def run_process_registry_updates(state, valid=True): 15 """ 16 Run ``process_crosslinks``, yielding: 17 - pre-state ('pre') 18 - post-state ('post'). 19 If ``valid == False``, run expecting ``AssertionError`` 20 """ 21 # transition state to slot before state transition 22 slot = state.slot + (spec.SLOTS_PER_EPOCH - state.slot % spec.SLOTS_PER_EPOCH) - 1 23 block = build_empty_block_for_next_slot(state) 24 block.slot = slot 25 sign_block(state, block) 26 state_transition(state, block) 27 28 # cache state before epoch transition 29 spec.cache_state(state) 30 31 # process components of epoch transition before registry update 32 spec.process_justification_and_finalization(state) 33 spec.process_crosslinks(state) 34 spec.process_rewards_and_penalties(state) 35 36 yield 'pre', state 37 process_registry_updates(state) 38 yield 'post', state 39 40 41 @spec_state_test 42 def test_activation(state): 43 index = 0 44 assert is_active_validator(state.validator_registry[index], get_current_epoch(state)) 45 46 # Mock a new deposit 47 state.validator_registry[index].activation_eligibility_epoch = spec.FAR_FUTURE_EPOCH 48 state.validator_registry[index].activation_epoch = spec.FAR_FUTURE_EPOCH 49 state.validator_registry[index].effective_balance = spec.MAX_EFFECTIVE_BALANCE 50 assert not is_active_validator(state.validator_registry[index], get_current_epoch(state)) 51 52 for _ in range(spec.ACTIVATION_EXIT_DELAY + 1): 53 next_epoch(state) 54 55 yield from run_process_registry_updates(state) 56 57 assert state.validator_registry[index].activation_eligibility_epoch != spec.FAR_FUTURE_EPOCH 58 assert state.validator_registry[index].activation_epoch != spec.FAR_FUTURE_EPOCH 59 assert is_active_validator( 60 state.validator_registry[index], 61 get_current_epoch(state), 62 ) 63 64 65 @spec_state_test 66 def test_ejection(state): 67 index = 0 68 assert is_active_validator(state.validator_registry[index], get_current_epoch(state)) 69 assert state.validator_registry[index].exit_epoch == spec.FAR_FUTURE_EPOCH 70 71 # Mock an ejection 72 state.validator_registry[index].effective_balance = spec.EJECTION_BALANCE 73 74 for _ in range(spec.ACTIVATION_EXIT_DELAY + 1): 75 next_epoch(state) 76 77 yield from run_process_registry_updates(state) 78 79 assert state.validator_registry[index].exit_epoch != spec.FAR_FUTURE_EPOCH 80 assert not is_active_validator( 81 state.validator_registry[index], 82 get_current_epoch(state), 83 )