github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/core/state/state_test.go (about) 1 package state_test 2 3 import ( 4 "context" 5 "testing" 6 7 types "github.com/prysmaticlabs/eth2-types" 8 "github.com/prysmaticlabs/prysm/beacon-chain/core/state" 9 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1" 10 pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" 11 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 12 "github.com/prysmaticlabs/prysm/shared/hashutil" 13 "github.com/prysmaticlabs/prysm/shared/params" 14 "github.com/prysmaticlabs/prysm/shared/testutil" 15 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 16 "github.com/prysmaticlabs/prysm/shared/testutil/require" 17 "google.golang.org/protobuf/proto" 18 ) 19 20 func TestGenesisBeaconState_OK(t *testing.T) { 21 genesisEpoch := types.Epoch(0) 22 23 assert.DeepEqual(t, []byte{0, 0, 0, 0}, params.BeaconConfig().GenesisForkVersion, "GenesisSlot( should be {0,0,0,0} for these tests to pass") 24 genesisForkVersion := params.BeaconConfig().GenesisForkVersion 25 26 assert.Equal(t, [32]byte{}, params.BeaconConfig().ZeroHash, "ZeroHash should be all 0s for these tests to pass") 27 assert.Equal(t, types.Epoch(65536), params.BeaconConfig().EpochsPerHistoricalVector, "EpochsPerHistoricalVector should be 8192 for these tests to pass") 28 29 latestRandaoMixesLength := params.BeaconConfig().EpochsPerHistoricalVector 30 assert.Equal(t, uint64(16777216), params.BeaconConfig().HistoricalRootsLimit, "HistoricalRootsLimit should be 16777216 for these tests to pass") 31 32 depositsForChainStart := 100 33 assert.Equal(t, types.Epoch(8192), params.BeaconConfig().EpochsPerSlashingsVector, "EpochsPerSlashingsVector should be 8192 for these tests to pass") 34 35 genesisTime := uint64(99999) 36 deposits, _, err := testutil.DeterministicDepositsAndKeys(uint64(depositsForChainStart)) 37 require.NoError(t, err) 38 eth1Data, err := testutil.DeterministicEth1Data(len(deposits)) 39 require.NoError(t, err) 40 newState, err := state.GenesisBeaconState(context.Background(), deposits, genesisTime, eth1Data) 41 require.NoError(t, err, "Could not execute GenesisBeaconState") 42 43 // Misc fields checks. 44 assert.Equal(t, types.Slot(0), newState.Slot(), "Slot was not correctly initialized") 45 if !proto.Equal(newState.Fork(), &pb.Fork{ 46 PreviousVersion: genesisForkVersion, 47 CurrentVersion: genesisForkVersion, 48 Epoch: genesisEpoch, 49 }) { 50 t.Error("Fork was not correctly initialized") 51 } 52 53 // Validator registry fields checks. 54 assert.Equal(t, depositsForChainStart, len(newState.Validators()), "Validators was not correctly initialized") 55 v, err := newState.ValidatorAtIndex(0) 56 require.NoError(t, err) 57 assert.Equal(t, types.Epoch(0), v.ActivationEpoch, "Validators was not correctly initialized") 58 v, err = newState.ValidatorAtIndex(0) 59 require.NoError(t, err) 60 assert.Equal(t, types.Epoch(0), v.ActivationEligibilityEpoch, "Validators was not correctly initialized") 61 assert.Equal(t, depositsForChainStart, len(newState.Balances()), "Balances was not correctly initialized") 62 63 // Randomness and committees fields checks. 64 assert.Equal(t, latestRandaoMixesLength, types.Epoch(len(newState.RandaoMixes())), "Length of RandaoMixes was not correctly initialized") 65 mix, err := newState.RandaoMixAtIndex(0) 66 require.NoError(t, err) 67 assert.DeepEqual(t, eth1Data.BlockHash, mix, "RandaoMixes was not correctly initialized") 68 69 // Finality fields checks. 70 assert.Equal(t, genesisEpoch, newState.PreviousJustifiedCheckpoint().Epoch, "PreviousJustifiedCheckpoint.Epoch was not correctly initialized") 71 assert.Equal(t, genesisEpoch, newState.CurrentJustifiedCheckpoint().Epoch, "JustifiedEpoch was not correctly initialized") 72 assert.Equal(t, genesisEpoch, newState.FinalizedCheckpointEpoch(), "FinalizedSlot was not correctly initialized") 73 assert.Equal(t, uint8(0x00), newState.JustificationBits()[0], "JustificationBits was not correctly initialized") 74 75 // Recent state checks. 76 assert.DeepEqual(t, make([]uint64, params.BeaconConfig().EpochsPerSlashingsVector), newState.Slashings(), "Slashings was not correctly initialized") 77 currAtt, err := newState.CurrentEpochAttestations() 78 require.NoError(t, err) 79 assert.DeepSSZEqual(t, []*pb.PendingAttestation{}, currAtt, "CurrentEpochAttestations was not correctly initialized") 80 prevAtt, err := newState.CurrentEpochAttestations() 81 require.NoError(t, err) 82 assert.DeepSSZEqual(t, []*pb.PendingAttestation{}, prevAtt, "PreviousEpochAttestations was not correctly initialized") 83 84 zeroHash := params.BeaconConfig().ZeroHash[:] 85 // History root checks. 86 assert.DeepEqual(t, zeroHash, newState.StateRoots()[0], "StateRoots was not correctly initialized") 87 assert.DeepEqual(t, zeroHash, newState.BlockRoots()[0], "BlockRoots was not correctly initialized") 88 89 // Deposit root checks. 90 assert.DeepEqual(t, eth1Data.DepositRoot, newState.Eth1Data().DepositRoot, "Eth1Data DepositRoot was not correctly initialized") 91 assert.DeepSSZEqual(t, []*ethpb.Eth1Data{}, newState.Eth1DataVotes(), "Eth1DataVotes was not correctly initialized") 92 } 93 94 func TestGenesisState_HashEquality(t *testing.T) { 95 deposits, _, err := testutil.DeterministicDepositsAndKeys(100) 96 require.NoError(t, err) 97 state1, err := state.GenesisBeaconState(context.Background(), deposits, 0, ðpb.Eth1Data{BlockHash: make([]byte, 32)}) 98 require.NoError(t, err) 99 state2, err := state.GenesisBeaconState(context.Background(), deposits, 0, ðpb.Eth1Data{BlockHash: make([]byte, 32)}) 100 require.NoError(t, err) 101 102 pbState1, err := v1.ProtobufBeaconState(state1.CloneInnerState()) 103 require.NoError(t, err) 104 pbState2, err := v1.ProtobufBeaconState(state2.CloneInnerState()) 105 require.NoError(t, err) 106 107 root1, err1 := hashutil.HashProto(pbState1) 108 root2, err2 := hashutil.HashProto(pbState2) 109 110 if err1 != nil || err2 != nil { 111 t.Fatalf("Failed to marshal state to bytes: %v %v", err1, err2) 112 } 113 require.DeepEqual(t, root1, root2, "Tree hash of two genesis states should be equal, received %#x == %#x", root1, root2) 114 } 115 116 func TestGenesisState_InitializesLatestBlockHashes(t *testing.T) { 117 s, err := state.GenesisBeaconState(context.Background(), nil, 0, ðpb.Eth1Data{}) 118 require.NoError(t, err) 119 got, want := uint64(len(s.BlockRoots())), uint64(params.BeaconConfig().SlotsPerHistoricalRoot) 120 assert.Equal(t, want, got, "Wrong number of recent block hashes") 121 122 got = uint64(cap(s.BlockRoots())) 123 assert.Equal(t, want, got, "The slice underlying array capacity is wrong") 124 125 for _, h := range s.BlockRoots() { 126 assert.DeepEqual(t, params.BeaconConfig().ZeroHash[:], h, "Unexpected non-zero hash data") 127 } 128 } 129 130 func TestGenesisState_FailsWithoutEth1data(t *testing.T) { 131 _, err := state.GenesisBeaconState(context.Background(), nil, 0, nil) 132 assert.ErrorContains(t, "no eth1data provided for genesis state", err) 133 }