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

Commit

Permalink
feature/ghost (#77)
Browse files Browse the repository at this point in the history
* started ghost implementation

* rename

* fixed
  • Loading branch information
Dean Eigenmann authored Mar 18, 2019
1 parent 171d21d commit d6e6418
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct BeaconBlock {
struct BeaconBlock: Equatable {
let slot: UInt64
let parentRoot: Data
let stateRoot: Data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct BeaconBlockBody {
struct BeaconBlockBody: Equatable {
let proposerSlashings: [ProposerSlashing]
let attesterSlashings: [AttesterSlashing]
let attestations: [Attestation]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct Proposal {
struct Proposal: Equatable {
let slot: UInt64
let shard: UInt64
let blockRoot: Data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct Attestation {
struct Attestation: Equatable {
let aggregationBitfield: Data
let data: AttestationData
let custodyBitfield: Data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct AttesterSlashing {
struct AttesterSlashing: Equatable {
let slashableAttestation1: SlashableAttestation
let slashableAttestation2: SlashableAttestation
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct SlashableAttestation {
struct SlashableAttestation: Equatable {
let validatorIndices: [UInt64]
let data: AttestationData
let custodyBitfield: Data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct Deposit {
struct Deposit: Equatable {
let branch: [Data]
let index: UInt64
let depositData: DepositData
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct DepositData {
struct DepositData: Equatable {
let amount: UInt64
let timestamp: UInt64
let depositInput: DepositInput
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct DepositInput {
struct DepositInput: Equatable {
let pubkey: Data
let withdrawalCredentials: Data
let proofOfPossession: Data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct ProposerSlashing {
struct ProposerSlashing: Equatable {
let proposerIndex: UInt64
let proposal1: Proposal
let proposal2: Proposal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct Transfer {
struct Transfer: Equatable {
let from: UInt64
let to: UInt64
let amount: UInt64
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct VoluntaryExit {
struct VoluntaryExit: Equatable {
let epoch: UInt64
let validatorIndex: UInt64
let signature: Data
Expand Down
7 changes: 7 additions & 0 deletions Sources/BeaconChain/ForkChoice/ForkChoice.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Foundation

protocol ForkChoice {

func execute(store: Store, startState: BeaconState, startBlock: BeaconBlock) -> BeaconBlock

}
43 changes: 43 additions & 0 deletions Sources/BeaconChain/ForkChoice/LMDGhost.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Foundation

class LMDGhost: ForkChoice {

func execute(store: Store, startState: BeaconState, startBlock: BeaconBlock) -> BeaconBlock {
let attestationTargets = startState.validatorRegistry.activeIndices(epoch: startState.slot.toEpoch()).map {
($0, store.latestAttestationTarget(validator: $0))
}

var head = startBlock
while true {
let children = store.children(head)
if children.count == 0 {
return head
}

head = children.max {
voteCount(store: store, state: startState, block: $0, attestationTargets: attestationTargets) < voteCount(store: store, state: startState, block: $1, attestationTargets: attestationTargets)
}!
}
}

private func voteCount(
store: Store,
state: BeaconState,
block: BeaconBlock,
attestationTargets: [(ValidatorIndex, BeaconBlock)]
) -> UInt64 {
return attestationTargets.compactMap {
(index, target) in
guard let ancestor = store.ancestor(block: target, slot: index) else {
return nil
}

if ancestor == block {
return BeaconChain.getEffectiveBalance(state: state, index: index) / FORK_CHOICE_BALANCE_INCREMENT
}

return nil
}
.reduce(0, +)
}
}
25 changes: 25 additions & 0 deletions Sources/BeaconChain/Store/Store.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation

protocol Store {

func parent(_ block: BeaconBlock) -> BeaconBlock
func children(_ block: BeaconBlock) -> [BeaconBlock]
func latestAttestation(validator: ValidatorIndex) -> Attestation
func latestAttestationTarget(validator: ValidatorIndex) -> BeaconBlock
}

extension Store {

func ancestor(block: BeaconBlock, slot: Slot) -> BeaconBlock? {
if block.slot == slot {
return block
}

if block.slot < slot {
return nil
}

return ancestor(block: parent(block), slot: slot)
}
}

0 comments on commit d6e6418

Please sign in to comment.