github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/core/helpers/block_test.go (about) 1 package helpers_test 2 3 import ( 4 "fmt" 5 "math" 6 "testing" 7 8 types "github.com/prysmaticlabs/eth2-types" 9 "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" 10 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1" 11 pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" 12 "github.com/prysmaticlabs/prysm/shared/params" 13 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 14 "github.com/prysmaticlabs/prysm/shared/testutil/require" 15 ) 16 17 func TestBlockRootAtSlot_CorrectBlockRoot(t *testing.T) { 18 var blockRoots [][]byte 19 20 for i := uint64(0); i < uint64(params.BeaconConfig().SlotsPerHistoricalRoot); i++ { 21 blockRoots = append(blockRoots, []byte{byte(i)}) 22 } 23 s := &pb.BeaconState{ 24 BlockRoots: blockRoots, 25 } 26 27 tests := []struct { 28 slot types.Slot 29 stateSlot types.Slot 30 expectedRoot [32]byte 31 }{ 32 { 33 slot: 0, 34 stateSlot: 1, 35 expectedRoot: [32]byte{0}, 36 }, 37 { 38 slot: 2, 39 stateSlot: 5, 40 expectedRoot: [32]byte{2}, 41 }, 42 { 43 slot: 64, 44 stateSlot: 128, 45 expectedRoot: [32]byte{64}, 46 }, { 47 slot: 2999, 48 stateSlot: 3000, 49 expectedRoot: [32]byte{183}, 50 }, { 51 slot: 2873, 52 stateSlot: 3000, 53 expectedRoot: [32]byte{57}, 54 }, 55 { 56 slot: 0, 57 stateSlot: params.BeaconConfig().SlotsPerHistoricalRoot, 58 expectedRoot: [32]byte{}, 59 }, 60 } 61 for i, tt := range tests { 62 t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { 63 s.Slot = tt.stateSlot 64 state, err := v1.InitializeFromProto(s) 65 require.NoError(t, err) 66 wantedSlot := tt.slot 67 result, err := helpers.BlockRootAtSlot(state, wantedSlot) 68 require.NoError(t, err, "Failed to get block root at slot %d", wantedSlot) 69 assert.DeepEqual(t, tt.expectedRoot[:], result, "Result block root was an unexpected value") 70 }) 71 } 72 } 73 74 func TestBlockRootAtSlot_OutOfBounds(t *testing.T) { 75 var blockRoots [][]byte 76 77 for i := uint64(0); i < uint64(params.BeaconConfig().SlotsPerHistoricalRoot); i++ { 78 blockRoots = append(blockRoots, []byte{byte(i)}) 79 } 80 state := &pb.BeaconState{ 81 BlockRoots: blockRoots, 82 } 83 84 tests := []struct { 85 slot types.Slot 86 stateSlot types.Slot 87 expectedErr string 88 }{ 89 { 90 slot: 1000, 91 stateSlot: 500, 92 expectedErr: "slot 1000 out of bounds", 93 }, 94 { 95 slot: 3000, 96 stateSlot: 3000, 97 expectedErr: "slot 3000 out of bounds", 98 }, 99 { 100 // Edge case where stateSlot is over slots per historical root and 101 // slot is not within (stateSlot - HistoricalRootsLimit, statSlot] 102 slot: 1, 103 stateSlot: params.BeaconConfig().SlotsPerHistoricalRoot + 2, 104 expectedErr: "slot 1 out of bounds", 105 }, 106 { 107 slot: math.MaxUint64 - 5, 108 stateSlot: 0, // Doesn't matter 109 expectedErr: "slot overflows uint64", 110 }, 111 } 112 for _, tt := range tests { 113 state.Slot = tt.stateSlot 114 s, err := v1.InitializeFromProto(state) 115 require.NoError(t, err) 116 _, err = helpers.BlockRootAtSlot(s, tt.slot) 117 assert.ErrorContains(t, tt.expectedErr, err) 118 } 119 }