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

Commit

Permalink
feature/epoch-extension (#55)
Browse files Browse the repository at this point in the history
* added EpochNumber extension

* removes getEntryExitEpoch

* added test
  • Loading branch information
Dean Eigenmann authored Feb 22, 2019
1 parent 25f194d commit bad5295
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 21 deletions.
14 changes: 3 additions & 11 deletions Sources/BeaconChain/BeaconChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ extension BeaconChain {
static func getCurrentEpoch(state: BeaconState) -> EpochNumber {
return state.slot.toEpoch()
}

static func getEpochStartSlot(_ epoch: EpochNumber) -> SlotNumber {
return epoch * EPOCH_LENGTH
}

static func getEntryExitEpoch(_ epoch: EpochNumber) -> EpochNumber {
return epoch + 1 + ENTRY_EXIT_DELAY
}
}

extension BeaconChain {
Expand Down Expand Up @@ -490,7 +482,7 @@ extension BeaconChain {
extension BeaconChain {

static func activateValidator(state: inout BeaconState, index: ValidatorIndex, genesis: Bool) {
state.validatorRegistry[Int(index)].activationEpoch = genesis ? GENESIS_EPOCH : getEntryExitEpoch(getCurrentEpoch(state: state))
state.validatorRegistry[Int(index)].activationEpoch = genesis ? GENESIS_EPOCH : getCurrentEpoch(state: state).entryExitEpoch()
}

static func initiateValidatorExit(state: inout BeaconState, index: ValidatorIndex) {
Expand All @@ -499,11 +491,11 @@ extension BeaconChain {

static func exitValidator(state: inout BeaconState, index: ValidatorIndex) {
var validator = state.validatorRegistry[Int(index)]
if validator.exitEpoch <= getEntryExitEpoch(getCurrentEpoch(state: state)) {
if validator.exitEpoch <= getCurrentEpoch(state: state).entryExitEpoch() {
return
}

validator.exitEpoch = getEntryExitEpoch(getCurrentEpoch(state: state))
validator.exitEpoch = getCurrentEpoch(state: state).entryExitEpoch()
state.validatorRegistry[Int(index)] = validator
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ struct AttestationData: Equatable {
let epochBoundaryRoot: Data
let shardBlockRoot: Data
let latestCrosslink: Crosslink
let justifiedEpoch: UInt64
let justifiedEpoch: EpochNumber
let justifiedBlockRoot: Data
}
14 changes: 14 additions & 0 deletions Sources/BeaconChain/Extensions/EpochNumber.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

extension EpochNumber {


func startSlot() -> SlotNumber {
return self * EPOCH_LENGTH
}

func entryExitEpoch() -> EpochNumber {
return self + 1 + ENTRY_EXIT_DELAY
}

}
2 changes: 1 addition & 1 deletion Sources/BeaconChain/Extensions/SlotNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ extension SlotNumber {
return self / EPOCH_LENGTH
}

}
}
16 changes: 8 additions & 8 deletions Sources/BeaconChain/StateTransition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ extension StateTransition {

let e = attestation.data.justifiedEpoch >= BeaconChain.getCurrentEpoch(state: state) ? state.justifiedEpoch : state.previousJustifiedEpoch
assert(attestation.data.justifiedEpoch == e)
assert(attestation.data.justifiedBlockRoot == BeaconChain.getBlockRoot(state: state, slot: BeaconChain.getEpochStartSlot(attestation.data.justifiedEpoch)))
assert(attestation.data.justifiedBlockRoot == BeaconChain.getBlockRoot(state: state, slot: attestation.data.justifiedEpoch.startSlot()))

assert(
state.latestCrosslinks[Int(attestation.data.shard)] == attestation.data.latestCrosslink ||
Expand Down Expand Up @@ -260,7 +260,7 @@ extension StateTransition {
let validator = state.validatorRegistry[Int(exit.validatorIndex)]

let epoch = BeaconChain.getCurrentEpoch(state: state)
assert(validator.exitEpoch > BeaconChain.getEntryExitEpoch(epoch))
assert(validator.exitEpoch > epoch.entryExitEpoch())
assert(epoch >= exit.epoch)

let exitMessage = BeaconChain.hashTreeRoot(
Expand Down Expand Up @@ -310,7 +310,7 @@ extension StateTransition {
}

let currentEpochBoundryAttestations = currentEpochAttestations.filter {
$0.data.epochBoundaryRoot == BeaconChain.getBlockRoot(state: state, slot: BeaconChain.getEpochStartSlot(currentEpoch))
$0.data.epochBoundaryRoot == BeaconChain.getBlockRoot(state: state, slot: currentEpoch.startSlot())
&& $0.data.justifiedEpoch == state.justifiedEpoch
}

Expand Down Expand Up @@ -341,7 +341,7 @@ extension StateTransition {
let previousEpochJustifiedAttestingBalance = (previousEpochJustifiedAttesterIndices as [ValidatorIndex]).totalBalance(state: state)

let previousEpochBoundaryAttestations = previousEpochJustifiedAttestations.filter {
$0.data.epochBoundaryRoot == BeaconChain.getBlockRoot(state: state, slot: BeaconChain.getEpochStartSlot(previousEpoch))
$0.data.epochBoundaryRoot == BeaconChain.getBlockRoot(state: state, slot: previousEpoch.startSlot())
}

let previousEpochBoundaryAttesterIndices = previousEpochBoundaryAttestations.flatMap {
Expand Down Expand Up @@ -501,7 +501,7 @@ extension StateTransition {
previousEpochAttestations: [PendingAttestation]
) {

for slot in BeaconChain.getEpochStartSlot(previousEpoch)..<BeaconChain.getEpochStartSlot(nextEpoch) {
for slot in previousEpoch.startSlot()..<nextEpoch.startSlot() {
let crosslinkCommitteesAtSlot = BeaconChain.getCrosslinkCommitteesAtSlot(state: state, slot: slot)

for (_, (crosslinkCommittee, shard)) in crosslinkCommitteesAtSlot.enumerated() {
Expand Down Expand Up @@ -622,7 +622,7 @@ extension StateTransition {
state.validatorBalances[Int(proposer)] += baseReward(state: state, index: index, baseRewardQuotient: baseRewardQuotient) / INCLUDER_REWARD_QUOTIENT
}

for slot in BeaconChain.getEpochStartSlot(previousEpoch)..<BeaconChain.getEpochStartSlot(currentEpoch) {
for slot in previousEpoch.startSlot()..<currentEpoch.startSlot() {
let crosslinkCommitteesAtSlot = BeaconChain.getCrosslinkCommitteesAtSlot(state: state, slot: slot)

for (_, (crosslinkCommittee, shard)) in crosslinkCommitteesAtSlot.enumerated() {
Expand Down Expand Up @@ -701,7 +701,7 @@ extension StateTransition {

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

balanceChurn = 0
for (i, v) in state.validatorRegistry.enumerated() {
if v.exitEpoch > BeaconChain.getEntryExitEpoch(currentEpoch) && v.statusFlags & StatusFlag.INITIATED_EXIT.rawValue == 1 {
if v.exitEpoch > currentEpoch.entryExitEpoch() && v.statusFlags & StatusFlag.INITIATED_EXIT.rawValue == 1 {
balanceChurn += BeaconChain.getEffectiveBalance(state: state, index: ValidatorIndex(i))
if balanceChurn > maxBalanceChurn {
break
Expand Down
13 changes: 13 additions & 0 deletions Tests/BeaconChainTests/Extensions/EpochNumberTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import XCTest
@testable import BeaconChain

final class EpochNumberTests: XCTestCase {

func testStartSlot() {
XCTAssertEqual(EpochNumber(1).startSlot(), 64)
}

func testEntryExitEpoch() {
XCTAssertEqual(EpochNumber(1).entryExitEpoch(), 6)
}
}
1 change: 1 addition & 0 deletions Tests/BeaconChainTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public func allTests() -> [XCTestCaseEntry] {
testCase(ArrayValidatorIndexTests.allTests),
testCase(ArrayValidatorTests.allTests),
testCase(SlotNumberTests.allTests),
testCase(EpochNumberTests.allTests),
// testCase(StateTransitionTests.allTests)
]
}
Expand Down

0 comments on commit bad5295

Please sign in to comment.