This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* moved attestationTargets function to array extension * renaming * updated * Update Types.swift * fix indent * cleaned up votes function. added test * added tests * fixed
- Loading branch information
Dean Eigenmann
authored
Mar 24, 2019
1 parent
d6e6418
commit 1f95768
Showing
8 changed files
with
93 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Sources/BeaconChain/DataStructures/Transactions/Attestations/Attestation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
Sources/BeaconChain/Extensions/Array+AttestationTarget.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Foundation | ||
|
||
extension Array where Element == AttestationTarget { | ||
|
||
func votes(store: Store, state: BeaconState, block: BeaconBlock) -> UInt64 { | ||
return compactMap { | ||
(index, target) in | ||
|
||
guard store.ancestor(block: target, slot: target.slot) == block else { | ||
return nil | ||
} | ||
|
||
return BeaconChain.getEffectiveBalance(state: state, index: index) / FORK_CHOICE_BALANCE_INCREMENT | ||
} | ||
.reduce(0, +) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import Foundation | ||
|
||
typealias Slot = UInt64 | ||
public typealias Slot = UInt64 | ||
typealias Epoch = UInt64 | ||
typealias Shard = UInt64 | ||
typealias ValidatorIndex = UInt64 | ||
public typealias ValidatorIndex = UInt64 | ||
typealias Gwei = UInt64 | ||
typealias Bytes32 = Data // @todo needs to be 32 fixed length data | ||
typealias BLSPubkey = Data // @todo needs to be 48 fixed length data | ||
typealias BLSSignature = Data // @todo needs to be 96 fixed length data | ||
|
||
typealias AttestationTarget = (ValidatorIndex, BeaconBlock) |
47 changes: 47 additions & 0 deletions
47
Tests/BeaconChainTests/Extensions/Array+AttestationTargetTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import XCTest | ||
@testable import BeaconChain | ||
|
||
final class ArrayAttestationTargetTests: XCTestCase { | ||
|
||
func testVotes() { | ||
var state = BeaconChain.genesisState( | ||
genesisTime: 10, | ||
latestEth1Data: Eth1Data(depositRoot: Data(count: 32), blockHash: Data(count: 32)), | ||
depositLength: 0 | ||
) | ||
|
||
state.validatorBalances.append(32000000000) | ||
state.validatorBalances.append(32000000000) | ||
state.validatorBalances.append(32000000000) | ||
|
||
let block = BeaconBlock( | ||
slot: GENESIS_SLOT, | ||
parentRoot: ZERO_HASH, | ||
stateRoot: ZERO_HASH, | ||
randaoReveal: ZERO_HASH, | ||
eth1Data: state.latestEth1Data, | ||
body: BeaconBlockBody( | ||
proposerSlashings: [ProposerSlashing](), | ||
attesterSlashings: [AttesterSlashing](), | ||
attestations: [Attestation](), | ||
deposits: [Deposit](), | ||
voluntaryExits: [VoluntaryExit](), | ||
transfers: [Transfer]() | ||
), | ||
signature: ZERO_HASH | ||
) | ||
|
||
let store = MockStore() | ||
|
||
var badBlock = block | ||
badBlock.signature = EMPTY_SIGNATURE | ||
|
||
var targets = [AttestationTarget]() | ||
targets.append((0, block)) | ||
targets.append((1, badBlock)) | ||
targets.append((2, block)) | ||
|
||
XCTAssertEqual(64, targets.votes(store: store, state: state, block: block)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import BeaconChain | ||
|
||
class MockStore: Store { | ||
|
||
func parent(_ block: BeaconBlock) -> BeaconBlock { | ||
return block | ||
} | ||
|
||
func children(_ block: BeaconBlock) -> [BeaconBlock] { | ||
return [block] | ||
} | ||
|
||
func latestAttestation(validator: ValidatorIndex) -> Attestation { | ||
fatalError() | ||
} | ||
|
||
func latestAttestationTarget(validator: ValidatorIndex) -> BeaconBlock { | ||
fatalError() | ||
} | ||
} |