From de2cad0e12b29ed1567649d4b1c81a3bcb763e77 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Fri, 22 Feb 2019 10:46:45 -0800 Subject: [PATCH] switch from legacy pre-spec sum_effective_balances(...) to get_total_balance(...) --- beacon_chain/spec/beaconstate.nim | 20 +++++++------------- beacon_chain/state_transition.nim | 20 +++++++++++--------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 50ea3a6ed..2ef4b7812 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -1,5 +1,5 @@ # beacon_chain -# Copyright (c) 2018 Status Research & Development GmbH +# Copyright (c) 2019 Status Research & Development GmbH # Licensed and distributed under either of # * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). # * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). @@ -16,12 +16,6 @@ func get_effective_balance*(state: BeaconState, index: ValidatorIndex): uint64 = ## validator with the given ``index``. min(state.validator_balances[index], MAX_DEPOSIT_AMOUNT) -func sum_effective_balances*( - state: BeaconState, validator_indices: openArray[ValidatorIndex]): uint64 = - # TODO spec - add as helper? Used pretty often - for index in validator_indices: - result += get_effective_balance(state, index) - # https://github.com/ethereum/eth2.0-specs/blob/v0.3.0/specs/core/0_beacon-chain.md#validate_proof_of_possession func validate_proof_of_possession(state: BeaconState, pubkey: ValidatorPubKey, @@ -302,6 +296,11 @@ func process_ejections*(state: var BeaconState) = if state.validator_balances[index] < EJECTION_BALANCE: exit_validator(state, index) +# https://github.com/ethereum/eth2.0-specs/blob/v0.3.0/specs/core/0_beacon-chain.md#get_total_balance +func get_total_balance*(state: BeaconState, validators: seq[ValidatorIndex]): Gwei = + # Return the combined effective balance of an array of validators. + foldl(validators, a + get_effective_balance(state, b), 0'u64) + func update_validator_registry*(state: var BeaconState) = let current_epoch = get_current_epoch(state) @@ -309,7 +308,7 @@ func update_validator_registry*(state: var BeaconState) = active_validator_indices = get_active_validator_indices(state.validator_registry, state.slot) # The total effective balance of active validators - total_balance = sum_effective_balances(state, active_validator_indices) + total_balance = get_total_balance(state, active_validator_indices) # The maximum balance churn in Gwei (for deposits and exits separately) max_balance_churn = max( @@ -470,11 +469,6 @@ proc checkAttestation*( true -# https://github.com/ethereum/eth2.0-specs/blob/v0.3.0/specs/core/0_beacon-chain.md#get_total_balance -func get_total_balance*(state: BeaconState, validators: seq[ValidatorIndex]): Gwei = - # Return the combined effective balance of an array of validators. - foldl(validators, a + get_effective_balance(state, b), 0'u64) - # https://github.com/ethereum/eth2.0-specs/blob/v0.3.0/specs/core/0_beacon-chain.md#prepare_validator_for_withdrawal func prepare_validator_for_withdrawal(state: var BeaconState, index: ValidatorIndex) = ## Set the validator with the given ``index`` as withdrawable diff --git a/beacon_chain/state_transition.nim b/beacon_chain/state_transition.nim index 0e8d54c07..e30ebdeeb 100644 --- a/beacon_chain/state_transition.nim +++ b/beacon_chain/state_transition.nim @@ -1,5 +1,5 @@ # beacon_chain -# Copyright (c) 2018 Status Research & Development GmbH +# Copyright (c) 2019 Status Research & Development GmbH # Licensed and distributed under either of # * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). # * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0). @@ -575,7 +575,7 @@ func processEpoch(state: var BeaconState) = get_attester_indices(state, previous_epoch_justified_attestations) previous_epoch_justified_attesting_balance = - sum_effective_balances(state, previous_epoch_justified_attester_indices) + get_total_balance(state, previous_epoch_justified_attester_indices) let # Previous epoch boundary # TODO check this with spec... @@ -591,7 +591,7 @@ func processEpoch(state: var BeaconState) = get_attester_indices(state, previous_epoch_boundary_attestations) previous_epoch_boundary_attesting_balance = - sum_effective_balances(state, previous_epoch_boundary_attester_indices) + get_total_balance(state, previous_epoch_boundary_attester_indices) let # Previous epoch head previous_epoch_head_attestations = @@ -602,7 +602,7 @@ func processEpoch(state: var BeaconState) = get_attester_indices(state, previous_epoch_head_attestations) previous_epoch_head_attesting_balance = - sum_effective_balances(state, previous_epoch_head_attester_indices) + get_total_balance(state, previous_epoch_head_attester_indices) # TODO this is really hairy - we cannot capture `state` directly, but we # can capture a pointer to it - this is safe because we don't leak @@ -632,10 +632,10 @@ func processEpoch(state: var BeaconState) = var max_hash = candidates[0] var max_val = - sum_effective_balances( + get_total_balance( statePtr[], attesting_validator_indices(crosslink_committee, max_hash)) for candidate in candidates[1..^1]: - let val = sum_effective_balances( + let val = get_total_balance( statePtr[], attesting_validator_indices(crosslink_committee, candidate)) if val > max_val or (val == max_val and candidate.lowerThan(max_hash)): max_hash = candidate @@ -649,11 +649,11 @@ func processEpoch(state: var BeaconState) = attesting_validator_indices(crosslink_committee, winning_root(crosslink_committee)) func total_attesting_balance(crosslink_committee: CrosslinkCommittee): uint64 = - sum_effective_balances( + get_total_balance( statePtr[], attesting_validator_indices(crosslink_committee)) func total_balance_sac(crosslink_committee: CrosslinkCommittee): uint64 = - sum_effective_balances(statePtr[], crosslink_committee.committee) + get_total_balance(statePtr[], crosslink_committee.committee) block: # Eth1 data if state.slot mod EPOCHS_PER_ETH1_VOTING_PERIOD == 0: @@ -668,7 +668,9 @@ func processEpoch(state: var BeaconState) = # Helpers for justification let - previous_total_balance = sum_effective_balances(state, get_active_validator_indices(state.validator_registry, previous_epoch)) + previous_total_balance = get_total_balance( + state, get_active_validator_indices( + state.validator_registry, previous_epoch)) block: # Justification # https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#justification