2019-01-19 02:57:51 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-02-14 20:04:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/ssz"
|
|
|
|
|
2019-01-19 02:57:51 +00:00
|
|
|
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
2019-02-13 23:13:00 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2019-01-19 02:57:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-01-31 02:53:58 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2019-01-19 02:57:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// LMDGhost applies the Latest Message Driven, Greediest Heaviest Observed Sub-Tree
|
|
|
|
// fork-choice rule defined in the Ethereum Serenity specification for the beacon chain.
|
2019-01-29 11:19:17 +00:00
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
|
|
|
// head = start_block
|
|
|
|
// while 1:
|
|
|
|
// children = get_children(store, head)
|
|
|
|
// if len(children) == 0:
|
|
|
|
// return head
|
|
|
|
// head = max(children, key=get_vote_count)
|
2019-01-19 02:57:51 +00:00
|
|
|
func LMDGhost(
|
|
|
|
block *pb.BeaconBlock,
|
2019-02-13 23:13:00 +00:00
|
|
|
state *pb.BeaconState,
|
|
|
|
voteTargets map[uint64]*pb.BeaconBlock,
|
2019-01-19 02:57:51 +00:00
|
|
|
observedBlocks []*pb.BeaconBlock,
|
|
|
|
beaconDB *db.BeaconDB,
|
|
|
|
) (*pb.BeaconBlock, error) {
|
|
|
|
head := block
|
|
|
|
for {
|
|
|
|
children, err := b.BlockChildren(head, observedBlocks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not fetch block children: %v", err)
|
|
|
|
}
|
|
|
|
if len(children) == 0 {
|
|
|
|
return head, nil
|
|
|
|
}
|
|
|
|
maxChild := children[0]
|
2019-02-13 23:13:00 +00:00
|
|
|
maxChildVotes, err := VoteCount(maxChild, state, voteTargets, beaconDB)
|
2019-01-19 02:57:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to determine vote count for block: %v", err)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(children); i++ {
|
2019-02-13 23:13:00 +00:00
|
|
|
candidateChildVotes, err := VoteCount(children[i], state, voteTargets, beaconDB)
|
2019-01-19 02:57:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to determine vote count for block: %v", err)
|
|
|
|
}
|
|
|
|
if candidateChildVotes > maxChildVotes {
|
|
|
|
maxChild = children[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
head = maxChild
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// VoteCount determines the number of votes on a beacon block by counting the number
|
|
|
|
// of target blocks that have such beacon block as a common ancestor.
|
2019-01-29 11:19:17 +00:00
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
2019-02-13 23:13:00 +00:00
|
|
|
// def get_vote_count(block: BeaconBlock) -> int:
|
|
|
|
// return sum(
|
|
|
|
// get_effective_balance(start_state.validator_balances[validator_index]) // FORK_CHOICE_BALANCE_INCREMENT
|
|
|
|
// for validator_index, target in attestation_targets
|
|
|
|
// if get_ancestor(store, target, block.slot) == block
|
|
|
|
// )
|
|
|
|
func VoteCount(block *pb.BeaconBlock, state *pb.BeaconState, targets map[uint64]*pb.BeaconBlock, beaconDB *db.BeaconDB) (int, error) {
|
|
|
|
balances := 0
|
|
|
|
for validatorIndex, targetBlock := range targets {
|
|
|
|
ancestor, err := BlockAncestor(targetBlock, block.Slot, beaconDB)
|
2019-01-19 02:57:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-02-14 20:04:47 +00:00
|
|
|
ancestorRoot, err := ssz.TreeHash(ancestor)
|
2019-01-19 02:57:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-02-14 20:04:47 +00:00
|
|
|
blockRoot, err := ssz.TreeHash(block)
|
2019-01-19 02:57:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-02-14 20:04:47 +00:00
|
|
|
if blockRoot == ancestorRoot {
|
2019-02-13 23:13:00 +00:00
|
|
|
balances += int(helpers.EffectiveBalance(state, validatorIndex))
|
2019-01-19 02:57:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-13 23:13:00 +00:00
|
|
|
return balances, nil
|
2019-01-19 02:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BlockAncestor obtains the ancestor at of a block at a certain slot.
|
2019-01-29 11:19:17 +00:00
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
|
|
|
// Let get_ancestor(store: Store, block: BeaconBlock, slot: SlotNumber) ->
|
|
|
|
// BeaconBlock be the ancestor of block with slot number slot.
|
|
|
|
// The get_ancestor function can be defined recursively as
|
|
|
|
// def get_ancestor(store: Store, block: BeaconBlock, slot: SlotNumber) ->
|
|
|
|
// BeaconBlock: return block if block.slot ==
|
|
|
|
// slot else get_ancestor(store, store.get_parent(block), slot)
|
2019-01-19 02:57:51 +00:00
|
|
|
func BlockAncestor(block *pb.BeaconBlock, slot uint64, beaconDB *db.BeaconDB) (*pb.BeaconBlock, error) {
|
|
|
|
if block.Slot == slot {
|
|
|
|
return block, nil
|
|
|
|
}
|
|
|
|
parentHash := bytesutil.ToBytes32(block.ParentRootHash32)
|
2019-01-21 09:34:11 +00:00
|
|
|
parent, err := beaconDB.Block(parentHash)
|
2019-01-19 02:57:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get parent block: %v", err)
|
|
|
|
}
|
|
|
|
if parent == nil {
|
|
|
|
return nil, fmt.Errorf("parent block does not exist: %v", err)
|
|
|
|
}
|
|
|
|
return BlockAncestor(parent, slot, beaconDB)
|
|
|
|
}
|