mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-12 04:30:04 +00:00
879e310332
* panic in SizeSSZ * moving slowly * adapt old code to new interfaces * return interfaces from factory functions * replace the rest of WrappedSignedBeaconBlock * WrappedBeaconBlock * WrappedBeaconBlockBody * miscellaneous * Test_BeaconBlockIsNil * replace usages of BeaconBlockIsNil * replace usages of mutator * fix all build errors * fix some more issues * mutator changes * relax assertions when initializing * revert changes in object_mapping.go * allow calling Proto on nil * Revert "allow calling Proto on nil" This reverts commit ecc84e455381b03d24aec2fa0fa17bddbec71705. * modify Copy and Proto methods * remove unused var * fix block batch tests * correct BUILD file * Error when initializing nil objects * one more error fix * add missing comma * rename alias to blocktest * add logging * error when SignedBeaconBlock is nil * fix last test * import fix * broken * working * test fixes * reduce complexity of processPendingBlocks * simplified
94 lines
3.4 KiB
Go
94 lines
3.4 KiB
Go
package blocks_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/signing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/time"
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
|
consensusblocks "github.com/prysmaticlabs/prysm/consensus-types/blocks"
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
|
)
|
|
|
|
func TestProcessRandao_IncorrectProposerFailsVerification(t *testing.T) {
|
|
beaconState, privKeys := util.DeterministicGenesisState(t, 100)
|
|
// We fetch the proposer's index as that is whom the RANDAO will be verified against.
|
|
proposerIdx, err := helpers.BeaconProposerIndex(context.Background(), beaconState)
|
|
require.NoError(t, err)
|
|
epoch := types.Epoch(0)
|
|
buf := make([]byte, 32)
|
|
binary.LittleEndian.PutUint64(buf, uint64(epoch))
|
|
domain, err := signing.Domain(beaconState.Fork(), epoch, params.BeaconConfig().DomainRandao, beaconState.GenesisValidatorsRoot())
|
|
require.NoError(t, err)
|
|
root, err := (ðpb.SigningData{ObjectRoot: buf, Domain: domain}).HashTreeRoot()
|
|
require.NoError(t, err)
|
|
// We make the previous validator's index sign the message instead of the proposer.
|
|
epochSignature := privKeys[proposerIdx-1].Sign(root[:])
|
|
b := util.NewBeaconBlock()
|
|
b.Block = ðpb.BeaconBlock{
|
|
Body: ðpb.BeaconBlockBody{
|
|
RandaoReveal: epochSignature.Marshal(),
|
|
},
|
|
}
|
|
|
|
want := "block randao: signature did not verify"
|
|
wsb, err := consensusblocks.NewSignedBeaconBlock(b)
|
|
require.NoError(t, err)
|
|
_, err = blocks.ProcessRandao(context.Background(), beaconState, wsb)
|
|
assert.ErrorContains(t, want, err)
|
|
}
|
|
|
|
func TestProcessRandao_SignatureVerifiesAndUpdatesLatestStateMixes(t *testing.T) {
|
|
beaconState, privKeys := util.DeterministicGenesisState(t, 100)
|
|
|
|
epoch := time.CurrentEpoch(beaconState)
|
|
epochSignature, err := util.RandaoReveal(beaconState, epoch, privKeys)
|
|
require.NoError(t, err)
|
|
|
|
b := util.NewBeaconBlock()
|
|
b.Block = ðpb.BeaconBlock{
|
|
Body: ðpb.BeaconBlockBody{
|
|
RandaoReveal: epochSignature,
|
|
},
|
|
}
|
|
wsb, err := consensusblocks.NewSignedBeaconBlock(b)
|
|
require.NoError(t, err)
|
|
newState, err := blocks.ProcessRandao(
|
|
context.Background(),
|
|
beaconState,
|
|
wsb,
|
|
)
|
|
require.NoError(t, err, "Unexpected error processing block randao")
|
|
currentEpoch := time.CurrentEpoch(beaconState)
|
|
mix := newState.RandaoMixes()[currentEpoch%params.BeaconConfig().EpochsPerHistoricalVector]
|
|
assert.DeepNotEqual(t, params.BeaconConfig().ZeroHash[:], mix, "Expected empty signature to be overwritten by randao reveal")
|
|
}
|
|
|
|
func TestRandaoSignatureSet_OK(t *testing.T) {
|
|
beaconState, privKeys := util.DeterministicGenesisState(t, 100)
|
|
|
|
epoch := time.CurrentEpoch(beaconState)
|
|
epochSignature, err := util.RandaoReveal(beaconState, epoch, privKeys)
|
|
require.NoError(t, err)
|
|
|
|
block := ðpb.BeaconBlock{
|
|
Body: ðpb.BeaconBlockBody{
|
|
RandaoReveal: epochSignature,
|
|
},
|
|
}
|
|
|
|
set, err := blocks.RandaoSignatureBatch(context.Background(), beaconState, block.Body.RandaoReveal)
|
|
require.NoError(t, err)
|
|
verified, err := set.Verify()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, true, verified, "Unable to verify randao signature set")
|
|
}
|