2019-01-09 23:49:50 +00:00
|
|
|
// Package epoch contains epoch processing libraries. These libraries
|
|
|
|
// process new balance for the validators, justify and finalize new
|
|
|
|
// check points, shuffle and reassign validators to different slots and
|
|
|
|
// shards.
|
2018-12-24 06:36:09 +00:00
|
|
|
package epoch
|
|
|
|
|
|
|
|
import (
|
2019-02-26 03:42:31 +00:00
|
|
|
"encoding/binary"
|
2018-12-24 14:58:18 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-02-26 03:42:31 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
|
|
|
|
2019-02-01 21:22:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2018-12-29 17:24:11 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
|
2018-12-24 06:36:09 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2018-12-31 18:24:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/mathutil"
|
2018-12-24 06:36:09 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-02-25 02:09:45 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-12-24 06:36:09 +00:00
|
|
|
)
|
|
|
|
|
2019-02-25 02:09:45 +00:00
|
|
|
var log = logrus.WithField("prefix", "core/state")
|
|
|
|
|
2018-12-24 06:36:09 +00:00
|
|
|
// CanProcessEpoch checks the eligibility to process epoch.
|
2019-02-25 02:09:45 +00:00
|
|
|
// The epoch can be processed at the end of the last slot of every epoch
|
2018-12-24 06:36:09 +00:00
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
2019-02-25 02:09:45 +00:00
|
|
|
// If (state.slot + 1) % SLOTS_PER_EPOCH == 0:
|
2018-12-24 06:36:09 +00:00
|
|
|
func CanProcessEpoch(state *pb.BeaconState) bool {
|
2019-02-25 02:09:45 +00:00
|
|
|
return (state.Slot+1)%params.BeaconConfig().SlotsPerEpoch == 0
|
2018-12-24 06:36:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-02 21:24:42 +00:00
|
|
|
// CanProcessEth1Data checks the eligibility to process the eth1 data.
|
2019-02-18 16:52:16 +00:00
|
|
|
// The eth1 data can be processed every EPOCHS_PER_ETH1_VOTING_PERIOD.
|
2018-12-24 06:36:09 +00:00
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
2019-02-18 16:52:16 +00:00
|
|
|
// If next_epoch % EPOCHS_PER_ETH1_VOTING_PERIOD == 0
|
2019-02-02 21:24:42 +00:00
|
|
|
func CanProcessEth1Data(state *pb.BeaconState) bool {
|
2019-02-09 20:15:59 +00:00
|
|
|
return helpers.NextEpoch(state)%
|
2019-02-18 16:52:16 +00:00
|
|
|
params.BeaconConfig().EpochsPerEth1VotingPeriod == 0
|
2018-12-24 06:36:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-29 18:21:34 +00:00
|
|
|
// CanProcessValidatorRegistry checks the eligibility to process validator registry.
|
2019-01-29 13:56:32 +00:00
|
|
|
// It checks crosslink committees last changed slot and finalized slot against
|
2018-12-29 18:21:34 +00:00
|
|
|
// latest change slot.
|
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
|
|
|
// If the following are satisfied:
|
2019-02-06 16:44:27 +00:00
|
|
|
// * state.finalized_epoch > state.validator_registry_latest_change_epoch
|
2019-02-06 14:00:50 +00:00
|
|
|
// * state.latest_crosslinks[shard].epoch > state.validator_registry_update_epoch
|
2019-01-29 13:56:32 +00:00
|
|
|
// for every shard number shard in [(state.current_epoch_start_shard + i) %
|
2019-02-06 14:00:50 +00:00
|
|
|
// SHARD_COUNT for i in range(get_current_epoch_committee_count(state) *
|
2019-02-18 16:52:16 +00:00
|
|
|
// SLOTS_PER_EPOCH)] (that is, for every shard in the current committees)
|
2018-12-29 18:21:34 +00:00
|
|
|
func CanProcessValidatorRegistry(state *pb.BeaconState) bool {
|
2019-02-06 16:44:27 +00:00
|
|
|
if state.FinalizedEpoch <= state.ValidatorRegistryUpdateEpoch {
|
2018-12-29 18:21:34 +00:00
|
|
|
return false
|
|
|
|
}
|
2019-02-18 16:52:16 +00:00
|
|
|
shardsProcessed := helpers.CurrentEpochCommitteeCount(state) * params.BeaconConfig().SlotsPerEpoch
|
|
|
|
startShard := state.CurrentShufflingStartShard
|
2019-01-29 13:56:32 +00:00
|
|
|
for i := startShard; i < shardsProcessed; i++ {
|
2019-02-06 02:18:55 +00:00
|
|
|
|
2019-02-07 06:07:25 +00:00
|
|
|
if state.LatestCrosslinks[i%params.BeaconConfig().ShardCount].Epoch <=
|
2019-02-05 20:14:29 +00:00
|
|
|
state.ValidatorRegistryUpdateEpoch {
|
2019-01-29 13:56:32 +00:00
|
|
|
return false
|
2018-12-29 18:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-02-02 21:24:42 +00:00
|
|
|
// ProcessEth1Data processes eth1 block deposit roots by checking its vote count.
|
2019-02-18 16:52:16 +00:00
|
|
|
// With sufficient votes (>2*EPOCHS_PER_ETH1_VOTING_PERIOD), it then
|
2019-02-02 21:24:42 +00:00
|
|
|
// marks the voted Eth1 data as the latest data set.
|
|
|
|
//
|
|
|
|
// Official spec definition:
|
2019-02-18 16:52:16 +00:00
|
|
|
// if eth1_data_vote.vote_count * 2 > EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH for
|
2019-02-02 21:24:42 +00:00
|
|
|
// some eth1_data_vote in state.eth1_data_votes.
|
2019-02-09 20:15:59 +00:00
|
|
|
// (ie. more than half the votes in this voting period were for that value)
|
|
|
|
// Set state.latest_eth1_data = eth1_data_vote.eth1_data.
|
|
|
|
// Set state.eth1_data_votes = [].
|
2019-02-02 21:24:42 +00:00
|
|
|
//
|
|
|
|
func ProcessEth1Data(state *pb.BeaconState) *pb.BeaconState {
|
2019-03-01 02:47:32 +00:00
|
|
|
for _, eth1DataVote := range state.Eth1DataVotes {
|
|
|
|
if eth1DataVote.VoteCount*2 > params.BeaconConfig().SlotsPerEpoch*
|
|
|
|
params.BeaconConfig().EpochsPerEth1VotingPeriod {
|
|
|
|
state.LatestEth1Data = eth1DataVote.Eth1Data
|
2018-12-24 06:36:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-01 02:47:32 +00:00
|
|
|
state.Eth1DataVotes = make([]*pb.Eth1DataVote, 0)
|
2018-12-24 06:36:09 +00:00
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessJustification processes for justified slot by comparing
|
|
|
|
// epoch boundary balance and total balance.
|
2019-02-25 21:16:12 +00:00
|
|
|
// First, update the justification bitfield:
|
|
|
|
// Let new_justified_epoch = state.justified_epoch.
|
|
|
|
// Set state.justification_bitfield = state.justification_bitfield << 1.
|
|
|
|
// Set state.justification_bitfield |= 2 and new_justified_epoch = previous_epoch if
|
|
|
|
// 3 * previous_epoch_boundary_attesting_balance >= 2 * previous_total_balance.
|
|
|
|
// Set state.justification_bitfield |= 1 and new_justified_epoch = current_epoch if
|
|
|
|
// 3 * current_epoch_boundary_attesting_balance >= 2 * current_total_balance.
|
|
|
|
// Next, update last finalized epoch if possible:
|
|
|
|
// Set state.finalized_epoch = state.previous_justified_epoch if (state.justification_bitfield >> 1) % 8
|
|
|
|
// == 0b111 and state.previous_justified_epoch == previous_epoch - 2.
|
|
|
|
// Set state.finalized_epoch = state.previous_justified_epoch if (state.justification_bitfield >> 1) % 4
|
|
|
|
// == 0b11 and state.previous_justified_epoch == previous_epoch - 1.
|
|
|
|
// Set state.finalized_epoch = state.justified_epoch if (state.justification_bitfield >> 0) % 8
|
|
|
|
// == 0b111 and state.justified_epoch == previous_epoch - 1.
|
|
|
|
// Set state.finalized_epoch = state.justified_epoch if (state.justification_bitfield >> 0) % 4
|
|
|
|
// == 0b11 and state.justified_epoch == previous_epoch.
|
|
|
|
// Finally, update the following:
|
|
|
|
// Set state.previous_justified_epoch = state.justified_epoch.
|
|
|
|
// Set state.justified_epoch = new_justified_epoch
|
2018-12-24 06:36:09 +00:00
|
|
|
func ProcessJustification(
|
|
|
|
state *pb.BeaconState,
|
|
|
|
thisEpochBoundaryAttestingBalance uint64,
|
|
|
|
prevEpochBoundaryAttestingBalance uint64,
|
2019-02-25 21:16:12 +00:00
|
|
|
prevTotalBalance uint64,
|
2018-12-24 06:36:09 +00:00
|
|
|
totalBalance uint64) *pb.BeaconState {
|
2019-02-25 21:16:12 +00:00
|
|
|
newJustifiedEpoch := state.JustifiedEpoch
|
|
|
|
prevEpoch := helpers.PrevEpoch(state)
|
|
|
|
currentEpoch := helpers.CurrentEpoch(state)
|
2018-12-24 06:36:09 +00:00
|
|
|
// Shifts all the bits over one to create a new bit for the recent epoch.
|
2019-02-25 21:16:12 +00:00
|
|
|
state.JustificationBitfield = state.JustificationBitfield << 1
|
2019-03-01 02:47:32 +00:00
|
|
|
log.Infof("Processing Total Balance: %d", totalBalance)
|
2018-12-24 06:36:09 +00:00
|
|
|
// If prev prev epoch was justified then we ensure the 2nd bit in the bitfield is set,
|
2019-02-18 16:52:16 +00:00
|
|
|
// assign new justified slot to 2 * SLOTS_PER_EPOCH before.
|
2019-03-01 02:47:32 +00:00
|
|
|
log.Infof("Previous Epoch Boundary Attesting Balance: %d", prevEpochBoundaryAttestingBalance)
|
2019-02-25 21:16:12 +00:00
|
|
|
if 3*prevEpochBoundaryAttestingBalance >= 2*prevTotalBalance {
|
2018-12-24 06:36:09 +00:00
|
|
|
state.JustificationBitfield |= 2
|
2019-02-25 21:16:12 +00:00
|
|
|
newJustifiedEpoch = prevEpoch
|
2019-03-01 02:47:32 +00:00
|
|
|
log.Infof("Previous epoch %d was justified", newJustifiedEpoch-params.BeaconConfig().GenesisEpoch)
|
2018-12-24 06:36:09 +00:00
|
|
|
}
|
2019-03-01 02:47:32 +00:00
|
|
|
log.Infof("Current Epoch Boundary Attesting Balance: %d", thisEpochBoundaryAttestingBalance)
|
2018-12-24 06:36:09 +00:00
|
|
|
// If this epoch was justified then we ensure the 1st bit in the bitfield is set,
|
2019-02-18 16:52:16 +00:00
|
|
|
// assign new justified slot to 1 * SLOTS_PER_EPOCH before.
|
2018-12-24 06:36:09 +00:00
|
|
|
if 3*thisEpochBoundaryAttestingBalance >= 2*totalBalance {
|
|
|
|
state.JustificationBitfield |= 1
|
2019-02-25 21:16:12 +00:00
|
|
|
newJustifiedEpoch = currentEpoch
|
|
|
|
log.Infof("Current epoch %d was justified", newJustifiedEpoch-params.BeaconConfig().GenesisEpoch)
|
2018-12-24 06:36:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 21:16:12 +00:00
|
|
|
// Process finality.
|
|
|
|
if state.PreviousJustifiedEpoch == prevEpoch-2 &&
|
|
|
|
(state.JustificationBitfield>>1)%8 == 7 {
|
|
|
|
state.FinalizedEpoch = state.PreviousJustifiedEpoch
|
2019-02-25 02:09:45 +00:00
|
|
|
log.Infof("New Finalized Epoch: %d", state.FinalizedEpoch-params.BeaconConfig().GenesisEpoch)
|
2018-12-24 06:36:09 +00:00
|
|
|
}
|
2019-02-25 21:16:12 +00:00
|
|
|
if state.PreviousJustifiedEpoch == prevEpoch-1 &&
|
|
|
|
(state.JustificationBitfield>>1)%4 == 3 {
|
|
|
|
state.FinalizedEpoch = state.PreviousJustifiedEpoch
|
|
|
|
log.Infof("New Finalized Epoch: %d", state.FinalizedEpoch-params.BeaconConfig().GenesisEpoch)
|
|
|
|
}
|
|
|
|
if state.JustifiedEpoch == prevEpoch-1 &&
|
|
|
|
(state.JustificationBitfield>>0)%8 == 7 {
|
2019-02-06 16:44:27 +00:00
|
|
|
state.FinalizedEpoch = state.JustifiedEpoch
|
2019-02-25 02:09:45 +00:00
|
|
|
log.Infof("New Finalized Epoch: %d", state.FinalizedEpoch-params.BeaconConfig().GenesisEpoch)
|
2018-12-24 06:36:09 +00:00
|
|
|
}
|
2019-02-25 21:16:12 +00:00
|
|
|
if state.JustifiedEpoch == prevEpoch &&
|
|
|
|
(state.JustificationBitfield>>0)%4 == 3 {
|
2019-02-06 16:44:27 +00:00
|
|
|
state.FinalizedEpoch = state.JustifiedEpoch
|
2019-02-25 02:09:45 +00:00
|
|
|
log.Infof("New Finalized Epoch: %d", state.FinalizedEpoch-params.BeaconConfig().GenesisEpoch)
|
2018-12-24 06:36:09 +00:00
|
|
|
}
|
2019-02-25 21:16:12 +00:00
|
|
|
state.PreviousJustifiedEpoch = state.JustifiedEpoch
|
|
|
|
state.JustifiedEpoch = newJustifiedEpoch
|
2018-12-24 06:36:09 +00:00
|
|
|
return state
|
|
|
|
}
|
2018-12-24 14:58:18 +00:00
|
|
|
|
2019-01-25 16:20:02 +00:00
|
|
|
// ProcessCrosslinks goes through each crosslink committee and check
|
|
|
|
// crosslink committee's attested balance * 3 is greater than total balance *2.
|
|
|
|
// If it's greater then beacon node updates crosslink committee with
|
2019-02-06 02:18:55 +00:00
|
|
|
// the state epoch and wining root.
|
2018-12-24 14:58:18 +00:00
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
2019-02-06 02:18:55 +00:00
|
|
|
// For every slot in range(get_epoch_start_slot(previous_epoch), get_epoch_start_slot(next_epoch)),
|
2019-01-25 16:20:02 +00:00
|
|
|
// let `crosslink_committees_at_slot = get_crosslink_committees_at_slot(state, slot)`.
|
|
|
|
// For every `(crosslink_committee, shard)` in `crosslink_committees_at_slot`, compute:
|
|
|
|
// Set state.latest_crosslinks[shard] = Crosslink(
|
2019-03-01 02:47:32 +00:00
|
|
|
// epoch=slot_to_epoch(slot), crosslink_data_root=winning_root(crosslink_committee))
|
2019-01-25 16:20:02 +00:00
|
|
|
// if 3 * total_attesting_balance(crosslink_committee) >= 2 * total_balance(crosslink_committee)
|
2018-12-24 14:58:18 +00:00
|
|
|
func ProcessCrosslinks(
|
|
|
|
state *pb.BeaconState,
|
2019-02-12 18:04:30 +00:00
|
|
|
thisEpochAttestations []*pb.PendingAttestation,
|
|
|
|
prevEpochAttestations []*pb.PendingAttestation) (*pb.BeaconState, error) {
|
2018-12-24 14:58:18 +00:00
|
|
|
|
2019-02-06 02:18:55 +00:00
|
|
|
prevEpoch := helpers.PrevEpoch(state)
|
2019-02-06 14:00:50 +00:00
|
|
|
currentEpoch := helpers.CurrentEpoch(state)
|
2019-02-06 02:18:55 +00:00
|
|
|
nextEpoch := helpers.NextEpoch(state)
|
|
|
|
startSlot := helpers.StartSlot(prevEpoch)
|
|
|
|
endSlot := helpers.StartSlot(nextEpoch)
|
2019-01-25 16:20:02 +00:00
|
|
|
|
2019-02-06 02:18:55 +00:00
|
|
|
for i := startSlot; i < endSlot; i++ {
|
2019-02-28 15:27:43 +00:00
|
|
|
// RegistryChange is a no-op when requesting slot in current and previous epoch.
|
|
|
|
// ProcessCrosslinks will never ask for slot in next epoch.
|
|
|
|
crosslinkCommittees, err := helpers.CrosslinkCommitteesAtSlot(state, i, false /* registryChange */)
|
2019-01-25 16:20:02 +00:00
|
|
|
if err != nil {
|
2019-02-25 02:09:45 +00:00
|
|
|
return nil, fmt.Errorf("could not get committees for slot %d: %v", i-params.BeaconConfig().GenesisSlot, err)
|
2019-01-25 16:20:02 +00:00
|
|
|
}
|
|
|
|
for _, crosslinkCommittee := range crosslinkCommittees {
|
|
|
|
shard := crosslinkCommittee.Shard
|
|
|
|
committee := crosslinkCommittee.Committee
|
|
|
|
attestingBalance, err := TotalAttestingBalance(state, shard, thisEpochAttestations, prevEpochAttestations)
|
2018-12-24 14:58:18 +00:00
|
|
|
if err != nil {
|
2019-01-25 16:20:02 +00:00
|
|
|
return nil, fmt.Errorf("could not get attesting balance for shard committee %d: %v", shard, err)
|
2018-12-24 14:58:18 +00:00
|
|
|
}
|
2019-01-25 16:20:02 +00:00
|
|
|
totalBalance := TotalBalance(state, committee)
|
2019-03-01 02:47:32 +00:00
|
|
|
if attestingBalance*3 >= totalBalance*2 {
|
2019-01-25 16:20:02 +00:00
|
|
|
winningRoot, err := winningRoot(state, shard, thisEpochAttestations, prevEpochAttestations)
|
2018-12-24 14:58:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get winning root: %v", err)
|
|
|
|
}
|
2019-02-10 17:59:17 +00:00
|
|
|
state.LatestCrosslinks[shard] = &pb.Crosslink{
|
2019-02-27 23:07:39 +00:00
|
|
|
Epoch: currentEpoch,
|
|
|
|
CrosslinkDataRootHash32: winningRoot,
|
2018-12-24 14:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|
2018-12-29 17:24:11 +00:00
|
|
|
|
|
|
|
// ProcessEjections iterates through every validator and find the ones below
|
|
|
|
// ejection balance and eject them.
|
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
|
|
|
// def process_ejections(state: BeaconState) -> None:
|
|
|
|
// """
|
|
|
|
// Iterate through the validator registry
|
|
|
|
// and eject active validators with balance below ``EJECTION_BALANCE``.
|
|
|
|
// """
|
2019-02-09 20:15:59 +00:00
|
|
|
// for index in get_active_validator_indices(state.validator_registry, current_epoch(state)):
|
2018-12-29 17:24:11 +00:00
|
|
|
// if state.validator_balances[index] < EJECTION_BALANCE:
|
2019-01-05 06:39:07 +00:00
|
|
|
// exit_validator(state, index)
|
2018-12-29 17:24:11 +00:00
|
|
|
func ProcessEjections(state *pb.BeaconState) (*pb.BeaconState, error) {
|
2019-02-09 20:15:59 +00:00
|
|
|
activeValidatorIndices := helpers.ActiveValidatorIndices(state.ValidatorRegistry, helpers.CurrentEpoch(state))
|
2018-12-29 17:24:11 +00:00
|
|
|
for _, index := range activeValidatorIndices {
|
2019-02-07 06:07:25 +00:00
|
|
|
if state.ValidatorBalances[index] < params.BeaconConfig().EjectionBalance {
|
2019-02-25 02:09:45 +00:00
|
|
|
log.Infof("Validator at index %d EJECTED", index)
|
|
|
|
state = validators.ExitValidator(state, index)
|
2018-12-29 17:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return state, nil
|
|
|
|
}
|
2018-12-31 18:24:52 +00:00
|
|
|
|
2019-02-01 21:22:52 +00:00
|
|
|
// ProcessPrevSlotShardSeed computes and sets current epoch's calculation slot
|
2019-01-24 07:09:52 +00:00
|
|
|
// and start shard to previous epoch. Then it returns the updated state.
|
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
|
|
|
// Set state.previous_epoch_randao_mix = state.current_epoch_randao_mix
|
2019-03-01 02:47:32 +00:00
|
|
|
// Set state.previous_shuffling_start_shard = state.current_shuffling_start_shard
|
2019-02-27 20:21:15 +00:00
|
|
|
// Set state.previous_shuffling_seed = state.current_shuffling_seed.
|
2019-02-01 21:22:52 +00:00
|
|
|
func ProcessPrevSlotShardSeed(state *pb.BeaconState) *pb.BeaconState {
|
2019-02-18 16:52:16 +00:00
|
|
|
state.PreviousShufflingEpoch = state.CurrentShufflingEpoch
|
|
|
|
state.PreviousShufflingStartShard = state.CurrentShufflingStartShard
|
|
|
|
state.PreviousShufflingSeedHash32 = state.CurrentShufflingSeedHash32
|
2019-01-24 07:09:52 +00:00
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2019-02-26 00:37:55 +00:00
|
|
|
// ProcessCurrSlotShardSeed sets the current shuffling information in the beacon state.
|
|
|
|
// Set state.current_shuffling_start_shard = (state.current_shuffling_start_shard +
|
|
|
|
// get_current_epoch_committee_count(state)) % SHARD_COUNT
|
2019-02-27 19:36:37 +00:00
|
|
|
// Set state.current_shuffling_epoch = next_epoch
|
2019-02-26 00:37:55 +00:00
|
|
|
// Set state.current_shuffling_seed = generate_seed(state, state.current_shuffling_epoch)
|
|
|
|
func ProcessCurrSlotShardSeed(state *pb.BeaconState) (*pb.BeaconState, error) {
|
|
|
|
state.CurrentShufflingStartShard = (state.CurrentShufflingStartShard +
|
|
|
|
helpers.CurrentEpochCommitteeCount(state)) % params.BeaconConfig().ShardCount
|
|
|
|
seed, err := helpers.GenerateSeed(state, state.CurrentShufflingEpoch)
|
2018-12-31 18:24:52 +00:00
|
|
|
if err != nil {
|
2019-02-26 00:37:55 +00:00
|
|
|
return nil, fmt.Errorf("could not update current shuffling seed: %v", err)
|
2018-12-31 18:24:52 +00:00
|
|
|
}
|
2019-02-27 19:36:37 +00:00
|
|
|
state.CurrentShufflingEpoch = helpers.NextEpoch(state)
|
2019-02-26 00:37:55 +00:00
|
|
|
state.CurrentShufflingSeedHash32 = seed[:]
|
2018-12-31 18:24:52 +00:00
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessPartialValidatorRegistry processes the portion of validator registry
|
|
|
|
// fields, it doesn't set registry latest change slot. This only gets called if
|
|
|
|
// validator registry update did not happen.
|
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
2019-02-09 20:15:59 +00:00
|
|
|
// Let epochs_since_last_registry_change = current_epoch -
|
|
|
|
// state.validator_registry_update_epoch
|
|
|
|
// If epochs_since_last_registry_update > 1 and
|
2019-03-01 02:47:32 +00:00
|
|
|
// is_power_of_two(epochs_since_last_registry_update):
|
2019-02-09 20:15:59 +00:00
|
|
|
// set state.current_calculation_epoch = next_epoch
|
2019-02-27 20:21:15 +00:00
|
|
|
// set state.current_shuffling_seed = generate_seed(
|
2019-02-09 20:15:59 +00:00
|
|
|
// state, state.current_calculation_epoch)
|
|
|
|
func ProcessPartialValidatorRegistry(state *pb.BeaconState) (*pb.BeaconState, error) {
|
|
|
|
epochsSinceLastRegistryChange := helpers.CurrentEpoch(state) -
|
|
|
|
state.ValidatorRegistryUpdateEpoch
|
|
|
|
if epochsSinceLastRegistryChange > 1 &&
|
|
|
|
mathutil.IsPowerOf2(epochsSinceLastRegistryChange) {
|
2019-02-18 16:52:16 +00:00
|
|
|
state.CurrentShufflingEpoch = helpers.NextEpoch(state)
|
|
|
|
seed, err := helpers.GenerateSeed(state, state.CurrentShufflingEpoch)
|
2019-02-09 20:15:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not generate seed: %v", err)
|
2018-12-31 18:24:52 +00:00
|
|
|
}
|
2019-02-18 16:52:16 +00:00
|
|
|
state.CurrentShufflingSeedHash32 = seed[:]
|
2018-12-31 18:24:52 +00:00
|
|
|
}
|
2019-02-09 20:15:59 +00:00
|
|
|
return state, nil
|
2018-12-31 18:24:52 +00:00
|
|
|
}
|
2019-01-02 20:19:08 +00:00
|
|
|
|
|
|
|
// CleanupAttestations removes any attestation in state's latest attestations
|
|
|
|
// such that the attestation slot is lower than state slot minus epoch length.
|
|
|
|
// Spec pseudocode definition:
|
|
|
|
// Remove any attestation in state.latest_attestations such
|
2019-02-06 16:44:27 +00:00
|
|
|
// that slot_to_epoch(att.data.slot) < slot_to_epoch(state) - 1
|
2019-01-02 20:19:08 +00:00
|
|
|
func CleanupAttestations(state *pb.BeaconState) *pb.BeaconState {
|
2019-02-01 16:52:35 +00:00
|
|
|
currEpoch := helpers.CurrentEpoch(state)
|
2019-01-02 20:19:08 +00:00
|
|
|
|
2019-02-12 18:04:30 +00:00
|
|
|
var latestAttestations []*pb.PendingAttestation
|
2019-01-02 20:19:08 +00:00
|
|
|
for _, attestation := range state.LatestAttestations {
|
2019-02-01 16:52:35 +00:00
|
|
|
if helpers.SlotToEpoch(attestation.Data.Slot) >= currEpoch {
|
2019-01-02 20:19:08 +00:00
|
|
|
latestAttestations = append(latestAttestations, attestation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state.LatestAttestations = latestAttestations
|
|
|
|
return state
|
|
|
|
}
|
2019-01-17 01:10:42 +00:00
|
|
|
|
2019-02-18 16:52:16 +00:00
|
|
|
// UpdateLatestActiveIndexRoots updates the latest index roots. Index root
|
2019-02-09 21:14:10 +00:00
|
|
|
// is computed by hashing validator indices of the next epoch + delay.
|
2019-01-17 01:10:42 +00:00
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
2019-02-18 16:52:16 +00:00
|
|
|
// Let e = state.slot // SLOTS_PER_EPOCH.
|
|
|
|
// Set state.latest_index_roots[(next_epoch + ACTIVATION_EXIT_DELAY) %
|
2019-02-09 21:14:10 +00:00
|
|
|
// LATEST_INDEX_ROOTS_LENGTH] =
|
|
|
|
// hash_tree_root(get_active_validator_indices(state,
|
2019-02-18 16:52:16 +00:00
|
|
|
// next_epoch + ACTIVATION_EXIT_DELAY))
|
|
|
|
func UpdateLatestActiveIndexRoots(state *pb.BeaconState) (*pb.BeaconState, error) {
|
|
|
|
nextEpoch := helpers.NextEpoch(state) + params.BeaconConfig().ActivationExitDelay
|
2019-02-09 21:14:10 +00:00
|
|
|
validatorIndices := helpers.ActiveValidatorIndices(state.ValidatorRegistry, nextEpoch)
|
2019-02-26 03:42:31 +00:00
|
|
|
indicesBytes := []byte{}
|
|
|
|
for _, val := range validatorIndices {
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
binary.LittleEndian.PutUint64(buf, val)
|
|
|
|
indicesBytes = append(indicesBytes, buf...)
|
2019-02-09 21:14:10 +00:00
|
|
|
}
|
2019-02-26 03:42:31 +00:00
|
|
|
indexRoot := hashutil.Hash(indicesBytes)
|
2019-02-18 16:52:16 +00:00
|
|
|
state.LatestIndexRootHash32S[nextEpoch%params.BeaconConfig().LatestActiveIndexRootsLength] =
|
2019-02-09 21:14:10 +00:00
|
|
|
indexRoot[:]
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:52:16 +00:00
|
|
|
// UpdateLatestSlashedBalances updates the latest slashed balances. It transfers
|
2019-02-09 21:14:10 +00:00
|
|
|
// the amount from the current epoch index to next epoch index.
|
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
2019-02-18 16:52:16 +00:00
|
|
|
// Set state.latest_slashed_balances[(next_epoch) % LATEST_PENALIZED_EXIT_LENGTH] =
|
|
|
|
// state.latest_slashed_balances[current_epoch % LATEST_PENALIZED_EXIT_LENGTH].
|
|
|
|
func UpdateLatestSlashedBalances(state *pb.BeaconState) *pb.BeaconState {
|
|
|
|
currentEpoch := helpers.CurrentEpoch(state) % params.BeaconConfig().LatestSlashedExitLength
|
|
|
|
nextEpoch := helpers.NextEpoch(state) % params.BeaconConfig().LatestSlashedExitLength
|
|
|
|
state.LatestSlashedBalances[nextEpoch] = state.LatestSlashedBalances[currentEpoch]
|
2019-01-17 01:10:42 +00:00
|
|
|
return state
|
|
|
|
}
|
2019-02-09 21:14:10 +00:00
|
|
|
|
|
|
|
// UpdateLatestRandaoMixes updates the latest seed mixes. It transfers
|
|
|
|
// the seed mix of current epoch to next epoch.
|
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
|
|
|
// Set state.latest_randao_mixes[next_epoch % LATEST_RANDAO_MIXES_LENGTH] =
|
|
|
|
// get_randao_mix(state, current_epoch).
|
|
|
|
func UpdateLatestRandaoMixes(state *pb.BeaconState) (*pb.BeaconState, error) {
|
|
|
|
nextEpoch := helpers.NextEpoch(state) % params.BeaconConfig().LatestRandaoMixesLength
|
|
|
|
randaoMix, err := helpers.RandaoMix(state, helpers.CurrentEpoch(state))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not get randaoMix mix: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-19 20:24:00 +00:00
|
|
|
state.LatestRandaoMixes[nextEpoch] = randaoMix
|
2019-02-09 21:14:10 +00:00
|
|
|
return state, nil
|
|
|
|
}
|