mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-12 04:30:04 +00:00
7842fd9da6
* beacon chain * format imports * Update beacon-chain/db/kv/migration_archived_index_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> * Update beacon-chain/db/kv/migration_block_slot_index_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> * remove unused imports * Update beacon-chain/core/state/skip_slot_cache_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> * Update beacon-chain/core/state/skip_slot_cache_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> * Update beacon-chain/core/state/skip_slot_cache_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> * Update beacon-chain/core/state/skip_slot_cache_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> * Update beacon-chain/db/kv/migration_archived_index_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> * Update beacon-chain/db/kv/migration_block_slot_index_test.go Co-authored-by: Victor Farazdagi <simple.square@gmail.com> Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
88 lines
3.0 KiB
Go
88 lines
3.0 KiB
Go
package blocks_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"testing"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
)
|
|
|
|
func TestProcessRandao_IncorrectProposerFailsVerification(t *testing.T) {
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, 100)
|
|
// We fetch the proposer's index as that is whom the RANDAO will be verified against.
|
|
proposerIdx, err := helpers.BeaconProposerIndex(beaconState)
|
|
require.NoError(t, err)
|
|
epoch := uint64(0)
|
|
buf := make([]byte, 32)
|
|
binary.LittleEndian.PutUint64(buf, epoch)
|
|
domain, err := helpers.Domain(beaconState.Fork(), epoch, params.BeaconConfig().DomainRandao, beaconState.GenesisValidatorRoot())
|
|
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 := testutil.NewBeaconBlock()
|
|
b.Block = ðpb.BeaconBlock{
|
|
Body: ðpb.BeaconBlockBody{
|
|
RandaoReveal: epochSignature.Marshal(),
|
|
},
|
|
}
|
|
|
|
want := "block randao: signature did not verify"
|
|
_, err = blocks.ProcessRandao(context.Background(), beaconState, b)
|
|
assert.ErrorContains(t, want, err)
|
|
}
|
|
|
|
func TestProcessRandao_SignatureVerifiesAndUpdatesLatestStateMixes(t *testing.T) {
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, 100)
|
|
|
|
epoch := helpers.CurrentEpoch(beaconState)
|
|
epochSignature, err := testutil.RandaoReveal(beaconState, epoch, privKeys)
|
|
require.NoError(t, err)
|
|
|
|
b := testutil.NewBeaconBlock()
|
|
b.Block = ðpb.BeaconBlock{
|
|
Body: ðpb.BeaconBlockBody{
|
|
RandaoReveal: epochSignature,
|
|
},
|
|
}
|
|
|
|
newState, err := blocks.ProcessRandao(
|
|
context.Background(),
|
|
beaconState,
|
|
b,
|
|
)
|
|
require.NoError(t, err, "Unexpected error processing block randao")
|
|
currentEpoch := helpers.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 := testutil.DeterministicGenesisState(t, 100)
|
|
|
|
epoch := helpers.CurrentEpoch(beaconState)
|
|
epochSignature, err := testutil.RandaoReveal(beaconState, epoch, privKeys)
|
|
require.NoError(t, err)
|
|
|
|
block := ðpb.BeaconBlock{
|
|
Body: ðpb.BeaconBlockBody{
|
|
RandaoReveal: epochSignature,
|
|
},
|
|
}
|
|
|
|
set, err := blocks.RandaoSignatureSet(beaconState, block.Body)
|
|
require.NoError(t, err)
|
|
verified, err := set.Verify()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, true, verified, "Unable to verify randao signature set")
|
|
}
|