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

Commit

Permalink
feature/int-extension (#57)
Browse files Browse the repository at this point in the history
* moved function

* updated
  • Loading branch information
Dean Eigenmann authored Feb 22, 2019
1 parent 921fbd3 commit 954bcb6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
10 changes: 1 addition & 9 deletions Sources/BeaconChain/BeaconChain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ extension BeaconChain {
if registryChange {
seed = generateSeed(state: state, epoch: nextEpoch)
shufflingStartShard = (state.currentEpochStartShard + UInt64(currentCommitteesPerEpoch)) % SHARD_COUNT
} else if epochsSinceLastRegistryUpdate > 1 && isPowerOfTwo(Int(epochsSinceLastRegistryUpdate)) {
} else if epochsSinceLastRegistryUpdate > 1 && Int(epochsSinceLastRegistryUpdate).isPowerOfTwo() {
seed = generateSeed(state: state, epoch: nextEpoch)
shufflingStartShard = state.currentEpochStartShard
} else {
Expand Down Expand Up @@ -234,14 +234,6 @@ extension BeaconChain {
}
}

static func isPowerOfTwo(_ value: Int) -> Bool {
if value == 0 {
return false
}

return 2 ** Int(log2(Double(value))) == value
}

static func getEffectiveBalance(state: BeaconState, index: ValidatorIndex) -> Gwei {
return min(state.validatorBalances[Int(index)], MAX_DEPOSIT_AMOUNT)
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/BeaconChain/Extensions/Int.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

extension Int {

func isPowerOfTwo() -> Bool {
return (self > 0) && (self & (self - 1) == 0)
}
}
2 changes: 1 addition & 1 deletion Sources/BeaconChain/StateTransition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ extension StateTransition {
updateValidatorRegistry(state: &state)
} else {
let epochsSinceLastRegistryUpdate = currentEpoch - state.validatorRegistryUpdateEpoch
if epochsSinceLastRegistryUpdate > 1 && BeaconChain.isPowerOfTwo(Int(epochsSinceLastRegistryUpdate)) {
if epochsSinceLastRegistryUpdate > 1 && Int(epochsSinceLastRegistryUpdate).isPowerOfTwo() {
state.currentCalculationEpoch = nextEpoch
state.currentEpochSeed = BeaconChain.generateSeed(state: state, epoch: state.currentCalculationEpoch)
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/BeaconChainTests/Extensions/IntTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import XCTest
@testable import BeaconChain

final class IntTests: XCTestCase {

func testIsPowerOfTwo() {
XCTAssertTrue(4.isPowerOfTwo())
XCTAssertFalse(3.isPowerOfTwo())
}
}

0 comments on commit 954bcb6

Please sign in to comment.