mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-27 21:57:16 +00:00
caf9bdbc6f
* commit initial work * checkpoint current work * gaz * checkpoint * req/resp changes * initial-sync * finally works * fix error * fix bugs * fix issue * fix issues * fix refs * tests * more text fixes * more text fixes * more text fixes * fix tests * fix tests * tests * finally fix builds * finally * comments * fix fuzz * share common library * fix * fix * add in more defensive nil checks * add in more defensive nil checks * imports * Apply suggestions from code review Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Apply suggestions from code review Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update shared/interfaces/block_interface.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update shared/interfaces/block_wrapper.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * Update shared/interfaces/block_interface.go Co-authored-by: terence tsao <terence@prysmaticlabs.com> * imports * fix bad changes * fix * terence's review * terence's review * fmt * Update beacon-chain/rpc/beacon/blocks.go Co-authored-by: Radosław Kapka <rkapka@wp.pl> * fix tests * fix * fix all tests Co-authored-by: terence tsao <terence@prysmaticlabs.com> Co-authored-by: Radosław Kapka <rkapka@wp.pl>
80 lines
2.7 KiB
Go
80 lines
2.7 KiB
Go
package testutil
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"testing"
|
|
|
|
"github.com/pkg/errors"
|
|
types "github.com/prysmaticlabs/eth2-types"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
|
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
"github.com/prysmaticlabs/prysm/shared/interfaces"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/rand"
|
|
)
|
|
|
|
// RandaoReveal returns a signature of the requested epoch using the beacon proposer private key.
|
|
func RandaoReveal(beaconState iface.ReadOnlyBeaconState, epoch types.Epoch, privKeys []bls.SecretKey) ([]byte, error) {
|
|
// We fetch the proposer's index as that is whom the RANDAO will be verified against.
|
|
proposerIdx, err := helpers.BeaconProposerIndex(beaconState)
|
|
if err != nil {
|
|
return []byte{}, errors.Wrap(err, "could not get beacon proposer index")
|
|
}
|
|
buf := make([]byte, 32)
|
|
binary.LittleEndian.PutUint64(buf, uint64(epoch))
|
|
|
|
// We make the previous validator's index sign the message instead of the proposer.
|
|
sszEpoch := types.SSZUint64(epoch)
|
|
return helpers.ComputeDomainAndSign(beaconState, epoch, &sszEpoch, params.BeaconConfig().DomainRandao, privKeys[proposerIdx])
|
|
}
|
|
|
|
// BlockSignature calculates the post-state root of the block and returns the signature.
|
|
func BlockSignature(
|
|
bState iface.BeaconState,
|
|
block *ethpb.BeaconBlock,
|
|
privKeys []bls.SecretKey,
|
|
) (bls.Signature, error) {
|
|
var err error
|
|
s, err := state.CalculateStateRoot(context.Background(), bState, interfaces.NewWrappedSignedBeaconBlock(ðpb.SignedBeaconBlock{Block: block}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
block.StateRoot = s[:]
|
|
domain, err := helpers.Domain(bState.Fork(), helpers.CurrentEpoch(bState), params.BeaconConfig().DomainBeaconProposer, bState.GenesisValidatorRoot())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
blockRoot, err := helpers.ComputeSigningRoot(block, domain)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Temporarily increasing the beacon state slot here since BeaconProposerIndex is a
|
|
// function deterministic on beacon state slot.
|
|
currentSlot := bState.Slot()
|
|
if err := bState.SetSlot(block.Slot); err != nil {
|
|
return nil, err
|
|
}
|
|
proposerIdx, err := helpers.BeaconProposerIndex(bState)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := bState.SetSlot(currentSlot); err != nil {
|
|
return nil, err
|
|
}
|
|
return privKeys[proposerIdx].Sign(blockRoot[:]), nil
|
|
}
|
|
|
|
// Random32Bytes generates a random 32 byte slice.
|
|
func Random32Bytes(t *testing.T) []byte {
|
|
b := make([]byte, 32)
|
|
_, err := rand.NewDeterministicGenerator().Read(b)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b
|
|
}
|