diff --git a/Sources/BeaconChain/BeaconChain.swift b/Sources/BeaconChain/BeaconChain.swift index 305452a..6cc12bc 100644 --- a/Sources/BeaconChain/BeaconChain.swift +++ b/Sources/BeaconChain/BeaconChain.swift @@ -238,10 +238,6 @@ extension BeaconChain { } } - static func getEffectiveBalance(state: BeaconState, index: ValidatorIndex) -> Gwei { - return min(state.validatorBalances[Int(index)], MAX_DEPOSIT_AMOUNT) - } - static func getBitfieldBit(bitfield: Data, i: Int) -> Int { return Int((bitfield[i / 8] >> (i % 8))) % 2 } @@ -348,7 +344,7 @@ extension BeaconChain { } for (i, _) in state.validatorRegistry.enumerated() { - if getEffectiveBalance(state: state, index: ValidatorIndex(i)) >= MAX_DEPOSIT_AMOUNT { + if state.effectiveBalance(ValidatorIndex(i)) >= MAX_DEPOSIT_AMOUNT { state.validatorRegistry[i].activate(state: state, genesis: true) } } @@ -446,10 +442,10 @@ extension BeaconChain { assert(state.slot < state.validatorRegistry[Int(index)].withdrawableEpoch.startSlot()) state.validatorRegistry[Int(index)].exit(state: state) - state.latestSlashedBalances[Int(getCurrentEpoch(state: state) % LATEST_SLASHED_EXIT_LENGTH)] += getEffectiveBalance(state: state, index: index) + state.latestSlashedBalances[Int(getCurrentEpoch(state: state) % LATEST_SLASHED_EXIT_LENGTH)] += state.effectiveBalance(index) let whistleblowerIndex = getBeaconProposerIndex(state: state, slot: state.slot) - let whistleblowerReward = getEffectiveBalance(state: state, index: index) / WHISTLEBLOWER_REWARD_QUOTIENT + let whistleblowerReward = state.effectiveBalance(index) / WHISTLEBLOWER_REWARD_QUOTIENT state.validatorBalances[Int(whistleblowerIndex)] += whistleblowerReward state.validatorBalances[Int(index)] -= whistleblowerReward diff --git a/Sources/BeaconChain/DataStructures/State/BeaconState.swift b/Sources/BeaconChain/DataStructures/State/BeaconState.swift index 1a36ae7..1440656 100644 --- a/Sources/BeaconChain/DataStructures/State/BeaconState.swift +++ b/Sources/BeaconChain/DataStructures/State/BeaconState.swift @@ -33,3 +33,10 @@ struct BeaconState { var eth1DataVotes: [Eth1DataVote] var depositIndex: UInt64 } + +extension BeaconState { + + func effectiveBalance(_ index: ValidatorIndex) -> Gwei { + return min(validatorBalances[Int(index)], MAX_DEPOSIT_AMOUNT) + } +} diff --git a/Sources/BeaconChain/Extensions/Array+AttestationTarget.swift b/Sources/BeaconChain/Extensions/Array+AttestationTarget.swift index 577446d..5daaf80 100644 --- a/Sources/BeaconChain/Extensions/Array+AttestationTarget.swift +++ b/Sources/BeaconChain/Extensions/Array+AttestationTarget.swift @@ -10,7 +10,7 @@ extension Array where Element == AttestationTarget { return nil } - return BeaconChain.getEffectiveBalance(state: state, index: index) / FORK_CHOICE_BALANCE_INCREMENT + return state.effectiveBalance(index) / FORK_CHOICE_BALANCE_INCREMENT } .reduce(0, +) } diff --git a/Sources/BeaconChain/Extensions/Array+ValidatorIndex.swift b/Sources/BeaconChain/Extensions/Array+ValidatorIndex.swift index f196d39..2cd06b9 100644 --- a/Sources/BeaconChain/Extensions/Array+ValidatorIndex.swift +++ b/Sources/BeaconChain/Extensions/Array+ValidatorIndex.swift @@ -3,6 +3,6 @@ import Foundation extension Array where Element == ValidatorIndex { func totalBalance(state: BeaconState) -> Gwei { - return map { BeaconChain.getEffectiveBalance(state: state, index: $0) }.reduce(0, +) + return map { state.effectiveBalance($0) }.reduce(0, +) } } diff --git a/Sources/BeaconChain/StateTransition.swift b/Sources/BeaconChain/StateTransition.swift index 16587eb..6248af3 100644 --- a/Sources/BeaconChain/StateTransition.swift +++ b/Sources/BeaconChain/StateTransition.swift @@ -672,7 +672,7 @@ extension StateTransition { let totalAtStart = state.latestSlashedBalances[Int((epochIndex + 1) % LATEST_SLASHED_EXIT_LENGTH)] let totalAtEnd = state.latestSlashedBalances[Int(epochIndex)] let totalPenalties = totalAtEnd - totalAtStart - let penalty = BeaconChain.getEffectiveBalance(state: state, index: ValidatorIndex(i)) * min(totalPenalties * 3, totalBalance) / totalBalance + let penalty = state.effectiveBalance(ValidatorIndex(i)) * min(totalPenalties * 3, totalBalance) / totalBalance state.validatorBalances[i] -= penalty } } @@ -712,7 +712,7 @@ extension StateTransition { var balanceChurn = UInt64(0) for (i, v) in state.validatorRegistry.enumerated() { if v.activationEpoch == FAR_FUTURE_EPOCH && state.validatorBalances[Int(i)] >= MAX_DEPOSIT_AMOUNT { - balanceChurn += BeaconChain.getEffectiveBalance(state: state, index: ValidatorIndex(i)) + balanceChurn += state.effectiveBalance(ValidatorIndex(i)) if balanceChurn > maxBalanceChurn { break } @@ -724,7 +724,7 @@ extension StateTransition { balanceChurn = 0 for (i, v) in state.validatorRegistry.enumerated() { if v.activationEpoch == FAR_FUTURE_EPOCH && v.initiatedExit { - balanceChurn += BeaconChain.getEffectiveBalance(state: state, index: ValidatorIndex(i)) + balanceChurn += state.effectiveBalance(ValidatorIndex(i)) if balanceChurn > maxBalanceChurn { break } @@ -817,7 +817,7 @@ extension StateTransition { } private static func baseReward(state: BeaconState, index: ValidatorIndex, baseRewardQuotient: UInt64) -> UInt64 { - return BeaconChain.getEffectiveBalance(state: state, index: index) / baseRewardQuotient / 5 + return state.effectiveBalance(index) / baseRewardQuotient / 5 } private static func inactivityPenalty( @@ -826,7 +826,7 @@ extension StateTransition { epochsSinceFinality: UInt64, baseRewardQuotient: UInt64 ) -> UInt64 { - return baseReward(state: state, index: index, baseRewardQuotient: baseRewardQuotient) + BeaconChain.getEffectiveBalance(state: state, index: index) * epochsSinceFinality / INACTIVITY_PENALTY_QUOTIENT / 2 + return baseReward(state: state, index: index, baseRewardQuotient: baseRewardQuotient) + state.effectiveBalance(index) * epochsSinceFinality / INACTIVITY_PENALTY_QUOTIENT / 2 } private static func attestingValidators(