def test_full_attestations_misc_balances(spec, state): attestations = prepare_state_with_attestations(spec, state) pre_state = state.copy() yield from run_process_rewards_and_penalties(spec, state) attesting_indices = spec.get_unslashed_attesting_indices(state, attestations) assert len(attesting_indices) > 0 assert len(attesting_indices) != len(pre_state.validators) assert any(v.effective_balance != spec.MAX_EFFECTIVE_BALANCE for v in state.validators) for index in range(len(pre_state.validators)): if index in attesting_indices: assert state.balances[index] > pre_state.balances[index] elif spec.is_active_validator(pre_state.validators[index], spec.compute_epoch_at_slot(state.slot)): assert state.balances[index] < pre_state.balances[index] else: assert state.balances[index] == pre_state.balances[index] # Check if base rewards are consistent with effective balance. brs = {} for index in attesting_indices: br = spec.get_base_reward(state, index) if br in brs: assert brs[br] == state.validators[index].effective_balance else: brs[br] = state.validators[index].effective_balance
def run_with_participation(spec, state, participation_fn): participated = set() def participation_tracker(slot, comm_index, comm): att_participants = participation_fn(slot, comm_index, comm) participated.update(att_participants) return att_participants attestations = prepare_state_with_attestations(spec, state, participation_fn=participation_tracker) proposer_indices = [a.proposer_index for a in state.previous_epoch_attestations] pre_state = state.copy() yield from run_process_rewards_and_penalties(spec, state) attesting_indices = spec.get_unslashed_attesting_indices(state, attestations) assert len(attesting_indices) == len(participated) for index in range(len(pre_state.validators)): if spec.is_in_inactivity_leak(state): # Proposers can still make money during a leak if index in proposer_indices and index in participated: assert state.balances[index] > pre_state.balances[index] # If not proposer but participated optimally, should have exactly neutral balance elif index in attesting_indices: assert state.balances[index] == pre_state.balances[index] else: assert state.balances[index] < pre_state.balances[index] else: if index in participated: assert state.balances[index] > pre_state.balances[index] else: assert state.balances[index] < pre_state.balances[index]
def test_attestations_some_slashed(spec, state): attestations = prepare_state_with_attestations(spec, state) attesting_indices_before_slashings = list( spec.get_unslashed_attesting_indices(state, attestations)) # Slash maximum amount of validators allowed per epoch. for i in range(spec.MIN_PER_EPOCH_CHURN_LIMIT): spec.slash_validator(state, attesting_indices_before_slashings[i]) assert len(state.previous_epoch_attestations) == len(attestations) pre_state = state.copy() yield from run_process_rewards_and_penalties(spec, state) attesting_indices = spec.get_unslashed_attesting_indices( state, attestations) assert len(attesting_indices) > 0 assert len(attesting_indices_before_slashings) - len( attesting_indices) == spec.MIN_PER_EPOCH_CHURN_LIMIT for index in range(len(pre_state.validators)): if index in attesting_indices: # non-slashed attester should gain reward assert state.balances[index] > pre_state.balances[index] else: # Slashed non-proposer attester should have penalty assert state.balances[index] < pre_state.balances[index]
def test_full_attestations_one_validaor_one_gwei(spec, state): attestations = prepare_state_with_attestations(spec, state) yield from run_process_rewards_and_penalties(spec, state) # Few assertions. Mainly to check that this extreme case can run without exception attesting_indices = spec.get_unslashed_attesting_indices(state, attestations) assert len(attesting_indices) == 1
def test_full_attestations_random_incorrect_fields(spec, state): attestations = prepare_state_with_attestations(spec, state) for i, attestation in enumerate(state.previous_epoch_attestations): if i % 3 == 0: # Mess up some head votes attestation.data.beacon_block_root = b'\x56' * 32 if i % 3 == 1: # Message up some target votes attestation.data.target.root = b'\x23' * 32 if i % 3 == 2: # Keep some votes 100% correct pass yield from run_process_rewards_and_penalties(spec, state) attesting_indices = spec.get_unslashed_attesting_indices(state, attestations) assert len(attesting_indices) > 0
def run_with_participation(spec, state, participation_fn): participated = set() def participation_tracker(slot, comm_index, comm): att_participants = participation_fn(slot, comm_index, comm) participated.update(att_participants) return att_participants attestations = prepare_state_with_attestations(spec, state, participation_fn=participation_tracker) pre_state = state.copy() yield from run_process_rewards_and_penalties(spec, state) attesting_indices = spec.get_unslashed_attesting_indices(state, attestations) assert len(attesting_indices) == len(participated) validate_resulting_balances(spec, pre_state, state, attestations)
def test_full_attestations_misc_balances(spec, state): attestations = prepare_state_with_attestations(spec, state) pre_state = state.copy() yield from run_process_rewards_and_penalties(spec, state) validate_resulting_balances(spec, pre_state, state, attestations) # Check if base rewards are consistent with effective balance. brs = {} attesting_indices = spec.get_unslashed_attesting_indices(state, attestations) for index in attesting_indices: br = spec.get_base_reward(state, index) if br in brs: assert brs[br] == state.validators[index].effective_balance else: brs[br] = state.validators[index].effective_balance
def test_attestations_some_slashed(spec, state): attestations = prepare_state_with_attestations(spec, state) attesting_indices_before_slashings = list(spec.get_unslashed_attesting_indices(state, attestations)) # Slash maximum amount of validators allowed per epoch. for i in range(spec.MIN_PER_EPOCH_CHURN_LIMIT): spec.slash_validator(state, attesting_indices_before_slashings[i]) if not is_post_altair(spec): assert len(state.previous_epoch_attestations) == len(attestations) pre_state = state.copy() yield from run_process_rewards_and_penalties(spec, state) attesting_indices = spec.get_unslashed_attesting_indices(state, attestations) assert len(attesting_indices) > 0 assert len(attesting_indices_before_slashings) - len(attesting_indices) == spec.MIN_PER_EPOCH_CHURN_LIMIT validate_resulting_balances(spec, pre_state, state, attestations)