2018-11-26 13:33:06 +00:00
|
|
|
import
|
2019-07-10 12:23:02 +00:00
|
|
|
deques, math, options, sequtils, tables,
|
2019-02-19 23:35:02 +00:00
|
|
|
chronicles,
|
|
|
|
./spec/[beaconstate, datatypes, crypto, digest, helpers, validator], extras,
|
2019-03-13 22:59:20 +00:00
|
|
|
./attestation_pool, ./beacon_node_types, ./beacon_chain_db, ./ssz
|
2018-12-15 16:32:36 +00:00
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
proc get_ancestor(blck: BlockRef, slot: Slot): BlockRef =
|
|
|
|
if blck.slot == slot:
|
2019-01-14 12:19:44 +00:00
|
|
|
blck
|
2019-03-13 22:59:20 +00:00
|
|
|
elif blck.slot < slot:
|
|
|
|
nil
|
2019-01-08 17:28:21 +00:00
|
|
|
else:
|
2019-03-13 22:59:20 +00:00
|
|
|
get_ancestor(blck.parent, slot)
|
2019-01-08 17:28:21 +00:00
|
|
|
|
2019-10-24 13:36:36 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/core/0_fork-choice.md
|
2019-09-01 15:02:49 +00:00
|
|
|
# The structure of this code differs from the spec since we use a different
|
|
|
|
# strategy for storing states and justification points - it should nonetheless
|
|
|
|
# be close in terms of functionality.
|
2019-01-14 12:19:44 +00:00
|
|
|
proc lmdGhost*(
|
2019-03-13 22:59:20 +00:00
|
|
|
pool: AttestationPool, start_state: BeaconState,
|
|
|
|
start_block: BlockRef): BlockRef =
|
2019-01-08 17:28:21 +00:00
|
|
|
# TODO: a Fenwick Tree datastructure to keep track of cumulated votes
|
|
|
|
# in O(log N) complexity
|
|
|
|
# https://en.wikipedia.org/wiki/Fenwick_tree
|
|
|
|
# Nim implementation for cumulative frequencies at
|
|
|
|
# https://github.com/numforge/laser/blob/990e59fffe50779cdef33aa0b8f22da19e1eb328/benchmarks/random_sampling/fenwicktree.nim
|
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
let
|
|
|
|
active_validator_indices =
|
|
|
|
get_active_validator_indices(
|
2019-07-01 07:53:42 +00:00
|
|
|
start_state, compute_epoch_of_slot(start_state.slot))
|
2019-03-13 22:59:20 +00:00
|
|
|
|
2019-09-01 15:02:49 +00:00
|
|
|
var latest_messages: seq[tuple[validator: ValidatorIndex, blck: BlockRef]]
|
2019-03-13 22:59:20 +00:00
|
|
|
for i in active_validator_indices:
|
2019-07-01 09:13:14 +00:00
|
|
|
let pubKey = start_state.validators[i].pubkey
|
2019-03-13 22:59:20 +00:00
|
|
|
if (let vote = pool.latestAttestation(pubKey); not vote.isNil):
|
2019-09-01 15:02:49 +00:00
|
|
|
latest_messages.add((i, vote))
|
2019-03-13 22:59:20 +00:00
|
|
|
|
2019-09-01 15:02:49 +00:00
|
|
|
template get_latest_attesting_balance(blck: BlockRef): uint64 =
|
2019-03-13 22:59:20 +00:00
|
|
|
var res: uint64
|
2019-09-01 15:02:49 +00:00
|
|
|
for validator_index, target in latest_messages.items():
|
2019-03-13 22:59:20 +00:00
|
|
|
if get_ancestor(target, blck.slot) == blck:
|
2019-09-01 15:02:49 +00:00
|
|
|
res += start_state.validators[validator_index].effective_balance
|
2019-03-13 22:59:20 +00:00
|
|
|
res
|
|
|
|
|
|
|
|
var head = start_block
|
|
|
|
while true:
|
|
|
|
if head.children.len() == 0:
|
|
|
|
return head
|
|
|
|
|
|
|
|
head = head.children[0]
|
|
|
|
var
|
2019-09-01 15:02:49 +00:00
|
|
|
headCount = get_latest_attesting_balance(head)
|
2019-03-13 22:59:20 +00:00
|
|
|
|
|
|
|
for i in 1..<head.children.len:
|
2019-09-01 15:02:49 +00:00
|
|
|
if (let hc = get_latest_attesting_balance(head.children[i]); hc > headCount):
|
2019-03-13 22:59:20 +00:00
|
|
|
head = head.children[i]
|
|
|
|
headCount = hc
|