Skip to content

Commit

Permalink
undo base reward changes, just memoize instead
Browse files Browse the repository at this point in the history
  • Loading branch information
protolambda committed Jan 24, 2020
1 parent 45fef45 commit 5c5b528
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
4 changes: 4 additions & 0 deletions scripts/build_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ def wrapper(*args, **kw): # type: ignore
return wrapper
get_base_reward = cache_this(
lambda state, index: (state.validators.hash_tree_root(), state.slot),
get_base_reward)
get_committee_count_at_slot = cache_this(
lambda state, epoch: (state.validators.hash_tree_root(), epoch),
get_committee_count_at_slot)
Expand Down
13 changes: 7 additions & 6 deletions specs/phase0/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,8 @@ def process_justification_and_finalization(state: BeaconState) -> None:
#### Rewards and penalties

```python
def get_base_reward(state: BeaconState, total_balance: Gwei, index: ValidatorIndex) -> Gwei:
def get_base_reward(state: BeaconState, index: ValidatorIndex) -> Gwei:
total_balance = get_total_active_balance(state)
effective_balance = state.validators[index].effective_balance
return Gwei(effective_balance * BASE_REWARD_FACTOR // integer_squareroot(total_balance) // BASE_REWARDS_PER_EPOCH)
```
Expand All @@ -1307,27 +1308,27 @@ def get_attestation_deltas(state: BeaconState) -> Tuple[Sequence[Gwei], Sequence
attesting_balance = get_total_balance(state, unslashed_attesting_indices)
for index in eligible_validator_indices:
if index in unslashed_attesting_indices:
rewards[index] += get_base_reward(state, total_balance, index) * attesting_balance // total_balance
rewards[index] += get_base_reward(state, index) * attesting_balance // total_balance
else:
penalties[index] += get_base_reward(state, total_balance, index)
penalties[index] += get_base_reward(state, index)

# Proposer and inclusion delay micro-rewards
for index in get_unslashed_attesting_indices(state, matching_source_attestations):
attestation = min([
a for a in matching_source_attestations
if index in get_attesting_indices(state, a.data, a.aggregation_bits)
], key=lambda a: a.inclusion_delay)
proposer_reward = Gwei(get_base_reward(state, total_balance, index) // PROPOSER_REWARD_QUOTIENT)
proposer_reward = Gwei(get_base_reward(state, index) // PROPOSER_REWARD_QUOTIENT)
rewards[attestation.proposer_index] += proposer_reward
max_attester_reward = get_base_reward(state, total_balance, index) - proposer_reward
max_attester_reward = get_base_reward(state, index) - proposer_reward
rewards[index] += Gwei(max_attester_reward // attestation.inclusion_delay)

# Inactivity penalty
finality_delay = previous_epoch - state.finalized_checkpoint.epoch
if finality_delay > MIN_EPOCHS_TO_INACTIVITY_PENALTY:
matching_target_attesting_indices = get_unslashed_attesting_indices(state, matching_target_attestations)
for index in eligible_validator_indices:
penalties[index] += Gwei(BASE_REWARDS_PER_EPOCH * get_base_reward(state, total_balance, index))
penalties[index] += Gwei(BASE_REWARDS_PER_EPOCH * get_base_reward(state, index))
if index not in matching_target_attesting_indices:
effective_balance = state.validators[index].effective_balance
penalties[index] += Gwei(effective_balance * finality_delay // INACTIVITY_PENALTY_QUOTIENT)
Expand Down
9 changes: 3 additions & 6 deletions specs/phase1/beacon-chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,6 @@ def process_crosslink_for_shard(state: BeaconState,
attestations: Sequence[Attestation]) -> Root:
committee = get_beacon_committee(state, get_current_epoch(state), shard)
online_indices = get_online_validator_indices(state)
total_balance = get_total_active_balance(state)

# Loop over all shard transition roots
shard_transition_roots = set([a.data.shard_transition_root for a in attestations])
Expand Down Expand Up @@ -734,8 +733,7 @@ def process_crosslink_for_shard(state: BeaconState,
apply_shard_transition(state, shard, shard_transition)
# Apply proposer reward and cost
beacon_proposer_index = get_beacon_proposer_index(state)
estimated_attester_reward = sum([get_base_reward(state, total_balance, attester)
for attester in transition_participants])
estimated_attester_reward = sum([get_base_reward(state, attester) for attester in transition_participants])
proposer_reward = Gwei(estimated_attester_reward // PROPOSER_REWARD_QUOTIENT)
increase_balance(state, beacon_proposer_index, proposer_reward)
states_slots_lengths = zip(
Expand Down Expand Up @@ -860,14 +858,13 @@ def verify_shard_transition_false_positives(state: BeaconState, block_body: Beac
```python
def process_light_client_signatures(state: BeaconState, block_body: BeaconBlockBody) -> None:
committee = get_light_client_committee(state, get_current_epoch(state))
total_balance = get_total_active_balance(state)
total_reward = Gwei(0)
signer_pubkeys = []
for bit_index, participant_index in enumerate(committee):
if block_body.light_client_signature_bitfield[bit_index]:
signer_pubkeys.append(state.validators[participant_index].pubkey)
increase_balance(state, participant_index, get_base_reward(state, total_balance, participant_index))
total_reward += get_base_reward(state, total_balance, participant_index)
increase_balance(state, participant_index, get_base_reward(state, participant_index))
total_reward += get_base_reward(state, participant_index)

increase_balance(state, get_beacon_proposer_index(state), Gwei(total_reward // PROPOSER_REWARD_QUOTIENT))

Expand Down
6 changes: 2 additions & 4 deletions specs/phase1/custody-game.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,10 @@ def process_custody_key_reveal(state: BeaconState, reveal: CustodyKeyReveal) ->

# Reward Block Proposer
proposer_index = get_beacon_proposer_index(state)
total_balance = get_total_active_balance(state)
increase_balance(
state,
proposer_index,
Gwei(get_base_reward(state, total_balance, reveal.revealer_index) // MINOR_REWARD_QUOTIENT)
Gwei(get_base_reward(state, reveal.revealer_index) // MINOR_REWARD_QUOTIENT)
)
```

Expand Down Expand Up @@ -312,10 +311,9 @@ def process_early_derived_secret_reveal(state: BeaconState, reveal: EarlyDerived
# that does not interfere with the custody period
# The penalty is proportional to the max proposer reward

total_balance = get_total_active_balance(state)
# Calculate penalty
max_proposer_slot_reward = (
get_base_reward(state, total_balance, reveal.revealed_index)
get_base_reward(state, reveal.revealed_index)
* SLOTS_PER_EPOCH
// len(get_active_validator_indices(state, get_current_epoch(state)))
// PROPOSER_REWARD_QUOTIENT
Expand Down

0 comments on commit 5c5b528

Please sign in to comment.