github.com/prysmaticlabs/prysm@v1.4.4/shared/testutil/helpers_test.go (about) 1 package testutil 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "testing" 7 8 types "github.com/prysmaticlabs/eth2-types" 9 "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" 10 "github.com/prysmaticlabs/prysm/shared/params" 11 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 12 "github.com/prysmaticlabs/prysm/shared/testutil/require" 13 ) 14 15 func TestBlockSignature(t *testing.T) { 16 beaconState, privKeys := DeterministicGenesisState(t, 100) 17 block, err := GenerateFullBlock(beaconState, privKeys, nil, 0) 18 require.NoError(t, err) 19 20 require.NoError(t, beaconState.SetSlot(beaconState.Slot()+1)) 21 proposerIdx, err := helpers.BeaconProposerIndex(beaconState) 22 assert.NoError(t, err) 23 24 assert.NoError(t, beaconState.SetSlot(beaconState.Slot()-1)) 25 epoch := helpers.SlotToEpoch(block.Block.Slot) 26 blockSig, err := helpers.ComputeDomainAndSign(beaconState, epoch, block.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx]) 27 require.NoError(t, err) 28 29 signature, err := BlockSignature(beaconState, block.Block, privKeys) 30 assert.NoError(t, err) 31 32 if !bytes.Equal(blockSig, signature.Marshal()) { 33 t.Errorf("Expected block signatures to be equal, received %#x != %#x", blockSig, signature.Marshal()) 34 } 35 } 36 37 func TestRandaoReveal(t *testing.T) { 38 beaconState, privKeys := DeterministicGenesisState(t, 100) 39 40 epoch := helpers.CurrentEpoch(beaconState) 41 randaoReveal, err := RandaoReveal(beaconState, epoch, privKeys) 42 assert.NoError(t, err) 43 44 proposerIdx, err := helpers.BeaconProposerIndex(beaconState) 45 assert.NoError(t, err) 46 buf := make([]byte, 32) 47 binary.LittleEndian.PutUint64(buf, uint64(epoch)) 48 // We make the previous validator's index sign the message instead of the proposer. 49 sszUint := types.SSZUint64(epoch) 50 epochSignature, err := helpers.ComputeDomainAndSign(beaconState, epoch, &sszUint, params.BeaconConfig().DomainRandao, privKeys[proposerIdx]) 51 require.NoError(t, err) 52 53 if !bytes.Equal(randaoReveal, epochSignature) { 54 t.Errorf("Expected randao reveals to be equal, received %#x != %#x", randaoReveal, epochSignature) 55 } 56 }