prysm-pulse/beacon-chain/core/state/state.go
Raul Jordan e19920aec1
Miscellaneous Runtime Fixes & Improvements - Raul (#1674)
* fatal if impossible to receive chainstart

* fix tests

* fix

* custom delay

* completed custom delay

* errors

* better logs, nothing at genesis

* use demo in val

* add gazelle

* log

* starting to log stuff

* pass in ops

* avoid printing the large #s for debug, still working on tests..

* all around better logging

* fixed build error in epoch process

* fixed state transiton tests

* fixed block tests

* lint

* verify sigs in randao

* ready for inclusion falg

* only print waiting when slot is not valid

* fix build

* mod config

* fixed last justified slot issue

* fix inclusion

* fixed attestation issue

* using zero hash from params instead

* fix tests

* update balance

* removed swp

* more `- genesis_slot` for logs

* rem unused log

* fix broken tests

* account for skip slots in state root computation

* fixes done

* validator guide bug fixes - 671

* epoch boundary at the last slot of the epoch

* fix epoch issue

* more balance cal logs for debugging

* greater balance

* attestaton fixes

* fixes

* addressed testrun

* fixed ejection balance

* fix tests with far future epoch

* revert sync change

* revert initial sync change

* fix changes

* off by one att fix

* revert the att fix

* address comments

* format

* fix build

* rem file
2019-02-24 20:09:45 -06:00

179 lines
6.1 KiB
Go

// Package state implements the whole state transition
// function which consists of per slot, per-epoch transitions.
// It also bootstraps the genesis beacon state for slot 0.
package state
import (
"fmt"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state/stateutils"
v "github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
)
// GenesisBeaconState gets called when DepositsForChainStart count of
// full deposits were made to the deposit contract and the ChainStart log gets emitted.
func GenesisBeaconState(
genesisValidatorDeposits []*pb.Deposit,
genesisTime uint64,
processedPowReceiptRoot []byte,
) (*pb.BeaconState, error) {
latestRandaoMixes := make(
[][]byte,
params.BeaconConfig().LatestRandaoMixesLength,
)
for i := 0; i < len(latestRandaoMixes); i++ {
emptySig := params.BeaconConfig().EmptySignature
latestRandaoMixes[i] = emptySig[:]
}
zeroHash := params.BeaconConfig().ZeroHash[:]
latestActiveIndexRoots := make(
[][]byte,
params.BeaconConfig().LatestActiveIndexRootsLength,
)
for i := 0; i < len(latestActiveIndexRoots); i++ {
latestActiveIndexRoots[i] = zeroHash
}
latestVDFOutputs := make([][]byte,
params.BeaconConfig().LatestRandaoMixesLength/params.BeaconConfig().SlotsPerEpoch)
for i := 0; i < len(latestVDFOutputs); i++ {
latestVDFOutputs[i] = zeroHash
}
latestCrosslinks := make([]*pb.Crosslink, params.BeaconConfig().ShardCount)
for i := 0; i < len(latestCrosslinks); i++ {
latestCrosslinks[i] = &pb.Crosslink{
Epoch: params.BeaconConfig().GenesisEpoch,
ShardBlockRootHash32: zeroHash,
}
}
latestBlockRoots := make([][]byte, params.BeaconConfig().LatestBlockRootsLength)
for i := 0; i < len(latestBlockRoots); i++ {
latestBlockRoots[i] = zeroHash
}
validatorRegistry := make([]*pb.Validator, len(genesisValidatorDeposits))
latestBalances := make([]uint64, len(genesisValidatorDeposits))
for i, d := range genesisValidatorDeposits {
depositInput, err := helpers.DecodeDepositInput(d.DepositData)
if err != nil {
return nil, fmt.Errorf("could decode deposit input %v", err)
}
validator := &pb.Validator{
Pubkey: depositInput.Pubkey,
WithdrawalCredentialsHash32: depositInput.WithdrawalCredentialsHash32,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
SlashedEpoch: params.BeaconConfig().FarFutureEpoch,
WithdrawalEpoch: params.BeaconConfig().FarFutureEpoch,
}
validatorRegistry[i] = validator
}
latestSlashedExitBalances := make([]uint64, params.BeaconConfig().LatestSlashedExitLength)
state := &pb.BeaconState{
// Misc fields.
Slot: params.BeaconConfig().GenesisSlot,
GenesisTime: genesisTime,
Fork: &pb.Fork{
PreviousVersion: params.BeaconConfig().GenesisForkVersion,
CurrentVersion: params.BeaconConfig().GenesisForkVersion,
Epoch: params.BeaconConfig().GenesisEpoch,
},
// Validator registry fields.
ValidatorRegistry: validatorRegistry,
ValidatorBalances: latestBalances,
ValidatorRegistryUpdateEpoch: params.BeaconConfig().FarFutureEpoch,
// Randomness and committees.
LatestRandaoMixes: latestRandaoMixes,
PreviousShufflingStartShard: params.BeaconConfig().GenesisStartShard,
CurrentShufflingStartShard: params.BeaconConfig().GenesisStartShard,
PreviousShufflingEpoch: params.BeaconConfig().GenesisEpoch,
CurrentShufflingEpoch: params.BeaconConfig().GenesisEpoch,
PreviousShufflingSeedHash32: zeroHash,
CurrentShufflingSeedHash32: zeroHash,
// Finality.
PreviousJustifiedEpoch: params.BeaconConfig().GenesisEpoch,
JustifiedEpoch: params.BeaconConfig().GenesisEpoch,
JustificationBitfield: 0,
FinalizedEpoch: params.BeaconConfig().GenesisEpoch,
// Recent state.
LatestCrosslinks: latestCrosslinks,
LatestBlockRootHash32S: latestBlockRoots,
LatestIndexRootHash32S: latestActiveIndexRoots,
LatestSlashedBalances: latestSlashedExitBalances,
LatestAttestations: []*pb.PendingAttestation{},
BatchedBlockRootHash32S: [][]byte{},
// Eth1 data.
LatestEth1Data: &pb.Eth1Data{
DepositRootHash32: processedPowReceiptRoot,
BlockHash32: []byte{},
},
Eth1DataVotes: []*pb.Eth1DataVote{},
}
// Process initial deposits.
var err error
validatorMap := stateutils.ValidatorIndexMap(state)
for _, deposit := range genesisValidatorDeposits {
depositData := deposit.DepositData
depositInput, err := helpers.DecodeDepositInput(depositData)
if err != nil {
return nil, fmt.Errorf("could not decode deposit input: %v", err)
}
value, _, err := helpers.DecodeDepositAmountAndTimeStamp(depositData)
if err != nil {
return nil, fmt.Errorf("could not decode deposit value and timestamp: %v", err)
}
state, err = v.ProcessDeposit(
state,
validatorMap,
depositInput.Pubkey,
value,
depositInput.ProofOfPossession,
depositInput.WithdrawalCredentialsHash32,
)
if err != nil {
return nil, fmt.Errorf("could not process validator deposit: %v", err)
}
}
for i := 0; i < len(state.ValidatorRegistry); i++ {
if helpers.EffectiveBalance(state, uint64(i)) >=
params.BeaconConfig().MaxDepositAmount {
state, err = v.ActivateValidator(state, uint64(i), true)
if err != nil {
return nil, fmt.Errorf("could not activate validator: %v", err)
}
}
}
activeValidators := helpers.ActiveValidatorIndices(state.ValidatorRegistry, params.BeaconConfig().GenesisEpoch)
genesisActiveIndexRoot, err := ssz.TreeHash(activeValidators)
if err != nil {
return nil, fmt.Errorf("could not determine genesis active index root: %v", err)
}
for i := uint64(0); i < params.BeaconConfig().LatestActiveIndexRootsLength; i++ {
state.LatestIndexRootHash32S[i] = genesisActiveIndexRoot[:]
}
seed, err := helpers.GenerateSeed(state, params.BeaconConfig().GenesisEpoch)
if err != nil {
return nil, fmt.Errorf("could not generate initial seed: %v", err)
}
state.CurrentShufflingSeedHash32 = seed[:]
return state, nil
}