mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 11:57:18 +00:00
d17996f8b0
* Update V3 from V4 * Fix build v3 -> v4 * Update ssz * Update beacon_chain.pb.go * Fix formatter import * Update update-mockgen.sh comment to v4 * Fix conflicts. Pass build and tests * Fix test
62 lines
2.3 KiB
Go
62 lines
2.3 KiB
Go
package util
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/binary"
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/helpers"
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/signing"
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/time"
|
|
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
|
"github.com/prysmaticlabs/prysm/v4/time/slots"
|
|
)
|
|
|
|
func TestBlockSignature(t *testing.T) {
|
|
beaconState, privKeys := DeterministicGenesisState(t, 100)
|
|
block, err := GenerateFullBlock(beaconState, privKeys, nil, 0)
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, beaconState.SetSlot(beaconState.Slot()+1))
|
|
proposerIdx, err := helpers.BeaconProposerIndex(context.Background(), beaconState)
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, beaconState.SetSlot(beaconState.Slot()-1))
|
|
epoch := slots.ToEpoch(block.Block.Slot)
|
|
blockSig, err := signing.ComputeDomainAndSign(beaconState, epoch, block.Block, params.BeaconConfig().DomainBeaconProposer, privKeys[proposerIdx])
|
|
require.NoError(t, err)
|
|
|
|
signature, err := BlockSignature(beaconState, block.Block, privKeys)
|
|
assert.NoError(t, err)
|
|
|
|
if !bytes.Equal(blockSig, signature.Marshal()) {
|
|
t.Errorf("Expected block signatures to be equal, received %#x != %#x", blockSig, signature.Marshal())
|
|
}
|
|
}
|
|
|
|
func TestRandaoReveal(t *testing.T) {
|
|
beaconState, privKeys := DeterministicGenesisState(t, 100)
|
|
|
|
epoch := time.CurrentEpoch(beaconState)
|
|
randaoReveal, err := RandaoReveal(beaconState, epoch, privKeys)
|
|
assert.NoError(t, err)
|
|
|
|
proposerIdx, err := helpers.BeaconProposerIndex(context.Background(), beaconState)
|
|
assert.NoError(t, err)
|
|
buf := make([]byte, fieldparams.RootLength)
|
|
binary.LittleEndian.PutUint64(buf, uint64(epoch))
|
|
// We make the previous validator's index sign the message instead of the proposer.
|
|
sszUint := primitives.SSZUint64(epoch)
|
|
epochSignature, err := signing.ComputeDomainAndSign(beaconState, epoch, &sszUint, params.BeaconConfig().DomainRandao, privKeys[proposerIdx])
|
|
require.NoError(t, err)
|
|
|
|
if !bytes.Equal(randaoReveal, epochSignature) {
|
|
t.Errorf("Expected randao reveals to be equal, received %#x != %#x", randaoReveal, epochSignature)
|
|
}
|
|
}
|