switch from legacy pre-spec sum_effective_balances(...) to get_total_balance(...)

This commit is contained in:
Dustin Brody 2019-02-22 10:46:45 -08:00
parent a42601e29e
commit de2cad0e12
2 changed files with 18 additions and 22 deletions

View File

@ -1,5 +1,5 @@
# beacon_chain # beacon_chain
# Copyright (c) 2018 Status Research & Development GmbH # Copyright (c) 2019 Status Research & Development GmbH
# Licensed and distributed under either of # Licensed and distributed under either of
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). # * 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). # * 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``. ## validator with the given ``index``.
min(state.validator_balances[index], MAX_DEPOSIT_AMOUNT) 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 # 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, func validate_proof_of_possession(state: BeaconState,
pubkey: ValidatorPubKey, pubkey: ValidatorPubKey,
@ -302,6 +296,11 @@ func process_ejections*(state: var BeaconState) =
if state.validator_balances[index] < EJECTION_BALANCE: if state.validator_balances[index] < EJECTION_BALANCE:
exit_validator(state, index) 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) = func update_validator_registry*(state: var BeaconState) =
let let
current_epoch = get_current_epoch(state) current_epoch = get_current_epoch(state)
@ -309,7 +308,7 @@ func update_validator_registry*(state: var BeaconState) =
active_validator_indices = active_validator_indices =
get_active_validator_indices(state.validator_registry, state.slot) get_active_validator_indices(state.validator_registry, state.slot)
# The total effective balance of active validators # 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) # The maximum balance churn in Gwei (for deposits and exits separately)
max_balance_churn = max( max_balance_churn = max(
@ -470,11 +469,6 @@ proc checkAttestation*(
true 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 # 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) = func prepare_validator_for_withdrawal(state: var BeaconState, index: ValidatorIndex) =
## Set the validator with the given ``index`` as withdrawable ## Set the validator with the given ``index`` as withdrawable

View File

@ -1,5 +1,5 @@
# beacon_chain # beacon_chain
# Copyright (c) 2018 Status Research & Development GmbH # Copyright (c) 2019 Status Research & Development GmbH
# Licensed and distributed under either of # Licensed and distributed under either of
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT). # * 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). # * 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) get_attester_indices(state, previous_epoch_justified_attestations)
previous_epoch_justified_attesting_balance = 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 let # Previous epoch boundary
# TODO check this with spec... # TODO check this with spec...
@ -591,7 +591,7 @@ func processEpoch(state: var BeaconState) =
get_attester_indices(state, previous_epoch_boundary_attestations) get_attester_indices(state, previous_epoch_boundary_attestations)
previous_epoch_boundary_attesting_balance = 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 let # Previous epoch head
previous_epoch_head_attestations = previous_epoch_head_attestations =
@ -602,7 +602,7 @@ func processEpoch(state: var BeaconState) =
get_attester_indices(state, previous_epoch_head_attestations) get_attester_indices(state, previous_epoch_head_attestations)
previous_epoch_head_attesting_balance = 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 # 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 # 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_hash = candidates[0]
var max_val = var max_val =
sum_effective_balances( get_total_balance(
statePtr[], attesting_validator_indices(crosslink_committee, max_hash)) statePtr[], attesting_validator_indices(crosslink_committee, max_hash))
for candidate in candidates[1..^1]: for candidate in candidates[1..^1]:
let val = sum_effective_balances( let val = get_total_balance(
statePtr[], attesting_validator_indices(crosslink_committee, candidate)) statePtr[], attesting_validator_indices(crosslink_committee, candidate))
if val > max_val or (val == max_val and candidate.lowerThan(max_hash)): if val > max_val or (val == max_val and candidate.lowerThan(max_hash)):
max_hash = candidate max_hash = candidate
@ -649,11 +649,11 @@ func processEpoch(state: var BeaconState) =
attesting_validator_indices(crosslink_committee, winning_root(crosslink_committee)) attesting_validator_indices(crosslink_committee, winning_root(crosslink_committee))
func total_attesting_balance(crosslink_committee: CrosslinkCommittee): uint64 = func total_attesting_balance(crosslink_committee: CrosslinkCommittee): uint64 =
sum_effective_balances( get_total_balance(
statePtr[], attesting_validator_indices(crosslink_committee)) statePtr[], attesting_validator_indices(crosslink_committee))
func total_balance_sac(crosslink_committee: CrosslinkCommittee): uint64 = 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 block: # Eth1 data
if state.slot mod EPOCHS_PER_ETH1_VOTING_PERIOD == 0: if state.slot mod EPOCHS_PER_ETH1_VOTING_PERIOD == 0:
@ -668,7 +668,9 @@ func processEpoch(state: var BeaconState) =
# Helpers for justification # Helpers for justification
let 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 block: # Justification
# https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#justification # https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#justification