def get_attesting_balance_from_attestations(
        *, state: 'BeaconState', effective_balances: Dict[ValidatorIndex,
                                                          Gwei],
        attestations: Sequence[PendingAttestation],
        committee_config: CommitteeConfig) -> Gwei:
    return get_total_balance_from_effective_balances(
        effective_balances,
        get_attester_indices_from_attestations(
            state=state,
            attestations=attestations,
            committee_config=committee_config,
        ),
    )
def get_winning_root_and_participants(
    *, state: 'BeaconState', shard: Shard,
    effective_balances: Dict[ValidatorIndex,
                             Gwei], committee_config: CommitteeConfig
) -> Tuple[Hash32, Tuple[ValidatorIndex, ...]]:
    valid_attestations = _filter_attestations_by_latest_crosslinks_and_shard(
        state.current_epoch_attestations + state.previous_epoch_attestations,
        state.latest_crosslinks[shard],
        shard,
    )
    all_roots = set([a.data.crosslink_data_root for a in valid_attestations])

    # handle when no attestations for shard available
    if len(all_roots) == 0:
        return (Hash32(ZERO_HASH32), tuple())

    def get_attestations_for(root: Hash32) -> Sequence[PendingAttestation]:
        return [
            a for a in valid_attestations if a.data.crosslink_data_root == root
        ]

    # Winning crosslink root is the root with the most votes for it, ties broken in favor of
    # lexicographically higher hash
    winning_root: Hash32 = max(
        all_roots,
        key=lambda r: (
            get_attesting_balance_from_attestations(
                state=state,
                effective_balances=effective_balances,
                attestations=get_attestations_for(r),
                committee_config=committee_config,
            ),
            r,
        ),
    )

    return (
        winning_root,
        get_attester_indices_from_attestations(
            state=state,
            attestations=get_attestations_for(winning_root),
            committee_config=committee_config,
        ),
    )
Exemple #3
0
def process_rewards_and_penalties(state: BeaconState,
                                  config: Eth2Config) -> BeaconState:
    # Compute previous epoch active validator indices and the total balance they account for
    # for later use.
    previous_epoch_active_validator_indices = set(
        get_active_validator_indices(
            state.validator_registry,
            state.previous_epoch(config.SLOTS_PER_EPOCH)))
    previous_total_balance: Gwei = get_total_balance(
        state.validator_balances,
        tuple(previous_epoch_active_validator_indices),
        config.MAX_DEPOSIT_AMOUNT,
    )

    # Compute previous epoch attester indices and the total balance they account for
    # for later use.
    previous_epoch_attestations = state.previous_epoch_attestations
    previous_epoch_attester_indices = get_attester_indices_from_attestations(
        state=state,
        attestations=previous_epoch_attestations,
        committee_config=CommitteeConfig(config),
    )

    # Compute inclusion slot/distance of previous attestations for later use.
    inclusion_infos = get_inclusion_infos(
        state=state,
        attestations=previous_epoch_attestations,
        committee_config=CommitteeConfig(config),
    )

    # Compute effective balance of each previous epoch active validator for later use
    effective_balances = {
        ValidatorIndex(index): get_effective_balance(
            state.validator_balances,
            ValidatorIndex(index),
            config.MAX_DEPOSIT_AMOUNT,
        )
        for index in range(len(state.validator_registry))
    }
    # Compute base reward of each previous epoch active validator for later use
    base_rewards = {
        ValidatorIndex(index): get_base_reward(
            state=state,
            index=ValidatorIndex(index),
            base_reward_quotient=config.BASE_REWARD_QUOTIENT,
            previous_total_balance=previous_total_balance,
            max_deposit_amount=config.MAX_DEPOSIT_AMOUNT,
        )
        for index in range(len(state.validator_registry))
    }

    # 1. Process rewards and penalties for justification and finalization
    finality_rewards, finality_penalties = _process_rewards_and_penalties_for_finality(
        state,
        config,
        previous_epoch_active_validator_indices,
        previous_total_balance,
        previous_epoch_attestations,
        previous_epoch_attester_indices,
        inclusion_infos,
        effective_balances,
        base_rewards,
    )
    # 2. Process rewards and penalties for crosslinks
    crosslinks_rewards, crosslinks_penalties = _process_rewards_and_penalties_for_crosslinks(
        state,
        config,
        effective_balances,
        base_rewards,
    )

    # Apply the overall rewards/penalties
    for index in range(len(state.validator_registry)):
        state = state.update_validator_balance(
            ValidatorIndex(index),
            # Prevent validator balance under flow
            max(
                (state.validator_balances[index] + finality_rewards[index] +
                 crosslinks_rewards[index] - finality_penalties[index] -
                 crosslinks_penalties[index]),
                0,
            ),
        )

    return state
Exemple #4
0
def _process_rewards_and_penalties_for_finality(
    state: BeaconState, config: Eth2Config,
    previous_epoch_active_validator_indices: Sequence[ValidatorIndex],
    previous_total_balance: Gwei,
    previous_epoch_attestations: Sequence[Attestation],
    previous_epoch_attester_indices: Sequence[ValidatorIndex],
    inclusion_infos: Dict[ValidatorIndex, InclusionInfo],
    effective_balances: Dict[ValidatorIndex,
                             Gwei], base_rewards: Dict[ValidatorIndex, Gwei]
) -> Tuple[Dict[ValidatorIndex, Gwei], Dict[ValidatorIndex,
                                            Gwei]]:  # noqa: E501
    previous_epoch_boundary_attestations = get_previous_epoch_boundary_attestations(
        state,
        config.SLOTS_PER_EPOCH,
        config.SLOTS_PER_HISTORICAL_ROOT,
    )
    previous_epoch_boundary_attester_indices = get_attester_indices_from_attestations(
        state=state,
        attestations=previous_epoch_boundary_attestations,
        committee_config=CommitteeConfig(config),
    )

    previous_epoch_head_attestations = get_previous_epoch_matching_head_attestations(
        state,
        config.SLOTS_PER_EPOCH,
        config.SLOTS_PER_HISTORICAL_ROOT,
    )
    previous_epoch_head_attester_indices = get_attester_indices_from_attestations(
        state=state,
        attestations=previous_epoch_head_attestations,
        committee_config=CommitteeConfig(config),
    )

    epochs_since_finality = state.next_epoch(
        config.SLOTS_PER_EPOCH) - state.finalized_epoch
    if epochs_since_finality <= 4:
        return _compute_normal_justification_and_finalization_deltas(
            state,
            config,
            previous_epoch_active_validator_indices,
            previous_total_balance,
            previous_epoch_attester_indices,
            previous_epoch_boundary_attester_indices,
            previous_epoch_head_attester_indices,
            inclusion_infos,
            effective_balances,
            base_rewards,
        )

    # epochs_since_finality > 4
    else:
        return _compute_inactivity_leak_deltas(
            state,
            config,
            previous_epoch_active_validator_indices,
            previous_epoch_attester_indices,
            previous_epoch_boundary_attester_indices,
            previous_epoch_head_attester_indices,
            inclusion_infos,
            effective_balances,
            base_rewards,
            epochs_since_finality,
        )