Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions Sources/BeaconChain/BeaconChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Sources/BeaconChain/DataStructures/State/BeaconState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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, +)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/BeaconChain/Extensions/Array+ValidatorIndex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, +)
}
}
10 changes: 5 additions & 5 deletions Sources/BeaconChain/StateTransition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down