initial epoch processing update switching from slot-oriented to epoch-oriented, using newer helper functions
This commit is contained in:
parent
fb3bbfccaa
commit
a42601e29e
|
@ -471,7 +471,7 @@ proc checkAttestation*(
|
||||||
true
|
true
|
||||||
|
|
||||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.3.0/specs/core/0_beacon-chain.md#get_total_balance
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.3.0/specs/core/0_beacon-chain.md#get_total_balance
|
||||||
func get_total_balance(state: BeaconState, validators: seq[ValidatorIndex]): Gwei =
|
func get_total_balance*(state: BeaconState, validators: seq[ValidatorIndex]): Gwei =
|
||||||
# Return the combined effective balance of an array of validators.
|
# Return the combined effective balance of an array of validators.
|
||||||
foldl(validators, a + get_effective_balance(state, b), 0'u64)
|
foldl(validators, a + get_effective_balance(state, b), 0'u64)
|
||||||
|
|
||||||
|
|
|
@ -519,7 +519,7 @@ func lowerThan(candidate, current: Eth2Digest): bool =
|
||||||
# TODO spec - clarify hash ordering..
|
# TODO spec - clarify hash ordering..
|
||||||
for i, v in current.data:
|
for i, v in current.data:
|
||||||
if v > candidate.data[i]: return true
|
if v > candidate.data[i]: return true
|
||||||
return false
|
false
|
||||||
|
|
||||||
func processEpoch(state: var BeaconState) =
|
func processEpoch(state: var BeaconState) =
|
||||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.3.0/specs/core/0_beacon-chain.md#per-epoch-processing
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.3.0/specs/core/0_beacon-chain.md#per-epoch-processing
|
||||||
|
@ -528,32 +528,34 @@ func processEpoch(state: var BeaconState) =
|
||||||
|
|
||||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.3.0/specs/core/0_beacon-chain.md#helper-variables
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.3.0/specs/core/0_beacon-chain.md#helper-variables
|
||||||
let
|
let
|
||||||
active_validator_indices =
|
|
||||||
get_active_validator_indices(state.validator_registry, state.slot)
|
|
||||||
total_balance = sum_effective_balances(state, active_validator_indices)
|
|
||||||
|
|
||||||
current_epoch = get_current_epoch(state)
|
current_epoch = get_current_epoch(state)
|
||||||
previous_epoch = if current_epoch > GENESIS_EPOCH: current_epoch - 1 else: current_epoch
|
previous_epoch =
|
||||||
|
if current_epoch > GENESIS_EPOCH:
|
||||||
|
current_epoch - 1
|
||||||
|
else:
|
||||||
|
current_epoch
|
||||||
next_epoch = (current_epoch + 1).Epoch
|
next_epoch = (current_epoch + 1).Epoch
|
||||||
|
|
||||||
# TODO doing this with iterators failed:
|
current_total_balance = get_total_balance(
|
||||||
# https://github.com/nim-lang/Nim/issues/9827
|
state, get_active_validator_indices(
|
||||||
let
|
state.validator_registry, current_epoch))
|
||||||
|
|
||||||
|
# TODO doing this with iterators failed:
|
||||||
|
# https://github.com/nim-lang/Nim/issues/9827
|
||||||
current_epoch_attestations =
|
current_epoch_attestations =
|
||||||
state.latest_attestations.filterIt(
|
state.latest_attestations.filterIt(
|
||||||
state.slot <= it.data.slot + SLOTS_PER_EPOCH and
|
current_epoch == slot_to_epoch(it.data.slot))
|
||||||
it.data.slot < state.slot)
|
|
||||||
|
|
||||||
current_epoch_boundary_attestations =
|
current_epoch_boundary_attestations =
|
||||||
boundary_attestations(
|
boundary_attestations(
|
||||||
state, get_block_root(state, state.slot-SLOTS_PER_EPOCH),
|
state, get_block_root(state, get_epoch_start_slot(current_epoch)),
|
||||||
current_epoch_attestations)
|
current_epoch_attestations)
|
||||||
|
|
||||||
current_epoch_boundary_attester_indices =
|
current_epoch_boundary_attester_indices =
|
||||||
get_attester_indices(state, current_epoch_attestations)
|
get_attester_indices(state, current_epoch_boundary_attestations)
|
||||||
|
|
||||||
current_epoch_boundary_attesting_balance =
|
current_epoch_boundary_attesting_balance =
|
||||||
sum_effective_balances(state, current_epoch_boundary_attester_indices)
|
get_total_balance(state, current_epoch_boundary_attester_indices)
|
||||||
|
|
||||||
let
|
let
|
||||||
previous_epoch_attestations =
|
previous_epoch_attestations =
|
||||||
|
@ -667,7 +669,6 @@ func processEpoch(state: var BeaconState) =
|
||||||
# Helpers for justification
|
# Helpers for justification
|
||||||
let
|
let
|
||||||
previous_total_balance = sum_effective_balances(state, get_active_validator_indices(state.validator_registry, previous_epoch))
|
previous_total_balance = sum_effective_balances(state, get_active_validator_indices(state.validator_registry, previous_epoch))
|
||||||
current_total_balance = sum_effective_balances(state, get_active_validator_indices(state.validator_registry, current_epoch))
|
|
||||||
|
|
||||||
block: # Justification
|
block: # Justification
|
||||||
# https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#justification
|
# https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md#justification
|
||||||
|
@ -733,6 +734,10 @@ func processEpoch(state: var BeaconState) =
|
||||||
return a.inclusion_slot - a.data.slot
|
return a.inclusion_slot - a.data.slot
|
||||||
doAssert false # shouldn't happen..
|
doAssert false # shouldn't happen..
|
||||||
|
|
||||||
|
# TODO check if/how still useful
|
||||||
|
let active_validator_indices =
|
||||||
|
get_active_validator_indices(state.validator_registry, state.slot)
|
||||||
|
|
||||||
block: # Justification and finalization
|
block: # Justification and finalization
|
||||||
let epochs_since_finality = next_epoch - state.finalized_epoch
|
let epochs_since_finality = next_epoch - state.finalized_epoch
|
||||||
|
|
||||||
|
@ -741,7 +746,7 @@ func processEpoch(state: var BeaconState) =
|
||||||
for v in attesters:
|
for v in attesters:
|
||||||
statePtr.validator_balances[v] +=
|
statePtr.validator_balances[v] +=
|
||||||
base_reward(statePtr[], v) *
|
base_reward(statePtr[], v) *
|
||||||
attesting_balance div total_balance
|
attesting_balance div current_total_balance
|
||||||
|
|
||||||
for v in active_validator_indices:
|
for v in active_validator_indices:
|
||||||
if v notin attesters:
|
if v notin attesters:
|
||||||
|
|
Loading…
Reference in New Issue