github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/core/helpers/randao_test.go (about)

     1  package helpers
     2  
     3  import (
     4  	"encoding/binary"
     5  	"testing"
     6  
     7  	types "github.com/prysmaticlabs/eth2-types"
     8  	"github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
     9  	pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
    10  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
    11  	"github.com/prysmaticlabs/prysm/shared/params"
    12  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    13  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    14  )
    15  
    16  func TestRandaoMix_OK(t *testing.T) {
    17  	randaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
    18  	for i := 0; i < len(randaoMixes); i++ {
    19  		intInBytes := make([]byte, 32)
    20  		binary.LittleEndian.PutUint64(intInBytes, uint64(i))
    21  		randaoMixes[i] = intInBytes
    22  	}
    23  	state, err := v1.InitializeFromProto(&pb.BeaconState{RandaoMixes: randaoMixes})
    24  	require.NoError(t, err)
    25  	tests := []struct {
    26  		epoch     types.Epoch
    27  		randaoMix []byte
    28  	}{
    29  		{
    30  			epoch:     10,
    31  			randaoMix: randaoMixes[10],
    32  		},
    33  		{
    34  			epoch:     2344,
    35  			randaoMix: randaoMixes[2344],
    36  		},
    37  		{
    38  			epoch:     99999,
    39  			randaoMix: randaoMixes[99999%params.BeaconConfig().EpochsPerHistoricalVector],
    40  		},
    41  	}
    42  	for _, test := range tests {
    43  		require.NoError(t, state.SetSlot(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(test.epoch+1))))
    44  		mix, err := RandaoMix(state, test.epoch)
    45  		require.NoError(t, err)
    46  		assert.DeepEqual(t, test.randaoMix, mix, "Incorrect randao mix")
    47  	}
    48  }
    49  
    50  func TestRandaoMix_CopyOK(t *testing.T) {
    51  	randaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
    52  	for i := 0; i < len(randaoMixes); i++ {
    53  		intInBytes := make([]byte, 32)
    54  		binary.LittleEndian.PutUint64(intInBytes, uint64(i))
    55  		randaoMixes[i] = intInBytes
    56  	}
    57  	state, err := v1.InitializeFromProto(&pb.BeaconState{RandaoMixes: randaoMixes})
    58  	require.NoError(t, err)
    59  	tests := []struct {
    60  		epoch     types.Epoch
    61  		randaoMix []byte
    62  	}{
    63  		{
    64  			epoch:     10,
    65  			randaoMix: randaoMixes[10],
    66  		},
    67  		{
    68  			epoch:     2344,
    69  			randaoMix: randaoMixes[2344],
    70  		},
    71  		{
    72  			epoch:     99999,
    73  			randaoMix: randaoMixes[99999%params.BeaconConfig().EpochsPerHistoricalVector],
    74  		},
    75  	}
    76  	for _, test := range tests {
    77  		require.NoError(t, state.SetSlot(params.BeaconConfig().SlotsPerEpoch.Mul(uint64(test.epoch+1))))
    78  		mix, err := RandaoMix(state, test.epoch)
    79  		require.NoError(t, err)
    80  		uniqueNumber := uint64(params.BeaconConfig().EpochsPerHistoricalVector.Add(1000))
    81  		binary.LittleEndian.PutUint64(mix, uniqueNumber)
    82  
    83  		for _, mx := range randaoMixes {
    84  			mxNum := bytesutil.FromBytes8(mx)
    85  			assert.NotEqual(t, uniqueNumber, mxNum, "two distinct slices which have different representations in memory still contain the same value: %d", mxNum)
    86  		}
    87  	}
    88  }
    89  
    90  func TestGenerateSeed_OK(t *testing.T) {
    91  	randaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
    92  	for i := 0; i < len(randaoMixes); i++ {
    93  		intInBytes := make([]byte, 32)
    94  		binary.LittleEndian.PutUint64(intInBytes, uint64(i))
    95  		randaoMixes[i] = intInBytes
    96  	}
    97  	slot := params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().MinSeedLookahead * 10))
    98  	state, err := v1.InitializeFromProto(&pb.BeaconState{
    99  		RandaoMixes: randaoMixes,
   100  		Slot:        slot,
   101  	})
   102  	require.NoError(t, err)
   103  
   104  	got, err := Seed(state, 10, params.BeaconConfig().DomainBeaconAttester)
   105  	require.NoError(t, err)
   106  
   107  	wanted := [32]byte{102, 82, 23, 40, 226, 79, 171, 11, 203, 23, 175, 7, 88, 202, 80,
   108  		103, 68, 126, 195, 143, 190, 249, 210, 85, 138, 196, 158, 208, 11, 18, 136, 23}
   109  	assert.Equal(t, wanted, got, "Incorrect generated seeds")
   110  }