From 5c5b528fe489788435a66ce385eb9ce8c5806177 Mon Sep 17 00:00:00 2001 From: protolambda Date: Fri, 24 Jan 2020 21:58:32 +0100 Subject: [PATCH] undo base reward changes, just memoize instead --- scripts/build_spec.py | 4 ++++ specs/phase0/beacon-chain.md | 13 +++++++------ specs/phase1/beacon-chain.md | 9 +++------ specs/phase1/custody-game.md | 6 ++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/scripts/build_spec.py b/scripts/build_spec.py index 7422833260..7633ba9bad 100644 --- a/scripts/build_spec.py +++ b/scripts/build_spec.py @@ -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) diff --git a/specs/phase0/beacon-chain.md b/specs/phase0/beacon-chain.md index ff30042b62..410d094b0c 100644 --- a/specs/phase0/beacon-chain.md +++ b/specs/phase0/beacon-chain.md @@ -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) ``` @@ -1307,9 +1308,9 @@ 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): @@ -1317,9 +1318,9 @@ def get_attestation_deltas(state: BeaconState) -> Tuple[Sequence[Gwei], Sequence 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 @@ -1327,7 +1328,7 @@ def get_attestation_deltas(state: BeaconState) -> Tuple[Sequence[Gwei], Sequence 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) diff --git a/specs/phase1/beacon-chain.md b/specs/phase1/beacon-chain.md index 678165a60a..7bef0f81d4 100644 --- a/specs/phase1/beacon-chain.md +++ b/specs/phase1/beacon-chain.md @@ -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]) @@ -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( @@ -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)) diff --git a/specs/phase1/custody-game.md b/specs/phase1/custody-game.md index 596c553cc2..eb6bbc835d 100644 --- a/specs/phase1/custody-game.md +++ b/specs/phase1/custody-game.md @@ -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) ) ``` @@ -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