Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
Remove SignedGwei type and RewardSettlementContext
Browse files Browse the repository at this point in the history
  • Loading branch information
NIC619 committed Mar 11, 2019
1 parent 4763355 commit be036eb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 67 deletions.
19 changes: 0 additions & 19 deletions eth2/beacon/datastructures/reward_settlement_context.py

This file was deleted.

77 changes: 33 additions & 44 deletions eth2/beacon/state_machines/forks/serenity/epoch_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
)
from eth_utils.toolz import (
curry,
pipe,
)

from eth2.beacon import helpers
Expand Down Expand Up @@ -42,6 +41,7 @@
get_base_reward,
get_inactivity_penalty,
get_inclusion_infos,
get_previous_epoch_boundary_attestations,
get_previous_epoch_head_attestations,
get_winning_root_and_participants,
get_total_balance,
Expand All @@ -50,7 +50,6 @@
)
from eth2.beacon.helpers import (
get_active_validator_indices,
get_block_root,
get_effective_balance,
get_epoch_start_slot,
get_randao_mix,
Expand All @@ -63,15 +62,12 @@
hash_eth2,
)
from eth2.beacon.datastructures.inclusion_info import InclusionInfo
from eth2.beacon.datastructures.reward_settlement_context import RewardSettlementContext
from eth2.beacon.types.attestations import Attestation
from eth2.beacon.types.crosslink_records import CrosslinkRecord
from eth2.beacon.types.states import BeaconState
from eth2.beacon.types.validator_records import ValidatorRecord
from eth2.beacon.typing import (
Epoch,
Gwei,
SignedGwei,
Slot,
ValidatorIndex,
)
Expand Down Expand Up @@ -293,25 +289,6 @@ def _update_rewards_or_penalies(
yield i, rewards_or_penalties[i]


def _apply_rewards_and_penalties(
reward_settlement_context: RewardSettlementContext) -> Tuple[Dict[ValidatorIndex, Gwei], Dict[ValidatorIndex, Gwei]]: # noqa: E501
rewards_received = reward_settlement_context.rewards_received
penalties_received = reward_settlement_context.penalties_received
for index in reward_settlement_context.indices_to_reward:
rewards_received = _update_rewards_or_penalies(
index,
reward_settlement_context.rewards[index],
rewards_received,
)
for index in reward_settlement_context.indices_to_penalize:
penalties_received = _update_rewards_or_penalies(
index,
reward_settlement_context.penalties[index],
penalties_received,
)
return rewards_received, penalties_received


def _compute_normal_justification_and_finalization_deltas(
state: BeaconState,
config: BeaconConfig,
Expand All @@ -322,9 +299,9 @@ def _compute_normal_justification_and_finalization_deltas(
previous_epoch_head_attester_indices: Set[ValidatorIndex],
inclusion_infos: Dict[ValidatorIndex, InclusionInfo],
effective_balances: Dict[ValidatorIndex, Gwei],
base_rewards: Dict[ValidatorIndex, Gwei]) -> Tuple[Dict[ValidatorIndex, SignedGwei], Dict[ValidatorIndex, SignedGwei]]: # noqa: E501
base_rewards: Dict[ValidatorIndex, Gwei]) -> Tuple[Dict[ValidatorIndex, Gwei], Dict[ValidatorIndex, Gwei]]: # noqa: E501
rewards_received = {
index: Gwei(0)
ValidatorIndex(index): Gwei(0)
for index in range(len(state.validator_registry))
}
penalties_received = rewards_received.copy()
Expand All @@ -351,7 +328,10 @@ def _compute_normal_justification_and_finalization_deltas(
# Inclusion speed bonus
rewards_received = _update_rewards_or_penalies(
index,
base_rewards[index] * config.MIN_ATTESTATION_INCLUSION_DELAY // inclusion_infos[index].inclusion_distance,
(
base_rewards[index] * config.MIN_ATTESTATION_INCLUSION_DELAY //
inclusion_infos[index].inclusion_distance
),
rewards_received,
)
else:
Expand All @@ -364,7 +344,10 @@ def _compute_normal_justification_and_finalization_deltas(
if index in previous_epoch_boundary_attester_indices:
rewards_received = _update_rewards_or_penalies(
index,
base_rewards[index] * previous_epoch_boundary_attesting_balance // previous_total_balance,
(
base_rewards[index] * previous_epoch_boundary_attesting_balance //
previous_total_balance
),
rewards_received,
)
else:
Expand All @@ -377,7 +360,10 @@ def _compute_normal_justification_and_finalization_deltas(
if index in previous_epoch_head_attester_indices:
rewards_received = _update_rewards_or_penalies(
index,
base_rewards[index] * previous_epoch_head_attesting_balance // previous_total_balance,
(
base_rewards[index] * previous_epoch_head_attesting_balance //
previous_total_balance
),
rewards_received,
)
else:
Expand Down Expand Up @@ -411,18 +397,18 @@ def _compute_inactivity_leak_deltas(
inclusion_infos: Dict[ValidatorIndex, InclusionInfo],
effective_balances: Dict[ValidatorIndex, Gwei],
base_rewards: Dict[ValidatorIndex, Gwei],
epochs_since_finality: int) -> Tuple[Dict[ValidatorIndex, SignedGwei], Dict[ValidatorIndex, SignedGwei]]: # noqa: E501
epochs_since_finality: int) -> Tuple[Dict[ValidatorIndex, Gwei], Dict[ValidatorIndex, Gwei]]: # noqa: E501
inactivity_penalties = {
index: get_inactivity_penalty(
base_reward=base_rewards[index],
effective_balance=effective_balances[index],
ValidatorIndex(index): get_inactivity_penalty(
base_reward=base_rewards[ValidatorIndex(index)],
effective_balance=effective_balances[ValidatorIndex(index)],
epochs_since_finality=epochs_since_finality,
inactivity_penalty_quotient=config.INACTIVITY_PENALTY_QUOTIENT,
)
for index in range(len(state.validator_registry))
}
rewards_received = {
index: Gwei(0)
ValidatorIndex(index): Gwei(0)
for index in range(len(state.validator_registry))
}
penalties_received = rewards_received.copy()
Expand All @@ -438,7 +424,10 @@ def _compute_inactivity_leak_deltas(
# for getting attestations included late
rewards_received = _update_rewards_or_penalies(
index,
base_rewards[index] // config.MIN_ATTESTATION_INCLUSION_DELAY // inclusion_infos[index].inclusion_distance,
(
base_rewards[index] // config.MIN_ATTESTATION_INCLUSION_DELAY //
inclusion_infos[index].inclusion_distance
),
rewards_received,
)
penalties_received = _update_rewards_or_penalies(
Expand All @@ -461,16 +450,16 @@ def _compute_inactivity_leak_deltas(

# Penalize slashed-but-inactive validators as though they were active but offline
current_epoch = state.current_epoch(config.SLOTS_PER_EPOCH)
for index in range(len(state.validator_registry)):
for i in range(len(state.validator_registry)):
eligible = (
index not in previous_epoch_active_validator_indices and
state.validator_registry[index].slashed and
current_epoch < state.validator_registry[index].withdrawable_epoch
i not in previous_epoch_active_validator_indices and
state.validator_registry[ValidatorIndex(i)].slashed and
current_epoch < state.validator_registry[i].withdrawable_epoch
)
if eligible:
penalties_received = _update_rewards_or_penalies(
index,
2 * inactivity_penalties[index] + base_rewards[index],
ValidatorIndex(i),
2 * inactivity_penalties[ValidatorIndex(i)] + base_rewards[ValidatorIndex(i)],
penalties_received,
)
return (rewards_received, penalties_received)
Expand All @@ -486,7 +475,7 @@ def _process_rewards_and_penalties_for_finality(
previous_epoch_attester_indices: Set[ValidatorIndex],
inclusion_infos: Dict[ValidatorIndex, InclusionInfo],
effective_balances: Dict[ValidatorIndex, Gwei],
base_rewards: Dict[ValidatorIndex, Gwei]) -> Tuple[Dict[ValidatorIndex, SignedGwei], Dict[ValidatorIndex, SignedGwei]]: # noqa: E501
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,
Expand Down Expand Up @@ -547,7 +536,7 @@ def _process_rewards_and_penalties_for_crosslinks(
state: BeaconState,
config: BeaconConfig,
effective_balances: Dict[ValidatorIndex, Gwei],
base_rewards: Dict[ValidatorIndex, Gwei]) -> Tuple[Dict[ValidatorIndex, SignedGwei], Dict[ValidatorIndex, SignedGwei]]: # noqa: E501
base_rewards: Dict[ValidatorIndex, Gwei]) -> Tuple[Dict[ValidatorIndex, Gwei], Dict[ValidatorIndex, Gwei]]: # noqa: E501
previous_epoch_start_slot = get_epoch_start_slot(
state.previous_epoch(config.SLOTS_PER_EPOCH, config.GENESIS_EPOCH),
config.SLOTS_PER_EPOCH,
Expand All @@ -557,7 +546,7 @@ def _process_rewards_and_penalties_for_crosslinks(
config.SLOTS_PER_EPOCH,
)
rewards_received = {
index: Gwei(0)
ValidatorIndex(index): Gwei(0)
for index in range(len(state.validator_registry))
}
penalties_received = rewards_received.copy()
Expand Down
1 change: 0 additions & 1 deletion eth2/beacon/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
CommitteeIndex = NewType('CommitteeIndex', int)

Gwei = NewType('Gwei', int) # uint64
SignedGwei = NewType('SignedGwei', int) # uint64

Timestamp = NewType('Timestamp', int)
Second = NewType('Second', int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,14 +527,14 @@ def test_process_crosslinks(
},
1000, 100,
{
0: -800, # 2 * (100 + 1000 * 5 // 10 // 2) - 100
1: -800, # 2 * (100 + 1000 * 5 // 10 // 2) - 100
0: -800, # -2 * (100 + 1000 * 5 // 10 // 2) - 100
1: -800, # -2 * (100 + 1000 * 5 // 10 // 2) - 100
2: 0, # -(100 - 100 * 1 // 1)
3: 0, # -(100 - 100 * 1 // 1)
4: 0, # -(100 - 100 * 1 // 1)
5: -500, # -(100 - 100 * 1 // 2) - (100 * 2 + 1000 * 5 // 10 // 2)
6: -517, # -(100 - 100 * 1 // 3) - (100 * 2 + 1000 * 5 // 10 // 2)
7: -800, # 2 * (100 + 1000 * 5 // 10 // 2) - 100
7: -800, # -2 * (100 + 1000 * 5 // 10 // 2) - 100
8: -800, # -(2 * (100 + 1000 * 5 // 10 // 2) + 100)
9: -800, # -(2 * (100 + 1000 * 5 // 10 // 2) + 100)
10: 0,
Expand Down

0 comments on commit be036eb

Please sign in to comment.