split out get_previous_epoch into helper function

This commit is contained in:
Dustin Brody 2019-02-11 16:10:46 +01:00
parent d44999abc5
commit b03ac3427f
4 changed files with 38 additions and 26 deletions

View File

@ -315,7 +315,7 @@ func update_validator_registry*(state: var BeaconState) =
process_penalties_and_exits(state)
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#get_epoch_start_slot
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#get_epoch_start_slot
func get_epoch_start_slot*(epoch: EpochNumber): SlotNumber =
# Return the starting slot of the given ``epoch``.
epoch * EPOCH_LENGTH

View File

@ -274,7 +274,7 @@ type
# Validator signature
signature*: ValidatorSig
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#beaconblock
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#beaconblock
BeaconBlock* = object
## For each slot, a proposer is chosen from the validator pool to propose
## a new block. Once the block as been proposed, it is transmitted to
@ -299,7 +299,7 @@ type
body*: BeaconBlockBody
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#beaconblockbody
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#beaconblockbody
BeaconBlockBody* = object
proposer_slashings*: seq[ProposerSlashing]
attester_slashings*: seq[AttesterSlashing]
@ -307,14 +307,14 @@ type
deposits*: seq[Deposit]
exits*: seq[Exit]
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#proposalsigneddata
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#proposalsigneddata
ProposalSignedData* = object
slot*: uint64
shard*: uint64 ##\
## Shard number (or `BEACON_CHAIN_SHARD_NUMBER` for beacon chain)
block_root*: Eth2Digest
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#beaconstate
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#beaconstate
BeaconState* = object
slot*: uint64
genesis_time*: uint64
@ -328,7 +328,7 @@ type
validator_registry_update_epoch*: uint64
# TODO remove, not in spec anymore
# TODO remove or conditionally compile; not in spec anymore
validator_registry_delta_chain_tip*: Eth2Digest ##\
## For light clients to easily track delta
@ -336,16 +336,16 @@ type
latest_randao_mixes*: array[LATEST_BLOCK_ROOTS_LENGTH.int, Eth2Digest]
previous_epoch_start_shard*: uint64
current_epoch_start_shard*: uint64
previous_calculation_epoch*: EpochNumber
current_calculation_epoch*: EpochNumber
previous_calculation_epoch*: uint64
current_calculation_epoch*: uint64
previous_epoch_seed*: Eth2Digest
current_epoch_seed*: Eth2Digest
# Finality
previous_justified_epoch*: EpochNumber
justified_epoch*: EpochNumber
previous_justified_epoch*: uint64
justified_epoch*: uint64
justification_bitfield*: uint64
finalized_epoch*: EpochNumber
finalized_epoch*: uint64
# Recent state
latest_crosslinks*: array[SHARD_COUNT, Crosslink]
@ -359,10 +359,11 @@ type
latest_attestations*: seq[PendingAttestation]
batched_block_roots*: seq[Eth2Digest]
# Ethereum 1.0 chain data
latest_eth1_data*: Eth1Data
eth1_data_votes*: seq[Eth1DataVote]
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#validator
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#validator
Validator* = object
pubkey*: ValidatorPubKey ##\
## BLS public key
@ -370,40 +371,40 @@ type
withdrawal_credentials*: Eth2Digest ##\
## Withdrawal credentials
activation_epoch*: EpochNumber ##\
activation_epoch*: uint64 ##\
## Epoch when validator activated
exit_epoch*: EpochNumber ##\
exit_epoch*: uint64 ##\
## Epoch when validator exited
withdrawal_epoch*: EpochNumber ##\
withdrawal_epoch*: uint64 ##\
## Epoch when validator withdrew
penalized_epoch*: EpochNumber ##\
penalized_epoch*: uint64 ##\
## Epoch when validator penalized
status_flags*: uint64
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#crosslink
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#crosslink
Crosslink* = object
epoch*: uint64
shard_block_root*: Eth2Digest ##\
## Shard chain block root
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#pendingattestation
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#pendingattestation
PendingAttestation* = object
aggregation_bitfield*: seq[byte] # Attester participation bitfield
data*: AttestationData # Attestation data
custody_bitfield*: seq[byte] # Custody bitfield
inclusion_slot*: uint64 # Inclusion slot
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#fork
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#fork
Fork* = object
previous_version*: uint64 # Previous fork version
current_version*: uint64 # Current fork version
epoch*: uint64 # Fork epoch number
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#eth1data
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#eth1data
Eth1Data* = object
deposit_root*: Eth2Digest ##\
## Data being voted for
@ -411,7 +412,7 @@ type
block_hash*: Eth2Digest ##\
## Block hash
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#eth1datavote
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#eth1datavote
Eth1DataVote* = object
eth1_data*: Eth1Data
vote_count*: uint64 # Vote count

View File

@ -155,7 +155,7 @@ func merkle_root*(values: openArray[Eth2Digest]): Eth2Digest =
#TODO
discard
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#slot_to_epoch
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#slot_to_epoch
func slot_to_epoch*(slot: SlotNumber): EpochNumber =
slot div EPOCH_LENGTH
@ -181,12 +181,12 @@ func is_surround_vote*(attestation_data_1: AttestationData,
source_epoch_1 < source_epoch_2 and target_epoch_2 < target_epoch_1
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#is_active_validator
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#is_active_validator
func is_active_validator*(validator: Validator, epoch: EpochNumber): bool =
### Checks if validator is active
validator.activation_epoch <= epoch and epoch < validator.exit_epoch
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#get_active_validator_indices
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#get_active_validator_indices
func get_active_validator_indices*(validators: openArray[Validator], epoch: EpochNumber): seq[ValidatorIndex] =
## Gets indices of active validators from validators
for idx, val in validators:
@ -206,8 +206,9 @@ func get_current_epoch_committee_count*(state: BeaconState): uint64 =
)
return get_epoch_committee_count(len(current_active_validators))
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#get_current_epoch
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#get_current_epoch
func get_current_epoch*(state: BeaconState): EpochNumber =
# Return the current epoch of the given ``state``.
slot_to_epoch(state.slot)
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#get_randao_mix

View File

@ -76,6 +76,16 @@ func get_next_epoch_committee_count(state: BeaconState): uint64 =
)
get_epoch_committee_count(len(next_active_validators))
# https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#get_previous_epoch
func get_previous_epoch(state: BeaconState): EpochNumber =
## Return the previous epoch of the given ``state``.
## If the current epoch is ``GENESIS_EPOCH``, return ``GENESIS_EPOCH``.
let current_epoch = get_current_epoch(state)
if current_epoch == GENESIS_EPOCH:
GENESIS_EPOCH
else:
current_epoch - 1
# https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#get_crosslink_committees_at_slot
func get_crosslink_committees_at_slot*(state: BeaconState, slot: uint64,
registry_change: bool = false):
@ -88,7 +98,7 @@ func get_crosslink_committees_at_slot*(state: BeaconState, slot: uint64,
let
epoch = slot_to_epoch(slot)
current_epoch = get_current_epoch(state)
previous_epoch = if current_epoch > GENESIS_EPOCH: (current_epoch - 1) else: current_epoch
previous_epoch = get_previous_epoch(state)
next_epoch = current_epoch + 1
assert previous_epoch <= epoch