add some helper functions, change epoch processing, add some new constants

This commit is contained in:
Dustin Brody 2019-01-16 03:07:41 -08:00
parent d101c117fd
commit cb1096c581
4 changed files with 52 additions and 4 deletions

View File

@ -267,6 +267,13 @@ func get_block_root*(state: BeaconState,
doAssert slot < state.slot
state.latest_block_roots[slot mod LATEST_BLOCK_ROOTS_LENGTH]
func get_randao_mix*(state: BeaconState,
slot: uint64): Eth2Digest =
## Returns the randao mix at a recent ``slot``.
assert state.slot < slot + LATEST_RANDAO_MIXES_LENGTH
assert slot <= state.slot
state.latest_randao_mixes[slot mod LATEST_RANDAO_MIXES_LENGTH]
func get_attestation_participants*(state: BeaconState,
attestation_data: AttestationData,
participation_bitfield: seq[byte]): seq[Uint24] =
@ -356,6 +363,18 @@ func update_validator_registry*(state: var BeaconState) =
get_effective_balance(state, index.Uint24) *
min(total_penalties * 3, total_balance) div total_balance
# Perform additional updates
state.previous_epoch_calculation_slot = state.current_epoch_calculation_slot
state.previous_epoch_start_shard = state.current_epoch_start_shard
state.previous_epoch_randao_mix = state.current_epoch_randao_mix
state.current_epoch_calculation_slot = state.slot
state.current_epoch_start_shard = (state.current_epoch_start_shard + get_current_epoch_committee_count_per_slot(state) * EPOCH_LENGTH) mod SHARD_COUNT
state.current_epoch_randao_mix = get_randao_mix(state, state.current_epoch_calculation_slot - SEED_LOOKAHEAD)
# TODO "If a validator registry update does not happen do the following: ..."
#process_penalties_and_exits(state)
proc checkAttestation*(
state: BeaconState, attestation: Attestation, flags: UpdateFlags): bool =
## Check that an attestation follows the rules of being included in the state

View File

@ -135,6 +135,12 @@ const
ZERO_BALANCE_VALIDATOR_TTL* = 2'u64^22 ##\
## slots (~291 days)
DEPOSIT_ROOT_VOTING_PERIOD* = 2'u64^10 ##\
## slots (~1.7 hours)
MIN_VALIDATOR_WITHDRAWAL_TIME* = 2'u64^14 ##\
## slots (~27 hours)
# Quotients
BASE_REWARD_QUOTIENT* = 2'u64^10 ##\
## The `BASE_REWARD_QUOTIENT` parameter dictates the per-epoch reward. It
@ -414,7 +420,7 @@ type
## When
DepositRootVote* = object
deposit_root*: Eth2Digest # Candidate PoW receipt root
deposit_root*: Eth2Digest
vote_count*: uint64 # Vote count
PendingAttestationRecord* = object

View File

@ -180,3 +180,29 @@ proc is_surround_vote*(attestation_data_1: AttestationData,
(attestation_data_1.justified_slot + 1 == attestation_data_2.slot) and
(attestation_data_2.slot < attestation_data_1.slot)
)
#func is_active_validator*(validator: ValidatorRecord, slot: uint64): bool =
# ### Checks if validator is active
# validator.activation_slot <= slot and slot < validator.exit_slot
func is_active_validator*(validator: ValidatorRecord): bool =
validator.status in {ACTIVE, ACTIVE_PENDING_EXIT}
func get_active_validator_indices*(validators: openArray[ValidatorRecord], slot: uint64): seq[Uint24] =
## Gets indices of active validators from validators
for idx, val in validators:
#if is_active_validator(val, slot):
if is_active_validator(val):
result.add idx.Uint24
func get_committee_count_per_slot*(active_validator_count: int): uint64 =
clamp(
active_validator_count div EPOCH_LENGTH div TARGET_COMMITTEE_SIZE,
1, SHARD_COUNT div EPOCH_LENGTH).uint64
func get_current_epoch_committee_count_per_slot*(state: BeaconState): uint64 =
let current_active_validators = get_active_validator_indices(
state.validator_registry,
state.current_epoch_calculation_slot,
)
return get_committee_count_per_slot(len(current_active_validators))

View File

@ -12,9 +12,6 @@ import
../ssz,
./crypto, ./datatypes, ./digest, ./helpers
func is_active_validator*(validator: ValidatorRecord): bool =
validator.status in {ACTIVE, ACTIVE_PENDING_EXIT}
func min_empty_validator_index*(
validators: seq[ValidatorRecord],
validator_balances: seq[uint64],