2018-11-26 13:33:06 +00:00
|
|
|
import
|
2019-10-25 10:59:56 +00:00
|
|
|
deques, options, sequtils, tables,
|
2019-02-19 23:35:02 +00:00
|
|
|
chronicles,
|
initial 0.9.0 spec sync (#509)
* rename compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* remove some unnecessary imports; remove some crosslink-related code and tests; complete renaming of compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* rm more transfer-related code and tests; rm more unnecessary strutils imports
* rm remaining unused imports
* remove useless get_empty_per_epoch_cache(...)/compute_start_slot_of_epoch(...) calls
* rename compute_start_slot_of_epoch(...) to compute_start_slot_at_epoch(...)
* rename ACTIVATION_EXIT_DELAY to MAX_SEED_LOOKAHEAD
* update domain types to 0.9.0
* mark AttesterSlashing, IndexedAttestation, AttestationDataAndCustodyBit, DepositData, BeaconBlockHeader, Fork, integer_squareroot(...), and process_voluntary_exit(...) as 0.9.0
* mark increase_balance(...), decrease_balance(...), get_block_root(...), CheckPoint, Deposit, PendingAttestation, HistoricalBatch, is_active_validator(...), and is_slashable_attestation_data(...) as 0.9.0
* mark compute_activation_exit_epoch(...), bls_verify(...), Validator, get_active_validator_indices(...), get_current_epoch(...), get_total_active_balance(...), and get_previous_epoch(...) as 0.9.0
* mark get_block_root_at_slot(...), ProposerSlashing, get_domain(...), VoluntaryExit, mainnet preset Gwei values, minimal preset max operations, process_block_header(...), and is_slashable_validator(...) as 0.9.0
* mark makeWithdrawalCredentials(...), get_validator_churn_limit(...), get_total_balance(...), is_valid_indexed_attestation(...), bls_aggregate_pubkeys(...), initial genesis value/constants, Attestation, get_randao_mix(...), mainnet preset max operations per block constants, minimal preset Gwei values and time parameters, process_eth1_data(...), get_shuffled_seq(...), compute_committee(...), and process_slots(...) as 0.9.0; partially update get_indexed_attestation(...) to 0.9.0 by removing crosslink refs and associated tests
* mark initiate_validator_exit(...), process_registry_updates(...), BeaconBlock, Eth1Data, compute_domain(...), process_randao(...), process_attester_slashing(...), get_base_reward(...), and process_slot(...) as 0.9.0
2019-10-30 19:41:19 +00:00
|
|
|
./spec/[datatypes, crypto, helpers],
|
2019-10-25 10:59:56 +00:00
|
|
|
./attestation_pool, ./beacon_node_types, ./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(
|
initial 0.9.0 spec sync (#509)
* rename compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* remove some unnecessary imports; remove some crosslink-related code and tests; complete renaming of compute_epoch_of_slot(...) to compute_epoch_at_slot(...)
* rm more transfer-related code and tests; rm more unnecessary strutils imports
* rm remaining unused imports
* remove useless get_empty_per_epoch_cache(...)/compute_start_slot_of_epoch(...) calls
* rename compute_start_slot_of_epoch(...) to compute_start_slot_at_epoch(...)
* rename ACTIVATION_EXIT_DELAY to MAX_SEED_LOOKAHEAD
* update domain types to 0.9.0
* mark AttesterSlashing, IndexedAttestation, AttestationDataAndCustodyBit, DepositData, BeaconBlockHeader, Fork, integer_squareroot(...), and process_voluntary_exit(...) as 0.9.0
* mark increase_balance(...), decrease_balance(...), get_block_root(...), CheckPoint, Deposit, PendingAttestation, HistoricalBatch, is_active_validator(...), and is_slashable_attestation_data(...) as 0.9.0
* mark compute_activation_exit_epoch(...), bls_verify(...), Validator, get_active_validator_indices(...), get_current_epoch(...), get_total_active_balance(...), and get_previous_epoch(...) as 0.9.0
* mark get_block_root_at_slot(...), ProposerSlashing, get_domain(...), VoluntaryExit, mainnet preset Gwei values, minimal preset max operations, process_block_header(...), and is_slashable_validator(...) as 0.9.0
* mark makeWithdrawalCredentials(...), get_validator_churn_limit(...), get_total_balance(...), is_valid_indexed_attestation(...), bls_aggregate_pubkeys(...), initial genesis value/constants, Attestation, get_randao_mix(...), mainnet preset max operations per block constants, minimal preset Gwei values and time parameters, process_eth1_data(...), get_shuffled_seq(...), compute_committee(...), and process_slots(...) as 0.9.0; partially update get_indexed_attestation(...) to 0.9.0 by removing crosslink refs and associated tests
* mark initiate_validator_exit(...), process_registry_updates(...), BeaconBlock, Eth1Data, compute_domain(...), process_randao(...), process_attester_slashing(...), get_base_reward(...), and process_slot(...) as 0.9.0
2019-10-30 19:41:19 +00:00
|
|
|
start_state, compute_epoch_at_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
|