github.com/prysmaticlabs/prysm@v1.4.4/shared/testutil/helpers.go (about) 1 package testutil 2 3 import ( 4 "context" 5 "encoding/binary" 6 "testing" 7 8 "github.com/pkg/errors" 9 types "github.com/prysmaticlabs/eth2-types" 10 "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" 11 "github.com/prysmaticlabs/prysm/beacon-chain/core/state" 12 iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface" 13 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 14 "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1/wrapper" 15 "github.com/prysmaticlabs/prysm/shared/bls" 16 "github.com/prysmaticlabs/prysm/shared/params" 17 "github.com/prysmaticlabs/prysm/shared/rand" 18 ) 19 20 // RandaoReveal returns a signature of the requested epoch using the beacon proposer private key. 21 func RandaoReveal(beaconState iface.ReadOnlyBeaconState, epoch types.Epoch, privKeys []bls.SecretKey) ([]byte, error) { 22 // We fetch the proposer's index as that is whom the RANDAO will be verified against. 23 proposerIdx, err := helpers.BeaconProposerIndex(beaconState) 24 if err != nil { 25 return []byte{}, errors.Wrap(err, "could not get beacon proposer index") 26 } 27 buf := make([]byte, 32) 28 binary.LittleEndian.PutUint64(buf, uint64(epoch)) 29 30 // We make the previous validator's index sign the message instead of the proposer. 31 sszEpoch := types.SSZUint64(epoch) 32 return helpers.ComputeDomainAndSign(beaconState, epoch, &sszEpoch, params.BeaconConfig().DomainRandao, privKeys[proposerIdx]) 33 } 34 35 // BlockSignature calculates the post-state root of the block and returns the signature. 36 func BlockSignature( 37 bState iface.BeaconState, 38 block *ethpb.BeaconBlock, 39 privKeys []bls.SecretKey, 40 ) (bls.Signature, error) { 41 var err error 42 s, err := state.CalculateStateRoot(context.Background(), bState, wrapper.WrappedPhase0SignedBeaconBlock(ðpb.SignedBeaconBlock{Block: block})) 43 if err != nil { 44 return nil, err 45 } 46 block.StateRoot = s[:] 47 domain, err := helpers.Domain(bState.Fork(), helpers.CurrentEpoch(bState), params.BeaconConfig().DomainBeaconProposer, bState.GenesisValidatorRoot()) 48 if err != nil { 49 return nil, err 50 } 51 blockRoot, err := helpers.ComputeSigningRoot(block, domain) 52 if err != nil { 53 return nil, err 54 } 55 // Temporarily increasing the beacon state slot here since BeaconProposerIndex is a 56 // function deterministic on beacon state slot. 57 currentSlot := bState.Slot() 58 if err := bState.SetSlot(block.Slot); err != nil { 59 return nil, err 60 } 61 proposerIdx, err := helpers.BeaconProposerIndex(bState) 62 if err != nil { 63 return nil, err 64 } 65 if err := bState.SetSlot(currentSlot); err != nil { 66 return nil, err 67 } 68 return privKeys[proposerIdx].Sign(blockRoot[:]), nil 69 } 70 71 // Random32Bytes generates a random 32 byte slice. 72 func Random32Bytes(t *testing.T) []byte { 73 b := make([]byte, 32) 74 _, err := rand.NewDeterministicGenerator().Read(b) 75 if err != nil { 76 t.Fatal(err) 77 } 78 return b 79 }