Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

WIP: Namespace and CamelCase global constants #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
52 changes: 26 additions & 26 deletions Sources/BeaconChain/BeaconChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BeaconChain {
extension BeaconChain {

static func getPreviousEpoch(state: BeaconState) -> Epoch {
return max(getCurrentEpoch(state: state) - 1, GENESIS_EPOCH)
return max(getCurrentEpoch(state: state) - 1, InitialValues.GenesisEpoch)
}

static func getCurrentEpoch(state: BeaconState) -> Epoch {
Expand Down Expand Up @@ -239,7 +239,7 @@ extension BeaconChain {
}

static func getEffectiveBalance(state: BeaconState, index: ValidatorIndex) -> Gwei {
return min(state.validatorBalances[Int(index)], MAX_DEPOSIT_AMOUNT)
return min(state.validatorBalances[Int(index)], GWEIValues.MaxDepositAmount)
}

static func getBitfieldBit(bitfield: Data, i: Int) -> Int {
Expand Down Expand Up @@ -348,48 +348,48 @@ extension BeaconChain {
}

for (i, _) in state.validatorRegistry.enumerated() {
if getEffectiveBalance(state: state, index: ValidatorIndex(i)) >= MAX_DEPOSIT_AMOUNT {
if getEffectiveBalance(state: state, index: ValidatorIndex(i)) >= GWEIValues.MaxDepositAmount {
state.validatorRegistry[i].activate(state: state, genesis: true)
}
}

let genesisActiveIndexRoot = hashTreeRoot(state.validatorRegistry.activeIndices(epoch: GENESIS_EPOCH))
let genesisActiveIndexRoot = hashTreeRoot(state.validatorRegistry.activeIndices(epoch: InitialValues.GenesisEpoch))

for i in 0..<LATEST_ACTIVE_INDEX_ROOTS_LENGTH {
state.latestActiveIndexRoots[Int(i)] = genesisActiveIndexRoot
}

state.currentShufflingSeed = generateSeed(state: state, epoch: GENESIS_EPOCH)
state.currentShufflingSeed = generateSeed(state: state, epoch: InitialValues.GenesisEpoch)

return state
}

static func genesisState(genesisTime: UInt64, latestEth1Data: Eth1Data, depositLength: Int) -> BeaconState {
return BeaconState(
slot: GENESIS_SLOT,
slot: InitialValues.GenesisSlot,
genesisTime: genesisTime,
fork: Fork(
previousVersion: GENESIS_FORK_VERSION,
currentVersion: GENESIS_FORK_VERSION,
epoch: GENESIS_EPOCH
previousVersion: InitialValues.GenesisForkVersion,
currentVersion: InitialValues.GenesisForkVersion,
epoch: InitialValues.GenesisEpoch
),
validatorRegistry: [Validator](),
validatorBalances: [UInt64](),
validatorRegistryUpdateEpoch: GENESIS_EPOCH,
latestRandaoMixes: [Data](repeating: EMPTY_SIGNATURE, count: Int(LATEST_RANDAO_MIXES_LENGTH)),
previousShufflingStartShard: GENESIS_START_SHARD,
currentShufflingStartShard: GENESIS_START_SHARD,
previousShufflingEpoch: GENESIS_EPOCH,
currentShufflingEpoch: GENESIS_EPOCH,
previousShufflingSeed: ZERO_HASH,
currentShufflingSeed: ZERO_HASH,
previousJustifiedEpoch: GENESIS_EPOCH,
justifiedEpoch: GENESIS_EPOCH,
validatorRegistryUpdateEpoch: InitialValues.GenesisEpoch,
latestRandaoMixes: [Data](repeating: InitialValues.EmptySignature, count: Int(LATEST_RANDAO_MIXES_LENGTH)),
previousShufflingStartShard: InitialValues.GenesisStartShard,
currentShufflingStartShard: InitialValues.GenesisStartShard,
previousShufflingEpoch: InitialValues.GenesisEpoch,
currentShufflingEpoch: InitialValues.GenesisEpoch,
previousShufflingSeed: InitialValues.ZeroHash,
currentShufflingSeed: InitialValues.ZeroHash,
previousJustifiedEpoch: InitialValues.GenesisEpoch,
justifiedEpoch: InitialValues.GenesisEpoch,
justificationBitfield: 0,
finalizedEpoch: GENESIS_EPOCH,
latestCrosslinks: [Crosslink](repeating: Crosslink(epoch: GENESIS_EPOCH, crosslinkDataRoot: ZERO_HASH), count: Int(SHARD_COUNT)),
latestBlockRoots: [Data](repeating: ZERO_HASH, count: Int(LATEST_BLOCK_ROOTS_LENGTH)),
latestActiveIndexRoots: [Data](repeating: ZERO_HASH, count: Int(LATEST_ACTIVE_INDEX_ROOTS_LENGTH)),
finalizedEpoch: InitialValues.GenesisEpoch,
latestCrosslinks: [Crosslink](repeating: Crosslink(epoch: InitialValues.GenesisEpoch, crosslinkDataRoot: InitialValues.ZeroHash), count: Int(SHARD_COUNT)),
latestBlockRoots: [Data](repeating: InitialValues.ZeroHash, count: Int(LATEST_BLOCK_ROOTS_LENGTH)),
latestActiveIndexRoots: [Data](repeating: InitialValues.ZeroHash, count: Int(LATEST_ACTIVE_INDEX_ROOTS_LENGTH)),
latestSlashedBalances: [UInt64](repeating: 0, count: Int(LATEST_SLASHED_EXIT_LENGTH)),
latestAttestations: [PendingAttestation](),
batchedBlockRoots: [Data](),
Expand Down Expand Up @@ -427,9 +427,9 @@ extension BeaconChain {
let validator = Validator(
pubkey: pubkey,
withdrawalCredentials: withdrawalCredentials,
activationEpoch: FAR_FUTURE_EPOCH,
exitEpoch: FAR_FUTURE_EPOCH,
withdrawableEpoch: FAR_FUTURE_EPOCH,
activationEpoch: InitialValues.FarFutureEpoch,
exitEpoch: InitialValues.FarFutureEpoch,
withdrawableEpoch: InitialValues.FarFutureEpoch,
initiatedExit: false,
slashed: false
)
Expand Down
40 changes: 25 additions & 15 deletions Sources/BeaconChain/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,31 @@ let SHUFFLE_ROUND_COUNT = 90
//let DEPOSIT_CONTRACT_ADDRESS =
let DEPOSIT_CONTRACT_TREE_DEPTH = UInt64(2**5)

// GWEI Values
let MIN_DEPOSIT_AMOUNT = UInt64(2**0 * UInt64(1e9))
let MAX_DEPOSIT_AMOUNT = UInt64(2**5 * UInt64(1e9))
let FORK_CHOICE_BALANCE_INCREMENT = UInt64(2**0 * UInt64(1e9))
let EJECTION_BALANCE = UInt64(2**4 * UInt64(1e9))

// Initial Values
let GENESIS_FORK_VERSION = UInt64(0)
let GENESIS_SLOT = UInt64(2**32)
let GENESIS_EPOCH = Slot(GENESIS_SLOT).toEpoch()
let GENESIS_START_SHARD = UInt64(0)
let FAR_FUTURE_EPOCH = UInt64.max
let ZERO_HASH = Data(repeating: 0, count: 32) // @todo create type
let EMPTY_SIGNATURE = Data(repeating: 0, count: 96)
let BLS_WITHDRAWAL_PREFIX_BYTE = Data(repeating: 0, count: 1)


// MARK: GWEI Values

struct GWEIValues {

static let MinDepositAmount = UInt64(2**0 * UInt64(1e9))
static let MaxDepositAmount = UInt64(2**5 * UInt64(1e9))
static let ForkChoiceBalanceIncremement = UInt64(2**0 * UInt64(1e9))
static let EjectionBalance = UInt64(2**4 * UInt64(1e9))
}

// MARK: Initial Values

struct InitialValues {

static let GenesisForkVersion = UInt64(0)
static let GenesisSlot = UInt64(2**32)
static let GenesisEpoch = Slot(GenesisSlot).toEpoch()
static let GenesisStartShard = UInt64(0)
static let FarFutureEpoch = UInt64.max
static let ZeroHash = Data(repeating: 0, count: 32) // @todo create type
static let EmptySignature = Data(repeating: 0, count: 96)
static let BLSWithdrawalPrefixByte = Data(repeating: 0, count: 1)
}

// Time parameters
let SECONDS_PER_SLOT = UInt64(6)
Expand Down
2 changes: 1 addition & 1 deletion Sources/BeaconChain/DataStructures/State/Validator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Validator {

// @todo passing state kinda seems ugly
mutating func activate(state: BeaconState, genesis: Bool) {
activationEpoch = genesis ? GENESIS_EPOCH : BeaconChain.getCurrentEpoch(state: state).delayedActivationExitEpoch()
activationEpoch = genesis ? InitialValues.GenesisEpoch : BeaconChain.getCurrentEpoch(state: state).delayedActivationExitEpoch()
}

mutating func exit(state: BeaconState) {
Expand Down
20 changes: 10 additions & 10 deletions Sources/BeaconChain/StateTransition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ extension StateTransition {
assert(block.body.attestations.count <= MAX_ATTESTATIONS)

for attestation in block.body.attestations {
assert(attestation.data.slot >= GENESIS_SLOT)
assert(attestation.data.slot >= InitialValues.GenesisSlot)
assert(attestation.data.slot + MIN_ATTESTATION_INCLUSION_DELAY <= state.slot)
assert(state.slot < attestation.data.slot + SLOTS_PER_EPOCH)

Expand Down Expand Up @@ -205,7 +205,7 @@ extension StateTransition {
)
)

assert(attestation.data.crosslinkDataRoot == ZERO_HASH) // @todo remove in phase 1
assert(attestation.data.crosslinkDataRoot == InitialValues.ZeroHash) // @todo remove in phase 1

state.latestAttestations.append(
PendingAttestation(
Expand Down Expand Up @@ -273,15 +273,15 @@ extension StateTransition {
assert(state.validatorBalances[Int(transfer.from)] >= transfer.fee)
assert(
state.validatorBalances[Int(transfer.from)] == transfer.amount + transfer.fee
|| state.validatorBalances[Int(transfer.from)] >= transfer.amount + transfer.fee + MIN_DEPOSIT_AMOUNT
|| state.validatorBalances[Int(transfer.from)] >= transfer.amount + transfer.fee + GWEIValues.MinDepositAmount
)

assert(state.slot == transfer.slot)
assert(
BeaconChain.getCurrentEpoch(state: state) >= state.validatorRegistry[Int(transfer.from)].withdrawableEpoch
|| state.validatorRegistry[Int(transfer.from)].activationEpoch == FAR_FUTURE_EPOCH
|| state.validatorRegistry[Int(transfer.from)].activationEpoch == InitialValues.FarFutureEpoch
)
assert(state.validatorRegistry[Int(transfer.from)].withdrawalCredentials == BLS_WITHDRAWAL_PREFIX_BYTE + BeaconChain.hash(transfer.pubkey).suffix(from: 1))
assert(state.validatorRegistry[Int(transfer.from)].withdrawalCredentials == InitialValues.BLSWithdrawalPrefixByte + BeaconChain.hash(transfer.pubkey).suffix(from: 1))

assert(
BLS.verify(
Expand Down Expand Up @@ -682,7 +682,7 @@ extension StateTransition {

var eligibleIndices = (0..<state.validatorRegistry.count).filter {
let validator = state.validatorRegistry[$0]
if validator.withdrawableEpoch != FAR_FUTURE_EPOCH {
if validator.withdrawableEpoch != InitialValues.FarFutureEpoch {
return false
} else {
return currentEpoch >= validator.exitEpoch + MIN_VALIDATOR_WITHDRAWABILITY_DELAY
Expand All @@ -707,11 +707,11 @@ extension StateTransition {
let activeValidatorIndices = state.validatorRegistry.activeIndices(epoch: currentEpoch)

let totalBalance = activeValidatorIndices.totalBalance(state: state)
let maxBalanceChurn = max(MAX_DEPOSIT_AMOUNT, totalBalance / (2 * MAX_BALANCE_CHURN_QUOTIENT))
let maxBalanceChurn = max(GWEIValues.MaxDepositAmount, totalBalance / (2 * MAX_BALANCE_CHURN_QUOTIENT))

var balanceChurn = UInt64(0)
for (i, v) in state.validatorRegistry.enumerated() {
if v.activationEpoch == FAR_FUTURE_EPOCH && state.validatorBalances[Int(i)] >= MAX_DEPOSIT_AMOUNT {
if v.activationEpoch == InitialValues.FarFutureEpoch && state.validatorBalances[Int(i)] >= GWEIValues.MaxDepositAmount {
balanceChurn += BeaconChain.getEffectiveBalance(state: state, index: ValidatorIndex(i))
if balanceChurn > maxBalanceChurn {
break
Expand All @@ -723,7 +723,7 @@ extension StateTransition {

balanceChurn = 0
for (i, v) in state.validatorRegistry.enumerated() {
if v.activationEpoch == FAR_FUTURE_EPOCH && v.initiatedExit {
if v.activationEpoch == InitialValues.FarFutureEpoch && v.initiatedExit {
balanceChurn += BeaconChain.getEffectiveBalance(state: state, index: ValidatorIndex(i))
if balanceChurn > maxBalanceChurn {
break
Expand All @@ -744,7 +744,7 @@ extension StateTransition {

static func processEjections(state: inout BeaconState) {
for i in state.validatorRegistry.activeIndices(epoch: BeaconChain.getCurrentEpoch(state: state)) {
if state.validatorBalances[Int(i)] < EJECTION_BALANCE {
if state.validatorBalances[Int(i)] < GWEIValues.EjectionBalance {
state.validatorRegistry[Int(i)].exit(state: state)
}
}
Expand Down